Radar_RTOS

Dependencies:   mbed

Dependents:   Radar_RTOS_Kart

Fork of Nucleo_radar_buz by Contest IOT GSE5

Revision:
2:97cbf2a5ec78
Parent:
1:8558d2451e3e
--- 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