joe carter
/
nitrocarproject
main_programs/main.cpp@2:a079de4fd5b9, 2010-08-20 (annotated)
- Committer:
- joe
- Date:
- Fri Aug 20 15:38:52 2010 +0000
- Revision:
- 2:a079de4fd5b9
- Parent:
- 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 | #include "MSCFileSystem.h" |
joe | 0:960b355eaa84 | 7 | |
joe | 0:960b355eaa84 | 8 | Servo myservo(p21); |
joe | 0:960b355eaa84 | 9 | LIS302 acc (p5,p6,p7,p8); |
joe | 0:960b355eaa84 | 10 | DigitalIn on(p20); |
joe | 0:960b355eaa84 | 11 | DigitalOut LIS302(LIS302); |
joe | 0:960b355eaa84 | 12 | DigitalOut led1(LED1); |
joe | 0:960b355eaa84 | 13 | DigitalOut led2(LED2); |
joe | 0:960b355eaa84 | 14 | DigitalOut led3(LED3); |
joe | 0:960b355eaa84 | 15 | DigitalOut led4(LED4); |
joe | 0:960b355eaa84 | 16 | #define TICK_PERIOD 0.5 |
joe | 0:960b355eaa84 | 17 | MSCFileSystem fs("fs"); |
joe | 0:960b355eaa84 | 18 | Counter counter(p18); |
joe | 0:960b355eaa84 | 19 | |
joe | 0:960b355eaa84 | 20 | |
joe | 0:960b355eaa84 | 21 | Ticker tick; |
joe | 0:960b355eaa84 | 22 | |
joe | 0:960b355eaa84 | 23 | Timer timer; |
joe | 0:960b355eaa84 | 24 | volatile int tick_active = 0; |
joe | 0:960b355eaa84 | 25 | |
joe | 0:960b355eaa84 | 26 | void dotick (void) { |
joe | 0:960b355eaa84 | 27 | tick_active = 1; |
joe | 0:960b355eaa84 | 28 | |
joe | 0:960b355eaa84 | 29 | } |
joe | 0:960b355eaa84 | 30 | float samples [2] = {0}; |
joe | 0:960b355eaa84 | 31 | int index2 = 0; |
joe | 0:960b355eaa84 | 32 | int start = 1; |
joe | 0:960b355eaa84 | 33 | |
joe | 0:960b355eaa84 | 34 | char FileName[20]; // this is where we store the file name |
joe | 0:960b355eaa84 | 35 | |
joe | 0:960b355eaa84 | 36 | int rpm_counter=0; |
joe | 0:960b355eaa84 | 37 | int main() { |
joe | 0:960b355eaa84 | 38 | |
joe | 0:960b355eaa84 | 39 | float samples [1] = {0}; |
joe | 0:960b355eaa84 | 40 | int index = 0; |
joe | 0:960b355eaa84 | 41 | int flipped_right = 1; |
joe | 0:960b355eaa84 | 42 | int flipped_left = 1; |
joe | 0:960b355eaa84 | 43 | |
joe | 2:a079de4fd5b9 | 44 | |
joe | 0:960b355eaa84 | 45 | // read the current time, and con convert it to a DD/MM/YYYY HH:MM:SS structure |
joe | 0:960b355eaa84 | 46 | time_t seconds = time(NULL); |
joe | 0:960b355eaa84 | 47 | struct tm *t = localtime(&seconds); |
joe | 0:960b355eaa84 | 48 | |
joe | 0:960b355eaa84 | 49 | // use the time to make a new file name |
joe | 0:960b355eaa84 | 50 | // file name is /fs/ddmmHHMM.csv |
joe | 0:960b355eaa84 | 51 | // Note we have to add one to the month as it is represented as 0-11 rather than 1-12 :-( |
joe | 0:960b355eaa84 | 52 | sprintf(FileName,"/fs/%02d%02d%02d%02d.csv",t->tm_mday,1+(t->tm_mon),t->tm_hour,t->tm_min); |
joe | 0:960b355eaa84 | 53 | FILE *fp = fopen(FileName,"w"); |
joe | 0:960b355eaa84 | 54 | |
joe | 0:960b355eaa84 | 55 | |
joe | 0:960b355eaa84 | 56 | |
joe | 0:960b355eaa84 | 57 | fprintf(fp, "RPM,X,Y,Z\n");//this part prints the columns headers |
joe | 0:960b355eaa84 | 58 | |
joe | 0:960b355eaa84 | 59 | fclose(fp); |
joe | 0:960b355eaa84 | 60 | |
joe | 0:960b355eaa84 | 61 | tick.attach(dotick,0.5); |
joe | 0:960b355eaa84 | 62 | |
joe | 0:960b355eaa84 | 63 | while (1) { |
joe | 0:960b355eaa84 | 64 | while (tick_active == 0) {} |
joe | 0:960b355eaa84 | 65 | rpm_counter = counter.read(); |
joe | 0:960b355eaa84 | 66 | counter.reset(); |
joe | 0:960b355eaa84 | 67 | |
joe | 0:960b355eaa84 | 68 | tick_active = 0; |
joe | 0:960b355eaa84 | 69 | samples[index2] = 120*rpm_counter; |
joe | 0:960b355eaa84 | 70 | index2++; |
joe | 0:960b355eaa84 | 71 | if (index2 >= 2) { |
joe | 0:960b355eaa84 | 72 | index2 = 0; |
joe | 0:960b355eaa84 | 73 | } |
joe | 0:960b355eaa84 | 74 | int i; |
joe | 0:960b355eaa84 | 75 | start = 1; |
joe | 0:960b355eaa84 | 76 | for (i=0; i<2; i++) { |
joe | 0:960b355eaa84 | 77 | if (samples[i] <60) { |
joe | 0:960b355eaa84 | 78 | start = 0; |
joe | 0:960b355eaa84 | 79 | } |
joe | 0:960b355eaa84 | 80 | } |
joe | 0:960b355eaa84 | 81 | if (on) { |
joe | 0:960b355eaa84 | 82 | led2 = 1; |
joe | 0:960b355eaa84 | 83 | } |
joe | 0:960b355eaa84 | 84 | if (!on) { |
joe | 0:960b355eaa84 | 85 | led2 = 0; |
joe | 0:960b355eaa84 | 86 | } |
joe | 0:960b355eaa84 | 87 | if (start) { |
joe | 0:960b355eaa84 | 88 | led3 = 1; |
joe | 0:960b355eaa84 | 89 | } |
joe | 0:960b355eaa84 | 90 | if (!start) { |
joe | 0:960b355eaa84 | 91 | led3 = 0; // the leds are only to check the program is doing what i want it to! |
joe | 0:960b355eaa84 | 92 | } |
joe | 0:960b355eaa84 | 93 | |
joe | 0:960b355eaa84 | 94 | if (start&&on) |
joe | 0:960b355eaa84 | 95 | FILE *fp = fopen(FileName,"a"); |
joe | 0:960b355eaa84 | 96 | timer.start (); |
joe | 0:960b355eaa84 | 97 | while (start&&on) { //if the light gate reads over 60RPM and the swich is on then it will enter this loop |
joe | 0:960b355eaa84 | 98 | |
joe | 0:960b355eaa84 | 99 | while (tick_active == 0) {} |
joe | 0:960b355eaa84 | 100 | |
joe | 0:960b355eaa84 | 101 | rpm_counter = counter.read(); |
joe | 0:960b355eaa84 | 102 | counter.reset(); |
joe | 0:960b355eaa84 | 103 | |
joe | 0:960b355eaa84 | 104 | |
joe | 0:960b355eaa84 | 105 | fprintf(fp,"%d,%.2f,%.2f,%.2f\n",60*rpm_counter,acc.x(),acc.y(),acc.z()); //the data colected is saved in the file that was opend |
joe | 0:960b355eaa84 | 106 | led1 = !led1; |
joe | 0:960b355eaa84 | 107 | tick_active = 0; |
joe | 0:960b355eaa84 | 108 | |
joe | 0:960b355eaa84 | 109 | |
joe | 0:960b355eaa84 | 110 | samples[index] = acc.y(); |
joe | 0:960b355eaa84 | 111 | index++; |
joe | 0:960b355eaa84 | 112 | if (index >= 1) { |
joe | 0:960b355eaa84 | 113 | index = 0; |
joe | 0:960b355eaa84 | 114 | } |
joe | 0:960b355eaa84 | 115 | int i; |
joe | 0:960b355eaa84 | 116 | |
joe | 0:960b355eaa84 | 117 | flipped_right = 1; |
joe | 0:960b355eaa84 | 118 | flipped_left =1; |
joe | 0:960b355eaa84 | 119 | |
joe | 0:960b355eaa84 | 120 | for (i=0; i<1; i++) { |
joe | 0:960b355eaa84 | 121 | if (samples[i] >= -0.8) { |
joe | 0:960b355eaa84 | 122 | flipped_left = 0; |
joe | 0:960b355eaa84 | 123 | } |
joe | 0:960b355eaa84 | 124 | |
joe | 0:960b355eaa84 | 125 | if (samples[i] <= 0.8) { |
joe | 0:960b355eaa84 | 126 | flipped_right = 0; //the bit abouve is an array that is looking to see if the accelerometer is reading between 0.8 and -0.8 |
joe | 0:960b355eaa84 | 127 | |
joe | 0:960b355eaa84 | 128 | |
joe | 0:960b355eaa84 | 129 | |
joe | 0:960b355eaa84 | 130 | |
joe | 0:960b355eaa84 | 131 | } |
joe | 0:960b355eaa84 | 132 | |
joe | 0:960b355eaa84 | 133 | if (flipped_left||flipped_right||!on) { |
joe | 0:960b355eaa84 | 134 | // if the accelerometer is not reading between 0.8 and -0.8 on the y.acc or the swich is off the following will happen |
joe | 0:960b355eaa84 | 135 | |
joe | 0:960b355eaa84 | 136 | |
joe | 0:960b355eaa84 | 137 | myservo = 1; // 1. the servo will turn to turn off the engine |
joe | 0:960b355eaa84 | 138 | |
joe | 0:960b355eaa84 | 139 | timer.stop(); // 2. the timer will stop |
joe | 0:960b355eaa84 | 140 | |
joe | 0:960b355eaa84 | 141 | fprintf(fp,"this run lasted %f seconds \n", timer.read()); // 3. the timer time will be printed to the file |
joe | 0:960b355eaa84 | 142 | fprintf(fp,"off,off,off,off"); |
joe | 0:960b355eaa84 | 143 | fclose(fp); // and last the file will be closed |
joe | 0:960b355eaa84 | 144 | led1 = 0; |
joe | 0:960b355eaa84 | 145 | led4 = 1; |
joe | 0:960b355eaa84 | 146 | } |
joe | 0:960b355eaa84 | 147 | } |
joe | 0:960b355eaa84 | 148 | } |
joe | 0:960b355eaa84 | 149 | } |
joe | 0:960b355eaa84 | 150 | } |