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