Kart+xxxxx // Radar_RTOS
Dependencies: Radar_RTOS mbed
Fork of Radar_RTOS by
main.cpp@3:422918db0559, 2016-01-15 (annotated)
- Committer:
- Alex_Hochart
- Date:
- Fri Jan 15 08:44:59 2016 +0000
- Revision:
- 3:422918db0559
- Parent:
- 2:97cbf2a5ec78
Kart+xxxx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mickado | 0:d93aff6b153f | 1 | #include "mbed.h" |
Mickado | 0:d93aff6b153f | 2 | #include <string.h> |
Alex_Hochart | 2:97cbf2a5ec78 | 3 | #include "UART_Radar.h" |
Alex_Hochart | 2:97cbf2a5ec78 | 4 | #include "buzzer.h" |
Mickado | 0:d93aff6b153f | 5 | |
Mickado | 0:d93aff6b153f | 6 | #define TRIG_1 PA_8 |
Mickado | 0:d93aff6b153f | 7 | #define ECHO_1 PC_7 |
Alex_Hochart | 2:97cbf2a5ec78 | 8 | |
Alex_Hochart | 3:422918db0559 | 9 | Serial pc(USBTX, USBRX); //reception --- tx, rx |
Alex_Hochart | 2:97cbf2a5ec78 | 10 | |
Alex_Hochart | 3:422918db0559 | 11 | //Instanciation des GPIOs. |
Alex_Hochart | 3:422918db0559 | 12 | DigitalOut trig(TRIG_1); //Sortie digital pour le trigger. |
Alex_Hochart | 3:422918db0559 | 13 | InterruptIn echo(ECHO_1); //Entrée digital sur interuption pour l'écho. |
Alex_Hochart | 3:422918db0559 | 14 | InterruptIn position(USER_BUTTON); //Entrée digital sur interuption pour le levier de vitesse. |
Alex_Hochart | 2:97cbf2a5ec78 | 15 | |
Alex_Hochart | 3:422918db0559 | 16 | //Instanciation du Timer. |
Alex_Hochart | 3:422918db0559 | 17 | Timer distance_time; //Timer pour la mesure de distance. |
Alex_Hochart | 3:422918db0559 | 18 | Ticker mesure_time; //Timer (fixe) avec interruption pour l'acquisition de la distance. |
Alex_Hochart | 3:422918db0559 | 19 | Ticker send_time; //Timer (fixe) avec interruption pour l'envoie de la distance. |
Alex_Hochart | 3:422918db0559 | 20 | Ticker bip; //Timer (fixe) avec interruption pour l'actualisation du buzzer. |
Mickado | 0:d93aff6b153f | 21 | |
Alex_Hochart | 3:422918db0559 | 22 | //Instanciation des variables. |
Alex_Hochart | 3:422918db0559 | 23 | Buzzer buzzo; |
Alex_Hochart | 3:422918db0559 | 24 | int distance; |
Alex_Hochart | 2:97cbf2a5ec78 | 25 | |
Alex_Hochart | 2:97cbf2a5ec78 | 26 | |
Alex_Hochart | 3:422918db0559 | 27 | /********************Fonction qui génére le burst pour la mesure de distance.****************************/ |
Alex_Hochart | 3:422918db0559 | 28 | //But : Générer un burst et reset le timer de sitance. |
Alex_Hochart | 3:422918db0559 | 29 | // |
Alex_Hochart | 3:422918db0559 | 30 | //Donnée : Le timer distance_time est un timer global. |
Alex_Hochart | 3:422918db0559 | 31 | // trig est un sortie digital. |
Alex_Hochart | 3:422918db0559 | 32 | // _____________ |
Alex_Hochart | 3:422918db0559 | 33 | // TRIG => ________/ \__.... |
Alex_Hochart | 3:422918db0559 | 34 | // 2 µs 10 µs |
Alex_Hochart | 3:422918db0559 | 35 | // |
Alex_Hochart | 2:97cbf2a5ec78 | 36 | void send_burst(){ |
Alex_Hochart | 2:97cbf2a5ec78 | 37 | distance_time.reset(); |
Alex_Hochart | 2:97cbf2a5ec78 | 38 | trig = 0; |
Alex_Hochart | 2:97cbf2a5ec78 | 39 | wait_us(2); |
Alex_Hochart | 2:97cbf2a5ec78 | 40 | trig = 1; |
Alex_Hochart | 2:97cbf2a5ec78 | 41 | wait_us(10); |
Alex_Hochart | 2:97cbf2a5ec78 | 42 | trig = 0; |
Alex_Hochart | 2:97cbf2a5ec78 | 43 | } |
Alex_Hochart | 3:422918db0559 | 44 | /*******************************************************************************************************/ |
Alex_Hochart | 3:422918db0559 | 45 | |
Alex_Hochart | 2:97cbf2a5ec78 | 46 | |
Mickado | 0:d93aff6b153f | 47 | |
Alex_Hochart | 3:422918db0559 | 48 | |
Alex_Hochart | 3:422918db0559 | 49 | /*******************************Fonction de minimum et maximum.*****************************************/ |
Alex_Hochart | 3:422918db0559 | 50 | //But : Renvoie le min et le max entre deux entier. |
Alex_Hochart | 3:422918db0559 | 51 | // |
Mickado | 0:d93aff6b153f | 52 | int max (int a, int b) { |
Mickado | 0:d93aff6b153f | 53 | return (a<b)?b:a; |
Mickado | 0:d93aff6b153f | 54 | } |
Mickado | 0:d93aff6b153f | 55 | int min (int a, int b) { |
Mickado | 0:d93aff6b153f | 56 | return (a<b)?a:b; |
Mickado | 0:d93aff6b153f | 57 | } |
Alex_Hochart | 3:422918db0559 | 58 | /********************************************************************************************************/ |
Mickado | 0:d93aff6b153f | 59 | |
Alex_Hochart | 3:422918db0559 | 60 | |
Alex_Hochart | 3:422918db0559 | 61 | /******************Routine d'interruption sur front montant du signal echo.*****************************/ |
Alex_Hochart | 3:422918db0559 | 62 | //But : Lancer le timer. |
Alex_Hochart | 3:422918db0559 | 63 | // |
Alex_Hochart | 3:422918db0559 | 64 | //Donnée : Le timer distance_time est un timer global. |
Alex_Hochart | 3:422918db0559 | 65 | // _.... |
Alex_Hochart | 3:422918db0559 | 66 | // ECHO => ....____/ Start timer. |
Alex_Hochart | 3:422918db0559 | 67 | // ^ |
Alex_Hochart | 3:422918db0559 | 68 | // |
Alex_Hochart | 2:97cbf2a5ec78 | 69 | void echo_rise(){ |
Alex_Hochart | 2:97cbf2a5ec78 | 70 | distance_time.start(); |
Mickado | 0:d93aff6b153f | 71 | } |
Alex_Hochart | 3:422918db0559 | 72 | /*********************************************************************************************************/ |
Mickado | 0:d93aff6b153f | 73 | |
Alex_Hochart | 3:422918db0559 | 74 | |
Alex_Hochart | 3:422918db0559 | 75 | |
Alex_Hochart | 3:422918db0559 | 76 | /***************************Routine d'envoie de la distance via l'uart.***********************************/ |
Alex_Hochart | 3:422918db0559 | 77 | //But : Transformer la distance en chaine de caractéres. |
Alex_Hochart | 3:422918db0559 | 78 | // Envoier via la fonction send() et attendre un acquitement. |
Alex_Hochart | 3:422918db0559 | 79 | // Error si on recoie 3 non-acquitement succésif ou atteinte du timeout. |
Alex_Hochart | 3:422918db0559 | 80 | // |
Alex_Hochart | 3:422918db0559 | 81 | //Donnée : L'entier distance est une variable global. |
Alex_Hochart | 3:422918db0559 | 82 | // La chaine message contient la distance sur 3 caractére + '\0'. |
Alex_Hochart | 3:422918db0559 | 83 | // L'envoie est répété 3 fois quand il y a erreur. Si 3 erreurs, arret des émission. |
Alex_Hochart | 3:422918db0559 | 84 | // Il y a erreur si : receive ne recoie pas d'acquitement dans un temps impartie. |
Alex_Hochart | 3:422918db0559 | 85 | // controle renvoie 0, qui corespond à une erreur de checksum. |
Alex_Hochart | 2:97cbf2a5ec78 | 86 | void transmit_data(){ |
Alex_Hochart | 3:422918db0559 | 87 | |
Alex_Hochart | 3:422918db0559 | 88 | int essai, err; |
Alex_Hochart | 3:422918db0559 | 89 | char message[4]; |
Alex_Hochart | 3:422918db0559 | 90 | char trame[2]; |
Alex_Hochart | 2:97cbf2a5ec78 | 91 | message[0]=48+(distance/100)%10; |
Alex_Hochart | 2:97cbf2a5ec78 | 92 | message[1]=48+(distance/10)%10; |
Alex_Hochart | 2:97cbf2a5ec78 | 93 | message[2]=48+distance%10; |
Alex_Hochart | 1:8558d2451e3e | 94 | message[3]=0; |
Mickado | 0:d93aff6b153f | 95 | |
Alex_Hochart | 1:8558d2451e3e | 96 | essai=0; |
Alex_Hochart | 1:8558d2451e3e | 97 | send(message); |
Alex_Hochart | 2:97cbf2a5ec78 | 98 | |
Alex_Hochart | 3:422918db0559 | 99 | |
Alex_Hochart | 3:422918db0559 | 100 | pc.printf("%c%c%c\n\r",message[0],message[1],message[2]); |
Alex_Hochart | 3:422918db0559 | 101 | err=receive(trame); //Bug avec le nouvelle affichage |
Alex_Hochart | 3:422918db0559 | 102 | pc.printf("error%d\n\r",err); |
Alex_Hochart | 3:422918db0559 | 103 | |
Alex_Hochart | 3:422918db0559 | 104 | while((controle(trame)==1 || err!=0) && essai < 3){ |
Alex_Hochart | 3:422918db0559 | 105 | essai++; |
Alex_Hochart | 3:422918db0559 | 106 | if( essai >= 3) while(1); |
Alex_Hochart | 1:8558d2451e3e | 107 | send(message); |
Alex_Hochart | 2:97cbf2a5ec78 | 108 | err=receive(trame); |
Alex_Hochart | 3:422918db0559 | 109 | pc.printf("error%d\n\r",err); |
Alex_Hochart | 3:422918db0559 | 110 | |
Alex_Hochart | 1:8558d2451e3e | 111 | } |
Alex_Hochart | 2:97cbf2a5ec78 | 112 | } |
Alex_Hochart | 3:422918db0559 | 113 | /**********************************************************************************************************/ |
Alex_Hochart | 2:97cbf2a5ec78 | 114 | |
Alex_Hochart | 2:97cbf2a5ec78 | 115 | |
Alex_Hochart | 2:97cbf2a5ec78 | 116 | |
Alex_Hochart | 2:97cbf2a5ec78 | 117 | |
Alex_Hochart | 3:422918db0559 | 118 | /*********************Routine d'interruption sur front descendant du signal echo.**************************/ |
Alex_Hochart | 3:422918db0559 | 119 | //But : Stoper le timer, récupérer la distance en cm |
Alex_Hochart | 3:422918db0559 | 120 | // et modifier le silence entre deux bip du buzzer |
Alex_Hochart | 3:422918db0559 | 121 | // |
Alex_Hochart | 3:422918db0559 | 122 | //Donnée : Le timer distance_time est un timer global. |
Alex_Hochart | 3:422918db0559 | 123 | // L'entier distance est une variable global. |
Alex_Hochart | 3:422918db0559 | 124 | // Le buzzer buzzo est un objet global. |
Alex_Hochart | 3:422918db0559 | 125 | // _____________________________________ |
Alex_Hochart | 3:422918db0559 | 126 | // ECHO => ________/ Distant = Temps / 29/2 \__.... Stop timer. |
Alex_Hochart | 3:422918db0559 | 127 | // ^ |
Alex_Hochart | 3:422918db0559 | 128 | // |
Alex_Hochart | 3:422918db0559 | 129 | void echo_fall(){ |
Alex_Hochart | 3:422918db0559 | 130 | distance_time.stop(); //Stop le timer. |
Alex_Hochart | 3:422918db0559 | 131 | |
Alex_Hochart | 3:422918db0559 | 132 | distance = distance_time.read_us() /29 / 2; //Converti le temps (µs) en distance (cm). |
Alex_Hochart | 3:422918db0559 | 133 | distance = min(distance,230); //Definie la valeur max de la distance à 230. |
Alex_Hochart | 3:422918db0559 | 134 | distance = max(distance,0); //Definie la valeur min de la distance à 0. |
Alex_Hochart | 3:422918db0559 | 135 | |
Alex_Hochart | 3:422918db0559 | 136 | buzzo.changeMode(distance); //Mise à jour de l'intervale entre chaque bip |
Alex_Hochart | 3:422918db0559 | 137 | } |
Alex_Hochart | 3:422918db0559 | 138 | /**********************************************************************************************************/ |
Alex_Hochart | 3:422918db0559 | 139 | |
Alex_Hochart | 3:422918db0559 | 140 | /******************Routine d'interruption sur front montant du signal pos.*****************************/ |
Alex_Hochart | 3:422918db0559 | 141 | //But : Lancer les tâches. |
Alex_Hochart | 3:422918db0559 | 142 | // |
Alex_Hochart | 3:422918db0559 | 143 | // |
Alex_Hochart | 3:422918db0559 | 144 | void position_rise(){ |
Alex_Hochart | 3:422918db0559 | 145 | |
Alex_Hochart | 3:422918db0559 | 146 | echo.rise(NULL); //Routine NULL sur interruption de echo en front montant. |
Alex_Hochart | 3:422918db0559 | 147 | echo.fall(NULL); //Routine NULL sur interruption de echo en front descendant. |
Alex_Hochart | 3:422918db0559 | 148 | mesure_time.detach(); //Stop la routine send_burst. |
Alex_Hochart | 3:422918db0559 | 149 | send_time.detach(); //Stop la routine transmist_data. |
Alex_Hochart | 3:422918db0559 | 150 | bip.detach(); //Stop la routine l'actualisation du buzzer. |
Alex_Hochart | 3:422918db0559 | 151 | |
Alex_Hochart | 3:422918db0559 | 152 | } |
Alex_Hochart | 3:422918db0559 | 153 | /*********************************************************************************************************/ |
Alex_Hochart | 3:422918db0559 | 154 | |
Alex_Hochart | 3:422918db0559 | 155 | /******************Routine d'interruption sur front descendant du signal pos.*****************************/ |
Alex_Hochart | 3:422918db0559 | 156 | //But : Stop les tâches. |
Alex_Hochart | 3:422918db0559 | 157 | // |
Alex_Hochart | 3:422918db0559 | 158 | // |
Alex_Hochart | 3:422918db0559 | 159 | void position_fall(){ |
Alex_Hochart | 3:422918db0559 | 160 | |
Alex_Hochart | 3:422918db0559 | 161 | echo.rise(&echo_rise); //Routine echo_rise sur interruption de echo en front montant. |
Alex_Hochart | 3:422918db0559 | 162 | echo.fall(&echo_fall); //Routine echo_fall sur interruption de echo en front descendant. |
Alex_Hochart | 3:422918db0559 | 163 | mesure_time.attach(&send_burst, 0.5); //Lance send_burst toutes les 500ms. |
Alex_Hochart | 3:422918db0559 | 164 | wait(0.1); //Décalage entre send et transmit. |
Alex_Hochart | 3:422918db0559 | 165 | send_time.attach(&transmit_data, 0.5); //Lance transmist_data toutes les 500ms. |
Alex_Hochart | 3:422918db0559 | 166 | wait(0.1); //Décalage entre transmit et buzz. |
Alex_Hochart | 3:422918db0559 | 167 | bip.attach_us(&buzzo, &Buzzer::run, 400); //Lance l'actualisation du buzzer toutes les 400µs ( F = 2.5 kHz ). |
Alex_Hochart | 3:422918db0559 | 168 | |
Alex_Hochart | 3:422918db0559 | 169 | } |
Alex_Hochart | 3:422918db0559 | 170 | /*********************************************************************************************************/ |
Alex_Hochart | 3:422918db0559 | 171 | |
Alex_Hochart | 3:422918db0559 | 172 | |
Alex_Hochart | 3:422918db0559 | 173 | |
Alex_Hochart | 2:97cbf2a5ec78 | 174 | int main() { |
Alex_Hochart | 3:422918db0559 | 175 | |
Alex_Hochart | 3:422918db0559 | 176 | position.rise(&position_rise); //Routine position_rise sur interruption de position en front montant. |
Alex_Hochart | 3:422918db0559 | 177 | position.fall(&position_fall); //Routine position_fall sur interruption de position en front descendant. |
Alex_Hochart | 3:422918db0559 | 178 | |
Alex_Hochart | 2:97cbf2a5ec78 | 179 | while(1); |
Alex_Hochart | 2:97cbf2a5ec78 | 180 | |
Mickado | 0:d93aff6b153f | 181 | } |