Code final recep

Dependencies:   mbed SimpleBLE X_NUCLEO_IDB0XA1 LIS3DH_spi

Committer:
Nthnthj
Date:
Wed Jan 22 09:35:13 2020 +0000
Revision:
9:504c19ac8bba
Parent:
7:f5e10b18984d
Code final recep

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nthnthj 9:504c19ac8bba 1 //Includes
jimbaud 6:1670244c4eb4 2
janjongboom 0:ba1c49874d3c 3 #include "mbed.h"
janjongboom 0:ba1c49874d3c 4 #include "SimpleBLE.h"
jimbaud 6:1670244c4eb4 5 #include "LIS3DH.h"
Nthnthj 9:504c19ac8bba 6 #include "stdlib.h"
janjongboom 0:ba1c49874d3c 7
jimbaud 6:1670244c4eb4 8 //Accelerometer
jimbaud 6:1670244c4eb4 9
jimbaud 6:1670244c4eb4 10 #define MOSI PC_12
jimbaud 6:1670244c4eb4 11 #define MISO PC_11
jimbaud 6:1670244c4eb4 12 #define CS PC_5
jimbaud 6:1670244c4eb4 13 #define SCLK PC_10
jimbaud 6:1670244c4eb4 14
Nthnthj 9:504c19ac8bba 15 //Bluetooth hc05-6
Nthnthj 9:504c19ac8bba 16
Nthnthj 9:504c19ac8bba 17 #define TX D0
Nthnthj 9:504c19ac8bba 18 #define RX D1
Nthnthj 9:504c19ac8bba 19
jimbaud 6:1670244c4eb4 20 //Init simpleBLE
jimbaud 6:1670244c4eb4 21
Nthnthj 9:504c19ac8bba 22 SimpleBLE ble("ObCP_Roller_Catcher2");
jimbaud 6:1670244c4eb4 23
jimbaud 6:1670244c4eb4 24
jimbaud 6:1670244c4eb4 25 // GPIO set
jimbaud 6:1670244c4eb4 26
jimbaud 6:1670244c4eb4 27 //Interrupt input
jimbaud 6:1670244c4eb4 28
jimbaud 6:1670244c4eb4 29 InterruptIn user1(PC_13); //User1
Nthnthj 9:504c19ac8bba 30 InterruptIn boutton1(D3); //Bouton 1 shield
Nthnthj 9:504c19ac8bba 31 InterruptIn boutton2(D4); //Bouton 2 shield
Nthnthj 9:504c19ac8bba 32 InterruptIn event(A0); //Passage dans la porte laser
Nthnthj 9:504c19ac8bba 33
Nthnthj 9:504c19ac8bba 34 //Création du Timer
Nthnthj 9:504c19ac8bba 35
Nthnthj 9:504c19ac8bba 36 Timer timer;
Nthnthj 9:504c19ac8bba 37
Nthnthj 9:504c19ac8bba 38 //Sorties numériques
Nthnthj 9:504c19ac8bba 39
Nthnthj 9:504c19ac8bba 40 DigitalOut led1(D14);
Nthnthj 9:504c19ac8bba 41 DigitalOut transistor(D6);
jimbaud 6:1670244c4eb4 42
jimbaud 6:1670244c4eb4 43 //PWM output
jimbaud 6:1670244c4eb4 44
jimbaud 6:1670244c4eb4 45 PwmOut PWMoutput(PB_1); //Main PWM output
jimbaud 6:1670244c4eb4 46 PwmOut Green(PC_8); //PWM Red LED
jimbaud 6:1670244c4eb4 47 PwmOut Red(PC_6); //PWM Green LED
jimbaud 6:1670244c4eb4 48 PwmOut Blue(PC_9); //PWM Blue LED
jimbaud 6:1670244c4eb4 49
Nthnthj 9:504c19ac8bba 50 //Création des variables
Nthnthj 9:504c19ac8bba 51
Nthnthj 9:504c19ac8bba 52 float flag = 0;
Nthnthj 9:504c19ac8bba 53 bool flag2=false;
Nthnthj 9:504c19ac8bba 54 int c;
Nthnthj 9:504c19ac8bba 55 int temps1;
Nthnthj 9:504c19ac8bba 56 int tref;
Nthnthj 9:504c19ac8bba 57 char message ;
Nthnthj 9:504c19ac8bba 58 float end,begin;
Nthnthj 9:504c19ac8bba 59 int temps2 =0;
Nthnthj 9:504c19ac8bba 60
jimbaud 6:1670244c4eb4 61 //Init accelerometer
janjongboom 2:12a235e7691a 62
jimbaud 6:1670244c4eb4 63 LIS3DH acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G);
jimbaud 6:1670244c4eb4 64
Nthnthj 9:504c19ac8bba 65 //Création des liaisons série (pc et bluetooth hc-05)
Nthnthj 9:504c19ac8bba 66
Nthnthj 9:504c19ac8bba 67 //Serial pc(USBTX, USBRX);
Nthnthj 9:504c19ac8bba 68 Serial BT(USBTX,USBRX);
jimbaud 6:1670244c4eb4 69
Nthnthj 9:504c19ac8bba 70 // Characteristics pour affiche sur l'appli Android via BLE
Nthnthj 9:504c19ac8bba 71
Nthnthj 9:504c19ac8bba 72 SimpleChar<float> compteur = ble.readOnly_float(0xA000, 0xA003);
Nthnthj 9:504c19ac8bba 73 SimpleChar<float> Temps = ble.readOnly_float(0xA000, 0xA004);
Nthnthj 9:504c19ac8bba 74 SimpleChar<float> Temps2 = ble.readOnly_float(0xA000, 0xA007);
jimbaud 6:1670244c4eb4 75
jimbaud 6:1670244c4eb4 76
Nthnthj 9:504c19ac8bba 77 void envoi(char message)
Nthnthj 9:504c19ac8bba 78 {
Nthnthj 9:504c19ac8bba 79 BT.printf("%c\n", message);
Nthnthj 9:504c19ac8bba 80 // pc.printf("Message envoye \r\n");
Nthnthj 9:504c19ac8bba 81 }
Nthnthj 9:504c19ac8bba 82
Nthnthj 9:504c19ac8bba 83 void resetUpdate(float rst)
Nthnthj 9:504c19ac8bba 84 {
Nthnthj 9:504c19ac8bba 85 // pc.printf("reset");
Nthnthj 9:504c19ac8bba 86 message = 'R';
Nthnthj 9:504c19ac8bba 87 envoi(message);
Nthnthj 9:504c19ac8bba 88 timer.reset();
Nthnthj 9:504c19ac8bba 89 tref=timer.read_ms();
Nthnthj 9:504c19ac8bba 90 }
Nthnthj 9:504c19ac8bba 91
Nthnthj 9:504c19ac8bba 92 // Changement de mode de course ou changements couleur led
jimbaud 6:1670244c4eb4 93
jimbaud 6:1670244c4eb4 94 void LEDupdate(uint32_t newColor)
jimbaud 6:1670244c4eb4 95 {
jimbaud 6:1670244c4eb4 96 // read individual bytes
jimbaud 6:1670244c4eb4 97 uint8_t* channels = (uint8_t*)&newColor;
janjongboom 0:ba1c49874d3c 98
jimbaud 6:1670244c4eb4 99 // cast to float, as PwmOut expects a value between 0.0f and 1.0f
jimbaud 6:1670244c4eb4 100 Red = static_cast<float>(channels[0]) / 255.0f;
jimbaud 6:1670244c4eb4 101 Green = static_cast<float>(channels[1]) / 255.0f;
jimbaud 6:1670244c4eb4 102 Blue = static_cast<float>(channels[2]) / 255.0f;
Nthnthj 9:504c19ac8bba 103 flag = static_cast<float>(channels[3]);
Nthnthj 9:504c19ac8bba 104
Nthnthj 9:504c19ac8bba 105 // pc.printf("%f\n",flag);
Nthnthj 9:504c19ac8bba 106 if(flag == 1) {
Nthnthj 9:504c19ac8bba 107 // pc.printf("Mode duo actif");
Nthnthj 9:504c19ac8bba 108 message = 'D';
Nthnthj 9:504c19ac8bba 109 envoi(message);
Nthnthj 9:504c19ac8bba 110 timer.reset();
Nthnthj 9:504c19ac8bba 111 flag2=true;
Nthnthj 9:504c19ac8bba 112 }else if(flag == 0) {
Nthnthj 9:504c19ac8bba 113 // pc.printf("Mode solo actif ");
Nthnthj 9:504c19ac8bba 114 message = 'S';
Nthnthj 9:504c19ac8bba 115 // pc.printf("%c ",message);
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 // pc.printf("Temps calcule, %i \r\n",temps2);
Nthnthj 9:504c19ac8bba 126
Nthnthj 9:504c19ac8bba 127 if(flag2==false){
Nthnthj 9:504c19ac8bba 128 Temps=(temps2-temps1)/1000.0f;
Nthnthj 9:504c19ac8bba 129 // pc.printf("Temps1 ; %i", temps1);
Nthnthj 9:504c19ac8bba 130 // pc.printf("Temps de course ; %i", temps2-temps1);
Nthnthj 9:504c19ac8bba 131
Nthnthj 9:504c19ac8bba 132 }else{
Nthnthj 9:504c19ac8bba 133 // pc.printf("Temps parcours2 : %i \n",temps2-tref);
Nthnthj 9:504c19ac8bba 134 // pc.printf("Temps parcours1 : %i \n", temps1-tref);
Nthnthj 9:504c19ac8bba 135 Temps = (temps1-tref)/1000.0f;
Nthnthj 9:504c19ac8bba 136 Temps2 = (temps2-tref)/1000.0f;
Nthnthj 9:504c19ac8bba 137 }
Nthnthj 9:504c19ac8bba 138 }
Nthnthj 9:504c19ac8bba 139
Nthnthj 9:504c19ac8bba 140
Nthnthj 9:504c19ac8bba 141
jimbaud 6:1670244c4eb4 142 // When characteristic PWM output changing
jimbaud 6:1670244c4eb4 143
jimbaud 6:1670244c4eb4 144 void PWMupdate(uint8_t pwmvalue)
jimbaud 6:1670244c4eb4 145 {
jimbaud 6:1670244c4eb4 146
jimbaud 6:1670244c4eb4 147 // cast to float, as PwmOut expects a value between 0.0f and 1.0f
jimbaud 6:1670244c4eb4 148 PWMoutput = static_cast<float>(pwmvalue) / 255.0f;
jimbaud 6:1670244c4eb4 149 }
jimbaud 6:1670244c4eb4 150
jimbaud 6:1670244c4eb4 151 // When characteristic input changing
jimbaud 6:1670244c4eb4 152 void Accupdate()
jimbaud 6:1670244c4eb4 153 {
janjongboom 0:ba1c49874d3c 154
Nthnthj 9:504c19ac8bba 155 //accX = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15;
Nthnthj 9:504c19ac8bba 156 //accY = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15;
Nthnthj 9:504c19ac8bba 157 //accZ = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15;
Nthnthj 9:504c19ac8bba 158 //Temps=15.68;
jimbaud 6:1670244c4eb4 159
jimbaud 6:1670244c4eb4 160 }
jimbaud 6:1670244c4eb4 161
jimbaud 6:1670244c4eb4 162 // Characteritic PWM LED RGB
jimbaud 6:1670244c4eb4 163 SimpleChar<uint32_t> color = ble.writeOnly_u32(0x6200, 0x6201, &LEDupdate);
jimbaud 6:1670244c4eb4 164
jimbaud 6:1670244c4eb4 165 // Characteristic PWM output
jimbaud 6:1670244c4eb4 166 SimpleChar<uint8_t> pwmout = ble.writeOnly_u8(0xA000, 0xA001, &PWMupdate);
jimbaud 6:1670244c4eb4 167
Nthnthj 9:504c19ac8bba 168 SimpleChar<float> reset = ble.writeOnly_float(0xA000, 0xA005, &resetUpdate);
Nthnthj 9:504c19ac8bba 169 SimpleChar<float> depart_course = ble.writeOnly_float(0xA000, 0xA006, &resetUpdate);
Nthnthj 9:504c19ac8bba 170
Nthnthj 9:504c19ac8bba 171
Nthnthj 9:504c19ac8bba 172
Nthnthj 9:504c19ac8bba 173 void skater_d()
Nthnthj 9:504c19ac8bba 174 {
Nthnthj 9:504c19ac8bba 175 if(flag==false) {
Nthnthj 9:504c19ac8bba 176 //printf("Ligne de depart coupee solo \n");
Nthnthj 9:504c19ac8bba 177 if( flag == false) {
Nthnthj 9:504c19ac8bba 178 //printf("Depart skate \n");
Nthnthj 9:504c19ac8bba 179 begin = timer.read_ms();
Nthnthj 9:504c19ac8bba 180 compteur=100;
Nthnthj 9:504c19ac8bba 181 //pc.printf(" skater lance %.0f \n", begin);
Nthnthj 9:504c19ac8bba 182 flag = true;
Nthnthj 9:504c19ac8bba 183 } else if(flag == true) {
Nthnthj 9:504c19ac8bba 184 //printf("erreur \n");
Nthnthj 9:504c19ac8bba 185 //pc.printf(" Temps du skater : %.0f \n", end-begin);
Nthnthj 9:504c19ac8bba 186 flag = false;
Nthnthj 9:504c19ac8bba 187 }
Nthnthj 9:504c19ac8bba 188 }
Nthnthj 9:504c19ac8bba 189 else if(flag==true) {
Nthnthj 9:504c19ac8bba 190 //printf("Ligne d'arrivee coupee \n");
Nthnthj 9:504c19ac8bba 191 if( flag == false ) {
Nthnthj 9:504c19ac8bba 192 //printf("arrivee coupe sans depart\n");
Nthnthj 9:504c19ac8bba 193 flag = true;
Nthnthj 9:504c19ac8bba 194 } else if(flag == true ) {
Nthnthj 9:504c19ac8bba 195 //printf("Arrivee skate \n");
Nthnthj 9:504c19ac8bba 196 end = timer.read_ms();
Nthnthj 9:504c19ac8bba 197 //Temps = end-begin;
Nthnthj 9:504c19ac8bba 198 compteur=200;
Nthnthj 9:504c19ac8bba 199 //wait(1);
Nthnthj 9:504c19ac8bba 200 //pc.printf(" Temps du skater : %.0f \n", end-begin);
Nthnthj 9:504c19ac8bba 201 flag = false;
Nthnthj 9:504c19ac8bba 202 }
Nthnthj 9:504c19ac8bba 203 }
Nthnthj 9:504c19ac8bba 204 }
jimbaud 6:1670244c4eb4 205
jimbaud 6:1670244c4eb4 206 //Main program
jimbaud 6:1670244c4eb4 207
Nthnthj 9:504c19ac8bba 208 void RXevent (){
Nthnthj 9:504c19ac8bba 209 //timer.start();
Nthnthj 9:504c19ac8bba 210 //c=BT.getc();
Nthnthj 9:504c19ac8bba 211 if(BT.readable()){
Nthnthj 9:504c19ac8bba 212 BT.scanf("%i", &temps1);
Nthnthj 9:504c19ac8bba 213 //wait(1);
Nthnthj 9:504c19ac8bba 214 }
Nthnthj 9:504c19ac8bba 215 // pc.printf(" Message recu :%i \n", temps1); // !!!!!!!!!!!!!!!Ca marche pas sans ça !!!!!!!!!!!
Nthnthj 9:504c19ac8bba 216 //temps2 = timer.read_ms();
Nthnthj 9:504c19ac8bba 217 //Temps= temps2-temps1;
Nthnthj 9:504c19ac8bba 218 //pc.printf("Temps calcule, %i \r\n",Temps);
Nthnthj 9:504c19ac8bba 219 }
Nthnthj 9:504c19ac8bba 220
Nthnthj 9:504c19ac8bba 221
Nthnthj 9:504c19ac8bba 222
Nthnthj 9:504c19ac8bba 223
jimbaud 6:1670244c4eb4 224 int main(int, char**)
jimbaud 6:1670244c4eb4 225 {
Nthnthj 9:504c19ac8bba 226 transistor=1;
jimbaud 6:1670244c4eb4 227 ble.start();
Nthnthj 9:504c19ac8bba 228 //Ticker t;
Nthnthj 9:504c19ac8bba 229 //t.attach(&Accupdate, 15.0f);
Nthnthj 9:504c19ac8bba 230 timer.start();
Nthnthj 9:504c19ac8bba 231 BT.attach(&RXevent);
Nthnthj 9:504c19ac8bba 232 user1.fall(&pressed2);
Nthnthj 9:504c19ac8bba 233 //boutton1.fall(&pressed);
Nthnthj 9:504c19ac8bba 234 //boutton2.fall(&pressed);
Nthnthj 9:504c19ac8bba 235 event.fall(&pressed2);
Nthnthj 9:504c19ac8bba 236 //char Buffer[10];
Nthnthj 9:504c19ac8bba 237
jimbaud 6:1670244c4eb4 238
jimbaud 6:1670244c4eb4 239 while (1) {
jimbaud 6:1670244c4eb4 240 ble.waitForEvent();
Nthnthj 9:504c19ac8bba 241 /*
Nthnthj 9:504c19ac8bba 242 pc.printf("%f\n",flag);
Nthnthj 9:504c19ac8bba 243 if(flag == 1 && flag2==false){
Nthnthj 9:504c19ac8bba 244 pc.printf("Mode duo actif");
Nthnthj 9:504c19ac8bba 245 }else if(flag == 0 && flag2==false){
Nthnthj 9:504c19ac8bba 246 pc.printf("Mode solo actif ");
Nthnthj 9:504c19ac8bba 247 message = 0;
Nthnthj 9:504c19ac8bba 248 pc.printf("%i ",message);
Nthnthj 9:504c19ac8bba 249 envoi(message);
Nthnthj 9:504c19ac8bba 250 timer.start();
Nthnthj 9:504c19ac8bba 251 flag2=true;
Nthnthj 9:504c19ac8bba 252 }
Nthnthj 9:504c19ac8bba 253 */
Nthnthj 9:504c19ac8bba 254
Nthnthj 9:504c19ac8bba 255 //if(BT.readable()){
Nthnthj 9:504c19ac8bba 256 //pc.printf("Je recois");
Nthnthj 9:504c19ac8bba 257 //BT.attach(&RXevent);
Nthnthj 9:504c19ac8bba 258 // c=BT.getc();
Nthnthj 9:504c19ac8bba 259 //pc.printf("%i ",c);
Nthnthj 9:504c19ac8bba 260 //BT.scanf("%s",&Buffer);
Nthnthj 9:504c19ac8bba 261 //pc.printf("%i\n",c);
Nthnthj 9:504c19ac8bba 262
Nthnthj 9:504c19ac8bba 263 //compteur = c;
Nthnthj 9:504c19ac8bba 264 //}
Nthnthj 9:504c19ac8bba 265 //else {
Nthnthj 9:504c19ac8bba 266 // pc.printf("bof");
Nthnthj 9:504c19ac8bba 267 //}
janjongboom 0:ba1c49874d3c 268 }
janjongboom 0:ba1c49874d3c 269 }
Nthnthj 9:504c19ac8bba 270
Nthnthj 9:504c19ac8bba 271 //blablabla