Gestione Timer per roboval
Timer di misura per roboval. Il programma è stato scritto per la scheda Nucleo STM32F401RE, prevede la misura del tempo totale e dei parziali sui tre giri
main.cpp@2:9fc2b9475b38, 2016-04-22 (annotated)
- Committer:
- fdalforno
- Date:
- Fri Apr 22 07:25:05 2016 +0000
- Revision:
- 2:9fc2b9475b38
- Parent:
- 1:289167b04f0d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fdalforno | 0:c7170b5cd312 | 1 | #include <stdlib.h> |
fdalforno | 0:c7170b5cd312 | 2 | |
fdalforno | 0:c7170b5cd312 | 3 | #include "mbed.h" |
fdalforno | 0:c7170b5cd312 | 4 | #include "TextLCD.h" |
fdalforno | 0:c7170b5cd312 | 5 | |
fdalforno | 0:c7170b5cd312 | 6 | #define NUM_LAP 3 |
fdalforno | 2:9fc2b9475b38 | 7 | #define VBAT_MIN 7.2 |
fdalforno | 0:c7170b5cd312 | 8 | |
fdalforno | 0:c7170b5cd312 | 9 | typedef struct time_screen { |
fdalforno | 0:c7170b5cd312 | 10 | int cents; |
fdalforno | 0:c7170b5cd312 | 11 | int seconds; |
fdalforno | 0:c7170b5cd312 | 12 | int minutes; |
fdalforno | 0:c7170b5cd312 | 13 | } measured_time; |
fdalforno | 0:c7170b5cd312 | 14 | |
fdalforno | 0:c7170b5cd312 | 15 | |
fdalforno | 0:c7170b5cd312 | 16 | DigitalOut heartbeat(LED1); |
fdalforno | 1:289167b04f0d | 17 | AnalogIn vbat(PC_3); |
fdalforno | 0:c7170b5cd312 | 18 | |
fdalforno | 0:c7170b5cd312 | 19 | InterruptIn user_button(USER_BUTTON); |
fdalforno | 0:c7170b5cd312 | 20 | InterruptIn proximity(D9); |
fdalforno | 0:c7170b5cd312 | 21 | |
fdalforno | 0:c7170b5cd312 | 22 | Timer t; |
fdalforno | 1:289167b04f0d | 23 | TextLCD lcd(D2,D3,D4,D5,D6,D7); |
fdalforno | 0:c7170b5cd312 | 24 | |
fdalforno | 0:c7170b5cd312 | 25 | int lap = 0; |
fdalforno | 0:c7170b5cd312 | 26 | int last_read = 0; |
fdalforno | 0:c7170b5cd312 | 27 | int lap_time = 0; |
fdalforno | 0:c7170b5cd312 | 28 | |
fdalforno | 0:c7170b5cd312 | 29 | |
fdalforno | 0:c7170b5cd312 | 30 | //Conversione da millisecondi a mm:ss:cc |
fdalforno | 0:c7170b5cd312 | 31 | measured_time human_read(int ms){ |
fdalforno | 0:c7170b5cd312 | 32 | measured_time read; |
fdalforno | 0:c7170b5cd312 | 33 | div_t qr = div(ms,1000); |
fdalforno | 0:c7170b5cd312 | 34 | |
fdalforno | 0:c7170b5cd312 | 35 | read.cents = qr.rem % 100; |
fdalforno | 0:c7170b5cd312 | 36 | |
fdalforno | 0:c7170b5cd312 | 37 | qr = div(qr.quot,60); |
fdalforno | 0:c7170b5cd312 | 38 | read.seconds = qr.rem; |
fdalforno | 0:c7170b5cd312 | 39 | |
fdalforno | 0:c7170b5cd312 | 40 | qr = div(qr.quot,60); |
fdalforno | 0:c7170b5cd312 | 41 | read.minutes = qr.rem; |
fdalforno | 0:c7170b5cd312 | 42 | |
fdalforno | 0:c7170b5cd312 | 43 | return read; |
fdalforno | 0:c7170b5cd312 | 44 | } |
fdalforno | 0:c7170b5cd312 | 45 | |
fdalforno | 0:c7170b5cd312 | 46 | void measure_time(){ |
fdalforno | 0:c7170b5cd312 | 47 | int read = t.read_ms(); |
fdalforno | 0:c7170b5cd312 | 48 | |
fdalforno | 0:c7170b5cd312 | 49 | if(lap == 0){ |
fdalforno | 0:c7170b5cd312 | 50 | t.start(); |
fdalforno | 0:c7170b5cd312 | 51 | lap++; |
fdalforno | 0:c7170b5cd312 | 52 | }else{ |
fdalforno | 0:c7170b5cd312 | 53 | //Dabouncing per evitare problemi |
fdalforno | 1:289167b04f0d | 54 | if(read - last_read > 1000){ |
fdalforno | 0:c7170b5cd312 | 55 | |
fdalforno | 0:c7170b5cd312 | 56 | lap_time = read - last_read; |
fdalforno | 0:c7170b5cd312 | 57 | |
fdalforno | 1:289167b04f0d | 58 | if(lap >= NUM_LAP){ |
fdalforno | 0:c7170b5cd312 | 59 | t.stop(); |
fdalforno | 1:289167b04f0d | 60 | lap++; |
fdalforno | 0:c7170b5cd312 | 61 | }else{ |
fdalforno | 0:c7170b5cd312 | 62 | lap++; |
fdalforno | 0:c7170b5cd312 | 63 | } |
fdalforno | 0:c7170b5cd312 | 64 | |
fdalforno | 0:c7170b5cd312 | 65 | last_read = read; |
fdalforno | 0:c7170b5cd312 | 66 | } |
fdalforno | 0:c7170b5cd312 | 67 | } |
fdalforno | 0:c7170b5cd312 | 68 | |
fdalforno | 0:c7170b5cd312 | 69 | } |
fdalforno | 0:c7170b5cd312 | 70 | |
fdalforno | 0:c7170b5cd312 | 71 | void reset_measure(){ |
fdalforno | 1:289167b04f0d | 72 | t.stop(); |
fdalforno | 0:c7170b5cd312 | 73 | t.reset(); |
fdalforno | 0:c7170b5cd312 | 74 | lap = 0; |
fdalforno | 0:c7170b5cd312 | 75 | last_read = 0; |
fdalforno | 1:289167b04f0d | 76 | lcd.cls(); |
fdalforno | 0:c7170b5cd312 | 77 | } |
fdalforno | 0:c7170b5cd312 | 78 | |
fdalforno | 0:c7170b5cd312 | 79 | |
fdalforno | 0:c7170b5cd312 | 80 | |
fdalforno | 0:c7170b5cd312 | 81 | |
fdalforno | 0:c7170b5cd312 | 82 | |
fdalforno | 0:c7170b5cd312 | 83 | int main() { |
fdalforno | 0:c7170b5cd312 | 84 | |
fdalforno | 0:c7170b5cd312 | 85 | proximity.mode(PullDown); |
fdalforno | 0:c7170b5cd312 | 86 | proximity.rise(&measure_time); |
fdalforno | 0:c7170b5cd312 | 87 | user_button.fall(&reset_measure); |
fdalforno | 0:c7170b5cd312 | 88 | |
fdalforno | 2:9fc2b9475b38 | 89 | |
fdalforno | 1:289167b04f0d | 90 | |
fdalforno | 1:289167b04f0d | 91 | |
fdalforno | 0:c7170b5cd312 | 92 | while(true) { |
fdalforno | 0:c7170b5cd312 | 93 | int read = t.read_ms(); |
fdalforno | 0:c7170b5cd312 | 94 | |
fdalforno | 0:c7170b5cd312 | 95 | measured_time time = human_read(read); |
fdalforno | 0:c7170b5cd312 | 96 | |
fdalforno | 0:c7170b5cd312 | 97 | lcd.locate(0,0); |
fdalforno | 0:c7170b5cd312 | 98 | lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents); |
fdalforno | 0:c7170b5cd312 | 99 | |
fdalforno | 0:c7170b5cd312 | 100 | //Gestione dei parziali |
fdalforno | 0:c7170b5cd312 | 101 | if(lap > 1){ |
fdalforno | 0:c7170b5cd312 | 102 | time = human_read(lap_time); |
fdalforno | 0:c7170b5cd312 | 103 | lcd.locate(0,1); |
fdalforno | 0:c7170b5cd312 | 104 | lcd.printf("Giro %d %02d:%02d:%02d",lap - 1,time.minutes,time.seconds,time.cents); |
fdalforno | 0:c7170b5cd312 | 105 | } |
fdalforno | 0:c7170b5cd312 | 106 | |
fdalforno | 0:c7170b5cd312 | 107 | heartbeat = !heartbeat; |
fdalforno | 0:c7170b5cd312 | 108 | |
fdalforno | 0:c7170b5cd312 | 109 | |
fdalforno | 1:289167b04f0d | 110 | if(lap > 0 && lap <= NUM_LAP){ |
fdalforno | 1:289167b04f0d | 111 | wait(0.1); |
fdalforno | 0:c7170b5cd312 | 112 | }else{ |
fdalforno | 1:289167b04f0d | 113 | if(lap == 0){ |
fdalforno | 2:9fc2b9475b38 | 114 | //Controllo batteria con partitore resistivo |
fdalforno | 2:9fc2b9475b38 | 115 | double battery = ((3.3L * vbat) * 57) / 10; |
fdalforno | 1:289167b04f0d | 116 | if(battery < VBAT_MIN){ |
fdalforno | 1:289167b04f0d | 117 | lcd.locate(0,1); |
fdalforno | 1:289167b04f0d | 118 | lcd.printf("LOW BAT %2.1f",battery); |
fdalforno | 1:289167b04f0d | 119 | }else{ |
fdalforno | 1:289167b04f0d | 120 | lcd.locate(0,1); |
fdalforno | 1:289167b04f0d | 121 | lcd.printf("OK BAT %2.1f",battery); |
fdalforno | 1:289167b04f0d | 122 | } |
fdalforno | 1:289167b04f0d | 123 | } |
fdalforno | 0:c7170b5cd312 | 124 | wait(1); |
fdalforno | 0:c7170b5cd312 | 125 | } |
fdalforno | 0:c7170b5cd312 | 126 | } |
fdalforno | 0:c7170b5cd312 | 127 | } |