Minor fixes
Dependencies: LSM9DS1_Library SDFileSystem mbed nrf51_rtc
Fork of LSM303DLHTest by
main.cpp@6:9db9f4bfaf98, 2016-07-15 (annotated)
- Committer:
- afmiee
- Date:
- Fri Jul 15 22:39:42 2016 +0000
- Revision:
- 6:9db9f4bfaf98
- Parent:
- 5:b1a689c55f59
- Child:
- 7:cbfdcc57f110
First version working on the nRF51-DK with the MKI159V1 prototyping board for the LSM9DS1. The SD card writes the raw data with time referenced file names.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
afmiee | 5:b1a689c55f59 | 1 | // Latch Inc. |
afmiee | 5:b1a689c55f59 | 2 | // Antonio F Mondragon |
afmiee | 5:b1a689c55f59 | 3 | // 20160714 |
afmiee | 5:b1a689c55f59 | 4 | // for the Adafruit 9DOF Modulke and the Sparkfun microSD card shield |
tosihisa | 0:750784997b84 | 5 | |
tosihisa | 0:750784997b84 | 6 | #include "mbed.h" |
afmiee | 6:9db9f4bfaf98 | 7 | #include "LSM9DS1.h" |
afmiee | 5:b1a689c55f59 | 8 | #include "SDFileSystem.h" |
afmiee | 5:b1a689c55f59 | 9 | #include "nrf51_rtc.h" |
afmiee | 5:b1a689c55f59 | 10 | |
afmiee | 5:b1a689c55f59 | 11 | #define M_PI 3.14158 |
afmiee | 5:b1a689c55f59 | 12 | |
afmiee | 5:b1a689c55f59 | 13 | // Create objects |
afmiee | 5:b1a689c55f59 | 14 | Serial debug(USBTX,USBRX); |
afmiee | 5:b1a689c55f59 | 15 | // For Nordic |
afmiee | 6:9db9f4bfaf98 | 16 | LSM9DS1 lol(p30, p7, 0xD6, 0x3C); |
afmiee | 5:b1a689c55f59 | 17 | |
afmiee | 5:b1a689c55f59 | 18 | // Create the SD filesystem |
afmiee | 5:b1a689c55f59 | 19 | SDFileSystem sd(p25, p28, p29, p20, "sd"); // MOSI, MISO, SCLK, SSEL |
afmiee | 5:b1a689c55f59 | 20 | |
afmiee | 5:b1a689c55f59 | 21 | // Create a ticker to use the nRF51 RTC |
afmiee | 5:b1a689c55f59 | 22 | Ticker flipper; |
afmiee | 5:b1a689c55f59 | 23 | |
afmiee | 5:b1a689c55f59 | 24 | // Assign interrupts to switches |
afmiee | 5:b1a689c55f59 | 25 | InterruptIn btn1(p17); // Start sampling |
afmiee | 5:b1a689c55f59 | 26 | InterruptIn btn2(p18); // Stop sampoling |
afmiee | 5:b1a689c55f59 | 27 | |
afmiee | 5:b1a689c55f59 | 28 | // LED definitions |
afmiee | 5:b1a689c55f59 | 29 | DigitalOut led1(LED1); |
afmiee | 5:b1a689c55f59 | 30 | DigitalOut led2(LED2); |
afmiee | 5:b1a689c55f59 | 31 | |
afmiee | 5:b1a689c55f59 | 32 | // Global variables |
afmiee | 5:b1a689c55f59 | 33 | int start = 0; |
afmiee | 5:b1a689c55f59 | 34 | int stop = 0; |
afmiee | 5:b1a689c55f59 | 35 | |
tosihisa | 0:750784997b84 | 36 | |
afmiee | 5:b1a689c55f59 | 37 | // Generated when button 1 is pressed on rising edge START |
afmiee | 5:b1a689c55f59 | 38 | void start_smpl() |
afmiee | 5:b1a689c55f59 | 39 | { |
afmiee | 5:b1a689c55f59 | 40 | start = 1; |
afmiee | 5:b1a689c55f59 | 41 | stop = 0; |
afmiee | 5:b1a689c55f59 | 42 | } |
afmiee | 5:b1a689c55f59 | 43 | |
afmiee | 5:b1a689c55f59 | 44 | // Generated when button 1 is pressed on rising edge STOP |
afmiee | 5:b1a689c55f59 | 45 | void stop_smpl() |
afmiee | 5:b1a689c55f59 | 46 | { |
afmiee | 5:b1a689c55f59 | 47 | stop = 1; |
afmiee | 5:b1a689c55f59 | 48 | start = 0; |
afmiee | 5:b1a689c55f59 | 49 | } |
afmiee | 5:b1a689c55f59 | 50 | |
afmiee | 5:b1a689c55f59 | 51 | // Fillped every second |
afmiee | 5:b1a689c55f59 | 52 | void flip() |
afmiee | 5:b1a689c55f59 | 53 | { |
afmiee | 5:b1a689c55f59 | 54 | led2 = !led2; |
afmiee | 5:b1a689c55f59 | 55 | } |
afmiee | 5:b1a689c55f59 | 56 | |
afmiee | 5:b1a689c55f59 | 57 | int main() |
afmiee | 5:b1a689c55f59 | 58 | { |
afmiee | 5:b1a689c55f59 | 59 | led1= 1; |
afmiee | 5:b1a689c55f59 | 60 | char filename[256]; |
afmiee | 5:b1a689c55f59 | 61 | char secs_str[256]; |
afmiee | 5:b1a689c55f59 | 62 | |
afmiee | 5:b1a689c55f59 | 63 | struct tm t; |
afmiee | 5:b1a689c55f59 | 64 | time_t seconds; |
tosihisa | 0:750784997b84 | 65 | |
afmiee | 5:b1a689c55f59 | 66 | FILE *fp; |
afmiee | 6:9db9f4bfaf98 | 67 | |
afmiee | 5:b1a689c55f59 | 68 | // Attach functions to interrupts |
afmiee | 5:b1a689c55f59 | 69 | btn1.rise(&start_smpl); |
afmiee | 5:b1a689c55f59 | 70 | btn2.rise(&stop_smpl); |
afmiee | 5:b1a689c55f59 | 71 | flipper.attach(&flip, 1.0); // the address of the function to be attached (flip) and the interval (2 seconds) |
afmiee | 6:9db9f4bfaf98 | 72 | |
afmiee | 5:b1a689c55f59 | 73 | // Enable serial port |
afmiee | 5:b1a689c55f59 | 74 | debug.format(8,Serial::None,1); |
afmiee | 5:b1a689c55f59 | 75 | debug.baud(115200); |
afmiee | 5:b1a689c55f59 | 76 | debug.printf("LSM303DLH Test\x0d\x0a"); |
afmiee | 5:b1a689c55f59 | 77 | |
afmiee | 6:9db9f4bfaf98 | 78 | // Initialize 9DOF |
afmiee | 6:9db9f4bfaf98 | 79 | lol.begin(); |
afmiee | 6:9db9f4bfaf98 | 80 | if (!lol.begin()) { |
afmiee | 6:9db9f4bfaf98 | 81 | debug.printf("Failed to communicate with LSM9DS1.\n"); |
afmiee | 6:9db9f4bfaf98 | 82 | } |
afmiee | 6:9db9f4bfaf98 | 83 | lol.calibrate(); |
afmiee | 6:9db9f4bfaf98 | 84 | |
afmiee | 6:9db9f4bfaf98 | 85 | // // Initialize current time if needed |
afmiee | 5:b1a689c55f59 | 86 | // printf("Enter current date and time:\n"); |
afmiee | 5:b1a689c55f59 | 87 | // printf("YYYY MM DD HH MM SS[enter]\n"); |
afmiee | 5:b1a689c55f59 | 88 | // scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday |
afmiee | 5:b1a689c55f59 | 89 | // , &t.tm_hour, &t.tm_min, &t.tm_sec); |
afmiee | 5:b1a689c55f59 | 90 | |
afmiee | 5:b1a689c55f59 | 91 | // adjust for tm structure required values |
afmiee | 5:b1a689c55f59 | 92 | t.tm_year = t.tm_year - 1900; |
afmiee | 5:b1a689c55f59 | 93 | t.tm_mon = t.tm_mon - 1; |
afmiee | 5:b1a689c55f59 | 94 | |
afmiee | 5:b1a689c55f59 | 95 | // set the time |
afmiee | 5:b1a689c55f59 | 96 | rtc.set_time(mktime(&t)); |
afmiee | 5:b1a689c55f59 | 97 | |
afmiee | 5:b1a689c55f59 | 98 | while(1) { |
afmiee | 5:b1a689c55f59 | 99 | debug.printf("Press Button 1 to Start sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 100 | debug.printf("Press Button 2 to Stop sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 101 | // Check for button 1 pressed |
afmiee | 5:b1a689c55f59 | 102 | while(!start) { |
afmiee | 5:b1a689c55f59 | 103 | led1 = 1; |
afmiee | 5:b1a689c55f59 | 104 | } |
afmiee | 5:b1a689c55f59 | 105 | // Start sampling |
afmiee | 5:b1a689c55f59 | 106 | led1 = 0; |
afmiee | 5:b1a689c55f59 | 107 | debug.printf("Started sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 108 | // Get the time and create a file with the number of seconds in hex appended |
afmiee | 6:9db9f4bfaf98 | 109 | seconds = rtc.time(); |
afmiee | 5:b1a689c55f59 | 110 | sprintf(secs_str, "%s", ctime(&seconds)); |
afmiee | 5:b1a689c55f59 | 111 | printf("Started at: %s\n\r", secs_str ); |
afmiee | 5:b1a689c55f59 | 112 | sprintf(filename, "/sd/latch9DOF_%08x",seconds); |
afmiee | 5:b1a689c55f59 | 113 | fp = fopen(filename, "w"); |
afmiee | 5:b1a689c55f59 | 114 | // Verify that file can be created |
afmiee | 5:b1a689c55f59 | 115 | if ( fp == NULL ) { |
afmiee | 5:b1a689c55f59 | 116 | debug.printf("Cannot create file %s\n\r", filename); |
afmiee | 5:b1a689c55f59 | 117 | wait(0.5); |
afmiee | 5:b1a689c55f59 | 118 | while(1) { |
afmiee | 5:b1a689c55f59 | 119 | led1 = !led1; |
afmiee | 5:b1a689c55f59 | 120 | wait(0.5); |
afmiee | 5:b1a689c55f59 | 121 | } |
afmiee | 5:b1a689c55f59 | 122 | } else |
afmiee | 5:b1a689c55f59 | 123 | debug.printf("File %s created successfully\n\r", filename); |
afmiee | 6:9db9f4bfaf98 | 124 | |
afmiee | 6:9db9f4bfaf98 | 125 | // Sample until button 2 is pressed |
afmiee | 5:b1a689c55f59 | 126 | while(!stop) { |
afmiee | 5:b1a689c55f59 | 127 | led1 = 0; |
afmiee | 6:9db9f4bfaf98 | 128 | lol.readAccel(); |
afmiee | 6:9db9f4bfaf98 | 129 | lol.readMag(); |
afmiee | 6:9db9f4bfaf98 | 130 | lol.readGyro(); |
afmiee | 6:9db9f4bfaf98 | 131 | debug.printf("%d, %d, %d, ", lol.ax, lol.ay, lol.az); |
afmiee | 6:9db9f4bfaf98 | 132 | debug.printf("%d, %d, %d,", lol.mx, lol.my, lol.mz); |
afmiee | 6:9db9f4bfaf98 | 133 | debug.printf("%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); |
afmiee | 6:9db9f4bfaf98 | 134 | fprintf(fp, "%d, %d, %d, ", lol.ax, lol.ay, lol.az); |
afmiee | 6:9db9f4bfaf98 | 135 | fprintf(fp, "%d, %d, %d,", lol.mx, lol.my, lol.mz); |
afmiee | 6:9db9f4bfaf98 | 136 | fprintf(fp, "%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); |
afmiee | 5:b1a689c55f59 | 137 | wait(0.1); |
afmiee | 5:b1a689c55f59 | 138 | } |
afmiee | 5:b1a689c55f59 | 139 | // Stop Sampling and close file |
afmiee | 5:b1a689c55f59 | 140 | led1 = 1; |
afmiee | 5:b1a689c55f59 | 141 | debug.printf("Stopped sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 142 | debug.printf("Results stored in %s\n\r", filename); |
afmiee | 5:b1a689c55f59 | 143 | fclose(fp); |
afmiee | 6:9db9f4bfaf98 | 144 | } |
tosihisa | 4:3c677edffb13 | 145 | } |