![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Minor fixes
Dependencies: LSM9DS1_Library SDFileSystem mbed nrf51_rtc
Fork of LSM303DLHTest by
main.cpp@7:cbfdcc57f110, 2016-07-19 (annotated)
- Committer:
- afmiee
- Date:
- Tue Jul 19 20:53:52 2016 +0000
- Revision:
- 7:cbfdcc57f110
- Parent:
- 6:9db9f4bfaf98
- Child:
- 8:8f4d7f1c588f
Made some correction to library
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 | 7:cbfdcc57f110 | 17 | I2C i2c(p30, p7); |
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 | 7:cbfdcc57f110 | 51 | void parp( int times ) |
afmiee | 7:cbfdcc57f110 | 52 | { |
afmiee | 7:cbfdcc57f110 | 53 | int i; |
afmiee | 7:cbfdcc57f110 | 54 | for( i = 0; i < times; i++) { |
afmiee | 7:cbfdcc57f110 | 55 | led1 = 0; |
afmiee | 7:cbfdcc57f110 | 56 | wait( 0.05); |
afmiee | 7:cbfdcc57f110 | 57 | led1 = 1; |
afmiee | 7:cbfdcc57f110 | 58 | wait( 0.05); |
afmiee | 7:cbfdcc57f110 | 59 | } |
afmiee | 7:cbfdcc57f110 | 60 | led2 = 1; |
afmiee | 7:cbfdcc57f110 | 61 | } |
afmiee | 7:cbfdcc57f110 | 62 | // Flipped every second |
afmiee | 5:b1a689c55f59 | 63 | void flip() |
afmiee | 5:b1a689c55f59 | 64 | { |
afmiee | 7:cbfdcc57f110 | 65 | led2 = 0; |
afmiee | 7:cbfdcc57f110 | 66 | wait(0.01); |
afmiee | 7:cbfdcc57f110 | 67 | led2 = 1; |
afmiee | 7:cbfdcc57f110 | 68 | } |
afmiee | 7:cbfdcc57f110 | 69 | |
afmiee | 7:cbfdcc57f110 | 70 | |
afmiee | 7:cbfdcc57f110 | 71 | uint8_t I2CreadByte(uint8_t address, uint8_t subAddress) |
afmiee | 7:cbfdcc57f110 | 72 | { |
afmiee | 7:cbfdcc57f110 | 73 | char data; |
afmiee | 7:cbfdcc57f110 | 74 | char temp= subAddress; |
afmiee | 7:cbfdcc57f110 | 75 | |
afmiee | 7:cbfdcc57f110 | 76 | //i2c.write(address, temp, 1); |
afmiee | 7:cbfdcc57f110 | 77 | //temp[1] = 0x00; |
afmiee | 7:cbfdcc57f110 | 78 | i2c.write(address, &temp, 1); |
afmiee | 7:cbfdcc57f110 | 79 | int a = i2c.read(address, &data, 1); |
afmiee | 7:cbfdcc57f110 | 80 | return data; |
afmiee | 5:b1a689c55f59 | 81 | } |
afmiee | 5:b1a689c55f59 | 82 | |
afmiee | 5:b1a689c55f59 | 83 | int main() |
afmiee | 5:b1a689c55f59 | 84 | { |
afmiee | 5:b1a689c55f59 | 85 | led1= 1; |
afmiee | 5:b1a689c55f59 | 86 | char filename[256]; |
afmiee | 5:b1a689c55f59 | 87 | char secs_str[256]; |
afmiee | 5:b1a689c55f59 | 88 | |
afmiee | 5:b1a689c55f59 | 89 | struct tm t; |
afmiee | 5:b1a689c55f59 | 90 | time_t seconds; |
tosihisa | 0:750784997b84 | 91 | |
afmiee | 5:b1a689c55f59 | 92 | FILE *fp; |
afmiee | 6:9db9f4bfaf98 | 93 | |
afmiee | 5:b1a689c55f59 | 94 | // Attach functions to interrupts |
afmiee | 5:b1a689c55f59 | 95 | btn1.rise(&start_smpl); |
afmiee | 5:b1a689c55f59 | 96 | btn2.rise(&stop_smpl); |
afmiee | 5:b1a689c55f59 | 97 | flipper.attach(&flip, 1.0); // the address of the function to be attached (flip) and the interval (2 seconds) |
afmiee | 6:9db9f4bfaf98 | 98 | |
afmiee | 5:b1a689c55f59 | 99 | // Enable serial port |
afmiee | 5:b1a689c55f59 | 100 | debug.format(8,Serial::None,1); |
afmiee | 5:b1a689c55f59 | 101 | debug.baud(115200); |
afmiee | 5:b1a689c55f59 | 102 | debug.printf("LSM303DLH Test\x0d\x0a"); |
afmiee | 5:b1a689c55f59 | 103 | |
afmiee | 6:9db9f4bfaf98 | 104 | // Initialize 9DOF |
afmiee | 7:cbfdcc57f110 | 105 | //lol.begin(); |
afmiee | 6:9db9f4bfaf98 | 106 | if (!lol.begin()) { |
afmiee | 6:9db9f4bfaf98 | 107 | debug.printf("Failed to communicate with LSM9DS1.\n"); |
afmiee | 6:9db9f4bfaf98 | 108 | } |
afmiee | 6:9db9f4bfaf98 | 109 | lol.calibrate(); |
afmiee | 6:9db9f4bfaf98 | 110 | |
afmiee | 7:cbfdcc57f110 | 111 | lol.configInt(XG_INT1, INT1_IG_G|INT1_IG_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) |
afmiee | 7:cbfdcc57f110 | 112 | lol.configInt(XG_INT2, INT2_DRDY_G|INT2_DRDY_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) |
afmiee | 7:cbfdcc57f110 | 113 | |
afmiee | 7:cbfdcc57f110 | 114 | lol.configAccelInt(YHIE_XL|XHIE_XL, false); // INT_GEN_CFG_XL (06h) |
afmiee | 7:cbfdcc57f110 | 115 | lol.configAccelThs((uint8_t)0x25, X_AXIS, (uint8_t)0x00, false); // INT_GEN_THS_X_XL (07h) |
afmiee | 7:cbfdcc57f110 | 116 | lol.configAccelThs((uint8_t)0x25, Y_AXIS, (uint8_t)0x00, false); // INT_GEN_THS_Y_XL (08h) |
afmiee | 7:cbfdcc57f110 | 117 | lol.configAccelThs((uint8_t)0x25, Z_AXIS, (uint8_t)0x00, false); // INT_GEN_THS_Z_XL (09h) |
afmiee | 7:cbfdcc57f110 | 118 | |
afmiee | 7:cbfdcc57f110 | 119 | lol.configGyroInt(ZLIE_G|YLIE_G|XLIE_G, false, true); // INT_GEN_CFG_G (30h) |
afmiee | 7:cbfdcc57f110 | 120 | lol.configGyroThs((int16_t )0x300, X_AXIS, (uint8_t) 0x00, false); // INT_GEN_THS_X_G (31h - 32h) |
afmiee | 7:cbfdcc57f110 | 121 | lol.configGyroThs((int16_t )0x300, Y_AXIS, (uint8_t) 0x00, false); // INT_GEN_THS_Y_G (33h - 34h) |
afmiee | 7:cbfdcc57f110 | 122 | lol.configGyroThs((int16_t )0x300, Z_AXIS, (uint8_t) 0x00, false); // INT_GEN_THS_Z_G (35h - 36h) |
afmiee | 7:cbfdcc57f110 | 123 | debug.printf( "\n\r"); |
afmiee | 7:cbfdcc57f110 | 124 | debug.printf( "GyroIntSrc (14h) %02x\n\r", lol.getGyroIntSrc()); // INT_GEN_SRC_G (14h) |
afmiee | 7:cbfdcc57f110 | 125 | debug.printf( "AccelIntSrc(26h) %02x\n\r", lol.getAccelIntSrc()); // INT_GEN_SRC_XL (26h) |
afmiee | 7:cbfdcc57f110 | 126 | debug.printf( "MagIntSrc (31h) %02x\n\r", lol.getMagIntSrc()); // INT_SRC_M (31h) |
afmiee | 7:cbfdcc57f110 | 127 | debug.printf( "Inactivity (17h) %02x\n\r", lol.getInactivity()); // STATUS_REG (17h) |
afmiee | 7:cbfdcc57f110 | 128 | |
afmiee | 7:cbfdcc57f110 | 129 | debug.printf( "INT1_CTRL (0Ch) %02x\n\r", I2CreadByte(0xD6, 0x0C)); |
afmiee | 7:cbfdcc57f110 | 130 | debug.printf( "INT2_CTRL (0CD) %02x\n\r", I2CreadByte(0xD6, 0x0D)); |
afmiee | 7:cbfdcc57f110 | 131 | debug.printf( "CTRL_REG8 (22h) %02x\n\r", I2CreadByte(0xD6, 0x22)); |
afmiee | 7:cbfdcc57f110 | 132 | debug.printf( "\n\r"); |
afmiee | 7:cbfdcc57f110 | 133 | debug.printf( "INT_GEN_CFG_XL (06h) %02x\n\r", I2CreadByte(0xD6, 0x06)); |
afmiee | 7:cbfdcc57f110 | 134 | debug.printf( "INT_GEN_SRC_XL (26h) %02x\n\r", I2CreadByte(0xD6, 0x26)); |
afmiee | 7:cbfdcc57f110 | 135 | debug.printf( "INT_GEN_THS_X_XL (07h) %02x\n\r", I2CreadByte(0xD6, 0x07)); |
afmiee | 7:cbfdcc57f110 | 136 | debug.printf( "INT_GEN_THS_Y_XL (08h) %02x\n\r", I2CreadByte(0xD6, 0x08)); |
afmiee | 7:cbfdcc57f110 | 137 | debug.printf( "INT_GEN_THS_Z_XL (09h) %02x\n\r", I2CreadByte(0xD6, 0x09)); |
afmiee | 7:cbfdcc57f110 | 138 | debug.printf( "INT_GEN_DUR_XL (0ah) %02x\n\r", I2CreadByte(0xD6, 0x0a)); |
afmiee | 7:cbfdcc57f110 | 139 | debug.printf( "\n\r"); |
afmiee | 7:cbfdcc57f110 | 140 | debug.printf( "INT_GEN_CFG_G (30h) %02x\n\r", I2CreadByte(0xD6, 0x30)); |
afmiee | 7:cbfdcc57f110 | 141 | debug.printf( "INT_GEN_SRC_G (14h) %02x\n\r", I2CreadByte(0xD6, 0x14)); |
afmiee | 7:cbfdcc57f110 | 142 | debug.printf( "INT_GEN_THS_XH_G (31h) %02x\n\r", I2CreadByte(0xD6, 0x31)); |
afmiee | 7:cbfdcc57f110 | 143 | debug.printf( "INT_GEN_THS_XL_G (32h) %02x\n\r", I2CreadByte(0xD6, 0x32)); |
afmiee | 7:cbfdcc57f110 | 144 | debug.printf( "INT_GEN_THS_YH_G (33h) %02x\n\r", I2CreadByte(0xD6, 0x33)); |
afmiee | 7:cbfdcc57f110 | 145 | debug.printf( "INT_GEN_THS_YL_G (34h) %02x\n\r", I2CreadByte(0xD6, 0x34)); |
afmiee | 7:cbfdcc57f110 | 146 | debug.printf( "INT_GEN_THS_ZH_G (35h) %02x\n\r", I2CreadByte(0xD6, 0x35)); |
afmiee | 7:cbfdcc57f110 | 147 | debug.printf( "INT_GEN_THS_ZL_G (36h) %02x\n\r", I2CreadByte(0xD6, 0x36)); |
afmiee | 7:cbfdcc57f110 | 148 | debug.printf( "INT_GEN_DUR_G (37h) %02x\n\r", I2CreadByte(0xD6, 0x37)); |
afmiee | 7:cbfdcc57f110 | 149 | |
afmiee | 6:9db9f4bfaf98 | 150 | // // Initialize current time if needed |
afmiee | 5:b1a689c55f59 | 151 | // printf("Enter current date and time:\n"); |
afmiee | 5:b1a689c55f59 | 152 | // printf("YYYY MM DD HH MM SS[enter]\n"); |
afmiee | 5:b1a689c55f59 | 153 | // scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday |
afmiee | 5:b1a689c55f59 | 154 | // , &t.tm_hour, &t.tm_min, &t.tm_sec); |
afmiee | 5:b1a689c55f59 | 155 | |
afmiee | 5:b1a689c55f59 | 156 | // adjust for tm structure required values |
afmiee | 5:b1a689c55f59 | 157 | t.tm_year = t.tm_year - 1900; |
afmiee | 5:b1a689c55f59 | 158 | t.tm_mon = t.tm_mon - 1; |
afmiee | 5:b1a689c55f59 | 159 | |
afmiee | 5:b1a689c55f59 | 160 | // set the time |
afmiee | 5:b1a689c55f59 | 161 | rtc.set_time(mktime(&t)); |
afmiee | 5:b1a689c55f59 | 162 | |
afmiee | 5:b1a689c55f59 | 163 | while(1) { |
afmiee | 5:b1a689c55f59 | 164 | debug.printf("Press Button 1 to Start sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 165 | debug.printf("Press Button 2 to Stop sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 166 | // Check for button 1 pressed |
afmiee | 5:b1a689c55f59 | 167 | while(!start) { |
afmiee | 5:b1a689c55f59 | 168 | led1 = 1; |
afmiee | 5:b1a689c55f59 | 169 | } |
afmiee | 5:b1a689c55f59 | 170 | // Start sampling |
afmiee | 7:cbfdcc57f110 | 171 | //led1 = 0; |
afmiee | 7:cbfdcc57f110 | 172 | parp(5); |
afmiee | 5:b1a689c55f59 | 173 | debug.printf("Started sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 174 | // Get the time and create a file with the number of seconds in hex appended |
afmiee | 6:9db9f4bfaf98 | 175 | seconds = rtc.time(); |
afmiee | 5:b1a689c55f59 | 176 | sprintf(secs_str, "%s", ctime(&seconds)); |
afmiee | 5:b1a689c55f59 | 177 | printf("Started at: %s\n\r", secs_str ); |
afmiee | 5:b1a689c55f59 | 178 | sprintf(filename, "/sd/latch9DOF_%08x",seconds); |
afmiee | 5:b1a689c55f59 | 179 | fp = fopen(filename, "w"); |
afmiee | 5:b1a689c55f59 | 180 | // Verify that file can be created |
afmiee | 5:b1a689c55f59 | 181 | if ( fp == NULL ) { |
afmiee | 5:b1a689c55f59 | 182 | debug.printf("Cannot create file %s\n\r", filename); |
afmiee | 5:b1a689c55f59 | 183 | wait(0.5); |
afmiee | 5:b1a689c55f59 | 184 | while(1) { |
afmiee | 5:b1a689c55f59 | 185 | led1 = !led1; |
afmiee | 5:b1a689c55f59 | 186 | wait(0.5); |
afmiee | 5:b1a689c55f59 | 187 | } |
afmiee | 5:b1a689c55f59 | 188 | } else |
afmiee | 5:b1a689c55f59 | 189 | debug.printf("File %s created successfully\n\r", filename); |
afmiee | 6:9db9f4bfaf98 | 190 | |
afmiee | 6:9db9f4bfaf98 | 191 | // Sample until button 2 is pressed |
afmiee | 5:b1a689c55f59 | 192 | while(!stop) { |
afmiee | 7:cbfdcc57f110 | 193 | //led1 = 0; |
afmiee | 6:9db9f4bfaf98 | 194 | lol.readAccel(); |
afmiee | 6:9db9f4bfaf98 | 195 | lol.readMag(); |
afmiee | 6:9db9f4bfaf98 | 196 | lol.readGyro(); |
afmiee | 6:9db9f4bfaf98 | 197 | debug.printf("%d, %d, %d, ", lol.ax, lol.ay, lol.az); |
afmiee | 6:9db9f4bfaf98 | 198 | debug.printf("%d, %d, %d,", lol.mx, lol.my, lol.mz); |
afmiee | 6:9db9f4bfaf98 | 199 | debug.printf("%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); |
afmiee | 6:9db9f4bfaf98 | 200 | fprintf(fp, "%d, %d, %d, ", lol.ax, lol.ay, lol.az); |
afmiee | 6:9db9f4bfaf98 | 201 | fprintf(fp, "%d, %d, %d,", lol.mx, lol.my, lol.mz); |
afmiee | 6:9db9f4bfaf98 | 202 | fprintf(fp, "%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); |
afmiee | 5:b1a689c55f59 | 203 | wait(0.1); |
afmiee | 5:b1a689c55f59 | 204 | } |
afmiee | 5:b1a689c55f59 | 205 | // Stop Sampling and close file |
afmiee | 7:cbfdcc57f110 | 206 | parp(10); |
afmiee | 5:b1a689c55f59 | 207 | led1 = 1; |
afmiee | 5:b1a689c55f59 | 208 | debug.printf("Stopped sampling\n\r"); |
afmiee | 5:b1a689c55f59 | 209 | debug.printf("Results stored in %s\n\r", filename); |
afmiee | 5:b1a689c55f59 | 210 | fclose(fp); |
afmiee | 6:9db9f4bfaf98 | 211 | } |
tosihisa | 4:3c677edffb13 | 212 | } |
afmiee | 7:cbfdcc57f110 | 213 | |
afmiee | 7:cbfdcc57f110 | 214 | |
afmiee | 7:cbfdcc57f110 | 215 |