
Projet interfaçage
Dependencies: mbed BSP_DISCO_F746NG
main.cpp@1:eb044e6a9033, 2021-06-22 (annotated)
- Committer:
- anthonyp08
- Date:
- Tue Jun 22 12:04:16 2021 +0000
- Revision:
- 1:eb044e6a9033
- Parent:
- 0:1c6757f4b61c
projet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anthonyp08 | 0:1c6757f4b61c | 1 | #include "mbed.h" |
anthonyp08 | 0:1c6757f4b61c | 2 | #include "MPU6050.h" |
anthonyp08 | 0:1c6757f4b61c | 3 | #include "stm32746g_discovery_lcd.h" // include les bibliothèques |
anthonyp08 | 1:eb044e6a9033 | 4 | #include "stm32746g_discovery_ts.h" |
anthonyp08 | 0:1c6757f4b61c | 5 | #include <math.h> |
anthonyp08 | 0:1c6757f4b61c | 6 | |
anthonyp08 | 0:1c6757f4b61c | 7 | |
anthonyp08 | 1:eb044e6a9033 | 8 | TS_StateTypeDef TS_State; //touchscreen |
anthonyp08 | 1:eb044e6a9033 | 9 | uint8_t status; |
anthonyp08 | 1:eb044e6a9033 | 10 | int TouchScreen=1; |
anthonyp08 | 0:1c6757f4b61c | 11 | Serial pc(USBTX, USBRX); |
anthonyp08 | 0:1c6757f4b61c | 12 | I2C i2c(I2C_SDA, I2C_SCL); //Déclaration de SDA et SCL ainsi que I2C |
anthonyp08 | 0:1c6757f4b61c | 13 | MPU6050 mpu(i2c); |
anthonyp08 | 0:1c6757f4b61c | 14 | |
anthonyp08 | 0:1c6757f4b61c | 15 | |
anthonyp08 | 0:1c6757f4b61c | 16 | PwmOut servo1 (D9); //Déclaration des pins des servo-moteurs |
anthonyp08 | 0:1c6757f4b61c | 17 | PwmOut servo2 (D10); |
anthonyp08 | 0:1c6757f4b61c | 18 | |
anthonyp08 | 0:1c6757f4b61c | 19 | int16_t ax, ay, az; |
anthonyp08 | 0:1c6757f4b61c | 20 | int16_t gx, gy, gz; |
anthonyp08 | 0:1c6757f4b61c | 21 | int moyx, moyy, moyz ; //Déclaration des variables |
anthonyp08 | 0:1c6757f4b61c | 22 | |
anthonyp08 | 0:1c6757f4b61c | 23 | Timer timer; |
anthonyp08 | 0:1c6757f4b61c | 24 | |
anthonyp08 | 0:1c6757f4b61c | 25 | |
anthonyp08 | 0:1c6757f4b61c | 26 | Ticker flipper; |
anthonyp08 | 0:1c6757f4b61c | 27 | |
anthonyp08 | 0:1c6757f4b61c | 28 | double t1, t2; |
anthonyp08 | 1:eb044e6a9033 | 29 | |
anthonyp08 | 1:eb044e6a9033 | 30 | |
anthonyp08 | 0:1c6757f4b61c | 31 | |
anthonyp08 | 0:1c6757f4b61c | 32 | void servo_ctrl() |
anthonyp08 | 0:1c6757f4b61c | 33 | { |
anthonyp08 | 0:1c6757f4b61c | 34 | if (t2 <0) { |
anthonyp08 | 0:1c6757f4b61c | 35 | servo1.pulsewidth_us(2000); |
anthonyp08 | 0:1c6757f4b61c | 36 | |
anthonyp08 | 0:1c6757f4b61c | 37 | } |
anthonyp08 | 0:1c6757f4b61c | 38 | else if (t2 > 0) { |
anthonyp08 | 0:1c6757f4b61c | 39 | servo1.pulsewidth_us(1200); |
anthonyp08 | 0:1c6757f4b61c | 40 | |
anthonyp08 | 0:1c6757f4b61c | 41 | } //fonctionnalité des servo-moteurs |
anthonyp08 | 0:1c6757f4b61c | 42 | if (t1 <0) { |
anthonyp08 | 0:1c6757f4b61c | 43 | servo2.pulsewidth_us(2000); |
anthonyp08 | 0:1c6757f4b61c | 44 | |
anthonyp08 | 0:1c6757f4b61c | 45 | } |
anthonyp08 | 0:1c6757f4b61c | 46 | else if (t1 > 0) { |
anthonyp08 | 0:1c6757f4b61c | 47 | servo2.pulsewidth_us(1200); |
anthonyp08 | 0:1c6757f4b61c | 48 | |
anthonyp08 | 0:1c6757f4b61c | 49 | } |
anthonyp08 | 0:1c6757f4b61c | 50 | } |
anthonyp08 | 1:eb044e6a9033 | 51 | |
anthonyp08 | 0:1c6757f4b61c | 52 | |
anthonyp08 | 0:1c6757f4b61c | 53 | int main() |
anthonyp08 | 0:1c6757f4b61c | 54 | { |
anthonyp08 | 0:1c6757f4b61c | 55 | |
anthonyp08 | 0:1c6757f4b61c | 56 | BSP_LCD_Init(); |
anthonyp08 | 0:1c6757f4b61c | 57 | BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); |
anthonyp08 | 0:1c6757f4b61c | 58 | BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); |
anthonyp08 | 1:eb044e6a9033 | 59 | |
anthonyp08 | 1:eb044e6a9033 | 60 | BSP_LCD_SetBackColor(LCD_COLOR_BLACK); |
anthonyp08 | 1:eb044e6a9033 | 61 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
anthonyp08 | 1:eb044e6a9033 | 62 | BSP_LCD_DisplayStringAt(0,LINE(5), (uint8_t *)"Start commencement?", CENTER_MODE); |
anthonyp08 | 1:eb044e6a9033 | 63 | status = BSP_TS_Init(BSP_LCD_GetXSize() , BSP_LCD_GetYSize()); //déclaration ecran pour le touchscreen |
anthonyp08 | 1:eb044e6a9033 | 64 | |
anthonyp08 | 1:eb044e6a9033 | 65 | while(TouchScreen) { |
anthonyp08 | 1:eb044e6a9033 | 66 | |
anthonyp08 | 1:eb044e6a9033 | 67 | BSP_TS_GetState(&TS_State); |
anthonyp08 | 1:eb044e6a9033 | 68 | if (TS_State.touchDetected){ //rien ne se passe tant qu'il n'y a aucun appui sur l'ecran |
anthonyp08 | 1:eb044e6a9033 | 69 | TouchScreen =0; |
anthonyp08 | 1:eb044e6a9033 | 70 | } |
anthonyp08 | 1:eb044e6a9033 | 71 | } |
anthonyp08 | 1:eb044e6a9033 | 72 | |
anthonyp08 | 1:eb044e6a9033 | 73 | |
anthonyp08 | 0:1c6757f4b61c | 74 | |
anthonyp08 | 0:1c6757f4b61c | 75 | BSP_LCD_Clear(LCD_COLOR_BLACK); |
anthonyp08 | 0:1c6757f4b61c | 76 | BSP_LCD_SetFont(&LCD_DEFAULT_FONT); |
anthonyp08 | 0:1c6757f4b61c | 77 | BSP_LCD_SetBackColor(LCD_COLOR_WHITE); |
anthonyp08 | 0:1c6757f4b61c | 78 | BSP_LCD_SetTextColor(LCD_COLOR_RED); |
anthonyp08 | 0:1c6757f4b61c | 79 | |
anthonyp08 | 0:1c6757f4b61c | 80 | int32_t xPos=240,yPos=150; // déclaration position initiale de la bille |
anthonyp08 | 0:1c6757f4b61c | 81 | |
anthonyp08 | 0:1c6757f4b61c | 82 | |
anthonyp08 | 0:1c6757f4b61c | 83 | char buffer[10] = {0}; |
anthonyp08 | 0:1c6757f4b61c | 84 | char tab [10] = {0}; |
anthonyp08 | 0:1c6757f4b61c | 85 | |
anthonyp08 | 0:1c6757f4b61c | 86 | pc.printf("MPU6050 test\r\n"); |
anthonyp08 | 0:1c6757f4b61c | 87 | pc.printf("MPU6050 initialize \r\n"); |
anthonyp08 | 0:1c6757f4b61c | 88 | |
anthonyp08 | 0:1c6757f4b61c | 89 | mpu.initialize(); //Initialisation du MPU-6050 |
anthonyp08 | 0:1c6757f4b61c | 90 | |
anthonyp08 | 0:1c6757f4b61c | 91 | |
anthonyp08 | 0:1c6757f4b61c | 92 | |
anthonyp08 | 0:1c6757f4b61c | 93 | pc.printf("MPU6050 testConnection\r\n"); |
anthonyp08 | 0:1c6757f4b61c | 94 | |
anthonyp08 | 0:1c6757f4b61c | 95 | bool mpu6050TestResult = mpu.testConnection(); |
anthonyp08 | 0:1c6757f4b61c | 96 | if(mpu6050TestResult) { |
anthonyp08 | 0:1c6757f4b61c | 97 | pc.printf("MPU6050 test passed \r\n"); |
anthonyp08 | 0:1c6757f4b61c | 98 | } else { |
anthonyp08 | 0:1c6757f4b61c | 99 | pc.printf("MPU6050 test failed \r\n"); |
anthonyp08 | 0:1c6757f4b61c | 100 | } |
anthonyp08 | 0:1c6757f4b61c | 101 | |
anthonyp08 | 0:1c6757f4b61c | 102 | |
anthonyp08 | 0:1c6757f4b61c | 103 | servo1.period_ms(20); // déclaration de la période des servo-moteurs |
anthonyp08 | 0:1c6757f4b61c | 104 | servo2.period_ms(20); |
anthonyp08 | 0:1c6757f4b61c | 105 | flipper.attach(&servo_ctrl, 1.0); // nous demandons toute les secondes si il y a une action |
anthonyp08 | 1:eb044e6a9033 | 106 | |
anthonyp08 | 0:1c6757f4b61c | 107 | |
anthonyp08 | 1:eb044e6a9033 | 108 | while (1){ |
anthonyp08 | 0:1c6757f4b61c | 109 | |
anthonyp08 | 0:1c6757f4b61c | 110 | BSP_LCD_Clear(LCD_COLOR_BLACK); |
anthonyp08 | 0:1c6757f4b61c | 111 | BSP_LCD_FillCircle(xPos,yPos,4); |
anthonyp08 | 0:1c6757f4b61c | 112 | |
anthonyp08 | 0:1c6757f4b61c | 113 | sprintf(buffer, "%d", xPos); |
anthonyp08 | 0:1c6757f4b61c | 114 | sprintf(tab, "%d", yPos); |
anthonyp08 | 0:1c6757f4b61c | 115 | BSP_LCD_DisplayStringAt(0,0,(uint8_t*)buffer, LEFT_MODE); |
anthonyp08 | 0:1c6757f4b61c | 116 | BSP_LCD_DisplayStringAt(60,0,(uint8_t*)tab, LEFT_MODE); |
anthonyp08 | 0:1c6757f4b61c | 117 | |
anthonyp08 | 0:1c6757f4b61c | 118 | |
anthonyp08 | 0:1c6757f4b61c | 119 | mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // récupération des valeurs bruts du MPU-6050 |
anthonyp08 | 0:1c6757f4b61c | 120 | t1=atan2(-(double)ay,-(double)ax)*(180/3.1415); // calcul en degrès des valeurs bruts |
anthonyp08 | 0:1c6757f4b61c | 121 | t2=atan2(-(double)az,-(double)ax)*(180/3.1415); |
anthonyp08 | 0:1c6757f4b61c | 122 | pc.printf("%6d %6d %6d %6.2f %6.2f\r\n",ax,ay,az,t1,t2); //affichage des valeurs bruts et degrès (voir sur putty) |
anthonyp08 | 0:1c6757f4b61c | 123 | |
anthonyp08 | 0:1c6757f4b61c | 124 | if (t2 < 0) { |
anthonyp08 | 0:1c6757f4b61c | 125 | xPos = xPos - 3; |
anthonyp08 | 0:1c6757f4b61c | 126 | |
anthonyp08 | 0:1c6757f4b61c | 127 | } |
anthonyp08 | 0:1c6757f4b61c | 128 | else if (t2 > 0) { |
anthonyp08 | 0:1c6757f4b61c | 129 | xPos = xPos + 3; |
anthonyp08 | 0:1c6757f4b61c | 130 | |
anthonyp08 | 0:1c6757f4b61c | 131 | // Selon l'angle des axes du MPU-6050 la position de la bille change |
anthonyp08 | 0:1c6757f4b61c | 132 | } |
anthonyp08 | 0:1c6757f4b61c | 133 | |
anthonyp08 | 0:1c6757f4b61c | 134 | if (t1 <0) { |
anthonyp08 | 0:1c6757f4b61c | 135 | yPos = yPos + 3; |
anthonyp08 | 0:1c6757f4b61c | 136 | |
anthonyp08 | 0:1c6757f4b61c | 137 | |
anthonyp08 | 0:1c6757f4b61c | 138 | } |
anthonyp08 | 0:1c6757f4b61c | 139 | else if (t1 > 0) { |
anthonyp08 | 0:1c6757f4b61c | 140 | yPos = yPos - 3; |
anthonyp08 | 0:1c6757f4b61c | 141 | |
anthonyp08 | 0:1c6757f4b61c | 142 | |
anthonyp08 | 0:1c6757f4b61c | 143 | } |
anthonyp08 | 0:1c6757f4b61c | 144 | |
anthonyp08 | 0:1c6757f4b61c | 145 | |
anthonyp08 | 0:1c6757f4b61c | 146 | if (xPos<=4) { |
anthonyp08 | 0:1c6757f4b61c | 147 | xPos = 4; |
anthonyp08 | 0:1c6757f4b61c | 148 | } else if (xPos>=475) { |
anthonyp08 | 0:1c6757f4b61c | 149 | xPos = 475; |
anthonyp08 | 0:1c6757f4b61c | 150 | } |
anthonyp08 | 0:1c6757f4b61c | 151 | |
anthonyp08 | 0:1c6757f4b61c | 152 | if (yPos<=4) { // Avec cela la bille ne peut pas sortir de l'écran |
anthonyp08 | 0:1c6757f4b61c | 153 | yPos = 4; |
anthonyp08 | 0:1c6757f4b61c | 154 | } else if (yPos>=267) { |
anthonyp08 | 0:1c6757f4b61c | 155 | yPos = 267; |
anthonyp08 | 0:1c6757f4b61c | 156 | } |
anthonyp08 | 0:1c6757f4b61c | 157 | |
anthonyp08 | 0:1c6757f4b61c | 158 | } |
anthonyp08 | 0:1c6757f4b61c | 159 | } |