![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
11/16
main.cpp
- Committer:
- el13ytg
- Date:
- 2015-11-20
- Revision:
- 1:e41af8c958b8
- Parent:
- 0:0dc99d71b2f3
- Child:
- 3:08f020c3d2b7
File content as of revision 1:e41af8c958b8:
//AccelerometerTest //Toby Galal //6 November 2015 //A program used to gather data from ADXL335 accelerometers and print them to file (.txt) in a MatLab-compliant format #include "mbed.h" //Hardware Initialisation AnalogIn XIn(p15); //X Input AnalogIn YIn(p16); //Y Input 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 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 //Function Prototypes void Calibrate(); 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) { 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; } for (int i = 0; i < 20; i++) { //sum readings Xo = Xo + CalX[i]; Yo = Yo + CalY[i]; Zo = Zo + CalZ[i]; } Xo = Xo / 20; //find mean and store it Yo = Yo / 20; Zo = Zo / 20; pc.printf("Calibration Values = %.3f | %.3f | %.3f\n\r", Xo, Yo, Zo); } void ISR() { flag = 1; } 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); } 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 }