Dependencies:   mbed

Committer:
joe
Date:
Fri Aug 20 15:38:52 2010 +0000
Revision:
2:a079de4fd5b9
Parent:
0:960b355eaa84

        

Who changed what in which revision?

UserRevisionLine numberNew 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 LocalFileSystem local("local");
joe 0:960b355eaa84 18 Counter counter(p18);
joe 0:960b355eaa84 19 char filename[64];
joe 0:960b355eaa84 20 int n = 0;
joe 0:960b355eaa84 21
joe 0:960b355eaa84 22
joe 0:960b355eaa84 23 Ticker tick;
joe 0:960b355eaa84 24
joe 0:960b355eaa84 25 Timer t;
joe 0:960b355eaa84 26 volatile int tick_active = 0;
joe 0:960b355eaa84 27
joe 0:960b355eaa84 28 void dotick (void) {
joe 0:960b355eaa84 29 tick_active = 1;
joe 0:960b355eaa84 30
joe 0:960b355eaa84 31 }
joe 0:960b355eaa84 32 float samples [2] = {0};
joe 0:960b355eaa84 33 int index2 = 0;
joe 0:960b355eaa84 34 int start = 1;
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 0:960b355eaa84 44 TextLCD lcd(p24, p25, p26, p27, p28, p29, p30);
joe 0:960b355eaa84 45 sprintf(filename, "/local/file%03d.txt", n);
joe 0:960b355eaa84 46 FILE *fp = fopen(filename, "r"); //this opens a file on the mbed drive
joe 0:960b355eaa84 47 fprintf(fp, "RPM,X,Y,Z\n"); //this part prints the columns headers
joe 0:960b355eaa84 48 if (fp == NULL) {
joe 0:960b355eaa84 49 break;
joe 0:960b355eaa84 50 }
joe 0:960b355eaa84 51 fclose(fp);
joe 0:960b355eaa84 52 n++;
joe 0:960b355eaa84 53 }
joe 0:960b355eaa84 54 tick.attach(dotick,0.5);
joe 0:960b355eaa84 55
joe 0:960b355eaa84 56 while (1) {
joe 0:960b355eaa84 57 while (tick_active == 0) {}
joe 0:960b355eaa84 58 rpm_counter = counter.read();
joe 0:960b355eaa84 59 counter.reset();
joe 0:960b355eaa84 60
joe 0:960b355eaa84 61 tick_active = 0;
joe 0:960b355eaa84 62 samples[index2] = 120*rpm_counter;
joe 0:960b355eaa84 63 index2++;
joe 0:960b355eaa84 64 if (index2 >= 2) {
joe 0:960b355eaa84 65 index2 = 0;
joe 0:960b355eaa84 66 }
joe 0:960b355eaa84 67 int i;
joe 0:960b355eaa84 68 start = 1;
joe 0:960b355eaa84 69 for (i=0; i<2; i++) {
joe 0:960b355eaa84 70 if (samples[i] <60) {
joe 0:960b355eaa84 71 start = 0;
joe 0:960b355eaa84 72 }
joe 0:960b355eaa84 73 }
joe 0:960b355eaa84 74 if (on) {
joe 0:960b355eaa84 75 led2 = 1;
joe 0:960b355eaa84 76 }
joe 0:960b355eaa84 77 if (!on) {
joe 0:960b355eaa84 78 led2 = 0;
joe 0:960b355eaa84 79 }
joe 0:960b355eaa84 80 if (start) {
joe 0:960b355eaa84 81 led3 = 1;
joe 0:960b355eaa84 82 }
joe 0:960b355eaa84 83 if (!start) {
joe 0:960b355eaa84 84 led3 = 0; // the leds are only to check the program is doing what i want it to!
joe 0:960b355eaa84 85 }
joe 0:960b355eaa84 86 if (start&&on)
joe 0:960b355eaa84 87 t.start ();
joe 0:960b355eaa84 88 while (start&&on) { //if the light gate reads over 60RPM and the swich is on then it will enter this loop
joe 0:960b355eaa84 89
joe 0:960b355eaa84 90 while (tick_active == 0) {}
joe 0:960b355eaa84 91
joe 0:960b355eaa84 92 rpm_counter = counter.read();
joe 0:960b355eaa84 93 counter.reset();
joe 0:960b355eaa84 94 FILE *fp = fopen(filename, "w");
joe 0:960b355eaa84 95 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 96 led1 = !led1;
joe 0:960b355eaa84 97 tick_active = 0;
joe 0:960b355eaa84 98
joe 0:960b355eaa84 99
joe 0:960b355eaa84 100 samples[index] = acc.y();
joe 0:960b355eaa84 101 index++;
joe 0:960b355eaa84 102 if (index >= 1) {
joe 0:960b355eaa84 103 index = 0;
joe 0:960b355eaa84 104 }
joe 0:960b355eaa84 105 int i;
joe 0:960b355eaa84 106
joe 0:960b355eaa84 107 flipped_right = 1;
joe 0:960b355eaa84 108 flipped_left =1;
joe 0:960b355eaa84 109
joe 0:960b355eaa84 110 for (i=0; i<1; i++) {
joe 0:960b355eaa84 111 if (samples[i] >= -0.8) {
joe 0:960b355eaa84 112 flipped_left = 0;
joe 0:960b355eaa84 113 }
joe 0:960b355eaa84 114
joe 0:960b355eaa84 115 if (samples[i] <= 0.8) {
joe 0:960b355eaa84 116 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 117
joe 0:960b355eaa84 118
joe 0:960b355eaa84 119
joe 0:960b355eaa84 120
joe 0:960b355eaa84 121 }
joe 0:960b355eaa84 122
joe 0:960b355eaa84 123 if (flipped_left||flipped_right||!on) {
joe 0:960b355eaa84 124 // 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 125
joe 0:960b355eaa84 126
joe 0:960b355eaa84 127 myservo = 1; // 1. the servo will turn to turn off the engine
joe 0:960b355eaa84 128
joe 0:960b355eaa84 129 t.stop(); // 2. the timer will stop
joe 0:960b355eaa84 130
joe 0:960b355eaa84 131 fprintf(fp,"this run lasted %f seconds \n", t.read()); // 3. the timer time will be printed to the file
joe 0:960b355eaa84 132 fprintf(fp,"off,off,off,off");
joe 0:960b355eaa84 133 fclose(fp); // and last the file will be closed
joe 0:960b355eaa84 134 led2 = 0
joe 0:960b355eaa84 135 led1 = 0;
joe 0:960b355eaa84 136 led4 = 1;
joe 0:960b355eaa84 137 }
joe 0:960b355eaa84 138 }
joe 0:960b355eaa84 139 }
joe 0:960b355eaa84 140 }
joe 0:960b355eaa84 141 }