Radar_RTOS
Dependencies: mbed
Fork of Nucleo_radar_buz by
Revision 2:97cbf2a5ec78, committed 2015-12-02
- Comitter:
- Alex_Hochart
- Date:
- Wed Dec 02 16:09:22 2015 +0000
- Parent:
- 1:8558d2451e3e
- Commit message:
- RTOS
Changed in this revision
diff -r 8558d2451e3e -r 97cbf2a5ec78 HCSR04.lib --- a/HCSR04.lib Wed Dec 02 12:43:30 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/aralshukaili/code/HCSR04/#0bda99bb39a4
diff -r 8558d2451e3e -r 97cbf2a5ec78 UART_Radar.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UART_Radar.cpp Wed Dec 02 16:09:22 2015 +0000 @@ -0,0 +1,100 @@ +#include "UART_Radar.h" +#include "mbed.h" +#include <string.h> +Serial carte(PA_9, PA_10); //emission --- tx, rx +Serial pc(USBTX, USBRX); //reception --- tx, rx + +void USART1_puts(char *text){ + while(*text != '\0'){ + + if(carte.writeable()){ + carte.putc(*text); + text++; + } + + } +} + + +void format(char *msg, char *msg_formated){ + int somme=5; + int i=0; + msg_formated[0]=2; + printf("\n\r"); + while(msg[i]!=0){ + somme= somme + msg[i]; + msg_formated[i+1]=msg[i]; + i++; + } + if(somme>255){ + somme++; + } + somme=somme%256; + msg_formated[i+1]=~somme; + msg_formated[i+2]=3; + msg_formated[i+3]=0; + + //A supprimer + i=0; + while(msg_formated[i]!=0){ + printf("[%d] ", msg_formated[i]); + i++; + } +} + + +void send(char *data){ + char trame[52]=""; + format(data, trame); + USART1_puts(trame); +} + +int receive(char *frame){ + int I; + unsigned int sommeBuffer; + char r; + Timer timeout; + timeout.start(); + while(1){ + if(timeout.read_ms() > 100) return 1; + if(carte.readable()){ + r = carte.getc(); + + switch(r){ + case 2: //Start + I=0; + sommeBuffer=2; + break; + + case 3: //Stop + frame[I]=0; + sommeBuffer+=3; + + if(sommeBuffer>255){ + sommeBuffer++; + } + sommeBuffer=~sommeBuffer; + sommeBuffer=sommeBuffer%256; + + if(sommeBuffer==0){ //Les sommes concordent, les données sont valides + frame[I-1]=0; + return 0; + }else{ //Message invalidé par la somme + frame[0]=0; + return 1; + } + + default : + frame[I]=r; + sommeBuffer+=r; + I++; + break; + } + } + } +} + +int controle(char *frame){ + if (*frame!=6) return 0; + else return 1; +} \ No newline at end of file
diff -r 8558d2451e3e -r 97cbf2a5ec78 UART_Radar.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UART_Radar.h Wed Dec 02 16:09:22 2015 +0000 @@ -0,0 +1,12 @@ + + + +void USART1_puts(char *text); + +void format(char *msg, char *msg_formated); + +void send(char *data); + +int receive(char *frame); + +int controle(char *frame);
diff -r 8558d2451e3e -r 97cbf2a5ec78 buzzer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/buzzer.h Wed Dec 02 16:09:22 2015 +0000 @@ -0,0 +1,168 @@ +#ifndef BUZZER_H +#define BUZZER_H + +#include "mbed.h" + +DigitalOut buzz(PB_8); + +class Buzzer{ + public : + Buzzer(){ + TsilenceMs=100; + etat = BipInit; + t.reset(); + i=0; + } + int TsilenceMs; + + typedef enum {BipInit, BipUp, BipDown, Silent} defetat; + defetat etat; + Timer t; + int i; + + void changeMode(int value); + void run(); +}; + +/* +Buzzer::Buzzer(){ + TsilenceMs=100; + etat = BipInit; + t.reset(); + i=0; +} +*/ + +/* + * Initialisation des différents modes de fonctionnement : + * + * 0 < d < 25 --> 0 : 5 b/s + * 25 < d < 50 --> 1 : 4 b/s + * 50 < d < 100 --> 2 : 3 b/s + * 100 < d < 150 --> 3 : 2 b/s + * 150 < d < 200 --> 4 : 1 b/s + * 200 < d --> 5 : 0 b/s + + fonction bip : (400µs+400µs)*50 = 40ms + -> timer sur interruption en fonction du temps à attendre avant le prochain bip + 0 --> 5 b/s --> TsilenceMs 0,16s + 1 --> 4 b/s --> TsilenceMs 0,21s + 2 --> 3 b/s --> TsilenceMs 0,29s + 3 --> 2 b/s --> TsilenceMs 0,46s + 4 --> 1 b/s --> TsilenceMs 0,96s + 5 --> TsilenceMs 1s + + */ + + +//Modifie la valeur du temps de silence entre 2 bips en fonction de la distance +void Buzzer::changeMode(int value){ + if(value<=25){ //0 + TsilenceMs=160; + }else if(value>25 && value<=50){ //1 + TsilenceMs=210; + }else if(value>50 && value<=100){ //2 + TsilenceMs=290; + }else if(value>100 && value<=150){ //3 + TsilenceMs=460; + }else if(value>150 && value<=200){ //4 + TsilenceMs=960; + }else{ //5 + TsilenceMs=1000000000; + } +} + +/* +Si la valeur sort de son intervalle de fonctionnement, +on modifie le mode de fonctionnement courant. + +fonction bip : (400µs+400µs)*50 = 40ms + -> timer sur interruption en fonction du temps à attendre avant le prochain bip + 0 --> 5 b/s --> silence 0,16s + 1 --> 4 b/s --> silence 0,21s + 2 --> 3 b/s --> silence 0,29s + 3 --> 2 b/s --> silence 0,46s + 4 --> 1 b/s --> silence 0,96s + 5 --> silence 1s + +Etat : BipInit + Timer t Start + Bip=0 + i=0 + Etat = BipUp +Etat : BipUp + si (t>=400µs && i<50) + bipCarreUp + i++ + Etat = BipDown + t Restart + si (i>=50) Etat = Silence; + t Restart +Etat : BipDown + si (t>=400µs && i<50) + bipCarreUp + i++ + Etat = BipUp + t Restart + si (i>=50) Etat = Silence; + t Restart +Etat : Silence + si (t>=Tsilence) + Etat=BipInit + +*/ + +void Buzzer::run(){ // paramètre : t (Timer), i, etat + switch(etat){ + + case BipInit : + buzz=0; + i=0; + etat = BipUp; + t.start(); + break; + + case BipUp : + if(t.read_us()>=400 && i<50){ + buzz=1; + i++; + etat = BipDown; + t.reset(); + t.start(); + } + if(i>=50){ + etat = Silent; + t.reset(); + t.start(); + } + break; + + case BipDown : + if(t.read_us()>=400 && i<50){ + buzz=0; + i++; + etat = BipUp; + t.reset(); + t.start(); + } + if(i>=50){ + etat = Silent; + t.reset(); + t.start(); + } + break; + + case Silent : + if(t.read_ms()>=TsilenceMs){ + etat=BipInit; + } + break; + + default : + etat = BipInit; + break; + } +} + + +#endif \ No newline at end of file
diff -r 8558d2451e3e -r 97cbf2a5ec78 main.cpp --- a/main.cpp Wed Dec 02 12:43:30 2015 +0000 +++ b/main.cpp Wed Dec 02 16:09:22 2015 +0000 @@ -1,15 +1,40 @@ #include "mbed.h" #include <string.h> -#include "HCSR04.h" +#include "UART_Radar.h" +#include "buzzer.h" #define TRIG_1 PA_8 #define ECHO_1 PC_7 - -Serial pc(USBTX, USBRX); //reception --- tx, rx -Serial carte(PA_9, PA_10); //emission --- tx, rx + + +//DigitalOut buzz(PB_8); +DigitalOut trig(TRIG_1); + +Ticker mesure_time; +Ticker send_time; +Timer distance_time; +InterruptIn echo(ECHO_1); + +Buzzer buzzo; +Ticker bip; -HCSR04 sensor1(TRIG_1, ECHO_1); -DigitalOut buzz(PB_8); +char trame[50]; +int essai, err; +int distance, distance_brut; +char message[4]; +int i=0; + + +//Envoie un burst +void send_burst(){ + distance_time.reset(); + trig = 0; + wait_us(2); + trig = 1; + wait_us(10); + trig = 0; +} + int max (int a, int b) { return (a<b)?b:a; @@ -19,167 +44,54 @@ return (a<b)?a:b; } -void USART1_puts(char *text){ - while(*text != '\0'){ - - if(carte.writeable()){ - carte.putc(*text); - text++; - } - - } -} - - -void format(char *msg, char *msg_formated){ - int somme=5; - int i=0; - msg_formated[0]=2; - printf("\n\r"); - while(msg[i]!=0){ - somme= somme + msg[i]; - msg_formated[i+1]=msg[i]; - i++; - } - if(somme>255){ - somme++; - } - somme=somme%256; - msg_formated[i+1]=~somme; - msg_formated[i+2]=3; - msg_formated[i+3]=0; - - //A supprimer - i=0; - while(msg_formated[i]!=0){ - printf("[%d] ", msg_formated[i]); - i++; - } -} - - -void send(char *data){ - char trame[52]=""; - format(data, trame); - USART1_puts(trame); +//Lance le timer +void echo_rise(){ + distance_time.start(); } -int receive(char *frame){ - int I; - unsigned int sommeBuffer; - char r; - Timer timeout; - timeout.start(); - while(1){ - if(timeout.read_ms() > 100) return 1; - if(carte.readable()){ - r = carte.getc(); - - switch(r){ - case 2: //Start - I=0; - sommeBuffer=2; - break; - - case 3: //Stop - frame[I]=0; - sommeBuffer+=3; - - if(sommeBuffer>255){ - sommeBuffer++; - } - sommeBuffer=~sommeBuffer; - sommeBuffer=sommeBuffer%256; - - if(sommeBuffer==0){ //Les sommes concordent, les données sont valides - frame[I-1]=0; - return 0; - }else{ //Message invalidé par la somme - frame[0]=0; - return 1; - } - - default : - frame[I]=r; - sommeBuffer+=r; - I++; - break; - } - } - } -} - -int controle(char *frame){ - if (*frame!=6) return 0; - else return 1; -} - -void buzz_pulse(){ - -} - -void startEmision(){ - -} - - //Emission d'une trame, reception d'acquittement et vérification -int main() { - char trame[50]; - int essai, error; - int value; - char message[4]; - int i=0; - Timer time; - - //int mem[2] = {-1, -1}; - - while(1){ - - time.reset(); - time.start(); - value=min(sensor1.distance(1),230); - value=max(value,0); - - /*if(mem[0]<0){ - mem[0]=value; - }else{ - if(abs(mem[0]-value)<40){ - mem[1]=mem[0]; - mem[0]=value; - }else{ - value=mem[0]; - } - }*/ - - - message[0]=48+(value/100)%10; - message[1]=48+(value/10)%10; - message[2]=48+value%10; +//Génere la trame et l'envoie à UART +void transmit_data(){ + message[0]=48+(distance/100)%10; + message[1]=48+(distance/10)%10; + message[2]=48+distance%10; message[3]=0; essai=0; printf("%s\n\r",message); send(message); - - error=receive(trame); + + err=receive(trame); - while((controle(trame)==0 || error==1) && essai<3){ + while((controle(trame)==0 || err==1) && essai<3){ send(message); - error=receive(trame); + err=receive(trame); essai++; } +} - if(essai<=3) { - while(time.read() <= 1){ - for(i =0; i<=50;i++) { - buzz = 1; - wait_us(400); - buzz = 0; - wait_us(400); - } - wait_ms(value*5); - } - time.stop(); - } - } +//Stop le timer et récupére la distance en cm. +void echo_fall(){ + distance_time.stop(); + distance_brut = distance_time.read_us() /29 / 2; + distance_brut = min(distance_brut,230); + distance = max(distance_brut,0); + //Mise à jour de l'intervale entre chaque bip + buzzo.changeMode(distance); +} + + + + +int main() { + + echo.rise(&echo_rise); //Routine echo_rise sur interruption de echo en front montant. + echo.fall(&echo_fall); //Routine echo_fall sur interruption de echo en front descendant. + mesure_time.attach(&send_burst, 0.5); //Lance send_burst toutes les 500ms. + wait(0.1); //Décalage entre send et transmit. + send_time.attach(&transmit_data, 0.5); //Lance transmist_data toutes les 500ms. + wait(0.1); //Décalage entre transmit et buzz. + bip.attach_us(&buzzo, &Buzzer::run, 400); + + while(1); + } \ No newline at end of file