Allows for reading accelerometer, gyroscope, and magnetometer data from an LSM9DS0 IMU device
Dependents: uVGA_4180 uLCD_4180_mini ECE4781_Project
main.cpp@4:bf8f4e7c9905, 2014-12-03 (annotated)
- Committer:
- randrews33
- Date:
- Wed Dec 03 23:08:09 2014 +0000
- Revision:
- 4:bf8f4e7c9905
- Parent:
- 3:7ede465deae1
- Child:
- 5:e6a15dcba942
Issues with the previous versions caused very strange values to be read back. This was due to the wrong addresses being read. Fixed here
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
randrews33 | 4:bf8f4e7c9905 | 1 | //Most of the Credit goes to jimblom |
randrews33 | 0:1b975a6ae539 | 2 | #include "LSM9DS0.h" |
randrews33 | 0:1b975a6ae539 | 3 | #include "mbed.h" |
randrews33 | 0:1b975a6ae539 | 4 | |
randrews33 | 0:1b975a6ae539 | 5 | // SDO_XM and SDO_G are both grounded, so our addresses are: |
randrews33 | 4:bf8f4e7c9905 | 6 | #define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW |
randrews33 | 4:bf8f4e7c9905 | 7 | #define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW |
randrews33 | 0:1b975a6ae539 | 8 | |
randrews33 | 0:1b975a6ae539 | 9 | |
randrews33 | 4:bf8f4e7c9905 | 10 | LSM9DS0 dof(p9, p10,LSM9DS0_G, LSM9DS0_XM); |
randrews33 | 0:1b975a6ae539 | 11 | Serial pc(USBTX, USBRX); // tx, rx |
randrews33 | 0:1b975a6ae539 | 12 | |
randrews33 | 4:bf8f4e7c9905 | 13 | //Uncomment If you want to use interrupts |
randrews33 | 4:bf8f4e7c9905 | 14 | /*DigitalIn pinDRDYG(p14); |
randrews33 | 4:bf8f4e7c9905 | 15 | DigitalIn pinINTG(p13); |
randrews33 | 4:bf8f4e7c9905 | 16 | DigitalIn pinINT1_XM(p14); |
randrews33 | 4:bf8f4e7c9905 | 17 | DigitalIn pinINT2_XM(p15);*/ |
randrews33 | 0:1b975a6ae539 | 18 | |
randrews33 | 4:bf8f4e7c9905 | 19 | #define PRINT_CALCULATED |
randrews33 | 4:bf8f4e7c9905 | 20 | #define PRINT_SPEED 500 // 500 ms between prints |
randrews33 | 4:bf8f4e7c9905 | 21 | |
randrews33 | 4:bf8f4e7c9905 | 22 | //Init Serial port and LSM9DS0 chip |
randrews33 | 0:1b975a6ae539 | 23 | void setup() |
randrews33 | 0:1b975a6ae539 | 24 | { |
randrews33 | 0:1b975a6ae539 | 25 | pc.baud(115200); // Start serial at 115200 bps |
randrews33 | 0:1b975a6ae539 | 26 | // Use the begin() function to initialize the LSM9DS0 library. |
randrews33 | 0:1b975a6ae539 | 27 | // You can either call it with no parameters (the easy way): |
randrews33 | 0:1b975a6ae539 | 28 | uint16_t status = dof.begin(); |
randrews33 | 0:1b975a6ae539 | 29 | |
randrews33 | 4:bf8f4e7c9905 | 30 | |
randrews33 | 4:bf8f4e7c9905 | 31 | //Make sure communication is working |
randrews33 | 0:1b975a6ae539 | 32 | pc.printf("LSM9DS0 WHO_AM_I's returned: 0x"); |
randrews33 | 0:1b975a6ae539 | 33 | pc.printf("%x\n",status); |
randrews33 | 0:1b975a6ae539 | 34 | pc.printf("Should be 0x49D4\n"); |
randrews33 | 0:1b975a6ae539 | 35 | pc.printf("\n"); |
randrews33 | 0:1b975a6ae539 | 36 | } |
randrews33 | 0:1b975a6ae539 | 37 | |
randrews33 | 4:bf8f4e7c9905 | 38 | |
randrews33 | 4:bf8f4e7c9905 | 39 | void printGyro() |
randrews33 | 4:bf8f4e7c9905 | 40 | { |
randrews33 | 4:bf8f4e7c9905 | 41 | // To read from the gyroscope, you must first call the |
randrews33 | 4:bf8f4e7c9905 | 42 | // readGyro() function. When this exits, it'll update the |
randrews33 | 4:bf8f4e7c9905 | 43 | // gx, gy, and gz variables with the most current data. |
randrews33 | 0:1b975a6ae539 | 44 | |
randrews33 | 4:bf8f4e7c9905 | 45 | //Uncomment code if you plan on using interrupts |
randrews33 | 4:bf8f4e7c9905 | 46 | /*while(pinDRDYG == 0) |
randrews33 | 4:bf8f4e7c9905 | 47 | { |
randrews33 | 4:bf8f4e7c9905 | 48 | ; |
randrews33 | 4:bf8f4e7c9905 | 49 | }*/ |
randrews33 | 4:bf8f4e7c9905 | 50 | |
randrews33 | 0:1b975a6ae539 | 51 | |
randrews33 | 4:bf8f4e7c9905 | 52 | dof.readGyro(); |
randrews33 | 4:bf8f4e7c9905 | 53 | |
randrews33 | 4:bf8f4e7c9905 | 54 | // Now we can use the gx, gy, and gz variables as we please. |
randrews33 | 4:bf8f4e7c9905 | 55 | // Either print them as raw ADC values, or calculated in DPS. |
randrews33 | 4:bf8f4e7c9905 | 56 | pc.printf("G: "); |
randrews33 | 4:bf8f4e7c9905 | 57 | #ifdef PRINT_CALCULATED |
randrews33 | 4:bf8f4e7c9905 | 58 | // If you want to print calculated values, you can use the |
randrews33 | 4:bf8f4e7c9905 | 59 | // calcGyro helper function to convert a raw ADC value to |
randrews33 | 4:bf8f4e7c9905 | 60 | // DPS. Give the function the value that you want to convert. |
randrews33 | 4:bf8f4e7c9905 | 61 | //pc.printf("The bias: %2f, %2f, %2f \n", dof.gbias[0], dof.gbias[1], dof.gbias[2]); |
randrews33 | 4:bf8f4e7c9905 | 62 | pc.printf("%2f",dof.calcGyro(dof.gx) - dof.gbias[0]); |
randrews33 | 0:1b975a6ae539 | 63 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 64 | pc.printf("%2f",dof.calcGyro(dof.gy) - dof.gbias[1]); |
randrews33 | 0:1b975a6ae539 | 65 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 66 | pc.printf("%2f\n",dof.calcGyro(dof.gz) - dof.gbias[2]); |
randrews33 | 4:bf8f4e7c9905 | 67 | #elif defined PRINT_RAW |
randrews33 | 4:bf8f4e7c9905 | 68 | pc.printf("%d", dof.gx); |
randrews33 | 4:bf8f4e7c9905 | 69 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 70 | pc.printf("%d",dof.gy); |
randrews33 | 4:bf8f4e7c9905 | 71 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 72 | pc.printf("%d\n",dof.gz); |
randrews33 | 4:bf8f4e7c9905 | 73 | #endif |
randrews33 | 0:1b975a6ae539 | 74 | } |
randrews33 | 0:1b975a6ae539 | 75 | |
randrews33 | 4:bf8f4e7c9905 | 76 | void printAccel() |
randrews33 | 4:bf8f4e7c9905 | 77 | { |
randrews33 | 4:bf8f4e7c9905 | 78 | //Uncomment code if you plan on using interrupts |
randrews33 | 4:bf8f4e7c9905 | 79 | /*while(pinINT1_XM == 0) |
randrews33 | 4:bf8f4e7c9905 | 80 | { |
randrews33 | 4:bf8f4e7c9905 | 81 | ; |
randrews33 | 4:bf8f4e7c9905 | 82 | }*/ |
randrews33 | 4:bf8f4e7c9905 | 83 | |
randrews33 | 4:bf8f4e7c9905 | 84 | |
randrews33 | 0:1b975a6ae539 | 85 | dof.readAccel(); |
randrews33 | 0:1b975a6ae539 | 86 | |
randrews33 | 4:bf8f4e7c9905 | 87 | // Now we can use the ax, ay, and az variables as we please. |
randrews33 | 4:bf8f4e7c9905 | 88 | // Either print them as raw ADC values, or calculated in g's. |
randrews33 | 0:1b975a6ae539 | 89 | pc.printf("A: "); |
randrews33 | 4:bf8f4e7c9905 | 90 | #ifdef PRINT_CALCULATED |
randrews33 | 4:bf8f4e7c9905 | 91 | // If you want to print calculated values, you can use the |
randrews33 | 4:bf8f4e7c9905 | 92 | // calcAccel helper function to convert a raw ADC value to |
randrews33 | 4:bf8f4e7c9905 | 93 | // g's. Give the function the value that you want to convert. |
randrews33 | 4:bf8f4e7c9905 | 94 | //pc.printf("The bias: %2f, %2f, %2f \n", dof.abias[0], dof.abias[1], dof.abias[2]); |
randrews33 | 4:bf8f4e7c9905 | 95 | pc.printf("%2f",dof.calcAccel(dof.ax) - dof.abias[0]); |
randrews33 | 0:1b975a6ae539 | 96 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 97 | pc.printf("%2f",dof.calcAccel(dof.ay) - dof.abias[1]); |
randrews33 | 0:1b975a6ae539 | 98 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 99 | pc.printf("%2f\n",dof.calcAccel(dof.az) - dof.abias[2]); |
randrews33 | 4:bf8f4e7c9905 | 100 | #elif defined PRINT_RAW |
randrews33 | 4:bf8f4e7c9905 | 101 | pc.printf("%d",dof.ax); |
randrews33 | 4:bf8f4e7c9905 | 102 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 103 | pc.printf("%d",dof.ay); |
randrews33 | 4:bf8f4e7c9905 | 104 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 105 | pc.printf("%d\n",dof.az); |
randrews33 | 4:bf8f4e7c9905 | 106 | #endif |
randrews33 | 0:1b975a6ae539 | 107 | |
randrews33 | 0:1b975a6ae539 | 108 | } |
randrews33 | 0:1b975a6ae539 | 109 | |
randrews33 | 4:bf8f4e7c9905 | 110 | void printMag() |
randrews33 | 4:bf8f4e7c9905 | 111 | { |
randrews33 | 4:bf8f4e7c9905 | 112 | //Uncomment code if you plan on using interrupts |
randrews33 | 4:bf8f4e7c9905 | 113 | /*while(pinINT2_XM == 0) |
randrews33 | 4:bf8f4e7c9905 | 114 | { |
randrews33 | 4:bf8f4e7c9905 | 115 | ; |
randrews33 | 4:bf8f4e7c9905 | 116 | }*/ |
randrews33 | 4:bf8f4e7c9905 | 117 | |
randrews33 | 4:bf8f4e7c9905 | 118 | // To read from the magnetometer, you must first call the |
randrews33 | 4:bf8f4e7c9905 | 119 | // readMag() function. When this exits, it'll update the |
randrews33 | 4:bf8f4e7c9905 | 120 | // mx, my, and mz variables with the most current data. |
randrews33 | 0:1b975a6ae539 | 121 | dof.readMag(); |
randrews33 | 0:1b975a6ae539 | 122 | |
randrews33 | 4:bf8f4e7c9905 | 123 | // Now we can use the mx, my, and mz variables as we please. |
randrews33 | 4:bf8f4e7c9905 | 124 | // Either print them as raw ADC values, or calculated in Gauss. |
randrews33 | 0:1b975a6ae539 | 125 | pc.printf("M: "); |
randrews33 | 4:bf8f4e7c9905 | 126 | #ifdef PRINT_CALCULATED |
randrews33 | 4:bf8f4e7c9905 | 127 | // If you want to print calculated values, you can use the |
randrews33 | 4:bf8f4e7c9905 | 128 | // calcMag helper function to convert a raw ADC value to |
randrews33 | 4:bf8f4e7c9905 | 129 | // Gauss. Give the function the value that you want to convert. |
randrews33 | 0:1b975a6ae539 | 130 | pc.printf("%2f",dof.calcMag(dof.mx)); |
randrews33 | 0:1b975a6ae539 | 131 | pc.printf(", "); |
randrews33 | 0:1b975a6ae539 | 132 | pc.printf("%2f",dof.calcMag(dof.my)); |
randrews33 | 0:1b975a6ae539 | 133 | pc.printf(", "); |
randrews33 | 0:1b975a6ae539 | 134 | pc.printf("%2f\n",dof.calcMag(dof.mz)); |
randrews33 | 4:bf8f4e7c9905 | 135 | #elif defined PRINT_RAW |
randrews33 | 4:bf8f4e7c9905 | 136 | pc.printf("%d", dof.mx); |
randrews33 | 4:bf8f4e7c9905 | 137 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 138 | pc.printf("%d", dof.my); |
randrews33 | 4:bf8f4e7c9905 | 139 | pc.printf(", "); |
randrews33 | 4:bf8f4e7c9905 | 140 | pc.printf("%d\n", dof.mz); |
randrews33 | 4:bf8f4e7c9905 | 141 | #endif |
randrews33 | 0:1b975a6ae539 | 142 | } |
randrews33 | 0:1b975a6ae539 | 143 | |
randrews33 | 4:bf8f4e7c9905 | 144 | void loop() |
randrews33 | 0:1b975a6ae539 | 145 | { |
randrews33 | 4:bf8f4e7c9905 | 146 | printGyro(); // Print "G: gx, gy, gz" |
randrews33 | 4:bf8f4e7c9905 | 147 | printAccel(); // Print "A: ax, ay, az" |
randrews33 | 4:bf8f4e7c9905 | 148 | printMag(); // Print "M: mx, my, mz" |
randrews33 | 4:bf8f4e7c9905 | 149 | wait_ms(PRINT_SPEED); |
randrews33 | 0:1b975a6ae539 | 150 | } |
randrews33 | 0:1b975a6ae539 | 151 | |
randrews33 | 0:1b975a6ae539 | 152 | |
randrews33 | 0:1b975a6ae539 | 153 | int main() |
randrews33 | 0:1b975a6ae539 | 154 | { |
randrews33 | 4:bf8f4e7c9905 | 155 | setup(); //Setup sensor and Serial |
randrews33 | 4:bf8f4e7c9905 | 156 | pc.printf("Hello World\n"); |
randrews33 | 4:bf8f4e7c9905 | 157 | |
randrews33 | 0:1b975a6ae539 | 158 | while (true) |
randrews33 | 0:1b975a6ae539 | 159 | { |
randrews33 | 4:bf8f4e7c9905 | 160 | //Calculate bias |
randrews33 | 4:bf8f4e7c9905 | 161 | dof.calLSM9DS0(dof.gbias, dof.abias); |
randrews33 | 4:bf8f4e7c9905 | 162 | //pc.printf("Setup Done\n"); |
randrews33 | 4:bf8f4e7c9905 | 163 | loop(); //Get sensor values |
randrews33 | 4:bf8f4e7c9905 | 164 | } |
randrews33 | 0:1b975a6ae539 | 165 | } |
randrews33 | 4:bf8f4e7c9905 | 166 |