Interfacage projet

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG herkulex mbed

Committer:
genath6
Date:
Thu Jun 21 10:30:45 2018 +0000
Revision:
1:0672bec4f646
Parent:
0:d109c328c59a
Creation du projet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
genath6 0:d109c328c59a 1 #include "mbed.h"
genath6 0:d109c328c59a 2 #include "TS_DISCO_F746NG.h"
genath6 0:d109c328c59a 3 #include "LCD_DISCO_F746NG.h"
genath6 0:d109c328c59a 4 #include "herkulex.h"
genath6 0:d109c328c59a 5
genath6 0:d109c328c59a 6
genath6 0:d109c328c59a 7 #define SERVO_ID 0x02
genath6 0:d109c328c59a 8
genath6 0:d109c328c59a 9 Herkulex sv(PC_6, PC_7, 115200);
genath6 0:d109c328c59a 10 DigitalOut led(LED1);
genath6 0:d109c328c59a 11
genath6 0:d109c328c59a 12 LCD_DISCO_F746NG lcd;
genath6 0:d109c328c59a 13 TS_DISCO_F746NG ts;
genath6 0:d109c328c59a 14
genath6 0:d109c328c59a 15 int main()
genath6 0:d109c328c59a 16 {
genath6 0:d109c328c59a 17 TS_StateTypeDef TS_State;
genath6 0:d109c328c59a 18 uint16_t x, y;
genath6 0:d109c328c59a 19 uint8_t text[28];
genath6 0:d109c328c59a 20 uint8_t nom[50];
genath6 0:d109c328c59a 21 uint8_t status;
genath6 0:d109c328c59a 22 uint8_t idx;
genath6 0:d109c328c59a 23 uint8_t cleared = 0;
genath6 0:d109c328c59a 24 uint8_t prev_nb_touches = 0;
genath6 0:d109c328c59a 25 int init=0, init1=0;
genath6 0:d109c328c59a 26 int position = 0;
genath6 0:d109c328c59a 27 float positiondeg = 0;
genath6 0:d109c328c59a 28
genath6 0:d109c328c59a 29
genath6 0:d109c328c59a 30 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"HerkuleX Smart Robot Servo", CENTER_MODE);
genath6 0:d109c328c59a 31 lcd.SetBackColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 32 lcd.SetTextColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 33 wait(1);
genath6 0:d109c328c59a 34
genath6 0:d109c328c59a 35 status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
genath6 0:d109c328c59a 36 if (status != TS_OK) {
genath6 0:d109c328c59a 37 lcd.Clear(LCD_COLOR_RED);
genath6 0:d109c328c59a 38 lcd.SetBackColor(LCD_COLOR_RED);
genath6 0:d109c328c59a 39 lcd.SetTextColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 40 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
genath6 0:d109c328c59a 41 } else {
genath6 0:d109c328c59a 42 lcd.Clear(LCD_COLOR_GREEN);
genath6 0:d109c328c59a 43 lcd.SetBackColor(LCD_COLOR_GREEN);
genath6 0:d109c328c59a 44 lcd.SetTextColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 45 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
genath6 0:d109c328c59a 46 }
genath6 0:d109c328c59a 47 /**
genath6 0:d109c328c59a 48 * @brief Draws a rectangle.
genath6 0:d109c328c59a 49 * @param Xpos: X position
genath6 0:d109c328c59a 50 * @param Ypos: Y position
genath6 0:d109c328c59a 51 * @param Width: Rectangle width
genath6 0:d109c328c59a 52 * @param Height: Rectangle height
genath6 0:d109c328c59a 53 * @retval None
genath6 0:d109c328c59a 54 */
genath6 0:d109c328c59a 55 lcd.SetFont(&Font12);
genath6 0:d109c328c59a 56 lcd.SetBackColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 57 lcd.SetTextColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 58
genath6 0:d109c328c59a 59
genath6 0:d109c328c59a 60 sv.clear(SERVO_ID);
genath6 0:d109c328c59a 61
genath6 0:d109c328c59a 62 sv.leds(SERVO_ID, 4);
genath6 0:d109c328c59a 63
genath6 0:d109c328c59a 64 sv.setTorque(SERVO_ID, TORQUE_ON);
genath6 0:d109c328c59a 65
genath6 0:d109c328c59a 66 //sv.getStatus(SERVO_ID);
genath6 0:d109c328c59a 67
genath6 0:d109c328c59a 68 //sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 69
genath6 0:d109c328c59a 70 wait(1);
genath6 0:d109c328c59a 71
genath6 0:d109c328c59a 72 sv.positionControl(SERVO_ID, 21, 60, GLED_ON);
genath6 0:d109c328c59a 73 wait(1);
genath6 0:d109c328c59a 74 sv.getStatus(SERVO_ID);
genath6 0:d109c328c59a 75 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 76
genath6 0:d109c328c59a 77 while(1) {
genath6 0:d109c328c59a 78
genath6 0:d109c328c59a 79 //position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 80 sprintf((char*)text, "position : %d", position);
genath6 0:d109c328c59a 81 lcd.DisplayStringAt(200, LINE(10), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 82
genath6 0:d109c328c59a 83 positiondeg = float(position)*0.325;
genath6 0:d109c328c59a 84
genath6 0:d109c328c59a 85 //display current position
genath6 0:d109c328c59a 86 lcd.SetBackColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 87 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 88 sprintf((char*)text, "current position: %d degrees", int(positiondeg));
genath6 0:d109c328c59a 89 lcd.DisplayStringAt(10, LINE(21), (uint8_t *)&text, CENTER_MODE);
genath6 0:d109c328c59a 90
genath6 0:d109c328c59a 91 //button Init
genath6 0:d109c328c59a 92 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 93 lcd.DrawRect(380,40,50,25);
genath6 0:d109c328c59a 94 lcd.SetBackColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 95 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 96 sprintf((char*)text,"Init");
genath6 0:d109c328c59a 97 lcd.DisplayStringAt(392, LINE(4), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 98
genath6 0:d109c328c59a 99 init=init+1;
genath6 0:d109c328c59a 100
genath6 0:d109c328c59a 101 /*sprintf((char*)text,"%d",init);
genath6 0:d109c328c59a 102 lcd.DisplayStringAt(0, LINE(21), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 103 wait_ms(20);*/
genath6 0:d109c328c59a 104
genath6 0:d109c328c59a 105 //button Uturn
genath6 0:d109c328c59a 106 lcd.SetTextColor(LCD_COLOR_BLACK); //choisi la couleur du texte
genath6 0:d109c328c59a 107 lcd.DrawRect(380,90,50,25); //trace le rectangle
genath6 0:d109c328c59a 108 lcd.SetBackColor(LCD_COLOR_WHITE); //choisi la couleur à l'arière du texte
genath6 0:d109c328c59a 109 lcd.SetTextColor(LCD_COLOR_BLACK); //choisi la couleur du texte
genath6 0:d109c328c59a 110 sprintf((char*)text,"U-turn"); //ecrit "Uturn"
genath6 0:d109c328c59a 111 lcd.DisplayStringAt(385, LINE(8), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 112 //choisi la ligne où ecrire
genath6 0:d109c328c59a 113
genath6 0:d109c328c59a 114 //button +10
genath6 0:d109c328c59a 115 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 116 lcd.DrawRect(380,140,50,25);
genath6 0:d109c328c59a 117 lcd.SetBackColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 118 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 119 sprintf((char*)text,"+10");
genath6 0:d109c328c59a 120 lcd.DisplayStringAt(391, LINE(12), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 121
genath6 0:d109c328c59a 122 //button -10
genath6 0:d109c328c59a 123 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 124 lcd.DrawRect(380,190,50,25);
genath6 0:d109c328c59a 125 lcd.SetBackColor(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 126 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 127 sprintf((char*)text,"-10");
genath6 0:d109c328c59a 128 lcd.DisplayStringAt(391, LINE(16), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 129
genath6 0:d109c328c59a 130 //middle circle
genath6 0:d109c328c59a 131 lcd.SetTextColor(LCD_COLOR_BLACK);
genath6 0:d109c328c59a 132 lcd.DrawCircle(240, 136, 100);
genath6 0:d109c328c59a 133
genath6 0:d109c328c59a 134 ts.GetState(&TS_State);
genath6 0:d109c328c59a 135 if (TS_State.touchDetected) { //when finger detected
genath6 0:d109c328c59a 136 // Clear lines corresponding to old touches coordinates
genath6 0:d109c328c59a 137 if (TS_State.touchDetected < prev_nb_touches) {
genath6 0:d109c328c59a 138 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
genath6 0:d109c328c59a 139 lcd.ClearStringLine(idx);
genath6 0:d109c328c59a 140 }
genath6 0:d109c328c59a 141 }
genath6 0:d109c328c59a 142 prev_nb_touches = TS_State.touchDetected;
genath6 0:d109c328c59a 143
genath6 0:d109c328c59a 144 cleared = 0;
genath6 0:d109c328c59a 145
genath6 0:d109c328c59a 146 //sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
genath6 0:d109c328c59a 147 //lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 148
genath6 0:d109c328c59a 149 for (idx = 0; idx < TS_State.touchDetected; idx++) {
genath6 0:d109c328c59a 150 x = TS_State.touchX[idx];
genath6 0:d109c328c59a 151 y = TS_State.touchY[idx];
genath6 0:d109c328c59a 152 //sprintf((char*)text, "Touch %d: x=%d y=%d ", idx+1, x, y);
genath6 0:d109c328c59a 153 //lcd.DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 154 }
genath6 0:d109c328c59a 155 //filling button Init
genath6 0:d109c328c59a 156 if (TS_State.touchDetected&(380<x & x<430)&(40<y & y<65)) {
genath6 0:d109c328c59a 157 lcd.FillRect(380, 40, 50, 25);
genath6 0:d109c328c59a 158 sv.positionControl(SERVO_ID, 21, 60, GLED_ON);
genath6 0:d109c328c59a 159 wait(1);
genath6 0:d109c328c59a 160 sv.getStatus(SERVO_ID);
genath6 0:d109c328c59a 161 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 162 }
genath6 0:d109c328c59a 163 //filling button Uturn
genath6 0:d109c328c59a 164 if (TS_State.touchDetected&(380<x & x<430)&(90<y & y<115)) {
genath6 0:d109c328c59a 165 lcd.FillRect(380,90,50,25);
genath6 0:d109c328c59a 166 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 167 if( position <= 490)
genath6 1:0672bec4f646 168 { sv.positionControl(SERVO_ID, 554 + position, 60, BLED_ON);
genath6 0:d109c328c59a 169 wait(1);
genath6 0:d109c328c59a 170 sv.getStatus(SERVO_ID);
genath6 0:d109c328c59a 171 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 172 }
genath6 0:d109c328c59a 173 }
genath6 0:d109c328c59a 174 //filling button +10
genath6 0:d109c328c59a 175 if (TS_State.touchDetected&(380<x & x<430)&(140<y & y<165)) {
genath6 0:d109c328c59a 176 lcd.FillRect(380,140,50,25);
genath6 0:d109c328c59a 177 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 178 if( position <= 972)
genath6 0:d109c328c59a 179 { sv.positionControl(SERVO_ID, 30 + position, 60, BLED_ON);
genath6 0:d109c328c59a 180 wait(1);
genath6 0:d109c328c59a 181 sv.getStatus(SERVO_ID);
genath6 0:d109c328c59a 182 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 183 }
genath6 0:d109c328c59a 184 }
genath6 0:d109c328c59a 185 //filling button -10
genath6 0:d109c328c59a 186 if (TS_State.touchDetected&(380<x & x<430)&(190<y & y<215)) {
genath6 0:d109c328c59a 187 lcd.FillRect(380,190,50,25);
genath6 0:d109c328c59a 188 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 189 if( position >= 51)
genath6 0:d109c328c59a 190 { sv.positionControl(SERVO_ID, position - 30, 60, BLED_ON);
genath6 0:d109c328c59a 191 wait(1);
genath6 0:d109c328c59a 192 sv.getStatus(SERVO_ID);
genath6 0:d109c328c59a 193 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 194 }
genath6 0:d109c328c59a 195 }
genath6 0:d109c328c59a 196
genath6 0:d109c328c59a 197 //filling circle
genath6 0:d109c328c59a 198 if (TS_State.touchDetected&(140<x & x<340)&(36<y & y<236)) {
genath6 0:d109c328c59a 199 lcd.FillCircle(240,136,100);
genath6 0:d109c328c59a 200
genath6 0:d109c328c59a 201 sv.positionControl(SERVO_ID, 1002-(((x-140)*5)), 60, BLED_ON);
genath6 0:d109c328c59a 202 wait(1);
genath6 0:d109c328c59a 203 sv.getStatus(SERVO_ID);
genath6 0:d109c328c59a 204 position = sv.getPos(SERVO_ID);
genath6 0:d109c328c59a 205 }
genath6 0:d109c328c59a 206
genath6 0:d109c328c59a 207 //init=0;
genath6 0:d109c328c59a 208
genath6 0:d109c328c59a 209
genath6 0:d109c328c59a 210 //finger follower
genath6 0:d109c328c59a 211 /*if (TS_State.touchDetected&(0<x & x<480)&(0<y & y<272)) {
genath6 0:d109c328c59a 212 lcd.FillCircle(x, y, 10);
genath6 0:d109c328c59a 213 }*/
genath6 0:d109c328c59a 214
genath6 0:d109c328c59a 215 //lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_WHITE);
genath6 0:d109c328c59a 216 } else { //when no finger detected
genath6 0:d109c328c59a 217 if (!cleared) {
genath6 0:d109c328c59a 218 lcd.Clear(LCD_COLOR_WHITE);
genath6 0:d109c328c59a 219 sprintf((char*)text, "Alim Voltage: 7.4 V");
genath6 0:d109c328c59a 220 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 221 sprintf((char*)text, "Current MAX: 450 mA");
genath6 0:d109c328c59a 222 lcd.DisplayStringAt(0, LINE(6), (uint8_t *)&text, LEFT_MODE);
genath6 0:d109c328c59a 223 //title display
genath6 0:d109c328c59a 224 lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"HerkuleX Servo Controler", CENTER_MODE);
genath6 0:d109c328c59a 225 //name display
genath6 0:d109c328c59a 226 sprintf((char*)nom, "Yann MARTIN");
genath6 0:d109c328c59a 227 lcd.DisplayStringAt(0, LINE(17), (uint8_t *)&nom, LEFT_MODE);
genath6 0:d109c328c59a 228 sprintf((char*)nom, "Karl BISWESE");
genath6 0:d109c328c59a 229 lcd.DisplayStringAt(0, LINE(18), (uint8_t *)&nom, LEFT_MODE);
genath6 0:d109c328c59a 230 sprintf((char*)nom, "Genath NAGENTHIRAN");
genath6 0:d109c328c59a 231 lcd.DisplayStringAt(0, LINE(19), (uint8_t *)&nom, LEFT_MODE);
genath6 0:d109c328c59a 232 cleared = 1;
genath6 0:d109c328c59a 233 }
genath6 0:d109c328c59a 234
genath6 0:d109c328c59a 235 }
genath6 0:d109c328c59a 236
genath6 0:d109c328c59a 237
genath6 0:d109c328c59a 238 }
genath6 0:d109c328c59a 239 }