Alain Pegatoquet
/
chrono_ER1
Chrono pour mesurer vitesse pour 1 tour (capteur analogique)
main.cpp@1:d5f22105a691, 2017-11-28 (annotated)
- Committer:
- alainpegatoquet
- Date:
- Tue Nov 28 11:35:11 2017 +0000
- Revision:
- 1:d5f22105a691
- Parent:
- 0:cf0fbe1e01d4
Chrono valid? (capteur a 20 cm distance de la ligne)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alainpegatoquet | 0:cf0fbe1e01d4 | 1 | #include "mbed.h" |
alainpegatoquet | 1:d5f22105a691 | 2 | #define DIST 15 |
alainpegatoquet | 1:d5f22105a691 | 3 | #define DEBUG 0 |
alainpegatoquet | 0:cf0fbe1e01d4 | 4 | |
alainpegatoquet | 0:cf0fbe1e01d4 | 5 | // Global variables |
alainpegatoquet | 0:cf0fbe1e01d4 | 6 | Serial pc(USBTX, USBRX); |
alainpegatoquet | 0:cf0fbe1e01d4 | 7 | AnalogIn Ain(A3); |
alainpegatoquet | 0:cf0fbe1e01d4 | 8 | Timer timer1; |
alainpegatoquet | 0:cf0fbe1e01d4 | 9 | |
alainpegatoquet | 0:cf0fbe1e01d4 | 10 | float IrSensor; |
alainpegatoquet | 1:d5f22105a691 | 11 | float dist_new = 0.0; |
alainpegatoquet | 1:d5f22105a691 | 12 | float dist_old = 0.0; |
alainpegatoquet | 1:d5f22105a691 | 13 | int elapsed_time = 0; |
alainpegatoquet | 0:cf0fbe1e01d4 | 14 | // Prototype |
alainpegatoquet | 0:cf0fbe1e01d4 | 15 | float distance(float); |
alainpegatoquet | 0:cf0fbe1e01d4 | 16 | |
alainpegatoquet | 0:cf0fbe1e01d4 | 17 | // main function |
alainpegatoquet | 0:cf0fbe1e01d4 | 18 | int main(void) |
alainpegatoquet | 0:cf0fbe1e01d4 | 19 | { |
alainpegatoquet | 0:cf0fbe1e01d4 | 20 | pc.printf("Debut du programme...\n"); |
alainpegatoquet | 1:d5f22105a691 | 21 | timer1.start(); |
alainpegatoquet | 0:cf0fbe1e01d4 | 22 | |
alainpegatoquet | 0:cf0fbe1e01d4 | 23 | while(1) { |
alainpegatoquet | 0:cf0fbe1e01d4 | 24 | IrSensor=Ain.read(); // IrSensor belongs to [0.0; 1.0] |
alainpegatoquet | 1:d5f22105a691 | 25 | //pc.printf("Distance en cm : %.1f \n\r",distance(IrSensor*3.3)); |
alainpegatoquet | 1:d5f22105a691 | 26 | #if DEBUG |
alainpegatoquet | 0:cf0fbe1e01d4 | 27 | pc.printf("%f \n\r",IrSensor*3.3); // Output voltage from 0 to 3.3V |
alainpegatoquet | 0:cf0fbe1e01d4 | 28 | pc.printf("Distance en cm : %.1f \n\r",distance(IrSensor*3.3)); |
alainpegatoquet | 1:d5f22105a691 | 29 | #endif |
alainpegatoquet | 1:d5f22105a691 | 30 | dist_old = dist_new; |
alainpegatoquet | 1:d5f22105a691 | 31 | dist_new = distance(IrSensor*3.3); |
alainpegatoquet | 1:d5f22105a691 | 32 | if ((dist_old > 40) && ((dist_new> 0) && (dist_new < 30))) |
alainpegatoquet | 1:d5f22105a691 | 33 | { |
alainpegatoquet | 1:d5f22105a691 | 34 | elapsed_time = timer1.read_ms(); |
alainpegatoquet | 1:d5f22105a691 | 35 | timer1.reset(); |
alainpegatoquet | 1:d5f22105a691 | 36 | pc.printf("************* %2.2f sec *************\r\n",(float)elapsed_time/1000.0); |
alainpegatoquet | 1:d5f22105a691 | 37 | wait_ms(1000); |
alainpegatoquet | 1:d5f22105a691 | 38 | } |
alainpegatoquet | 1:d5f22105a691 | 39 | wait(0.1); |
alainpegatoquet | 1:d5f22105a691 | 40 | float val = timer1.read_ms()/1000.0; |
alainpegatoquet | 1:d5f22105a691 | 41 | pc.printf("%3.2f sec\r",val); |
alainpegatoquet | 1:d5f22105a691 | 42 | |
alainpegatoquet | 0:cf0fbe1e01d4 | 43 | } |
alainpegatoquet | 1:d5f22105a691 | 44 | timer1.stop(); |
alainpegatoquet | 0:cf0fbe1e01d4 | 45 | } |
alainpegatoquet | 0:cf0fbe1e01d4 | 46 | |
alainpegatoquet | 0:cf0fbe1e01d4 | 47 | // Calcul de la distance |
alainpegatoquet | 0:cf0fbe1e01d4 | 48 | // Entre 20 et 80cm, on peut approximer la distance avec r=24/(V-0,1) |
alainpegatoquet | 0:cf0fbe1e01d4 | 49 | // r = distance en cm |
alainpegatoquet | 0:cf0fbe1e01d4 | 50 | // V = tension lue |
alainpegatoquet | 0:cf0fbe1e01d4 | 51 | float distance(float tension) |
alainpegatoquet | 0:cf0fbe1e01d4 | 52 | { |
alainpegatoquet | 0:cf0fbe1e01d4 | 53 | return(24.0/(tension - 0.1)); |
alainpegatoquet | 0:cf0fbe1e01d4 | 54 | } |
alainpegatoquet | 0:cf0fbe1e01d4 | 55 |