Use serial port, not SD for datalogging.
Dependencies: MMA8451Q_tb SDFileSystem mbed
Fork of StepLogger by
main.cpp@1:03f0a25c5a86, 2014-05-22 (annotated)
- Committer:
- donoman
- Date:
- Thu May 22 18:58:25 2014 +0000
- Revision:
- 1:03f0a25c5a86
- Parent:
- 0:75c161b9fc38
Step logging working, but double counting;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donoman | 0:75c161b9fc38 | 1 | #include "mbed.h" |
donoman | 0:75c161b9fc38 | 2 | #include "MMA8451Q_tb.h" |
donoman | 0:75c161b9fc38 | 3 | #include "SDFileSystem.h" |
donoman | 1:03f0a25c5a86 | 4 | #include "math.h" |
donoman | 0:75c161b9fc38 | 5 | |
donoman | 0:75c161b9fc38 | 6 | |
donoman | 0:75c161b9fc38 | 7 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
donoman | 0:75c161b9fc38 | 8 | float acc_all[3]; |
donoman | 0:75c161b9fc38 | 9 | DigitalOut pinout(PTD4); |
donoman | 0:75c161b9fc38 | 10 | Timer timer; |
donoman | 0:75c161b9fc38 | 11 | Serial pc(USBTX, USBRX); |
donoman | 0:75c161b9fc38 | 12 | SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // (mosi, miso, sclok, cs, name) |
donoman | 0:75c161b9fc38 | 13 | |
donoman | 0:75c161b9fc38 | 14 | int main(void) { |
donoman | 0:75c161b9fc38 | 15 | |
donoman | 0:75c161b9fc38 | 16 | //Initialize |
donoman | 1:03f0a25c5a86 | 17 | int now; //used for timestamping |
donoman | 1:03f0a25c5a86 | 18 | int counter = 0; //used for step counting |
donoman | 1:03f0a25c5a86 | 19 | int step_flag = 0; //used to sense transition into step |
donoman | 1:03f0a25c5a86 | 20 | float threshold = 1.43; //tunable in g's [m/s^2] |
donoman | 1:03f0a25c5a86 | 21 | float mag = 0; |
donoman | 1:03f0a25c5a86 | 22 | float x = 0; |
donoman | 1:03f0a25c5a86 | 23 | float y = 0; |
donoman | 1:03f0a25c5a86 | 24 | float z = 0; |
donoman | 0:75c161b9fc38 | 25 | timer.start(); |
donoman | 0:75c161b9fc38 | 26 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
donoman | 0:75c161b9fc38 | 27 | printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
donoman | 0:75c161b9fc38 | 28 | |
donoman | 0:75c161b9fc38 | 29 | //Open File |
donoman | 1:03f0a25c5a86 | 30 | //mkdir("/sd/mydir", 0777); |
donoman | 1:03f0a25c5a86 | 31 | //FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); |
donoman | 1:03f0a25c5a86 | 32 | //if(fp == NULL) { |
donoman | 1:03f0a25c5a86 | 33 | // error("Could not open file for write\n"); |
donoman | 1:03f0a25c5a86 | 34 | //} |
donoman | 0:75c161b9fc38 | 35 | |
donoman | 1:03f0a25c5a86 | 36 | while(1){ |
donoman | 1:03f0a25c5a86 | 37 | //for(int i=0; i<2000; i++) { |
donoman | 0:75c161b9fc38 | 38 | pinout = !pinout; |
donoman | 0:75c161b9fc38 | 39 | now = timer.read_ms(); |
donoman | 0:75c161b9fc38 | 40 | acc.fastRead(&acc_all[0]); |
donoman | 1:03f0a25c5a86 | 41 | x = acc_all[0]; |
donoman | 1:03f0a25c5a86 | 42 | y = acc_all[1]; |
donoman | 1:03f0a25c5a86 | 43 | z = acc_all[2]; |
donoman | 1:03f0a25c5a86 | 44 | |
donoman | 1:03f0a25c5a86 | 45 | mag = sqrt(x*x + y*y + z*z); |
donoman | 0:75c161b9fc38 | 46 | //Print to file |
donoman | 1:03f0a25c5a86 | 47 | // fprintf(fp, "%d, %f,%f,%f\n",now, acc_all[0],acc_all[1],acc_all[2]); |
donoman | 1:03f0a25c5a86 | 48 | |
donoman | 1:03f0a25c5a86 | 49 | //} |
donoman | 1:03f0a25c5a86 | 50 | //fclose(fp); |
donoman | 1:03f0a25c5a86 | 51 | // pc.printf("mag: %f\n", mag); |
donoman | 1:03f0a25c5a86 | 52 | if (mag > threshold){ |
donoman | 1:03f0a25c5a86 | 53 | if (step_flag == 0){ |
donoman | 1:03f0a25c5a86 | 54 | counter++; |
donoman | 1:03f0a25c5a86 | 55 | pc.printf("step_count:%d mag: %f\n", counter, mag); |
donoman | 1:03f0a25c5a86 | 56 | } |
donoman | 1:03f0a25c5a86 | 57 | step_flag = 1; |
donoman | 1:03f0a25c5a86 | 58 | } |
donoman | 1:03f0a25c5a86 | 59 | else { |
donoman | 1:03f0a25c5a86 | 60 | step_flag = 0; |
donoman | 1:03f0a25c5a86 | 61 | } |
donoman | 0:75c161b9fc38 | 62 | |
donoman | 0:75c161b9fc38 | 63 | } |
donoman | 0:75c161b9fc38 | 64 | } |