A data logger for the FRDM-K64F taking readings from the FXOS8700CQ accelerometer/magnometer at 200Hz.

Dependencies:   FXOS8700CQ SDFileSystem mbed

Fork of Hello_FXOS8700Q by Jim Carver

Committer:
trm
Date:
Fri May 30 21:02:54 2014 +0000
Revision:
7:169254b6eaf6
Parent:
6:02bfeec82bc1
Initial commit of 20 second, 4g logger. Binary to SD card, converted Binary to CSV on SD card.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:748fe54f0947 1 #include "mbed.h"
trm 7:169254b6eaf6 2 #include "FXOS8700CQ.h"
trm 7:169254b6eaf6 3 #include "SDFileSystem.h"
trm 7:169254b6eaf6 4
trm 7:169254b6eaf6 5 #define DATA_RECORD_TIME_MS 20000
trm 7:169254b6eaf6 6
trm 7:169254b6eaf6 7 Serial pc(USBTX, USBRX); // for diagnostic output, beware blocking on long text
JimCarver 0:748fe54f0947 8
trm 7:169254b6eaf6 9 // Pin names for FRDM-K64F
trm 7:169254b6eaf6 10 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCLK, SSEL, "name"
trm 7:169254b6eaf6 11 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
trm 7:169254b6eaf6 12
trm 7:169254b6eaf6 13 DigitalOut green(LED_GREEN);
trm 7:169254b6eaf6 14 DigitalOut blue(LED_BLUE);
trm 7:169254b6eaf6 15 DigitalOut red(LED_RED);
trm 7:169254b6eaf6 16
trm 7:169254b6eaf6 17 Timer t; // Microsecond timer, 32 bit int, maximum count of ~30 minutes
trm 7:169254b6eaf6 18 InterruptIn fxos_int1(PTC6);
trm 7:169254b6eaf6 19 InterruptIn fxos_int2(PTC13);
trm 7:169254b6eaf6 20 InterruptIn start_sw(PTA4); // switch SW3
JimCarver 4:4b494ca218ff 21
trm 7:169254b6eaf6 22 bool fxos_int1_triggered = false;
trm 7:169254b6eaf6 23 bool fxos_int2_triggered = false;
trm 7:169254b6eaf6 24 uint32_t us_ellapsed = 0;
trm 7:169254b6eaf6 25 bool start_sw_triggered = false;
trm 7:169254b6eaf6 26
trm 7:169254b6eaf6 27 uint32_t sample_count = 0;
trm 7:169254b6eaf6 28
trm 7:169254b6eaf6 29 SRAWDATA accel_data;
trm 7:169254b6eaf6 30 SRAWDATA magn_data;
trm 7:169254b6eaf6 31
trm 7:169254b6eaf6 32 char sd_buffer[BUFSIZ * 2]; // create an appropriately sized buffer
trm 7:169254b6eaf6 33
trm 7:169254b6eaf6 34 void trigger_fxos_int1(void)
trm 7:169254b6eaf6 35 {
trm 7:169254b6eaf6 36 fxos_int1_triggered = true;
trm 7:169254b6eaf6 37 }
trm 7:169254b6eaf6 38
trm 7:169254b6eaf6 39 void trigger_fxos_int2(void)
trm 7:169254b6eaf6 40 {
trm 7:169254b6eaf6 41 fxos_int2_triggered = true;
trm 7:169254b6eaf6 42 us_ellapsed = t.read_us();
trm 7:169254b6eaf6 43 }
JimCarver 4:4b494ca218ff 44
trm 7:169254b6eaf6 45 void trigger_start_sw(void)
trm 7:169254b6eaf6 46 {
trm 7:169254b6eaf6 47 start_sw_triggered = true;
trm 7:169254b6eaf6 48 }
trm 7:169254b6eaf6 49
trm 7:169254b6eaf6 50 void print_reading(void)
trm 7:169254b6eaf6 51 {
trm 7:169254b6eaf6 52 pc.printf("A X:%5d,Y:%5d,Z:%5d M X:%5d,Y:%5d,Z:%5d\r\n",
trm 7:169254b6eaf6 53 accel_data.x, accel_data.y, accel_data.z,
trm 7:169254b6eaf6 54 magn_data.x, magn_data.y, magn_data.z);
trm 7:169254b6eaf6 55 }
trm 7:169254b6eaf6 56
trm 7:169254b6eaf6 57 void write_reading(FILE* fp)
trm 7:169254b6eaf6 58 {
trm 7:169254b6eaf6 59 fprintf(fp, "%d, %d,%d,%d, %d,%d,%d\r\n",
trm 7:169254b6eaf6 60 us_ellapsed,
trm 7:169254b6eaf6 61 accel_data.x, accel_data.y, accel_data.z,
trm 7:169254b6eaf6 62 magn_data.x, magn_data.y, magn_data.z);
trm 7:169254b6eaf6 63 }
JimCarver 0:748fe54f0947 64
trm 7:169254b6eaf6 65 void write_csv_header(FILE* fp)
trm 7:169254b6eaf6 66 {
trm 7:169254b6eaf6 67 fprintf(fp, "timestamp, acc-x, acc-y, acc-z, magn-x, magn-y, magn-z, scale: %d\r\n",
trm 7:169254b6eaf6 68 fxos.get_accel_scale());
trm 7:169254b6eaf6 69 }
JimCarver 5:061ab9f2c002 70
trm 7:169254b6eaf6 71 /*
trm 7:169254b6eaf6 72 * Writing binary to the file:
trm 7:169254b6eaf6 73 * Timestamp: uint32_t written B0 B1 B2 B3
trm 7:169254b6eaf6 74 * Raw data: int16_t written B0 B1
trm 7:169254b6eaf6 75 * Packed resulting format, 16 bytes:
trm 7:169254b6eaf6 76 * TS0 TS1 TS2 TS3 AX0 AX1 AY0 AY1 AZ0 AZ1 MX0 MX1 MY0 MY1 MZ0 MZ1
trm 7:169254b6eaf6 77 */
trm 7:169254b6eaf6 78 void write_binary(FILE* fp)
trm 7:169254b6eaf6 79 {
trm 7:169254b6eaf6 80 uint8_t out_buffer[16] = {
trm 7:169254b6eaf6 81 (uint8_t)(us_ellapsed), (uint8_t)(us_ellapsed >> 8),
trm 7:169254b6eaf6 82 (uint8_t)(us_ellapsed >> 16), (uint8_t)(us_ellapsed >> 24),
JimCarver 4:4b494ca218ff 83
trm 7:169254b6eaf6 84 (uint8_t)(accel_data.x), (uint8_t)(accel_data.x >> 8),
trm 7:169254b6eaf6 85 (uint8_t)(accel_data.y), (uint8_t)(accel_data.y >> 8),
trm 7:169254b6eaf6 86 (uint8_t)(accel_data.z), (uint8_t)(accel_data.z >> 8),
trm 7:169254b6eaf6 87
trm 7:169254b6eaf6 88 (uint8_t)(magn_data.x), (uint8_t)(magn_data.x >> 8),
trm 7:169254b6eaf6 89 (uint8_t)(magn_data.y), (uint8_t)(magn_data.y >> 8),
trm 7:169254b6eaf6 90 (uint8_t)(magn_data.z), (uint8_t)(magn_data.z >> 8)
trm 7:169254b6eaf6 91 };
trm 7:169254b6eaf6 92
trm 7:169254b6eaf6 93 fwrite( (const uint8_t*) out_buffer, sizeof(uint8_t), 16, fp);
trm 7:169254b6eaf6 94 }
JimCarver 0:748fe54f0947 95
trm 7:169254b6eaf6 96 void read_binary(FILE* fp)
trm 7:169254b6eaf6 97 {
trm 7:169254b6eaf6 98 uint8_t in_buffer[16];
trm 7:169254b6eaf6 99 fread( in_buffer, sizeof(uint8_t), 16, fp);
trm 7:169254b6eaf6 100 us_ellapsed = (in_buffer[0]) | (in_buffer[1] << 8)|
trm 7:169254b6eaf6 101 (in_buffer[2] << 16) | (in_buffer[3] << 24);
trm 7:169254b6eaf6 102
trm 7:169254b6eaf6 103 accel_data.x = in_buffer[4] | (in_buffer[5] << 8);
trm 7:169254b6eaf6 104 accel_data.y = in_buffer[6] | (in_buffer[7] << 8);
trm 7:169254b6eaf6 105 accel_data.z = in_buffer[8] | (in_buffer[9] << 8);
trm 7:169254b6eaf6 106
trm 7:169254b6eaf6 107 magn_data.x = in_buffer[10] | (in_buffer[11] << 8);
trm 7:169254b6eaf6 108 magn_data.y = in_buffer[12] | (in_buffer[13] << 8);
trm 7:169254b6eaf6 109 magn_data.z = in_buffer[14] | (in_buffer[15] << 8);
trm 7:169254b6eaf6 110 }
trm 7:169254b6eaf6 111
trm 7:169254b6eaf6 112 int main(void)
trm 7:169254b6eaf6 113 {
trm 7:169254b6eaf6 114 // Setup
trm 7:169254b6eaf6 115 t.reset();
trm 7:169254b6eaf6 116 pc.baud(115200); // Move pc.printf's right along
trm 7:169254b6eaf6 117 green.write(1); // lights off
trm 7:169254b6eaf6 118 red.write(1);
trm 7:169254b6eaf6 119 blue.write(1);
trm 7:169254b6eaf6 120
trm 7:169254b6eaf6 121 pc.printf("\r\nTime to start setup!\r\n");
trm 7:169254b6eaf6 122
trm 7:169254b6eaf6 123 // Prepare file stream for saving data
trm 7:169254b6eaf6 124 FILE* fp_raw = fopen("/sd/data.bin", "w");
trm 7:169254b6eaf6 125
trm 7:169254b6eaf6 126 if(0 == setvbuf(fp_raw, &sd_buffer[0], _IOFBF, BUFSIZ)) {
trm 7:169254b6eaf6 127 pc.printf("Buffering \"/sd/\" with %d bytes.\r\n", BUFSIZ);
trm 7:169254b6eaf6 128 } else {
trm 7:169254b6eaf6 129 pc.printf("Failed to buffer SD card with %d bytes.\r\n", BUFSIZ);
trm 7:169254b6eaf6 130 }
trm 7:169254b6eaf6 131
trm 7:169254b6eaf6 132 // Iterrupt for active-low interrupt line from FXOS
trm 7:169254b6eaf6 133 fxos_int2.fall(&trigger_fxos_int2);
trm 7:169254b6eaf6 134 fxos.enable(); // enable our device, configured in constructor
trm 7:169254b6eaf6 135
trm 7:169254b6eaf6 136 // Interrupt for active-high trigger
trm 7:169254b6eaf6 137 start_sw.mode(PullUp); // Since the FRDM-K64F doesn't have its SW2/SW3 pull-ups populated
trm 7:169254b6eaf6 138 start_sw.fall(&trigger_start_sw);
trm 7:169254b6eaf6 139 green.write(0); // light up ready-green
trm 7:169254b6eaf6 140
trm 7:169254b6eaf6 141 // Example data printing
trm 7:169254b6eaf6 142 fxos.get_data(&accel_data, &magn_data);
trm 7:169254b6eaf6 143 print_reading();
trm 7:169254b6eaf6 144
trm 7:169254b6eaf6 145 pc.printf("Waiting for timed data collection trigger on SW3\r\n");
trm 7:169254b6eaf6 146
trm 7:169254b6eaf6 147 while(true) {
trm 7:169254b6eaf6 148 if(start_sw_triggered) {
trm 7:169254b6eaf6 149 start_sw_triggered = false;
trm 7:169254b6eaf6 150 break; // Received start button press
trm 7:169254b6eaf6 151 }
trm 7:169254b6eaf6 152 wait_ms(50); // short wait will have little effect on UX
trm 7:169254b6eaf6 153 }
trm 7:169254b6eaf6 154
trm 7:169254b6eaf6 155 green.write(1); // ready-green off
trm 7:169254b6eaf6 156 blue.write(0); // working-blue on
trm 7:169254b6eaf6 157
trm 7:169254b6eaf6 158 pc.printf("Started data collection\r\n");
trm 7:169254b6eaf6 159
trm 7:169254b6eaf6 160 sample_count = 0;
trm 7:169254b6eaf6 161 fxos.get_data(&accel_data, &magn_data); // clear interrupt from device
trm 7:169254b6eaf6 162 fxos_int2_triggered = false; // un-trigger
trm 7:169254b6eaf6 163
trm 7:169254b6eaf6 164 t.start(); // start timer and enter collection loop
trm 7:169254b6eaf6 165 while (t.read_ms() <= DATA_RECORD_TIME_MS) {
trm 7:169254b6eaf6 166 if(fxos_int2_triggered) {
trm 7:169254b6eaf6 167 fxos_int2_triggered = false; // un-trigger
trm 7:169254b6eaf6 168 fxos.get_data(&accel_data, &magn_data);
trm 7:169254b6eaf6 169 // pc.printf("S: %d\r\n", us_ellapsed); // complex status printout
trm 7:169254b6eaf6 170 pc.putc('.'); // show that a sample was taken
trm 7:169254b6eaf6 171 write_binary(fp_raw); // needs to take <5ms to avoid blocking next transfer
trm 7:169254b6eaf6 172 ++sample_count; // successfully sampled and wrote!
trm 7:169254b6eaf6 173 }
trm 7:169254b6eaf6 174 }
trm 7:169254b6eaf6 175
trm 7:169254b6eaf6 176 red.write(0); // red on, display purple for file reprocessing
trm 7:169254b6eaf6 177
trm 7:169254b6eaf6 178 pc.printf("Done collecting. Reprocessing data to CSV.\r\n");
trm 7:169254b6eaf6 179 fflush(fp_raw);
trm 7:169254b6eaf6 180 fclose(fp_raw);
trm 7:169254b6eaf6 181
trm 7:169254b6eaf6 182 // Reopen the binary data as read, open CSV for write
trm 7:169254b6eaf6 183 fp_raw = fopen("/sd/data.bin", "r");
trm 7:169254b6eaf6 184 FILE* fp_csv = fopen("/sd/data.csv", "w");
trm 7:169254b6eaf6 185
trm 7:169254b6eaf6 186 write_csv_header(fp_csv); // write out CSV header
trm 7:169254b6eaf6 187
trm 7:169254b6eaf6 188 // Split the buffer between the two
trm 7:169254b6eaf6 189 setvbuf(fp_csv, &sd_buffer[0], _IOFBF, BUFSIZ);
trm 7:169254b6eaf6 190 setvbuf(fp_raw, &sd_buffer[BUFSIZ], _IOFBF, BUFSIZ);
trm 7:169254b6eaf6 191
trm 7:169254b6eaf6 192 for(uint32_t i = sample_count; i > 0; --i) {
trm 7:169254b6eaf6 193 read_binary(fp_raw);
trm 7:169254b6eaf6 194 write_reading(fp_csv);
trm 7:169254b6eaf6 195 }
trm 7:169254b6eaf6 196
trm 7:169254b6eaf6 197 fclose(fp_csv);
trm 7:169254b6eaf6 198 fclose(fp_raw);
trm 7:169254b6eaf6 199
trm 7:169254b6eaf6 200 pc.printf("Done processing. Wrote binary into CSV.");
trm 7:169254b6eaf6 201
trm 7:169254b6eaf6 202 red.write(1); // red off, green on, display yellow for done
trm 7:169254b6eaf6 203 green.write(0);
trm 7:169254b6eaf6 204
trm 7:169254b6eaf6 205 while(true) {
trm 7:169254b6eaf6 206 pc.putc('.');
JimCarver 0:748fe54f0947 207 wait(1.0);
JimCarver 0:748fe54f0947 208 }
JimCarver 0:748fe54f0947 209 }