NXPCup_Cachan / Mbed 2 deprecated NXPCup

Dependencies:   mbed

Committer:
EISR
Date:
Thu Mar 12 15:22:02 2020 +0000
Revision:
3:07109a6bb472
Parent:
2:1103f5d61035
commentaires de la partie bluetooth;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EISR 3:07109a6bb472 1 #include "IHM_Bluetooth.h"
EISR 3:07109a6bb472 2 #include "NXPCam.h"
Wael_H 2:1103f5d61035 3
Wael_H 2:1103f5d61035 4 extern Serial bt;
EISR 3:07109a6bb472 5 extern int xG,xD;
Wael_H 2:1103f5d61035 6
Wael_H 2:1103f5d61035 7 // Constructeur
Wael_H 2:1103f5d61035 8 IHM_Bluetooth::IHM_Bluetooth() : /*blueTxBuffer{STARTBYTE,0,0,0,0,STOPBYTE}, paramRx{1, 0, 0, 0},*/ cb_lm(0), cb_rm(0), cb_err(0), cb_cerr(0), cb_speed(0)
Wael_H 2:1103f5d61035 9 {
Wael_H 2:1103f5d61035 10 unsigned int tab1[6] = {STARTBYTE,0,0,0,0,STOPBYTE}, tab2[4] = {1, 0, 0, 0};
Wael_H 2:1103f5d61035 11
Wael_H 2:1103f5d61035 12 for(int i=0; i<6; ++i)
Wael_H 2:1103f5d61035 13 blueRxBuffer[i] = tab1[i];
Wael_H 2:1103f5d61035 14
Wael_H 2:1103f5d61035 15 for(int i=0; i<4; ++i)
Wael_H 2:1103f5d61035 16 paramRx[i] = tab2[i];
Wael_H 2:1103f5d61035 17 }
Wael_H 2:1103f5d61035 18
EISR 3:07109a6bb472 19 /*************************************** Main ******************************************/
Wael_H 2:1103f5d61035 20
Wael_H 2:1103f5d61035 21 void IHM_Bluetooth::run(void)
Wael_H 2:1103f5d61035 22 {
EISR 3:07109a6bb472 23 bt.attach(this, &IHM_Bluetooth::receptionBlu, Serial::RxIrq); // Creation interruption de reception sur bt
EISR 3:07109a6bb472 24 bt.printf("Hello world"); // Message de test
EISR 3:07109a6bb472 25 timer.attach(this, &IHM_Bluetooth::automate_Tx,0.015); // Interruption toutes les 15ms, lance la fonction automate_Tx (timer est 1 Ticker)
Wael_H 2:1103f5d61035 26 }
Wael_H 2:1103f5d61035 27
Wael_H 2:1103f5d61035 28
Wael_H 2:1103f5d61035 29 /******************************* Fonctions de transmission *****************************/
Wael_H 2:1103f5d61035 30
EISR 3:07109a6bb472 31 void IHM_Bluetooth::transmitBlu(void) // Permet la transmission d'une trame de octets stockee dans un buffer
Wael_H 2:1103f5d61035 32 {
Wael_H 2:1103f5d61035 33 static short i = 0;
EISR 3:07109a6bb472 34 if(bt.writable()) // Fonctionne si la liaison est libre
Wael_H 2:1103f5d61035 35 {
EISR 3:07109a6bb472 36 bt.putc(blueTxBuffer[i]); // Transmission de la trame de 6 octets
Wael_H 2:1103f5d61035 37 i++;
Wael_H 2:1103f5d61035 38 }
EISR 3:07109a6bb472 39 if(i>5) // Quand la trame est transmise
Wael_H 2:1103f5d61035 40 {
Wael_H 2:1103f5d61035 41 i=0;
EISR 3:07109a6bb472 42 bt.attach(NULL, Serial::TxIrq); // Arret de la transmission lorsqu une trame est envoyee.
EISR 3:07109a6bb472 43 // fonction detachee de l'interruption de transmission sur bt
Wael_H 2:1103f5d61035 44 }
Wael_H 2:1103f5d61035 45 }
Wael_H 2:1103f5d61035 46
EISR 3:07109a6bb472 47 void IHM_Bluetooth::formatTrameT(short reg, int val) // Creation de trames de 6 octets pour des donnees a valeur decimale
Wael_H 2:1103f5d61035 48 {
EISR 3:07109a6bb472 49 val=val*10000; // Transformation de la valeur decimale en un entier
EISR 3:07109a6bb472 50 blueTxBuffer[1]=(char)reg; // entete de la donnee a transmettre
EISR 3:07109a6bb472 51 blueTxBuffer[2]=(char)((val&0xFF0000)>>16); // Conversion de la valeur sur trois octets independants
Wael_H 2:1103f5d61035 52 blueTxBuffer[3]=(char)((val&0x00FF00)>>8);
Wael_H 2:1103f5d61035 53 blueTxBuffer[4]=(char)((val&0x0000FF));
Wael_H 2:1103f5d61035 54 // La trame a envoyer est realisee
EISR 3:07109a6bb472 55
EISR 3:07109a6bb472 56 bt.attach(this, &IHM_Bluetooth::transmitBlu, Serial::TxIrq); // Ouverture de l'interruption de transmission.
EISR 3:07109a6bb472 57 // Fonction transmitBlu attachee a la transmission sur bt
Wael_H 2:1103f5d61035 58 }
Wael_H 2:1103f5d61035 59
EISR 3:07109a6bb472 60 void IHM_Bluetooth::TrameCAM( int CAM1, int CAM2) // Creation d'une trame de 6 octets permettant d'envoyer la position sur X
EISR 3:07109a6bb472 61 // des vecteurs gauche et droit vu par la camera
Wael_H 2:1103f5d61035 62 {
EISR 3:07109a6bb472 63 blueTxBuffer[1]=(char)CAMXBYTE; // entete des donnees de la camera
EISR 3:07109a6bb472 64 blueTxBuffer[2]=(char)1; // Valeur inutile. Ne peut pas etre 0 car cela ignifie une erreur
EISR 3:07109a6bb472 65 blueTxBuffer[3]=(char)CAM1; // Position de la ligne gauche entre 0 et 200
EISR 3:07109a6bb472 66 blueTxBuffer[4]=(char)CAM2; // Position de la ligne droite entre 0 et 200
Wael_H 2:1103f5d61035 67 // La trame a envoyer est realisee
EISR 3:07109a6bb472 68
EISR 3:07109a6bb472 69 bt.attach(this, &IHM_Bluetooth::transmitBlu, Serial::TxIrq); // Ouverture de l'interruption de transmission.
EISR 3:07109a6bb472 70 // Fonction transmitBlu attachee a la transmission sur bt
Wael_H 2:1103f5d61035 71 }
Wael_H 2:1103f5d61035 72
EISR 3:07109a6bb472 73 void IHM_Bluetooth::TramePos(int left, int barc, int right) // Creation d'une trame de 6 octets permettant d'envoyer la position du robot
EISR 3:07109a6bb472 74 // sur la piste (cote gauche, barycentre, cote droit)
Wael_H 2:1103f5d61035 75 {
EISR 3:07109a6bb472 76 blueTxBuffer[1]=(char)POSBYTE; // entete des donnees de position du robot sur la piste
EISR 3:07109a6bb472 77 blueTxBuffer[2]=(char)left; // Point gauche ???
EISR 3:07109a6bb472 78 blueTxBuffer[3]=(char)barc; // Barycentre ???
EISR 3:07109a6bb472 79 blueTxBuffer[4]=(char)right; // Point droit ???
Wael_H 2:1103f5d61035 80 // La trame a envoyer est realisee
EISR 3:07109a6bb472 81 bt.attach(this, &IHM_Bluetooth::transmitBlu, Serial::TxIrq); // Ouverture de l'interruption de transmission.
EISR 3:07109a6bb472 82 // Fonction transmitBlu attachee a la transmission sur bt
Wael_H 2:1103f5d61035 83 }
Wael_H 2:1103f5d61035 84
Wael_H 2:1103f5d61035 85
EISR 3:07109a6bb472 86 void IHM_Bluetooth::automate_Tx(void) // Automate pour la transmission. Permet de choisir la trame a creer
Wael_H 2:1103f5d61035 87 {
Wael_H 2:1103f5d61035 88 static int etat_futur=POSBYTE, val=50;
Wael_H 2:1103f5d61035 89 // Transmission des donnees à l'application
EISR 3:07109a6bb472 90 switch(etat_futur){
EISR 3:07109a6bb472 91 case POSBYTE : // Position du robot sur la piste
EISR 3:07109a6bb472 92 TramePos(xG+100, ((xD+xG)/2)+100, xD+100); // Envoie de 3 octets de position par rapport aux lignes, respectivement : Gauche, barycentre et droit
EISR 3:07109a6bb472 93 // Ces valeurs sont comprises entre -100 et 100 or on veut des nombres positifs
EISR 3:07109a6bb472 94 if (cb_lm == 1)etat_futur = LMBYTE;
EISR 3:07109a6bb472 95 else if(cb_rm == 1)etat_futur=RMBYTE; // Lecture de l'etat des checkboxes sur l'application
EISR 3:07109a6bb472 96 else if(cb_err == 1)etat_futur=ERRORBYTE; // Si la checkbox est activee, l'etat futur sera celui associé
EISR 3:07109a6bb472 97 else if(cb_cerr == 1)etat_futur=CERRBYTE;
EISR 3:07109a6bb472 98 else if(cb_speed == 1)etat_futur=SGRABYTE;
EISR 3:07109a6bb472 99 else etat_futur=CAMXBYTE; // Si aucune checkbox n'est active, l'etat futur sera la lecture de la camera
EISR 3:07109a6bb472 100 break;
EISR 3:07109a6bb472 101
EISR 3:07109a6bb472 102 case LMBYTE : // Vitesse du moteur gauche
EISR 3:07109a6bb472 103 formatTrameT(LMBYTE, xD ); // Utilisation de la fonction FormatTrameT
EISR 3:07109a6bb472 104 if(cb_rm == 1)etat_futur=RMBYTE;
EISR 3:07109a6bb472 105 else if(cb_err == 1)etat_futur=ERRORBYTE; // Meme fonctionnement des checkboxes que precedemment
Wael_H 2:1103f5d61035 106 else if(cb_cerr == 1)etat_futur=CERRBYTE;
Wael_H 2:1103f5d61035 107 else if(cb_speed == 1)etat_futur=SGRABYTE;
Wael_H 2:1103f5d61035 108 else etat_futur=CAMXBYTE;
Wael_H 2:1103f5d61035 109 break;
Wael_H 2:1103f5d61035 110
EISR 3:07109a6bb472 111 case RMBYTE : // Vitesse du moteur droit
EISR 3:07109a6bb472 112 formatTrameT(RMBYTE, xG );
Wael_H 2:1103f5d61035 113 if(cb_err == 1)etat_futur=ERRORBYTE;
Wael_H 2:1103f5d61035 114 else if(cb_cerr == 1)etat_futur=CERRBYTE;
Wael_H 2:1103f5d61035 115 else if(cb_speed == 1)etat_futur=SGRABYTE;
Wael_H 2:1103f5d61035 116 else etat_futur=CAMXBYTE;
Wael_H 2:1103f5d61035 117 break;
Wael_H 2:1103f5d61035 118
EISR 3:07109a6bb472 119 case ERRORBYTE : // Erreur de position
EISR 3:07109a6bb472 120 formatTrameT(ERRORBYTE, xD );
Wael_H 2:1103f5d61035 121 if(cb_cerr == 1)etat_futur=CERRBYTE;
Wael_H 2:1103f5d61035 122 else if(cb_speed == 1)etat_futur=SGRABYTE;
Wael_H 2:1103f5d61035 123 else etat_futur=CAMXBYTE;
Wael_H 2:1103f5d61035 124 break;
Wael_H 2:1103f5d61035 125
EISR 3:07109a6bb472 126 case CERRBYTE : // Correction de l'erreur de position
EISR 3:07109a6bb472 127 formatTrameT(CERRBYTE, xG );
Wael_H 2:1103f5d61035 128 if(cb_speed == 1)etat_futur=SGRABYTE;
Wael_H 2:1103f5d61035 129 else etat_futur=CAMXBYTE;
Wael_H 2:1103f5d61035 130 break;
Wael_H 2:1103f5d61035 131
Wael_H 2:1103f5d61035 132 case SGRABYTE : // Vitesse du véhicule
EISR 3:07109a6bb472 133 formatTrameT(SGRABYTE, xG );
Wael_H 2:1103f5d61035 134 etat_futur=CAMXBYTE;
Wael_H 2:1103f5d61035 135 break;
Wael_H 2:1103f5d61035 136
EISR 3:07109a6bb472 137 case CAMXBYTE : // Position sur X des vecteurs gauche et droit
EISR 3:07109a6bb472 138 TrameCAM(xG+100, xD+100); // Retour de la camera, respectivement vecteur Gauche et Droit
Wael_H 2:1103f5d61035 139 etat_futur=POSBYTE;
Wael_H 2:1103f5d61035 140 break;
Wael_H 2:1103f5d61035 141 }
Wael_H 2:1103f5d61035 142 }
Wael_H 2:1103f5d61035 143
Wael_H 2:1103f5d61035 144 /****************************** Fonctions de reception ******************************/
Wael_H 2:1103f5d61035 145
Wael_H 2:1103f5d61035 146 void IHM_Bluetooth::receptionBlu(void)
Wael_H 2:1103f5d61035 147 {
Wael_H 2:1103f5d61035 148 static short j = 0;
Wael_H 2:1103f5d61035 149
EISR 3:07109a6bb472 150 if(bt.readable()) // Si la liaison est libre
Wael_H 2:1103f5d61035 151 {
EISR 3:07109a6bb472 152 blueRxBuffer[j]=(unsigned int)bt.getc(); // Reception des octets dans un tableau
Wael_H 2:1103f5d61035 153 j++;
EISR 3:07109a6bb472 154 if(j==6) // Si 6 octets ont ete recus
Wael_H 2:1103f5d61035 155 {
Wael_H 2:1103f5d61035 156 if((blueRxBuffer[0]==STARTBYTE) && (blueRxBuffer[5]==STOPBYTE)) // Si la trame commence et termine par Start et Stop
Wael_H 2:1103f5d61035 157 {
EISR 3:07109a6bb472 158 switch(blueRxBuffer[1]){ // Automate en fonction de la valeur de l'entete
EISR 3:07109a6bb472 159 case KPBYTE : // Coefficient de proportionnalité pour correcteur PID de la position
EISR 3:07109a6bb472 160 paramRx[0] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000;
EISR 3:07109a6bb472 161 // Recomposition de la donnee et division par 10000 pour obtenir un decimal
Wael_H 2:1103f5d61035 162 break;
Wael_H 2:1103f5d61035 163
EISR 3:07109a6bb472 164 case KIBYTE : // Coefficient integral pour correcteur PID de la position
EISR 3:07109a6bb472 165 paramRx[1] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000;
Wael_H 2:1103f5d61035 166 break;
Wael_H 2:1103f5d61035 167
EISR 3:07109a6bb472 168 case KDBYTE : // Coefficient derive pour correcteur PID de la position
EISR 3:07109a6bb472 169 paramRx[2] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000;
Wael_H 2:1103f5d61035 170 break;
Wael_H 2:1103f5d61035 171
EISR 3:07109a6bb472 172 case SPCMDBYTE : // Commande de vitesse pour le robot
Wael_H 2:1103f5d61035 173 paramRx[3] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000;
Wael_H 2:1103f5d61035 174 break;
Wael_H 2:1103f5d61035 175
EISR 3:07109a6bb472 176 case ERRORBYTE : // Etat de la checkbox du graphe de d'erreur de position
Wael_H 2:1103f5d61035 177 if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1)
EISR 3:07109a6bb472 178 { // Si le
EISR 3:07109a6bb472 179 if(cb_err == 0)cb_err=1;
Wael_H 2:1103f5d61035 180 else if(cb_err == 1)cb_err=0;
Wael_H 2:1103f5d61035 181 }
Wael_H 2:1103f5d61035 182 break;
Wael_H 2:1103f5d61035 183
EISR 3:07109a6bb472 184 case CERRBYTE : // Etat de la checkbox du graphe de la correction d'erreur de position
Wael_H 2:1103f5d61035 185 if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1)
Wael_H 2:1103f5d61035 186 {
Wael_H 2:1103f5d61035 187 if(cb_cerr == 0)cb_cerr=1;
Wael_H 2:1103f5d61035 188 else if(cb_cerr == 1)cb_cerr=0;
Wael_H 2:1103f5d61035 189 }
Wael_H 2:1103f5d61035 190 break;
Wael_H 2:1103f5d61035 191
EISR 3:07109a6bb472 192 case LMBYTE : // Etat de la checkbox du graphe de vitesse du moteur gauche
Wael_H 2:1103f5d61035 193 if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1)
Wael_H 2:1103f5d61035 194 {
Wael_H 2:1103f5d61035 195 if(cb_lm == 0)cb_lm=1;
Wael_H 2:1103f5d61035 196 else if(cb_lm == 1)cb_lm=0;
Wael_H 2:1103f5d61035 197 }
Wael_H 2:1103f5d61035 198 break;
Wael_H 2:1103f5d61035 199 // Faire automate pour la transmission
EISR 3:07109a6bb472 200 case RMBYTE : // Etat de la checkbox du graphe de vitesse du moteur droit
Wael_H 2:1103f5d61035 201 if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1)
Wael_H 2:1103f5d61035 202 {
Wael_H 2:1103f5d61035 203 if(cb_rm == 0)cb_rm=1;
Wael_H 2:1103f5d61035 204 else if(cb_rm == 1)cb_rm=0;
Wael_H 2:1103f5d61035 205 }
Wael_H 2:1103f5d61035 206 break;
Wael_H 2:1103f5d61035 207
EISR 3:07109a6bb472 208 case SGRABYTE : // Etat de la checkbox du graphe de vitesse du robot
Wael_H 2:1103f5d61035 209 if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1)
Wael_H 2:1103f5d61035 210 {
Wael_H 2:1103f5d61035 211 if(cb_speed == 0)cb_speed=1;
Wael_H 2:1103f5d61035 212 else if(cb_speed == 1)cb_speed=0;
Wael_H 2:1103f5d61035 213 }
Wael_H 2:1103f5d61035 214 break;
Wael_H 2:1103f5d61035 215 }
Wael_H 2:1103f5d61035 216 j=0;
Wael_H 2:1103f5d61035 217 }else{ //Si le message ne commence pas par start ou ne finit pas par stop
Wael_H 2:1103f5d61035 218 for(int i = 0;i<5;i++){
Wael_H 2:1103f5d61035 219 blueRxBuffer[i]=blueRxBuffer[i+1]; // On decale le msg reçu dans le buffer
Wael_H 2:1103f5d61035 220 }
EISR 3:07109a6bb472 221 j=5; // le compteur est mis a 5 pour recevoir un autre octet
EISR 3:07109a6bb472 222 // avant de refaire le test sur la trame
Wael_H 2:1103f5d61035 223 }
Wael_H 2:1103f5d61035 224 }
Wael_H 2:1103f5d61035 225 }
Wael_H 2:1103f5d61035 226 }
Wael_H 2:1103f5d61035 227
EISR 3:07109a6bb472 228 int IHM_Bluetooth::get_val_pc(short reg) // reg = entete choisi par l'utilisateur pour connaitre
EISR 3:07109a6bb472 229 // la valeur reçue associee a celui-ci
Wael_H 2:1103f5d61035 230 {
Wael_H 2:1103f5d61035 231 short indice;
Wael_H 2:1103f5d61035 232
EISR 3:07109a6bb472 233 if(reg == KPBYTE)indice = 0; // On associe un indice en fonction de l'entete pour pouvoir
EISR 3:07109a6bb472 234 else if(reg == KIBYTE)indice = 1; // parcourir le tableau contenant les valeurs recues
Wael_H 2:1103f5d61035 235 else if(reg == KDBYTE)indice = 2;
Wael_H 2:1103f5d61035 236 else if(reg == SPCMDBYTE)indice = 3;
Wael_H 2:1103f5d61035 237
Wael_H 2:1103f5d61035 238 return paramRx[indice]; // Renvoie de la valeur souhaitee
Wael_H 2:1103f5d61035 239 }