this can make a serial acc data to be sent to a processing software
Dependencies: mbed PinDetect LSM9DS1_works
main.cpp@3:64a8188c5a44, 2020-03-02 (annotated)
- Committer:
- chebbi
- Date:
- Mon Mar 02 10:08:57 2020 +0000
- Revision:
- 3:64a8188c5a44
- Parent:
- 0:e8167f37725c
- Child:
- 4:7faa8147cabb
that lsm9ds1 works with EFM32WG
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmar7 | 0:e8167f37725c | 1 | #include "LSM9DS1.h" |
jmar7 | 0:e8167f37725c | 2 | |
chebbi | 3:64a8188c5a44 | 3 | //DigitalOut myled(LED1); |
jmar7 | 0:e8167f37725c | 4 | Serial pc(USBTX, USBRX); |
chebbi | 3:64a8188c5a44 | 5 | #define PI 3,14 |
chebbi | 3:64a8188c5a44 | 6 | #define printff_CALCULATED |
chebbi | 3:64a8188c5a44 | 7 | #define printff_SPEED 250 |
chebbi | 3:64a8188c5a44 | 8 | #define DECLINATION -8.58 |
jmar7 | 0:e8167f37725c | 9 | |
chebbi | 3:64a8188c5a44 | 10 | void printfGyro(); |
chebbi | 3:64a8188c5a44 | 11 | void printfAccel(); |
chebbi | 3:64a8188c5a44 | 12 | void printfMag(); |
chebbi | 3:64a8188c5a44 | 13 | void printfAttitude(float ax, float ay, float az, float mx, float my, float mz); |
chebbi | 3:64a8188c5a44 | 14 | |
chebbi | 3:64a8188c5a44 | 15 | LSM9DS1 lol(PC4, PC5,0xD6, 0x3C); |
jmar7 | 0:e8167f37725c | 16 | int main() { |
jmar7 | 0:e8167f37725c | 17 | //LSM9DS1 lol(p9, p10, 0x6B, 0x1E); |
chebbi | 3:64a8188c5a44 | 18 | |
jmar7 | 0:e8167f37725c | 19 | lol.begin(); |
jmar7 | 0:e8167f37725c | 20 | if (!lol.begin()) { |
jmar7 | 0:e8167f37725c | 21 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
chebbi | 3:64a8188c5a44 | 22 | while(1) ; |
jmar7 | 0:e8167f37725c | 23 | } |
jmar7 | 0:e8167f37725c | 24 | lol.calibrate(); |
jmar7 | 0:e8167f37725c | 25 | while(1) { |
chebbi | 3:64a8188c5a44 | 26 | |
chebbi | 3:64a8188c5a44 | 27 | //lol.readTemp(); |
chebbi | 3:64a8188c5a44 | 28 | |
chebbi | 3:64a8188c5a44 | 29 | if ( lol.magAvailable() ) |
chebbi | 3:64a8188c5a44 | 30 | { |
jmar7 | 0:e8167f37725c | 31 | lol.readMag(); |
chebbi | 3:64a8188c5a44 | 32 | } |
chebbi | 3:64a8188c5a44 | 33 | if ( lol.accelAvailable() ) |
chebbi | 3:64a8188c5a44 | 34 | { |
chebbi | 3:64a8188c5a44 | 35 | lol.readAccel(); |
chebbi | 3:64a8188c5a44 | 36 | } |
chebbi | 3:64a8188c5a44 | 37 | if ( lol.gyroAvailable() ) |
chebbi | 3:64a8188c5a44 | 38 | { |
jmar7 | 0:e8167f37725c | 39 | lol.readGyro(); |
chebbi | 3:64a8188c5a44 | 40 | } |
chebbi | 3:64a8188c5a44 | 41 | |
chebbi | 3:64a8188c5a44 | 42 | printfGyro(); // printfff "G: gx, gy, gz" |
chebbi | 3:64a8188c5a44 | 43 | pc.printf("\n") ; |
chebbi | 3:64a8188c5a44 | 44 | printfAccel(); // printfff "A: ax, ay, az" |
chebbi | 3:64a8188c5a44 | 45 | pc.printf("\n") ; |
chebbi | 3:64a8188c5a44 | 46 | printfMag(); // printfff "M: mx, my, mz" |
chebbi | 3:64a8188c5a44 | 47 | pc.printf("\n") ; |
chebbi | 3:64a8188c5a44 | 48 | // printff the heading and orientation for fun! |
chebbi | 3:64a8188c5a44 | 49 | // Call printfff attitude. The LSM9DS1's mag x and y |
chebbi | 3:64a8188c5a44 | 50 | // axes are opposite to the accelerometer, so my, mx are |
chebbi | 3:64a8188c5a44 | 51 | // substituted for each other. |
chebbi | 3:64a8188c5a44 | 52 | printfAttitude(lol.ax, lol.ay, lol.az,-lol.my, -lol.mx, lol.mz); |
chebbi | 3:64a8188c5a44 | 53 | pc.printf("\n") ; |
chebbi | 3:64a8188c5a44 | 54 | wait(1); |
chebbi | 3:64a8188c5a44 | 55 | } |
chebbi | 3:64a8188c5a44 | 56 | } |
jmar7 | 0:e8167f37725c | 57 | |
chebbi | 3:64a8188c5a44 | 58 | void printfGyro() |
chebbi | 3:64a8188c5a44 | 59 | { |
chebbi | 3:64a8188c5a44 | 60 | // Now we can use the gx, gy, and gz variables as we please. |
chebbi | 3:64a8188c5a44 | 61 | // Either printfff them as raw ADC values, or calculated in DPS. |
chebbi | 3:64a8188c5a44 | 62 | pc.printf("G: "); |
chebbi | 3:64a8188c5a44 | 63 | #ifdef printff_CALCULATED |
chebbi | 3:64a8188c5a44 | 64 | // If you want to printffff calculated values, you can use the |
chebbi | 3:64a8188c5a44 | 65 | // calcGyro helper function to convert a raw ADC value to |
chebbi | 3:64a8188c5a44 | 66 | // DPS. Give the function the value that you want to convert. |
chebbi | 3:64a8188c5a44 | 67 | pc.printf("%f" , lol.calcGyro(lol.gx), 2); |
chebbi | 3:64a8188c5a44 | 68 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 69 | pc.printf("%f" , lol.calcGyro(lol.gy), 2); |
chebbi | 3:64a8188c5a44 | 70 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 71 | pc.printf("%f" , lol.calcGyro(lol.gz), 2); |
chebbi | 3:64a8188c5a44 | 72 | pc.printf(" deg/s "); |
chebbi | 3:64a8188c5a44 | 73 | pc.printf("\n") ; |
chebbi | 3:64a8188c5a44 | 74 | /*#elif defined printfff_RAW |
chebbi | 3:64a8188c5a44 | 75 | pc.printf(lol.gx); |
chebbi | 3:64a8188c5a44 | 76 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 77 | pc.printf(lol.gy); |
chebbi | 3:64a8188c5a44 | 78 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 79 | pc.printfln(lol.gz);*/ |
chebbi | 3:64a8188c5a44 | 80 | #endif |
chebbi | 3:64a8188c5a44 | 81 | } |
chebbi | 3:64a8188c5a44 | 82 | |
chebbi | 3:64a8188c5a44 | 83 | void printfAccel() |
chebbi | 3:64a8188c5a44 | 84 | { |
chebbi | 3:64a8188c5a44 | 85 | // Now we can use the ax, ay, and az variables as we please. |
chebbi | 3:64a8188c5a44 | 86 | // Either printfff them as raw ADC values, or calculated in g's. |
chebbi | 3:64a8188c5a44 | 87 | pc.printf("A: "); |
chebbi | 3:64a8188c5a44 | 88 | #ifdef printff_CALCULATED |
chebbi | 3:64a8188c5a44 | 89 | // If you want to printffff calculated values, you can use the |
chebbi | 3:64a8188c5a44 | 90 | // calcAccel helper function to convert a raw ADC value to |
chebbi | 3:64a8188c5a44 | 91 | // g's. Give the function the value that you want to convert. |
chebbi | 3:64a8188c5a44 | 92 | pc.printf("%f" , lol.calcAccel(lol.ax), 2); |
chebbi | 3:64a8188c5a44 | 93 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 94 | pc.printf("%f" , lol.calcAccel(lol.ay), 2); |
chebbi | 3:64a8188c5a44 | 95 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 96 | pc.printf("%f" , lol.calcAccel(lol.az), 2); |
chebbi | 3:64a8188c5a44 | 97 | pc.printf(" g"); |
chebbi | 3:64a8188c5a44 | 98 | /*#elif defined printfff_RAW |
chebbi | 3:64a8188c5a44 | 99 | pc.printf(lol.ax); |
chebbi | 3:64a8188c5a44 | 100 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 101 | pc.printf(lol.ay); |
chebbi | 3:64a8188c5a44 | 102 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 103 | pc.printfln(lol.az);*/ |
chebbi | 3:64a8188c5a44 | 104 | |
chebbi | 3:64a8188c5a44 | 105 | #endif |
chebbi | 3:64a8188c5a44 | 106 | |
jmar7 | 0:e8167f37725c | 107 | } |
chebbi | 3:64a8188c5a44 | 108 | |
chebbi | 3:64a8188c5a44 | 109 | void printfMag() |
chebbi | 3:64a8188c5a44 | 110 | { |
chebbi | 3:64a8188c5a44 | 111 | // Now we can use the mx, my, and mz variables as we please. |
chebbi | 3:64a8188c5a44 | 112 | // Either printfff them as raw ADC values, or calculated in Gauss. |
chebbi | 3:64a8188c5a44 | 113 | pc.printf("M: "); |
chebbi | 3:64a8188c5a44 | 114 | #ifdef printff_CALCULATED |
chebbi | 3:64a8188c5a44 | 115 | // If you want to printffff calculated values, you can use the |
chebbi | 3:64a8188c5a44 | 116 | // calcMag helper function to convert a raw ADC value to |
chebbi | 3:64a8188c5a44 | 117 | // Gauss. Give the function the value that you want to convert. |
chebbi | 3:64a8188c5a44 | 118 | pc.printf("%f" , lol.calcMag(lol.mx), 2); |
chebbi | 3:64a8188c5a44 | 119 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 120 | pc.printf("%f" , lol.calcMag(lol.my), 2); |
chebbi | 3:64a8188c5a44 | 121 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 122 | pc.printf("%f" , lol.calcMag(lol.mz), 2); |
chebbi | 3:64a8188c5a44 | 123 | pc.printf(" gauss"); |
chebbi | 3:64a8188c5a44 | 124 | |
chebbi | 3:64a8188c5a44 | 125 | /*#elif defined printfff_RAW |
chebbi | 3:64a8188c5a44 | 126 | pc.printf(lol.mx); |
chebbi | 3:64a8188c5a44 | 127 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 128 | pc.printf(lol.my); |
chebbi | 3:64a8188c5a44 | 129 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 130 | pc.printfln(lol.mz);*/ |
chebbi | 3:64a8188c5a44 | 131 | #endif |
chebbi | 3:64a8188c5a44 | 132 | } |
chebbi | 3:64a8188c5a44 | 133 | |
chebbi | 3:64a8188c5a44 | 134 | void printfAttitude(float ax, float ay, float az, float mx, float my, float mz) |
chebbi | 3:64a8188c5a44 | 135 | { |
chebbi | 3:64a8188c5a44 | 136 | float roll = atan2(ay, az); |
chebbi | 3:64a8188c5a44 | 137 | float pitch = atan2(-ax, sqrt(ay * ay + az * az)); |
chebbi | 3:64a8188c5a44 | 138 | |
chebbi | 3:64a8188c5a44 | 139 | float heading; |
chebbi | 3:64a8188c5a44 | 140 | if (my == 0) |
chebbi | 3:64a8188c5a44 | 141 | heading = (mx < 0) ? PI : 0; |
chebbi | 3:64a8188c5a44 | 142 | else |
chebbi | 3:64a8188c5a44 | 143 | heading = atan2(mx, my); |
chebbi | 3:64a8188c5a44 | 144 | |
chebbi | 3:64a8188c5a44 | 145 | heading -= DECLINATION * PI / 180; |
chebbi | 3:64a8188c5a44 | 146 | |
chebbi | 3:64a8188c5a44 | 147 | if (heading > PI) heading -= (2 * PI); |
chebbi | 3:64a8188c5a44 | 148 | else if (heading < -PI) heading += (2 * PI); |
chebbi | 3:64a8188c5a44 | 149 | |
chebbi | 3:64a8188c5a44 | 150 | // Convert everything from radians to degrees: |
chebbi | 3:64a8188c5a44 | 151 | heading *= 180.0 / PI; |
chebbi | 3:64a8188c5a44 | 152 | pitch *= 180.0 / PI; |
chebbi | 3:64a8188c5a44 | 153 | roll *= 180.0 / PI; |
chebbi | 3:64a8188c5a44 | 154 | |
chebbi | 3:64a8188c5a44 | 155 | pc.printf("Pitch, Roll: "); |
chebbi | 3:64a8188c5a44 | 156 | pc.printf("%f" , pitch, 2); |
chebbi | 3:64a8188c5a44 | 157 | pc.printf(", "); |
chebbi | 3:64a8188c5a44 | 158 | pc.printf("%f" , roll, 2); |
chebbi | 3:64a8188c5a44 | 159 | pc.printf("\n") ; |
chebbi | 3:64a8188c5a44 | 160 | pc.printf("Heading: "); pc.printf("%f" , heading, 2); |
chebbi | 3:64a8188c5a44 | 161 | |
chebbi | 3:64a8188c5a44 | 162 | } |
chebbi | 3:64a8188c5a44 | 163 | |
chebbi | 3:64a8188c5a44 | 164 | //pc.printffff("%d %d %d %d %d %d %d %d %d\n\r", lol.calcGyro(lol.gx), lol.calcGyro(lol.gy), lol.calcGyro(lol.gz), lol.ax, lol.ay, lol.az, lol.mx, lol.my, lol.mz); |
chebbi | 3:64a8188c5a44 | 165 | //pc.printffff("%d %d %d\n\r", lol.calcGyro(lol.gx), lol.calcGyro(lol.gy), lol.calcGyro(lol.gz)); |
chebbi | 3:64a8188c5a44 | 166 | //pc.printffff("gyro: %d %d %d\n\r", lol.gx, lol.gy, lol.gz); |
chebbi | 3:64a8188c5a44 | 167 | //pc.printffff("accel: %d %d %d\n\r", lol.ax, lol.ay, lol.az); |
chebbi | 3:64a8188c5a44 | 168 | //pc.printffff("mag: %d %d %d\n\n\r", lol.mx, lol.my, lol.mz); |
chebbi | 3:64a8188c5a44 | 169 | //myled = 1; |
chebbi | 3:64a8188c5a44 | 170 | //wait_ms(500); |
chebbi | 3:64a8188c5a44 | 171 | //myled = 0; |
chebbi | 3:64a8188c5a44 | 172 | //wait_ms(500); |
chebbi | 3:64a8188c5a44 | 173 | |
chebbi | 3:64a8188c5a44 | 174 |