High speed logging example
Dependencies: Adafruit_RTCLib BNO055 SDFileSystem mbed
main.cpp
- Committer:
- bwang
- Date:
- 2018-01-22
- Revision:
- 0:2953603cfeee
- Child:
- 1:27070bac59e5
File content as of revision 0:2953603cfeee:
#include "mbed.h" #include "SDFileSystem.h" #include "BNO055.h" #include "DS1307.h" #include "string.h" #define DI D11 #define DO D12 #define SCK D13 #define CS D10 #define I2C1_SDA PB_9 #define I2C1_SCL PB_8 #define I2C2_SDA D5 #define I2C2_SCL D7 #define PAGE_SIZE 4096 #define HEADER_SIZE 10 #define PACKET_SIZE 8 #define BAUD_RATE 921600 #define BUF_SIZE (3*PAGE_SIZE) Serial serial4(D8, D2); DigitalOut test(PC_8); DigitalOut test2(PC_6); int flag=0, flag2=0; char fname[255]; char current_time[64]; int index = 0; unsigned char *front, *back, *tmp; unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE]; unsigned char hdr[HEADER_SIZE] = {0xff, 0xff,'M','A','N','B','L','O','R','T'}; void printDT(char *pre, DateTime &dt) { printf("%s %u/%u/%02u %2u:%02u:%02u\r\n" ,pre ,dt.month(),dt.day(),dt.year() ,dt.hour(),dt.minute(),dt.second() ); } bool rtcUpdate(RtcDs1307 &rtc, int32_t bias) // this must be signed { bool bUpdated = false; // Use the compiled date/time as a basis for setting the clock. // We assign it to a signed integer so that negative biases work correctly int64_t compiledTime = DateTime(__DATE__,__TIME__).unixtime(); // This assumes that the program is run VERY soon after the initial compile. time_t localt = DateTime(compiledTime + bias).unixtime(); // offset by bias // If the stored static time stamp does not equal the compiled time stamp, // then we need to update the RTC clock and the stored time stamp if(*((time_t *)&rtc[0]) != localt) { // Update the RTC time as local time, not GMT/UTC rtc.adjust(localt); // Store the new compiled time statically in the object ram image *((time_t *)&rtc[0]) = localt; // Push the object ram image to the RTC ram image bUpdated = rtc.commit(); } return bUpdated; } void rx_callback() { while(serial4.readable()) { if (index < BUF_SIZE) { front[index] = serial4.getc(); index++; } else { front[0] = serial4.getc(); index = 1; } } } int main() { front = buf1, back = buf2; test = 0; SDFileSystem sd(DI, DO, SCK, CS, "sd"); while (sd.disk_status()) { sd.disk_initialize(); wait(0.5); } /*init RTC, get time and date for filename*/ I2C i2c1(I2C1_SDA, I2C1_SCL); i2c1.frequency(100000); RtcDs1307 rtc(i2c1); rtcUpdate(rtc, -(5*60*60)); DateTime dt = rtc.now(); printDT("It is now", dt); strcpy(fname, "/sd/log_"); sprintf(current_time, "%u.%u.%02u_%02u.%02u.%02u" ,dt.month(),dt.day(),dt.year() ,dt.hour(),dt.minute(),dt.second() ); strcat(fname, current_time); strcat(fname, ".dat"); printf("Logging to %s\n", fname); FILE *fp = fopen(fname, "wb"); /*init IMU*/ BNO055 imu(I2C2_SDA, I2C2_SCL); imu.reset(); if (!imu.check()) { printf("%s\n", "IMU not ready"); } else { printf("BNO055 found\r\n\r\n"); printf("Chip ID: %03d\r\n",imu.ID.id); printf("Accelerometer ID: %03d\r\n",imu.ID.accel); printf("Gyroscope ID: %03d\r\n",imu.ID.gyro); printf("Magnetometer ID: %03d\r\n\r\n",imu.ID.mag); printf("Firmware version v%d.%0d\r\n",imu.ID.sw[0],imu.ID.sw[1]); printf("Bootloader version v%d\r\n\r\n",imu.ID.bootload); } imu.setmode(OPERATION_MODE_NDOF); imu.set_accel_units(MPERSPERS); serial4.baud(BAUD_RATE); serial4.attach(rx_callback); for (;;) { if (index >= PAGE_SIZE-HEADER_SIZE) { if (index > PAGE_SIZE-HEADER_SIZE) { printf("%d\n", index); } test2 = flag2; flag2 = !flag2; tmp = front; front = back; back = tmp; index = 0; test = 1; fwrite(hdr, 1, HEADER_SIZE, fp); fwrite(back, 1, PAGE_SIZE-HEADER_SIZE, fp); fflush(fp); test = 0; } } }