Thomas Allen
/
AccelerometerTest1611
11/16
Revision 3:08f020c3d2b7, committed 2015-11-22
- Comitter:
- el13ytg
- Date:
- Sun Nov 22 00:10:04 2015 +0000
- Parent:
- 1:e41af8c958b8
- Commit message:
- v3.0
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r e41af8c958b8 -r 08f020c3d2b7 main.cpp --- a/main.cpp Fri Nov 20 19:27:43 2015 +0000 +++ b/main.cpp Sun Nov 22 00:10:04 2015 +0000 @@ -11,114 +11,66 @@ AnalogIn ZIn(p17); //Z Input Serial pc(USBTX, USBRX); //Serial to print readings to terminal Ticker timer; //Timer to call ISR -LocalFileSystem local("local"); // create local filesystem -BusOut leds(LED4,LED3,LED2,LED1); //debugio //Global Variables -float XOut, YOut, ZOut; //variables to store XYZ readings +float X, Y, Z; //variables to store XYZ readings float Xo, Yo, Zo; //variables to store 0G bias readings int flag; //flag for ISR -float X[2000], Y[2000], Z[2000]; //arrays to store results -float CalX[20], CalY[20], CalZ[20]; //arrays to store calibration readings -int n; //variable to store the number of readings +float CalX[1000], CalY[1000], CalZ[1000]; //arrays to store calibration readings +int n = 0; //variable to store the number of readings //Function Prototypes void Calibrate(); +void Send_Data(); void ISR(); -void Send_Data(); -void writeDataToFile(); + //Main Function int main() { - wait(5.0); //allow accelerometer to stabilise - Calibrate(); - pc.printf("Ready in:"); - for (int i = 10; i > 0; i--) { - pc.printf("%d\n\r", i); - wait(1.0); - } - - timer.attach(&ISR, 0.005); //attach ISR function to timer (100Hz) - - while (n < 2000) { + wait(2.0); //allow accelerometer to stabilise + timer.attach(&Calibrate, 0.001); //attach Calibrate function to timer + + while (1) { if (flag) { //if flag has been set flag = 0; //reset flag Send_Data(); } } - pc.printf("Writing...\n\r"); - writeDataToFile(); - pc.printf("Done.\n\r"); } //Function Definitions -void Calibrate() { - - for (int i = 0; i < 20; i++) { //read and store 20 samples - CalX[i] = XIn.read() * 3.3; - CalY[i] = YIn.read() * 3.3; - CalZ[i] = ZIn.read() * 3.3; - } +void Calibrate() { + CalX[n] = XIn.read() * 3.3; //read XYZ pins and store in array + CalY[n] = YIn.read() * 3.3; + CalZ[n] = ZIn.read() * 3.3; + n++; //increment counter - for (int i = 0; i < 20; i++) { //sum readings - Xo = Xo + CalX[i]; - Yo = Yo + CalY[i]; - Zo = Zo + CalZ[i]; - } + if (n == 1000) { //when enough samples have been taken + timer.detach(); //detach timer from Calibrate() - Xo = Xo / 20; //find mean and store it - Yo = Yo / 20; - Zo = Zo / 20; + for (int i = 0; i < 1000; i++) { //sum sample readings + Xo = Xo + CalX[i]; + Yo = Yo + CalY[i]; + Zo = Zo + CalZ[i]; + } + + Xo = Xo / 1000; //find mean and store it + Yo = Yo / 1000; + Zo = Zo / 1000; - pc.printf("Calibration Values = %.3f | %.3f | %.3f\n\r", Xo, Yo, Zo); -} - -void ISR() { - flag = 1; + pc.printf("Calibration Values = %.3f | %.3f | %.3f\n\r", Xo, Yo, Zo); //debug + timer.attach(&ISR, 0.001); //attach timer to ISR for real data collection + } } void Send_Data() { - XOut = (XIn.read() * 3.3) - Xo; //differences - YOut = (YIn.read() * 3.3) - Yo; - ZOut = (ZIn.read() * 3.3) - Zo; - X[n] = XOut / 0.3; //convert voltage to g-force (300mv/g) and store in array - Y[n] = YOut / 0.3; - Z[n] = ZOut / 0.3; - n++; - - pc.printf("%d) X = %.2f | Y = %.2f | Z = %.2f\n\r", n, XOut, YOut, ZOut); + X = ((XIn.read() * 3.3) - Xo) / 0.3; //read input, subtract zeroed value to find offset, and convert to g (300mv/g) + Y = ((YIn.read() * 3.3) - Yo) / 0.3; + Z = ((ZIn.read() * 3.3) - Zo) / 0.3; + pc.printf("%.3f %.3f %.3f",X, Y, Z); //print output to serial } -void writeDataToFile() { - - leds = 15; // turn on LEDs for feedback - FILE *results = fopen("/local/results.txt", "w"); // open 'results.txt' for writing - - fprintf(results, "x = ["); //print X array - - for (int i = 0; i < 1999; i++) { - fprintf(results, "%.3f,", X[i]); - } - - fprintf(results, "%.3f]\n\r", X[1999]); - - fprintf(results, "y = ["); //print Y array - - for (int i = 0; i < 1999; i++) { - fprintf(results, "%.3f,", Y[i]); - } - - fprintf(results, "%.3f]\n\r", Y[1999]); - - fprintf(results, "z = ["); //print Z array - - for (int i = 0; i < 1999; i++) { - fprintf(results, "%.3f,", Z[i]); - } - - fprintf(results, "%.3f]\n\r", Z[1999]); - - fclose(results); // close file - leds = 0; // turn off LEDs to signify file access has finished +void ISR() { + flag = 1; //set flag }