joe carter
/
nitrocarproject
main_programs/main4.h@0:960b355eaa84, 2010-08-20 (annotated)
- Committer:
- joe
- Date:
- Fri Aug 20 11:18:40 2010 +0000
- Revision:
- 0:960b355eaa84
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joe | 0:960b355eaa84 | 1 | #include "mbed.h" |
joe | 0:960b355eaa84 | 2 | #include "TextLCD.h" |
joe | 0:960b355eaa84 | 3 | #include "Counter.h" |
joe | 0:960b355eaa84 | 4 | #include "LIS302.h" |
joe | 0:960b355eaa84 | 5 | #include "Servo.h" |
joe | 0:960b355eaa84 | 6 | |
joe | 0:960b355eaa84 | 7 | Servo myservo(p21); |
joe | 0:960b355eaa84 | 8 | LIS302 acc (p5,p6,p7,p8); |
joe | 0:960b355eaa84 | 9 | DigitalIn enable(p20); |
joe | 0:960b355eaa84 | 10 | DigitalOut LIS302(LIS302); |
joe | 0:960b355eaa84 | 11 | DigitalOut led1(LED1); |
joe | 0:960b355eaa84 | 12 | DigitalOut led2(LED2); |
joe | 0:960b355eaa84 | 13 | DigitalOut led3(LED3); |
joe | 0:960b355eaa84 | 14 | DigitalOut led4(LED4); |
joe | 0:960b355eaa84 | 15 | #define TICK_PERIOD 1.0 |
joe | 0:960b355eaa84 | 16 | LocalFileSystem local("local"); |
joe | 0:960b355eaa84 | 17 | Counter counter(p18); |
joe | 0:960b355eaa84 | 18 | |
joe | 0:960b355eaa84 | 19 | |
joe | 0:960b355eaa84 | 20 | Ticker tick; |
joe | 0:960b355eaa84 | 21 | |
joe | 0:960b355eaa84 | 22 | Timer t; |
joe | 0:960b355eaa84 | 23 | volatile int tick_active = 0; |
joe | 0:960b355eaa84 | 24 | |
joe | 0:960b355eaa84 | 25 | void dotick (void) { |
joe | 0:960b355eaa84 | 26 | tick_active = 1; |
joe | 0:960b355eaa84 | 27 | led1 = !led1; |
joe | 0:960b355eaa84 | 28 | } |
joe | 0:960b355eaa84 | 29 | int rpm_counter=0; |
joe | 0:960b355eaa84 | 30 | int main() { |
joe | 0:960b355eaa84 | 31 | |
joe | 0:960b355eaa84 | 32 | float samples [5] = {0}; |
joe | 0:960b355eaa84 | 33 | int index = 0; |
joe | 0:960b355eaa84 | 34 | int flipped_right = 1; |
joe | 0:960b355eaa84 | 35 | int flipped_left = 1; |
joe | 0:960b355eaa84 | 36 | |
joe | 0:960b355eaa84 | 37 | TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3 |
joe | 0:960b355eaa84 | 38 | FILE *fp = fopen("/local/car data.csv", "w"); |
joe | 0:960b355eaa84 | 39 | fprintf(fp, "RPM,X,Y,Z\n"); |
joe | 0:960b355eaa84 | 40 | tick.attach(dotick,1.0); |
joe | 0:960b355eaa84 | 41 | while (1) { |
joe | 0:960b355eaa84 | 42 | if (enable) { |
joe | 0:960b355eaa84 | 43 | |
joe | 0:960b355eaa84 | 44 | t.start (); |
joe | 0:960b355eaa84 | 45 | |
joe | 0:960b355eaa84 | 46 | |
joe | 0:960b355eaa84 | 47 | while (enable) { |
joe | 0:960b355eaa84 | 48 | led4 = 0; |
joe | 0:960b355eaa84 | 49 | while (tick_active == 0) {} |
joe | 0:960b355eaa84 | 50 | led2 = !led2; |
joe | 0:960b355eaa84 | 51 | rpm_counter = counter.read(); |
joe | 0:960b355eaa84 | 52 | counter.reset(); |
joe | 0:960b355eaa84 | 53 | |
joe | 0:960b355eaa84 | 54 | fprintf(fp,"%d,%.2f,%.2f,%.2f\n",60*rpm_counter,acc.x(),acc.y(),acc.z()); |
joe | 0:960b355eaa84 | 55 | |
joe | 0:960b355eaa84 | 56 | tick_active = 0; |
joe | 0:960b355eaa84 | 57 | led3 = !led3; |
joe | 0:960b355eaa84 | 58 | |
joe | 0:960b355eaa84 | 59 | samples[index] = acc.y(); |
joe | 0:960b355eaa84 | 60 | index++; |
joe | 0:960b355eaa84 | 61 | if (index >= 5) { |
joe | 0:960b355eaa84 | 62 | index = 0; |
joe | 0:960b355eaa84 | 63 | } |
joe | 0:960b355eaa84 | 64 | int i; |
joe | 0:960b355eaa84 | 65 | |
joe | 0:960b355eaa84 | 66 | flipped_right = 1; |
joe | 0:960b355eaa84 | 67 | flipped_left =1; |
joe | 0:960b355eaa84 | 68 | |
joe | 0:960b355eaa84 | 69 | for (i=0; i<5; i++) { |
joe | 0:960b355eaa84 | 70 | if (samples[i] > -0.8) { |
joe | 0:960b355eaa84 | 71 | flipped_left = 0; |
joe | 0:960b355eaa84 | 72 | } |
joe | 0:960b355eaa84 | 73 | |
joe | 0:960b355eaa84 | 74 | if (samples[i] < 0.8) { |
joe | 0:960b355eaa84 | 75 | flipped_right = 0; |
joe | 0:960b355eaa84 | 76 | } |
joe | 0:960b355eaa84 | 77 | } |
joe | 0:960b355eaa84 | 78 | |
joe | 0:960b355eaa84 | 79 | if (flipped_left||flipped_right||!enable) { |
joe | 0:960b355eaa84 | 80 | |
joe | 0:960b355eaa84 | 81 | |
joe | 0:960b355eaa84 | 82 | myservo = 1; |
joe | 0:960b355eaa84 | 83 | |
joe | 0:960b355eaa84 | 84 | t.stop(); |
joe | 0:960b355eaa84 | 85 | |
joe | 0:960b355eaa84 | 86 | fprintf(fp,"this run lasted %f seconds \n", t.read()); |
joe | 0:960b355eaa84 | 87 | fprintf(fp,"off,off,off,off"); |
joe | 0:960b355eaa84 | 88 | fclose(fp); |
joe | 0:960b355eaa84 | 89 | led4 = !led4; |
joe | 0:960b355eaa84 | 90 | led2 = 0; |
joe | 0:960b355eaa84 | 91 | led3 = 0; |
joe | 0:960b355eaa84 | 92 | } |
joe | 0:960b355eaa84 | 93 | } |
joe | 0:960b355eaa84 | 94 | } |
joe | 0:960b355eaa84 | 95 | } |
joe | 0:960b355eaa84 | 96 | } |