student_with_profs_snec / Mbed OS test_200WX
Committer:
snec_student
Date:
Wed Jun 08 15:02:53 2022 +0000
Revision:
28:6f0009bce093
Parent:
27:53f556d60967
Child:
29:fbc5f53d8d0f
programme de test capteur 200WX

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan 0:7dec7e9ac085 1 #include "mbed.h"
snec_student 28:6f0009bce093 2 /**************************************************************/
snec_student 28:6f0009bce093 3 /* Déclaration des Sous fonctions */
snec_student 28:6f0009bce093 4 /**************************************************************/
snec_student 28:6f0009bce093 5 void ISR_read(); // lecture liaison serie
snec_student 28:6f0009bce093 6 uint8_t gencrc2(uint8_t *data); // calcul crc NMEA
snec_student 28:6f0009bce093 7 DigitalOut myled(PTB22);
snec_student 28:6f0009bce093 8 // initialisation de la liaison serie
snec_student 28:6f0009bce093 9 static UnbufferedSerial capt(PTC4,PTC3,4800);
snec_student 28:6f0009bce093 10 char c;
snec_student 28:6f0009bce093 11 char Rx_buffer[100];
snec_student 28:6f0009bce093 12 char trame[100];
snec_student 28:6f0009bce093 13 char trame_cpy[100];
snec_student 28:6f0009bce093 14 volatile bool flag_ISR_read=0;
snec_student 28:6f0009bce093 15 volatile int index=0;
snec_student 28:6f0009bce093 16 // initialisation des variables du GPS
snec_student 28:6f0009bce093 17 static char type[20];
snec_student 28:6f0009bce093 18 static char horaire[20];
snec_student 28:6f0009bce093 19 static char alerte[3];
snec_student 28:6f0009bce093 20 static char lattitude[20];
snec_student 28:6f0009bce093 21 static char hemisphere[2];
snec_student 28:6f0009bce093 22 static char longitude[20];
snec_student 28:6f0009bce093 23 static char dir[2];
snec_student 28:6f0009bce093 24 static char vitesse[20];
snec_student 28:6f0009bce093 25 static char cap[20];
snec_student 28:6f0009bce093 26 static char date[20];
snec_student 28:6f0009bce093 27 static char magn[20];
snec_student 28:6f0009bce093 28 static char crc[10];
snec_student 28:6f0009bce093 29 static char qualif[10];
snec_student 28:6f0009bce093 30 static char nb_satellites[10];
snec_student 28:6f0009bce093 31 static char DOP[10];
snec_student 28:6f0009bce093 32 static char altitude[10];
snec_student 28:6f0009bce093 33 static char altitude_cor[10];
snec_student 28:6f0009bce093 34 const char * separators = ",";
snec_student 28:6f0009bce093 35 char i;
snec_student 28:6f0009bce093 36 size_t len;
snec_student 28:6f0009bce093 37 uint8_t val_crc;
dan 0:7dec7e9ac085 38
snec_student 28:6f0009bce093 39 int main()
snec_student 28:6f0009bce093 40 {
snec_student 28:6f0009bce093 41 capt.attach(&ISR_read,SerialBase::RxIrq);
dan 0:7dec7e9ac085 42 while(1) {
snec_student 28:6f0009bce093 43 if (flag_ISR_read==1) {
snec_student 28:6f0009bce093 44 //printf("%s",trame); // ligne de test gps
snec_student 28:6f0009bce093 45 char* token = strtok(trame,"*"); //on met dans trame cpy la trame sans crc
snec_student 28:6f0009bce093 46 strcpy(trame_cpy,token);
snec_student 28:6f0009bce093 47 token = strtok(NULL,"*"); // on copie le crc
snec_student 28:6f0009bce093 48 strcpy(crc,token);
snec_student 28:6f0009bce093 49 val_crc=gencrc2((uint8_t *)trame_cpy); //calcul du crc sur la trame
snec_student 28:6f0009bce093 50 uint8_t val_crc2;
snec_student 28:6f0009bce093 51 sscanf(crc,"%x",&val_crc2);
snec_student 28:6f0009bce093 52 if (val_crc!=val_crc2) {
snec_student 28:6f0009bce093 53 printf ("crc error\n");
snec_student 28:6f0009bce093 54 } else {
snec_student 28:6f0009bce093 55 printf ("crc OK\n");
snec_student 28:6f0009bce093 56 }
snec_student 28:6f0009bce093 57 } // fin flag
snec_student 28:6f0009bce093 58 }//fin while
snec_student 28:6f0009bce093 59 } // fin programme
snec_student 28:6f0009bce093 60 /* char* token = strtok(trame,"*"); //on met dans trame cpy la trame sans crc
snec_student 28:6f0009bce093 61 strcpy(trame_cpy,token);
snec_student 28:6f0009bce093 62 token = strtok(NULL,"*"); // on copie le crc
snec_student 28:6f0009bce093 63 strcpy(crc,token);
snec_student 28:6f0009bce093 64 val_crc=gencrc2((uint8_t *)trame_cpy); //calcul du crc sur la trame
snec_student 28:6f0009bce093 65 uint8_t val_crc2;
snec_student 28:6f0009bce093 66 sscanf(crc,"%x",&val_crc2);
snec_student 28:6f0009bce093 67 if (val_crc!=val_crc2) {
snec_student 28:6f0009bce093 68 printf ("crc error\n");
snec_student 28:6f0009bce093 69 } else {
snec_student 28:6f0009bce093 70 printf ("crc OK\n");
snec_student 28:6f0009bce093 71 char* token = strtok((char*)trame_cpy,separators);
snec_student 28:6f0009bce093 72 strcpy(type,token);
snec_student 28:6f0009bce093 73 if (strcmp(type,"$GPRMC")==0) { // traitement d'une trame RMC
snec_student 28:6f0009bce093 74 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 75 strcpy(horaire,token);
snec_student 28:6f0009bce093 76 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 77 strcpy(alerte,token);
snec_student 28:6f0009bce093 78 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 79 strcpy(lattitude,token);
snec_student 28:6f0009bce093 80 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 81 strcpy(hemisphere,token);
snec_student 28:6f0009bce093 82 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 83 strcpy(longitude,token);
snec_student 28:6f0009bce093 84 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 85 strcpy(dir,token);
snec_student 28:6f0009bce093 86 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 87 strcpy(vitesse,token);
snec_student 28:6f0009bce093 88 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 89 strcpy(cap,token);
snec_student 28:6f0009bce093 90 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 91 strcpy(date,token);
snec_student 28:6f0009bce093 92 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 93 strcpy(magn,token);
snec_student 28:6f0009bce093 94 //printf ("%s : %s : %s : %s \n",date,horaire,lattitude, longitude);
snec_student 28:6f0009bce093 95 } // fin compare chaines RMC
snec_student 28:6f0009bce093 96 if (strcmp(type,"$GPGGA")==0) { // traitement d'une trame GGA
snec_student 28:6f0009bce093 97 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 98 strcpy(horaire,token);
snec_student 28:6f0009bce093 99 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 100 strcpy(lattitude,token);
snec_student 28:6f0009bce093 101 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 102 strcpy(hemisphere,token);
snec_student 28:6f0009bce093 103 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 104 strcpy(longitude,token);
snec_student 28:6f0009bce093 105 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 106 strcpy(dir,token);
snec_student 28:6f0009bce093 107 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 108 strcpy(qualif,token);
snec_student 28:6f0009bce093 109 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 110 strcpy(nb_satellites,token);
snec_student 28:6f0009bce093 111 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 112 strcpy(DOP,token);
snec_student 28:6f0009bce093 113 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 114 strcpy(altitude,token);
snec_student 28:6f0009bce093 115 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 116 strcpy(altitude_cor,token);
snec_student 28:6f0009bce093 117 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 118 token = strtok(NULL,separators);
snec_student 28:6f0009bce093 119 int n=atoi(nb_satellites); // calcul du nombre de satellites
snec_student 28:6f0009bce093 120 printf("n=%d date = %s \n",n,date);
snec_student 28:6f0009bce093 121 if (n>3&&(strcmp(date,"")!=0)) { // si precision correcte on envoie la trame et on éteint le GPS
snec_student 28:6f0009bce093 122 // enregistrement des donnees GPS dans la carte SD
snec_student 28:6f0009bce093 123 // serial_port.attach(0,SerialBase::RxIrq);
snec_student 28:6f0009bce093 124 Led_rouge=0;
snec_student 28:6f0009bce093 125 // Montage de la carte SD (Led rouge temoin allumee 1s)
snec_student 28:6f0009bce093 126 Led_rouge=0;
snec_student 28:6f0009bce093 127 thread_sleep_for (1000);
snec_student 28:6f0009bce093 128 // Montage du systeme de fichier
snec_student 28:6f0009bce093 129 BlockDevice *bd = BlockDevice::get_default_instance();
snec_student 28:6f0009bce093 130 FATFileSystem fs("fs");
snec_student 28:6f0009bce093 131 printf("Montage de la carte SD ... : ");
snec_student 28:6f0009bce093 132 int err = fs.mount(bd);
snec_student 28:6f0009bce093 133 if (err!=0) printf("ERREUR !!! \n"); // Message d'erreur
snec_student 28:6f0009bce093 134 else {
snec_student 28:6f0009bce093 135 printf("OK \n");
snec_student 28:6f0009bce093 136 Led_rouge=1;
snec_student 28:6f0009bce093 137 }
snec_student 28:6f0009bce093 138 //thread_sleep_for (1000);
snec_student 28:6f0009bce093 139 printf("Opening \"/fs/test1.txt\"... : ");
snec_student 28:6f0009bce093 140 FILE *fichier = fopen("/fs/test1.txt", "a");
snec_student 28:6f0009bce093 141 if (fichier==NULL) printf("ERREUR !!! \n"); // Message d'erreur
snec_student 28:6f0009bce093 142 else {
snec_student 28:6f0009bce093 143 printf(" OK \n");
snec_student 28:6f0009bce093 144 fprintf (fichier,"%s : %s : %s : %s : %s\n : %s \n ",date,horaire,lattitude, longitude,altitude,nb_satellites);
snec_student 28:6f0009bce093 145 fclose(fichier); // Fermeture du fichier test1.txt
snec_student 28:6f0009bce093 146 err = fs.unmount();
snec_student 28:6f0009bce093 147 Cmd_GPS=0; // on eteintle GPS lors d'une lecture correcte
snec_student 28:6f0009bce093 148 Led_rouge=1;
snec_student 28:6f0009bce093 149 // on charge la nouvelle alarme
snec_student 28:6f0009bce093 150 horloge.get_time(&time); // lecture heure courante sur htr
snec_student 28:6f0009bce093 151 printf ("Programme Lance a %d : %d : %d \n",time.hours,time.minutes,time.seconds);
snec_student 28:6f0009bce093 152 secondes=time.seconds;
snec_student 28:6f0009bce093 153 minutes=time.minutes+1;
snec_student 28:6f0009bce093 154 heures=time.hours;
snec_student 28:6f0009bce093 155 if (secondes>60) {
snec_student 28:6f0009bce093 156 secondes = secondes-60;
snec_student 28:6f0009bce093 157 minutes=minutes+1; // calcul temps alarme prochain
snec_student 28:6f0009bce093 158 if (minutes >59) {
snec_student 28:6f0009bce093 159 minutes=minutes-60;
snec_student 28:6f0009bce093 160 heures = heures+1;
snec_student 28:6f0009bce093 161 if (heures>23) heures=0;
snec_student 28:6f0009bce093 162 }//fin minutes
snec_student 28:6f0009bce093 163 }//fin secondes
snec_student 28:6f0009bce093 164 alarm.seconds=secondes;
snec_student 28:6f0009bce093 165 alarm.minutes=minutes;
snec_student 28:6f0009bce093 166 alarm.hours=heures;
snec_student 28:6f0009bce093 167 alarm.am1=0; // on declenche ici une alarme à l'heure reglee
snec_student 28:6f0009bce093 168 alarm.am2=0;
snec_student 28:6f0009bce093 169 alarm.am3=0;
snec_student 28:6f0009bce093 170 alarm.am4=1;
snec_student 28:6f0009bce093 171 test = horloge.set_alarm(alarm, 1);
snec_student 28:6f0009bce093 172 // on coupe l'alarme alim repart a l'alerme programmee
snec_student 28:6f0009bce093 173 DS_3231_cntl_stat_t data = {0x05, 0x00}; // configuration registres etat et controle
snec_student 28:6f0009bce093 174 horloge.set_cntl_stat_reg(data);
snec_student 28:6f0009bce093 175 }// fin else
snec_student 28:6f0009bce093 176 }// fin n>4
snec_student 28:6f0009bce093 177 } // fin compare chaines GGA
snec_student 28:6f0009bce093 178 } //fin traitement chaine sans erreur de crc
snec_student 28:6f0009bce093 179 flag_ISR_read=0; // raz ISR
snec_student 28:6f0009bce093 180 } //fin if (flag_ISR_read==1)
snec_student 28:6f0009bce093 181 }// fin while true
snec_student 28:6f0009bce093 182 }// fin programme principal
stevep 4:81cea7a352b0 183 }
snec_student 28:6f0009bce093 184 }
snec_student 28:6f0009bce093 185 */
snec_student 28:6f0009bce093 186
snec_student 28:6f0009bce093 187
snec_student 28:6f0009bce093 188 /******* Sous fonctions ***************/
snec_student 28:6f0009bce093 189 void ISR_read() { // lecture liaison serie
snec_student 28:6f0009bce093 190 char carac;
snec_student 28:6f0009bce093 191 capt.read(&carac, 1);
snec_student 28:6f0009bce093 192 if (index==100 || carac=='$') index=0;
snec_student 28:6f0009bce093 193 Rx_buffer[index]=carac;
snec_student 28:6f0009bce093 194 index++;
snec_student 28:6f0009bce093 195 if (carac==0x0a) {
snec_student 28:6f0009bce093 196 Rx_buffer[index]=0;
snec_student 28:6f0009bce093 197 for (char i=0; i<index+1; i++) trame[i] = Rx_buffer[i];
snec_student 28:6f0009bce093 198 index=0;
snec_student 28:6f0009bce093 199 flag_ISR_read=1;
snec_student 28:6f0009bce093 200 }
snec_student 28:6f0009bce093 201 }
snec_student 28:6f0009bce093 202
snec_student 28:6f0009bce093 203 uint8_t gencrc2(uint8_t *data) { // calcul crc NMEA
snec_student 28:6f0009bce093 204 uint8_t crc;
snec_student 28:6f0009bce093 205 crc=data[1];
snec_student 28:6f0009bce093 206 char i=2;
snec_student 28:6f0009bce093 207 while (data[i]!=0) {
snec_student 28:6f0009bce093 208 crc = crc^data[i];
snec_student 28:6f0009bce093 209 i++;
snec_student 28:6f0009bce093 210 }
snec_student 28:6f0009bce093 211 return crc;
snec_student 28:6f0009bce093 212 }