Code final de la cellule du RC de réception, celle-ci est connectée en Bluetooth à l'autre cellule et en BLE à l'application Android.
Dependencies: mbed SimpleBLE X_NUCLEO_IDB0XA1 LIS3DH_spi
main.cpp@10:9911eb25b6a7, 2020-01-26 (annotated)
- Committer:
- MaxenceGalopin
- Date:
- Sun Jan 26 11:13:58 2020 +0000
- Revision:
- 10:9911eb25b6a7
- Parent:
- 9:504c19ac8bba
Ajout commentaires
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MaxenceGalopin | 10:9911eb25b6a7 | 1 | /* |
MaxenceGalopin | 10:9911eb25b6a7 | 2 | Programme pour l'ObCP connecté à la fois à l'autre ObCP et à l'application Android |
MaxenceGalopin | 10:9911eb25b6a7 | 3 | C'est la cellule qui fait la porte d'arrivée en mode solo |
MaxenceGalopin | 10:9911eb25b6a7 | 4 | La sortie de la photodiode est reliée à la broche A0 |
MaxenceGalopin | 10:9911eb25b6a7 | 5 | Le laser est alimenté en 3V3 (suffisant) ou en 5V. |
MaxenceGalopin | 10:9911eb25b6a7 | 6 | Les pattes RX et TX dumodule HC-05 sont reliées aux broches D0 et D1 (Attention à relier RX à TX et Tx à RX) |
MaxenceGalopin | 10:9911eb25b6a7 | 7 | Le shield est fonctionnel (sauf BLE) mais il faut inverser TX et RX pour le HC-05/HC06 |
MaxenceGalopin | 10:9911eb25b6a7 | 8 | Il comporte aussi 2 leds, 2 boutons, et un transistor pour controler le laser. |
MaxenceGalopin | 10:9911eb25b6a7 | 9 | */ |
MaxenceGalopin | 10:9911eb25b6a7 | 10 | /* |
MaxenceGalopin | 10:9911eb25b6a7 | 11 | Penser à mettre à jour les librairies |
MaxenceGalopin | 10:9911eb25b6a7 | 12 | */ |
MaxenceGalopin | 10:9911eb25b6a7 | 13 | |
Nthnthj | 9:504c19ac8bba | 14 | //Includes |
jimbaud | 6:1670244c4eb4 | 15 | |
janjongboom | 0:ba1c49874d3c | 16 | #include "mbed.h" |
janjongboom | 0:ba1c49874d3c | 17 | #include "SimpleBLE.h" |
Nthnthj | 9:504c19ac8bba | 18 | #include "stdlib.h" |
janjongboom | 0:ba1c49874d3c | 19 | |
jimbaud | 6:1670244c4eb4 | 20 | |
Nthnthj | 9:504c19ac8bba | 21 | //Bluetooth hc05-6 |
MaxenceGalopin | 10:9911eb25b6a7 | 22 | //USBTX et USBRX sont utilisés |
Nthnthj | 9:504c19ac8bba | 23 | #define TX D0 |
Nthnthj | 9:504c19ac8bba | 24 | #define RX D1 |
Nthnthj | 9:504c19ac8bba | 25 | |
jimbaud | 6:1670244c4eb4 | 26 | //Init simpleBLE |
jimbaud | 6:1670244c4eb4 | 27 | |
Nthnthj | 9:504c19ac8bba | 28 | SimpleBLE ble("ObCP_Roller_Catcher2"); |
jimbaud | 6:1670244c4eb4 | 29 | |
jimbaud | 6:1670244c4eb4 | 30 | |
jimbaud | 6:1670244c4eb4 | 31 | // GPIO set |
jimbaud | 6:1670244c4eb4 | 32 | |
jimbaud | 6:1670244c4eb4 | 33 | //Interrupt input |
jimbaud | 6:1670244c4eb4 | 34 | |
jimbaud | 6:1670244c4eb4 | 35 | InterruptIn user1(PC_13); //User1 |
Nthnthj | 9:504c19ac8bba | 36 | InterruptIn boutton1(D3); //Bouton 1 shield |
Nthnthj | 9:504c19ac8bba | 37 | InterruptIn boutton2(D4); //Bouton 2 shield |
Nthnthj | 9:504c19ac8bba | 38 | InterruptIn event(A0); //Passage dans la porte laser |
Nthnthj | 9:504c19ac8bba | 39 | |
Nthnthj | 9:504c19ac8bba | 40 | //Création du Timer |
Nthnthj | 9:504c19ac8bba | 41 | |
Nthnthj | 9:504c19ac8bba | 42 | Timer timer; |
Nthnthj | 9:504c19ac8bba | 43 | |
Nthnthj | 9:504c19ac8bba | 44 | //Sorties numériques |
Nthnthj | 9:504c19ac8bba | 45 | |
Nthnthj | 9:504c19ac8bba | 46 | DigitalOut led1(D14); |
Nthnthj | 9:504c19ac8bba | 47 | DigitalOut transistor(D6); |
jimbaud | 6:1670244c4eb4 | 48 | |
jimbaud | 6:1670244c4eb4 | 49 | //PWM output |
jimbaud | 6:1670244c4eb4 | 50 | |
jimbaud | 6:1670244c4eb4 | 51 | PwmOut PWMoutput(PB_1); //Main PWM output |
jimbaud | 6:1670244c4eb4 | 52 | PwmOut Green(PC_8); //PWM Red LED |
jimbaud | 6:1670244c4eb4 | 53 | PwmOut Red(PC_6); //PWM Green LED |
jimbaud | 6:1670244c4eb4 | 54 | PwmOut Blue(PC_9); //PWM Blue LED |
jimbaud | 6:1670244c4eb4 | 55 | |
Nthnthj | 9:504c19ac8bba | 56 | //Création des variables |
Nthnthj | 9:504c19ac8bba | 57 | |
Nthnthj | 9:504c19ac8bba | 58 | float flag = 0; |
Nthnthj | 9:504c19ac8bba | 59 | bool flag2=false; |
Nthnthj | 9:504c19ac8bba | 60 | int c; |
Nthnthj | 9:504c19ac8bba | 61 | int temps1; |
Nthnthj | 9:504c19ac8bba | 62 | int tref; |
Nthnthj | 9:504c19ac8bba | 63 | char message ; |
Nthnthj | 9:504c19ac8bba | 64 | float end,begin; |
Nthnthj | 9:504c19ac8bba | 65 | int temps2 =0; |
Nthnthj | 9:504c19ac8bba | 66 | |
jimbaud | 6:1670244c4eb4 | 67 | |
Nthnthj | 9:504c19ac8bba | 68 | //Création des liaisons série (pc et bluetooth hc-05) |
Nthnthj | 9:504c19ac8bba | 69 | |
Nthnthj | 9:504c19ac8bba | 70 | //Serial pc(USBTX, USBRX); |
Nthnthj | 9:504c19ac8bba | 71 | Serial BT(USBTX,USBRX); |
jimbaud | 6:1670244c4eb4 | 72 | |
Nthnthj | 9:504c19ac8bba | 73 | // Characteristics pour affiche sur l'appli Android via BLE |
Nthnthj | 9:504c19ac8bba | 74 | |
Nthnthj | 9:504c19ac8bba | 75 | SimpleChar<float> compteur = ble.readOnly_float(0xA000, 0xA003); |
Nthnthj | 9:504c19ac8bba | 76 | SimpleChar<float> Temps = ble.readOnly_float(0xA000, 0xA004); |
Nthnthj | 9:504c19ac8bba | 77 | SimpleChar<float> Temps2 = ble.readOnly_float(0xA000, 0xA007); |
jimbaud | 6:1670244c4eb4 | 78 | |
jimbaud | 6:1670244c4eb4 | 79 | |
Nthnthj | 9:504c19ac8bba | 80 | void envoi(char message) |
Nthnthj | 9:504c19ac8bba | 81 | { |
Nthnthj | 9:504c19ac8bba | 82 | BT.printf("%c\n", message); |
Nthnthj | 9:504c19ac8bba | 83 | } |
Nthnthj | 9:504c19ac8bba | 84 | |
Nthnthj | 9:504c19ac8bba | 85 | void resetUpdate(float rst) |
Nthnthj | 9:504c19ac8bba | 86 | { |
Nthnthj | 9:504c19ac8bba | 87 | message = 'R'; |
Nthnthj | 9:504c19ac8bba | 88 | envoi(message); |
Nthnthj | 9:504c19ac8bba | 89 | timer.reset(); |
Nthnthj | 9:504c19ac8bba | 90 | tref=timer.read_ms(); |
Nthnthj | 9:504c19ac8bba | 91 | } |
Nthnthj | 9:504c19ac8bba | 92 | |
Nthnthj | 9:504c19ac8bba | 93 | // Changement de mode de course ou changements couleur led |
jimbaud | 6:1670244c4eb4 | 94 | |
jimbaud | 6:1670244c4eb4 | 95 | void LEDupdate(uint32_t newColor) |
jimbaud | 6:1670244c4eb4 | 96 | { |
jimbaud | 6:1670244c4eb4 | 97 | // read individual bytes |
jimbaud | 6:1670244c4eb4 | 98 | uint8_t* channels = (uint8_t*)&newColor; |
janjongboom | 0:ba1c49874d3c | 99 | |
jimbaud | 6:1670244c4eb4 | 100 | // cast to float, as PwmOut expects a value between 0.0f and 1.0f |
jimbaud | 6:1670244c4eb4 | 101 | Red = static_cast<float>(channels[0]) / 255.0f; |
jimbaud | 6:1670244c4eb4 | 102 | Green = static_cast<float>(channels[1]) / 255.0f; |
jimbaud | 6:1670244c4eb4 | 103 | Blue = static_cast<float>(channels[2]) / 255.0f; |
Nthnthj | 9:504c19ac8bba | 104 | flag = static_cast<float>(channels[3]); |
Nthnthj | 9:504c19ac8bba | 105 | |
MaxenceGalopin | 10:9911eb25b6a7 | 106 | |
Nthnthj | 9:504c19ac8bba | 107 | if(flag == 1) { |
Nthnthj | 9:504c19ac8bba | 108 | // pc.printf("Mode duo actif"); |
Nthnthj | 9:504c19ac8bba | 109 | message = 'D'; |
Nthnthj | 9:504c19ac8bba | 110 | envoi(message); |
Nthnthj | 9:504c19ac8bba | 111 | timer.reset(); |
Nthnthj | 9:504c19ac8bba | 112 | flag2=true; |
Nthnthj | 9:504c19ac8bba | 113 | }else if(flag == 0) { |
Nthnthj | 9:504c19ac8bba | 114 | // pc.printf("Mode solo actif "); |
Nthnthj | 9:504c19ac8bba | 115 | message = 'S'; |
Nthnthj | 9:504c19ac8bba | 116 | envoi(message); |
Nthnthj | 9:504c19ac8bba | 117 | timer.reset(); |
Nthnthj | 9:504c19ac8bba | 118 | flag2=false; |
Nthnthj | 9:504c19ac8bba | 119 | } |
jimbaud | 6:1670244c4eb4 | 120 | } |
janjongboom | 0:ba1c49874d3c | 121 | |
Nthnthj | 9:504c19ac8bba | 122 | void pressed2(){ |
Nthnthj | 9:504c19ac8bba | 123 | led1 = !led1; |
Nthnthj | 9:504c19ac8bba | 124 | temps2 = timer.read_ms(); |
Nthnthj | 9:504c19ac8bba | 125 | |
Nthnthj | 9:504c19ac8bba | 126 | if(flag2==false){ |
MaxenceGalopin | 10:9911eb25b6a7 | 127 | Temps=(temps2-(temps1+1040))/1000.0f; //LE 1040 correspond au terme de correction du temps que nous avons calculé |
Nthnthj | 9:504c19ac8bba | 128 | |
Nthnthj | 9:504c19ac8bba | 129 | }else{ |
MaxenceGalopin | 10:9911eb25b6a7 | 130 | Temps = (temps1+1040-tref)/1000.0f; |
Nthnthj | 9:504c19ac8bba | 131 | Temps2 = (temps2-tref)/1000.0f; |
Nthnthj | 9:504c19ac8bba | 132 | } |
Nthnthj | 9:504c19ac8bba | 133 | } |
Nthnthj | 9:504c19ac8bba | 134 | |
MaxenceGalopin | 10:9911eb25b6a7 | 135 | void arret_laser(){ |
MaxenceGalopin | 10:9911eb25b6a7 | 136 | transistor = !transistor; |
jimbaud | 6:1670244c4eb4 | 137 | } |
jimbaud | 6:1670244c4eb4 | 138 | |
janjongboom | 0:ba1c49874d3c | 139 | |
jimbaud | 6:1670244c4eb4 | 140 | |
jimbaud | 6:1670244c4eb4 | 141 | // Characteritic PWM LED RGB |
jimbaud | 6:1670244c4eb4 | 142 | SimpleChar<uint32_t> color = ble.writeOnly_u32(0x6200, 0x6201, &LEDupdate); |
jimbaud | 6:1670244c4eb4 | 143 | |
jimbaud | 6:1670244c4eb4 | 144 | // Characteristic PWM output |
MaxenceGalopin | 10:9911eb25b6a7 | 145 | //SimpleChar<uint8_t> pwmout = ble.writeOnly_u8(0xA000, 0xA001, &PWMupdate); |
jimbaud | 6:1670244c4eb4 | 146 | |
Nthnthj | 9:504c19ac8bba | 147 | SimpleChar<float> reset = ble.writeOnly_float(0xA000, 0xA005, &resetUpdate); |
Nthnthj | 9:504c19ac8bba | 148 | SimpleChar<float> depart_course = ble.writeOnly_float(0xA000, 0xA006, &resetUpdate); |
Nthnthj | 9:504c19ac8bba | 149 | |
Nthnthj | 9:504c19ac8bba | 150 | |
MaxenceGalopin | 10:9911eb25b6a7 | 151 | /* |
Nthnthj | 9:504c19ac8bba | 152 | void skater_d() |
Nthnthj | 9:504c19ac8bba | 153 | { |
Nthnthj | 9:504c19ac8bba | 154 | if(flag==false) { |
Nthnthj | 9:504c19ac8bba | 155 | //printf("Ligne de depart coupee solo \n"); |
Nthnthj | 9:504c19ac8bba | 156 | if( flag == false) { |
Nthnthj | 9:504c19ac8bba | 157 | //printf("Depart skate \n"); |
Nthnthj | 9:504c19ac8bba | 158 | begin = timer.read_ms(); |
Nthnthj | 9:504c19ac8bba | 159 | compteur=100; |
Nthnthj | 9:504c19ac8bba | 160 | //pc.printf(" skater lance %.0f \n", begin); |
Nthnthj | 9:504c19ac8bba | 161 | flag = true; |
Nthnthj | 9:504c19ac8bba | 162 | } else if(flag == true) { |
Nthnthj | 9:504c19ac8bba | 163 | //printf("erreur \n"); |
Nthnthj | 9:504c19ac8bba | 164 | //pc.printf(" Temps du skater : %.0f \n", end-begin); |
Nthnthj | 9:504c19ac8bba | 165 | flag = false; |
Nthnthj | 9:504c19ac8bba | 166 | } |
Nthnthj | 9:504c19ac8bba | 167 | } |
Nthnthj | 9:504c19ac8bba | 168 | else if(flag==true) { |
Nthnthj | 9:504c19ac8bba | 169 | //printf("Ligne d'arrivee coupee \n"); |
Nthnthj | 9:504c19ac8bba | 170 | if( flag == false ) { |
Nthnthj | 9:504c19ac8bba | 171 | //printf("arrivee coupe sans depart\n"); |
Nthnthj | 9:504c19ac8bba | 172 | flag = true; |
Nthnthj | 9:504c19ac8bba | 173 | } else if(flag == true ) { |
Nthnthj | 9:504c19ac8bba | 174 | //printf("Arrivee skate \n"); |
Nthnthj | 9:504c19ac8bba | 175 | end = timer.read_ms(); |
Nthnthj | 9:504c19ac8bba | 176 | //Temps = end-begin; |
Nthnthj | 9:504c19ac8bba | 177 | compteur=200; |
Nthnthj | 9:504c19ac8bba | 178 | //wait(1); |
Nthnthj | 9:504c19ac8bba | 179 | //pc.printf(" Temps du skater : %.0f \n", end-begin); |
Nthnthj | 9:504c19ac8bba | 180 | flag = false; |
Nthnthj | 9:504c19ac8bba | 181 | } |
Nthnthj | 9:504c19ac8bba | 182 | } |
Nthnthj | 9:504c19ac8bba | 183 | } |
MaxenceGalopin | 10:9911eb25b6a7 | 184 | */ |
jimbaud | 6:1670244c4eb4 | 185 | |
MaxenceGalopin | 10:9911eb25b6a7 | 186 | |
jimbaud | 6:1670244c4eb4 | 187 | |
Nthnthj | 9:504c19ac8bba | 188 | void RXevent (){ |
MaxenceGalopin | 10:9911eb25b6a7 | 189 | |
Nthnthj | 9:504c19ac8bba | 190 | if(BT.readable()){ |
Nthnthj | 9:504c19ac8bba | 191 | BT.scanf("%i", &temps1); |
Nthnthj | 9:504c19ac8bba | 192 | } |
Nthnthj | 9:504c19ac8bba | 193 | } |
Nthnthj | 9:504c19ac8bba | 194 | |
Nthnthj | 9:504c19ac8bba | 195 | |
Nthnthj | 9:504c19ac8bba | 196 | |
Nthnthj | 9:504c19ac8bba | 197 | |
jimbaud | 6:1670244c4eb4 | 198 | int main(int, char**) |
jimbaud | 6:1670244c4eb4 | 199 | { |
Nthnthj | 9:504c19ac8bba | 200 | transistor=1; |
jimbaud | 6:1670244c4eb4 | 201 | ble.start(); |
Nthnthj | 9:504c19ac8bba | 202 | timer.start(); |
Nthnthj | 9:504c19ac8bba | 203 | BT.attach(&RXevent); |
Nthnthj | 9:504c19ac8bba | 204 | user1.fall(&pressed2); |
MaxenceGalopin | 10:9911eb25b6a7 | 205 | boutton1.fall(&arret_laser); |
Nthnthj | 9:504c19ac8bba | 206 | //boutton2.fall(&pressed); |
Nthnthj | 9:504c19ac8bba | 207 | event.fall(&pressed2); |
Nthnthj | 9:504c19ac8bba | 208 | |
jimbaud | 6:1670244c4eb4 | 209 | |
jimbaud | 6:1670244c4eb4 | 210 | while (1) { |
jimbaud | 6:1670244c4eb4 | 211 | ble.waitForEvent(); |
janjongboom | 0:ba1c49874d3c | 212 | } |
janjongboom | 0:ba1c49874d3c | 213 | } |