![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Minor fixes
Dependencies: LSM9DS1_Library SDFileSystem mbed nrf51_rtc
Fork of LSM303DLHTest by
Diff: main.cpp
- Revision:
- 11:a246c67d44b0
- Parent:
- 10:e7ab0493ed9f
- Child:
- 12:f361ccfc191e
--- a/main.cpp Tue Jul 26 21:18:15 2016 +0000 +++ b/main.cpp Wed Jul 27 21:55:46 2016 +0000 @@ -7,18 +7,18 @@ #include "LSM9DS1.h" #include "SDFileSystem.h" #include "nrf51_rtc.h" -//#include "utilities.h" #define M_PI 3.14158 -#define DEBUG 1 +//#define DEBUG 1 // Print Debug information to serial terminal +#define MOTION 1 // Define if operated by motion or by buttons // define threshold and Duration registers for interrupts #define ACT_THS_REG 0x10 // 0x04 #define ACT_DUR_REG 0x02 // 0x05 -#define INT_GEN_THS_X_XL_REG 0x10 // 0x07 +#define INT_GEN_THS_X_XL_REG 0x0C // 0x07 #define INT_GEN_THS_Y_YL_REG 0xFF // 0x08 -#define INT_GEN_THS_Z_XL_REG 0x10 // 0x09 +#define INT_GEN_THS_Z_XL_REG 0x0C // 0x09 #define INT_GEN_DUR_XL_REG 0x00 // 0x0A #define INT_GEN_THS_XHL_G_REG 0x0300 // 0x31-32 @@ -26,7 +26,13 @@ #define INT_GEN_THS_ZHL_G_REG 0x0300 // 0x35-36 #define INT_GEN_DUR_G_REG 0x02 // 0x37 +#define LED_ON 0 +#define LED_OFF 01 +#define DBG_ACTIVE 0 +#define DBG_INACTIVE 1 + typedef unsigned long int ulint; + // Create objects Serial debug(USBTX,USBRX); // For Nordic @@ -39,19 +45,27 @@ // Create a ticker to use the nRF51 RTC Ticker flipper; -// Assign interrupts to switches +#ifdef MOTION +// Assign interrupts to Interrupts from LSM9DS1 InterruptIn int1(p12); // Start sampling -InterruptIn int2(p13); // Stop sampoling +InterruptIn int2(p13); // Stop sampling +#else +// Assign interrupts to Buttons 1 and 2 on nrf51-DK +InterruptIn but1(p17); // Start sampling +InterruptIn but2(p18); // Stop sampling +#endif // LED definitions DigitalOut led1(LED1); DigitalOut led2(LED2); -DigitalOut dbg1(p16); -DigitalOut dbg2(p17); +DigitalOut led3(LED3); + +DigitalOut dbg1(p24); +DigitalOut dbg2(p0); // Global variables -int start = 0; -int stop = 0; +volatile int start = 0; +volatile int stop = 0; time_t seconds; FILE *fpA; @@ -61,6 +75,243 @@ char filename[80]; char secs_str[80]; +uint8_t I2CreadByte(uint8_t address, uint8_t subAddress); +uint8_t I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count); +int file_rename(const char *oldfname, const char *newfname); +int file_copy (const char *src, const char *dst); +void start_smpl(); +void stop_smpl(); +void parp( int times ); +void flip(); +void print_config_int_registers( void ); +void open_temp_files( void ); +void rename_files( void ); +void DumpAccelGyroRegs( void ); + + +int main() +{ + led1 = 1; + led2 = 1; + + // debug pins + dbg1 = DBG_INACTIVE; + dbg2 = DBG_INACTIVE; + + struct tm t; + start = 0; + stop = 0; + +#ifdef MOTION + int1.fall(&start_smpl); + int2.fall(&stop_smpl); +#else + but1.rise(&start_smpl); + but2.rise(&stop_smpl); +#endif + + // Attach functions to interrupts + flipper.attach(&flip, 1.0); // the address of the function to be attached (flip) and the interval (2 seconds) + + // Enable serial port + debug.format(8,Serial::None,1); + debug.baud(115200); + +#ifdef DEBUG + debug.printf("LSM9DS1 Test\x0d\x0a"); +#endif + + // Initialize 9DOF + if (!lol.begin()) { + debug.printf("Failed to communicate with LSM9DS1.\n"); + wait(0.5); + while(1) { + led1 = !led1; + wait(0.5); + } + } else { + debug.printf("Communication with the LSM9DS1 successfully\n\r"); + } + + lol.calibrate(true); + lol.setFIFO(FIFO_CONT, 0x1F); + + lol.getStatus(); + lol.getAccelIntSrc(); + lol.readAccel(); + lol.readGyro(); + +#ifdef MOTION + // Configure + lol.configAccelThs((uint8_t)INT_GEN_THS_X_XL_REG, X_AXIS, (uint8_t)INT_GEN_DUR_XL_REG, false); // INT_GEN_THS_X_XL (07h) + lol.configAccelThs((uint8_t)INT_GEN_THS_Y_YL_REG, Y_AXIS, (uint8_t)INT_GEN_DUR_XL_REG, false); // INT_GEN_THS_Y_XL (08h) + lol.configAccelThs((uint8_t)INT_GEN_THS_Z_XL_REG, Z_AXIS, (uint8_t)INT_GEN_DUR_XL_REG, false); // INT_GEN_THS_Z_XL (09h) + + lol.configGyroThs((int16_t )INT_GEN_THS_XHL_G_REG, X_AXIS, (uint8_t) INT_GEN_DUR_G_REG, false); // INT_GEN_THS_X_G (31h - 32h) + lol.configGyroThs((int16_t )INT_GEN_THS_YHL_G_REG, Y_AXIS, (uint8_t) INT_GEN_DUR_G_REG, false); // INT_GEN_THS_Y_G (33h - 34h) + lol.configGyroThs((int16_t )INT_GEN_THS_ZHL_G_REG, Z_AXIS, (uint8_t) INT_GEN_DUR_G_REG, false); // INT_GEN_THS_Z_G (35h - 36h) + //lol.configGyroInt(ZHIE_G|YHIE_G|XHIE_G, false, false); // INT_GEN_CFG_G (30h) + + lol.configInt(XG_INT1, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) + lol.configInt(XG_INT2, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) +#endif + +#ifdef DEBUG + print_config_int_registers(); +#endif +// Dump all registers + //DumpAccelGyroRegs(); +// // Initialize current time if needed +// printf("Enter current date and time:\n"); +// printf("YYYY MM DD HH MM SS[enter]\n"); +// scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday +// , &t.tm_hour, &t.tm_min, &t.tm_sec); + + // adjust for tm structure required values + t.tm_year = t.tm_year - 1900; + t.tm_mon = t.tm_mon - 1; + + // set the time + rtc.set_time(mktime(&t)); + // Set the interrupt service routines + + // Wait for the start Signal generated by the accelerometer interrupt +#ifdef MOTION + // Read the interrupt to clear it + lol.getStatus(); + lol.getAccelIntSrc(); + // Disable the interrupt + lol.configAccelInt(0, false); // INT_GEN_CFG_XL (06h) + lol.configInt(XG_INT1, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) +#endif + + + while(1) { +#ifdef MOTION + lol.getStatus(); + lol.getAccelIntSrc(); + lol.readAccel(); + lol.getGyroIntSrc(); + lol.readGyro(); + // Disable the interrupt + lol.configAccelInt(0, false); // INT_GEN_CFG_XL (06h) + lol.configInt(XG_INT1, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) + // Read the interrupt to clear it +#endif + // Create temporary files + led3 = LED_ON; + open_temp_files(); + led3 = LED_OFF; +#ifdef DEBUG + debug.printf( "\n\r"); + debug.printf( "Status (27h) %02x\n\r", lol.getStatus()); // STATUS_REG (27h) + debug.printf( "GyroIntSrc (14h) %02x\n\r", lol.getGyroIntSrc()); // INT_GEN_SRC_G (14h) + debug.printf( "AccelIntSrc(26h) %02x\n\r", lol.getAccelIntSrc()); // INT_GEN_SRC_XL (26h) + debug.printf( "MagIntSrc (31h) %02x\n\r", lol.getMagIntSrc()); // INT_SRC_M (31h) + debug.printf( "Inactivity (17h) %02x\n\r", lol.getInactivity()); // STATUS_REG (17h) + debug.printf( "\n\r"); +#endif +#ifdef DEBUG + print_config_int_registers(); +#endif +#ifdef MOTION + // Program the motion interrupt on accelerometer + lol.configAccelInt(ZHIE_XL|YHIE_XL|XHIE_XL, false); // INT_GEN_CFG_XL (06h) + lol.configInt(XG_INT1, INT1_IG_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); // INT1_CTRL (0x0C) + CTRL_REG8 (0x22) +#endif + // Check for button 1 pressed or + // Wait for the start Signal generated by the accelerometer interrupt + // Depends if MOTION is defined +#ifdef DEBUG +#ifdef MOTION + debug.printf("Waiting for motion to start sampling\n\r"); +#else + debug.printf("Waiting for Button 1 to be pressed to start sampling\n\r"); +#endif +#endif + while(start==0); + dbg1 = DBG_ACTIVE; + dbg2 = DBG_INACTIVE; +#ifdef DEBUG +#ifdef MOTION + debug.printf("Motion Detected\n\r"); +#else + debug.printf("Button 1 pressed\n\r"); +#endif +#endif +#ifdef MOTION + // Reset the Interrupt + lol.getStatus(); + lol.getAccelIntSrc(); + + // Disable the interrupt + lol.configAccelInt(0, false); // INT_GEN_CFG_XL (06h) + lol.configInt(XG_INT1, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) + // Read the interrupt to clear it +#endif +#ifdef DEBUG + debug.printf("Started sampling\n\r"); + // Get the time and create a file with the number of seconds in hex appended +#endif + // Caoture the seconds since Turn-on to name the files + seconds = rtc.time(); + sprintf(secs_str, "%s", ctime(&seconds)); +#ifdef DEBUG + debug.printf("\n\rStarted at: %s\n\r\n\r", secs_str ); +#endif +#ifdef MOTION + // program inactivity timer + lol.configInactivity(ACT_THS_REG, ACT_DUR_REG, true); + lol.configInt(XG_INT2, INT2_INACT, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) +#endif + //Execute until inactivity timer is triggered +#ifdef DEBUG + print_config_int_registers(); +#endif + + while(!stop) { + dbg1 = DBG_INACTIVE; + dbg2 = DBG_ACTIVE; + if (lol.accelAvailable()) { + lol.readAccel(); +#ifdef DEBUG + debug.printf("ACC %d, %d, %d\n\r", lol.ax, lol.ay, lol.az); +#endif + fprintf(fpA, "%d, %d, %d\n\r", lol.ax, lol.ay, lol.az); + } + if ( lol.magAvailable(X_AXIS) && lol.magAvailable(Y_AXIS) && lol.magAvailable(Z_AXIS)) { + lol.readMag(); +#ifdef DEBUG + debug.printf("MAG %d, %d, %d\n\r", lol.mx, lol.my, lol.mz); +#endif + fprintf(fpM, "%d, %d, %d\n\r", lol.mx, lol.my, lol.mz); + } + if ( lol.gyroAvailable()) { + lol.readGyro(); +#ifdef DEBUG + debug.printf("GYR %d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); +#endif + fprintf(fpG, "%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); + } + } +#ifdef MOTION + //Disable inactivity interrupt + lol.configInt(XG_INT2, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) +#endif + // Stop Sampling and close file + //parp(10); +#ifdef DEBUG + debug.printf("Stopped sampling\n\r"); +#endif + led3 = LED_ON; + fclose(fpA); + fclose(fpM); + fclose(fpG); + rename_files(); + led3 = LED_OFF; + } +} + uint8_t I2CreadByte(uint8_t address, uint8_t subAddress) { char data; @@ -153,10 +404,10 @@ // Modified to be generated by Interrupt 1 void start_smpl() { - //lol.readAccel(); start = 1; stop = 0; - //dbg2 = 1; + //dbg1 = 1; + //dbg2 = 0; } // Generated when button 1 is pressed on rising edge STOP @@ -165,30 +416,32 @@ { stop = 1; start = 0; - //dbg2 = 0; + //dbg1 = 0; + //dbg2 = 1; } void parp( int times ) { int i; for( i = 0; i < times; i++) { - led1 = 0; + led1 = LED_ON; wait( 0.05); - led1 = 1; + led1 = LED_OFF; wait( 0.05); } led2 = 1; } + // Flipped every second void flip() { - led2 = 0; + led2 = LED_ON; wait(0.01); - led2 = 1; - if ( !stop ) { - led1 = 0; + led2 = LED_OFF; + if ( start && !stop ) { + led1 = LED_ON; wait(0.01); - led1 = 1; + led1 = LED_OFF; } } @@ -222,6 +475,7 @@ void open_temp_files( void ) { + debug.printf("\n\r"); fpA = fopen("/sd/ACC.csv", "w"); // Verify that file can be created if ( fpA == NULL ) { @@ -262,194 +516,23 @@ void rename_files( void ) { sprintf(filename, "/sd/latch9DOFA_%08x.csv",seconds); - if((file_rename("/sd/ACC.csv",filename )) == 0) { - debug.printf("File ACC.csv renamed successfully to %s\n\r", filename); + debug.printf("\n\r"); + if((file_copy("/sd/ACC.csv",filename )) == 0) { + debug.printf("File ACC.csv copied successfully to %s\n\r", filename); } else { - debug.printf("Error: unable to rename the file ACC.csv"); + debug.printf("Error: unable to copy the file ACC.csv"); } sprintf(filename, "/sd/latch9DOFM_%08x.csv",seconds); - if((file_rename("/sd/MAG.csv",filename )) == 0) { - debug.printf("File MAG.csv renamed successfully to %s\n\r", filename); + if((file_copy("/sd/MAG.csv",filename )) == 0) { + debug.printf("File MAG.csv copied successfully to %s\n\r", filename); } else { - debug.printf("Error: unable to rename the file MAG.csv"); + debug.printf("Error: unable to copy the file MAG.csv"); } sprintf(filename, "/sd/latch9DOFG_%08x.csv",seconds); - if((file_rename("/sd/GYR.csv",filename )) == 0) { - debug.printf("File GYR.csv renamed successfully to %s\n\r", filename); - } else { - debug.printf("Error: unable to rename the file GYR.csv"); - } -} - -int main() -{ - led1= 1; - - struct tm t; - // debug pins - dbg1 = 0; - dbg2 = 0; - - // Attach functions to interrupts - flipper.attach(&flip, 1.0); // the address of the function to be attached (flip) and the interval (2 seconds) - - // Enable serial port - debug.format(8,Serial::None,1); - debug.baud(115200); -#ifdef DEBUG - debug.printf("LSM9DS1 Test\x0d\x0a"); -#endif - - // Initialize 9DOF - if (!lol.begin()) { - debug.printf("Failed to communicate with LSM9DS1.\n"); - wait(0.5); - while(1) { - led1 = !led1; - wait(0.5); - } + if((file_copy("/sd/GYR.csv",filename )) == 0) { + debug.printf("File GYR.csv copied successfully to %s\n\r", filename); } else { - debug.printf("Communication with the LSM9DS1 successfully\n\r"); - } - - lol.calibrate(true); - //lol.getStatus(); - //lol.getAccelIntSrc(); - //lol.readAccel(); - //lol.readGyro(); - - lol.configAccelThs((uint8_t)INT_GEN_THS_X_XL_REG, X_AXIS, (uint8_t)INT_GEN_DUR_XL_REG, false); // INT_GEN_THS_X_XL (07h) - lol.configAccelThs((uint8_t)INT_GEN_THS_Y_YL_REG, Y_AXIS, (uint8_t)INT_GEN_DUR_XL_REG, false); // INT_GEN_THS_Y_XL (08h) - lol.configAccelThs((uint8_t)INT_GEN_THS_Z_XL_REG, Z_AXIS, (uint8_t)INT_GEN_DUR_XL_REG, false); // INT_GEN_THS_Z_XL (09h) - - lol.configGyroThs((int16_t )INT_GEN_THS_XHL_G_REG, X_AXIS, (uint8_t) INT_GEN_DUR_G_REG, false); // INT_GEN_THS_X_G (31h - 32h) - lol.configGyroThs((int16_t )INT_GEN_THS_YHL_G_REG, Y_AXIS, (uint8_t) INT_GEN_DUR_G_REG, false); // INT_GEN_THS_Y_G (33h - 34h) - lol.configGyroThs((int16_t )INT_GEN_THS_ZHL_G_REG, Z_AXIS, (uint8_t) INT_GEN_DUR_G_REG, false); // INT_GEN_THS_Z_G (35h - 36h) - //lol.configGyroInt(ZHIE_G|YHIE_G|XHIE_G, false, false); // INT_GEN_CFG_G (30h) - - lol.configInt(XG_INT1, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) - lol.configInt(XG_INT2, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) -#ifdef DEBUG - print_config_int_registers(); -#endif -// Dump all registers - //DumpAccelGyroRegs(); -// // Initialize current time if needed -// printf("Enter current date and time:\n"); -// printf("YYYY MM DD HH MM SS[enter]\n"); -// scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday -// , &t.tm_hour, &t.tm_min, &t.tm_sec); - -// adjust for tm structure required values - t.tm_year = t.tm_year - 1900; - t.tm_mon = t.tm_mon - 1; - -// set the time - rtc.set_time(mktime(&t)); - // Set the interrupt service routines - int1.fall(&start_smpl); - int2.fall(&stop_smpl); - - // Wait for the start Signal generated by the accelerometer interrupt - lol.configAccelInt(ZHIE_XL|YHIE_XL|XHIE_XL, false); // INT_GEN_CFG_XL (06h) - lol.configInt(XG_INT1, INT1_IG_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) - lol.configInactivity(ACT_THS_REG, ACT_DUR_REG, true); - lol.configInt(XG_INT2, INT2_INACT, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) - - while(1) { - // Check for button 1 pressed -#ifdef DEBUG - debug.printf( "\n\r"); - debug.printf( "Status (27h) %02x\n\r", lol.getStatus()); // STATUS_REG (27h) - debug.printf( "GyroIntSrc (14h) %02x\n\r", lol.getGyroIntSrc()); // INT_GEN_SRC_G (14h) - debug.printf( "AccelIntSrc(26h) %02x\n\r", lol.getAccelIntSrc()); // INT_GEN_SRC_XL (26h) - debug.printf( "MagIntSrc (31h) %02x\n\r", lol.getMagIntSrc()); // INT_SRC_M (31h) - debug.printf( "Inactivity (17h) %02x\n\r", lol.getInactivity()); // STATUS_REG (17h) - debug.printf( "\n\r"); -#endif - // Wait for the start Signal generated by the accelerometer interrupt - //lol.configAccelInt(ZHIE_XL|YHIE_XL|XHIE_XL, false); // INT_GEN_CFG_XL (06h) - //lol.configInt(XG_INT1, INT1_IG_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) - //lol.configInactivity(ACT_THS_REG, ACT_DUR_REG, true); - //lol.configInt(XG_INT2, INT2_INACT, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) - -#ifdef DEBUG - print_config_int_registers(); -#endif - open_temp_files(); - start = 0; - while(!start) { - led1 = 1; - } - dbg1 = 1; - // Disable the interrupt - //lol.configAccelInt(0, false); // INT_GEN_CFG_XL (06h) - //lol.configInt(XG_INT1, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) - // Read the interrupt to clear it - //lol.getStatus(); - // lol.getAccelIntSrc(); - //lol.readAccel(); - //lol.getGyroIntSrc(); - //lol.readGyro(); - - dbg1 = 0; - // Start sampling - //led1 = 0; - //parp(5); -#ifdef DEBUG - debug.printf("Started sampling\n\r"); - // Get the time and create a file with the number of seconds in hex appended -#endif - seconds = rtc.time(); - - sprintf(secs_str, "%s", ctime(&seconds)); -#ifdef DEBUG - debug.printf("\n\rStarted at: %s\n\r\n\r", secs_str ); -#endif - // program inactivity timer - - //lol.configInactivity(ACT_THS_REG, ACT_DUR_REG, true); - //lol.configInt(XG_INT2, INT2_INACT, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) - // Execute until inctivity timer is triggered -#ifdef DEBUG - print_config_int_registers(); -#endif - while(!stop) { - dbg2 = 1; - if (lol.accelAvailable()) { - lol.readAccel(); -#ifdef DEBUG - debug.printf("ACC %d, %d, %d\n\r", lol.ax, lol.ay, lol.az); -#endif - fprintf(fpA, "%d, %d, %d\n\r", lol.ax, lol.ay, lol.az); - } - if ( lol.magAvailable(X_AXIS) && lol.magAvailable(Y_AXIS) && lol.magAvailable(Z_AXIS)) { - lol.readMag(); -#ifdef DEBUG - debug.printf("MAG %d, %d, %d\n\r", lol.mx, lol.my, lol.mz); -#endif - fprintf(fpM, "%d, %d, %d\n\r", lol.mx, lol.my, lol.mz); - } - if ( lol.gyroAvailable()) { - lol.readGyro(); -#ifdef DEBUG - debug.printf("GYR %d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); -#endif - fprintf(fpG, "%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); - } - } - dbg2 = 0; - //lol.configInt(XG_INT2, 0, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) - - // Stop Sampling and close file - parp(10); -#ifdef DEBUG - debug.printf("Stopped sampling\n\r"); -#endif - fclose(fpA); - fclose(fpM); - fclose(fpG); - rename_files(); + debug.printf("Error: unable to copy the file GYR.csv"); } }