Code to detect walking and convert to input for video game
Dependencies: LSM9DS1_Library_cal2 XBee mbed
Fork of FootModule by
main.cpp@2:ab7b95fb52aa, 2017-04-23 (annotated)
- Committer:
- jgensel3
- Date:
- Sun Apr 23 19:26:07 2017 +0000
- Revision:
- 2:ab7b95fb52aa
- Parent:
- 1:705baf131710
- Child:
- 3:2d6ff72599f1
asdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:e693d5bf0a25 | 1 | #include "mbed.h" |
4180_1 | 0:e693d5bf0a25 | 2 | #include "LSM9DS1.h" |
jgensel3 | 2:ab7b95fb52aa | 3 | //#include "USBKeyboard.h" |
4180_1 | 0:e693d5bf0a25 | 4 | #define PI 3.14159 |
4180_1 | 0:e693d5bf0a25 | 5 | // Earth's magnetic field varies by location. Add or subtract |
4180_1 | 0:e693d5bf0a25 | 6 | // a declination to get a more accurate heading. Calculate |
4180_1 | 0:e693d5bf0a25 | 7 | // your's here: |
4180_1 | 0:e693d5bf0a25 | 8 | // http://www.ngdc.noaa.gov/geomag-web/#declination |
4180_1 | 0:e693d5bf0a25 | 9 | #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA. |
4180_1 | 0:e693d5bf0a25 | 10 | |
jgensel3 | 2:ab7b95fb52aa | 11 | DigitalOut led1(LED1); |
jgensel3 | 2:ab7b95fb52aa | 12 | DigitalOut led2(LED2); |
jgensel3 | 2:ab7b95fb52aa | 13 | DigitalOut led3(LED3); |
jgensel3 | 2:ab7b95fb52aa | 14 | DigitalOut led4(LED4); |
4180_1 | 0:e693d5bf0a25 | 15 | Serial pc(USBTX, USBRX); |
jgensel3 | 2:ab7b95fb52aa | 16 | DigitalIn pb1(p21); |
jgensel3 | 2:ab7b95fb52aa | 17 | Timeout walkingTimer; |
jgensel3 | 2:ab7b95fb52aa | 18 | bool isWalking = false; |
4180_1 | 0:e693d5bf0a25 | 19 | // Calculate pitch, roll, and heading. |
4180_1 | 0:e693d5bf0a25 | 20 | // Pitch/roll calculations taken from this app note: |
4180_1 | 0:e693d5bf0a25 | 21 | // http://cache.freescale.com/files/sensors/doc/app_note/AN3461.pdf?fpsp=1 |
4180_1 | 0:e693d5bf0a25 | 22 | // Heading calculations taken from this app note: |
4180_1 | 0:e693d5bf0a25 | 23 | // http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/Magnetic__Literature_Application_notes-documents/AN203_Compass_Heading_Using_Magnetometers.pdf |
jgensel3 | 2:ab7b95fb52aa | 24 | |
jgensel3 | 2:ab7b95fb52aa | 25 | |
jgensel3 | 2:ab7b95fb52aa | 26 | //Function that timeout calls when detected that person stops walking |
jgensel3 | 2:ab7b95fb52aa | 27 | void printStop() |
jgensel3 | 2:ab7b95fb52aa | 28 | { |
jgensel3 | 2:ab7b95fb52aa | 29 | // pc.printf("stop\n\r"); |
jgensel3 | 2:ab7b95fb52aa | 30 | isWalking = false; |
jgensel3 | 2:ab7b95fb52aa | 31 | } |
jgensel3 | 2:ab7b95fb52aa | 32 | |
jgensel3 | 2:ab7b95fb52aa | 33 | //Rotates raw heading so 0 degrees is forward direction |
jgensel3 | 2:ab7b95fb52aa | 34 | float correctHeading(float currHeading, float forward) |
jgensel3 | 2:ab7b95fb52aa | 35 | { |
jgensel3 | 1:705baf131710 | 36 | float newHeading = currHeading - forward; |
jgensel3 | 1:705baf131710 | 37 | if(newHeading < 0) newHeading = 360 + newHeading; |
jgensel3 | 1:705baf131710 | 38 | return newHeading; |
jgensel3 | 1:705baf131710 | 39 | } |
jgensel3 | 1:705baf131710 | 40 | |
jgensel3 | 1:705baf131710 | 41 | float printAttitude(float ax, float ay, float az, float mx, float my, float mz) |
4180_1 | 0:e693d5bf0a25 | 42 | { |
4180_1 | 0:e693d5bf0a25 | 43 | float roll = atan2(ay, az); |
4180_1 | 0:e693d5bf0a25 | 44 | float pitch = atan2(-ax, sqrt(ay * ay + az * az)); |
4180_1 | 0:e693d5bf0a25 | 45 | // touchy trig stuff to use arctan to get compass heading (scale is 0..360) |
4180_1 | 0:e693d5bf0a25 | 46 | mx = -mx; |
4180_1 | 0:e693d5bf0a25 | 47 | float heading; |
4180_1 | 0:e693d5bf0a25 | 48 | if (my == 0.0) |
4180_1 | 0:e693d5bf0a25 | 49 | heading = (mx < 0.0) ? 180.0 : 0.0; |
4180_1 | 0:e693d5bf0a25 | 50 | else |
4180_1 | 0:e693d5bf0a25 | 51 | heading = atan2(mx, my)*360.0/(2.0*PI); |
4180_1 | 0:e693d5bf0a25 | 52 | //pc.printf("heading atan=%f \n\r",heading); |
4180_1 | 0:e693d5bf0a25 | 53 | heading -= DECLINATION; //correct for geo location |
4180_1 | 0:e693d5bf0a25 | 54 | if(heading>180.0) heading = heading - 360.0; |
4180_1 | 0:e693d5bf0a25 | 55 | else if(heading<-180.0) heading = 360.0 + heading; |
4180_1 | 0:e693d5bf0a25 | 56 | else if(heading<0.0) heading = 360.0 + heading; |
4180_1 | 0:e693d5bf0a25 | 57 | |
4180_1 | 0:e693d5bf0a25 | 58 | |
4180_1 | 0:e693d5bf0a25 | 59 | // Convert everything from radians to degrees: |
4180_1 | 0:e693d5bf0a25 | 60 | //heading *= 180.0 / PI; |
4180_1 | 0:e693d5bf0a25 | 61 | pitch *= 180.0 / PI; |
4180_1 | 0:e693d5bf0a25 | 62 | roll *= 180.0 / PI; |
4180_1 | 0:e693d5bf0a25 | 63 | |
jgensel3 | 1:705baf131710 | 64 | //pc.printf("Pitch: %f, Roll: %f degress\n\r",pitch,roll); |
jgensel3 | 1:705baf131710 | 65 | //pc.printf("Magnetic Heading: %f degress\n\r",heading); |
jgensel3 | 1:705baf131710 | 66 | return heading; |
4180_1 | 0:e693d5bf0a25 | 67 | } |
4180_1 | 0:e693d5bf0a25 | 68 | |
4180_1 | 0:e693d5bf0a25 | 69 | |
4180_1 | 0:e693d5bf0a25 | 70 | |
4180_1 | 0:e693d5bf0a25 | 71 | |
4180_1 | 0:e693d5bf0a25 | 72 | int main() |
4180_1 | 0:e693d5bf0a25 | 73 | { |
4180_1 | 0:e693d5bf0a25 | 74 | //LSM9DS1 lol(p9, p10, 0x6B, 0x1E); |
jgensel3 | 2:ab7b95fb52aa | 75 | LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
jgensel3 | 1:705baf131710 | 76 | pb1.mode(PullUp); |
4180_1 | 0:e693d5bf0a25 | 77 | IMU.begin(); |
jgensel3 | 1:705baf131710 | 78 | float forward; |
4180_1 | 0:e693d5bf0a25 | 79 | if (!IMU.begin()) { |
4180_1 | 0:e693d5bf0a25 | 80 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
4180_1 | 0:e693d5bf0a25 | 81 | } |
jgensel3 | 2:ab7b95fb52aa | 82 | led3 = 1; |
jgensel3 | 2:ab7b95fb52aa | 83 | wait(1); |
4180_1 | 0:e693d5bf0a25 | 84 | IMU.calibrate(1); |
jgensel3 | 2:ab7b95fb52aa | 85 | led3 = 0; |
jgensel3 | 2:ab7b95fb52aa | 86 | led1 = 1; |
jgensel3 | 2:ab7b95fb52aa | 87 | wait(0.5); |
jgensel3 | 2:ab7b95fb52aa | 88 | led4 = 1; |
jgensel3 | 2:ab7b95fb52aa | 89 | wait(0.5); |
4180_1 | 0:e693d5bf0a25 | 90 | IMU.calibrateMag(0); |
jgensel3 | 2:ab7b95fb52aa | 91 | led4 = 0; |
jgensel3 | 2:ab7b95fb52aa | 92 | led2 = 1; |
jgensel3 | 2:ab7b95fb52aa | 93 | |
jgensel3 | 2:ab7b95fb52aa | 94 | //Get forward direction relative to user |
jgensel3 | 1:705baf131710 | 95 | pc.printf("Press button to set forward direction"); |
jgensel3 | 2:ab7b95fb52aa | 96 | while(pb1 == 1) { |
jgensel3 | 1:705baf131710 | 97 | IMU.readMag(); |
jgensel3 | 1:705baf131710 | 98 | IMU.readAccel(); |
jgensel3 | 1:705baf131710 | 99 | forward = printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx), |
jgensel3 | 2:ab7b95fb52aa | 100 | IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));; |
jgensel3 | 2:ab7b95fb52aa | 101 | } |
jgensel3 | 2:ab7b95fb52aa | 102 | pc.printf("forward: %f\n\r", forward); |
4180_1 | 0:e693d5bf0a25 | 103 | while(1) { |
4180_1 | 0:e693d5bf0a25 | 104 | while(!IMU.tempAvailable()); |
4180_1 | 0:e693d5bf0a25 | 105 | IMU.readTemp(); |
4180_1 | 0:e693d5bf0a25 | 106 | while(!IMU.magAvailable(X_AXIS)); |
4180_1 | 0:e693d5bf0a25 | 107 | IMU.readMag(); |
4180_1 | 0:e693d5bf0a25 | 108 | while(!IMU.accelAvailable()); |
4180_1 | 0:e693d5bf0a25 | 109 | IMU.readAccel(); |
4180_1 | 0:e693d5bf0a25 | 110 | while(!IMU.gyroAvailable()); |
4180_1 | 0:e693d5bf0a25 | 111 | IMU.readGyro(); |
jgensel3 | 2:ab7b95fb52aa | 112 | if(abs(IMU.calcGyro(IMU.gy)) > 100) { |
jgensel3 | 2:ab7b95fb52aa | 113 | |
jgensel3 | 2:ab7b95fb52aa | 114 | |
jgensel3 | 2:ab7b95fb52aa | 115 | IMU.readAccel(); |
jgensel3 | 2:ab7b95fb52aa | 116 | |
jgensel3 | 2:ab7b95fb52aa | 117 | IMU.readMag(); |
jgensel3 | 2:ab7b95fb52aa | 118 | |
jgensel3 | 2:ab7b95fb52aa | 119 | |
jgensel3 | 2:ab7b95fb52aa | 120 | //Calculate heading relative to forward direction |
jgensel3 | 1:705baf131710 | 121 | float currHeading = printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz)); |
jgensel3 | 1:705baf131710 | 122 | currHeading = correctHeading(currHeading, forward) + 45; |
jgensel3 | 2:ab7b95fb52aa | 123 | //Start timeout to detect when stopped walking |
jgensel3 | 2:ab7b95fb52aa | 124 | walkingTimer.attach(printStop, 0.3); |
jgensel3 | 2:ab7b95fb52aa | 125 | |
jgensel3 | 2:ab7b95fb52aa | 126 | //Detect direction and send command to main mbed |
jgensel3 | 2:ab7b95fb52aa | 127 | if((currHeading > 270 && currHeading < 360) && !isWalking) { |
jgensel3 | 2:ab7b95fb52aa | 128 | |
jgensel3 | 2:ab7b95fb52aa | 129 | pc.printf("walking L\n\r"); |
jgensel3 | 2:ab7b95fb52aa | 130 | |
jgensel3 | 2:ab7b95fb52aa | 131 | isWalking = true; |
jgensel3 | 2:ab7b95fb52aa | 132 | } else if((currHeading > 90 && currHeading < 180) && !isWalking) { |
jgensel3 | 2:ab7b95fb52aa | 133 | |
jgensel3 | 2:ab7b95fb52aa | 134 | pc.printf("walking R\n\r"); |
jgensel3 | 2:ab7b95fb52aa | 135 | isWalking = true; |
jgensel3 | 2:ab7b95fb52aa | 136 | } else if((currHeading > 180 && currHeading < 270) && !isWalking) { |
jgensel3 | 2:ab7b95fb52aa | 137 | |
jgensel3 | 2:ab7b95fb52aa | 138 | pc.printf("walking D\n\r"); |
jgensel3 | 2:ab7b95fb52aa | 139 | isWalking = true; |
jgensel3 | 2:ab7b95fb52aa | 140 | } else if((currHeading > 360 || currHeading < 90) && !isWalking) { |
jgensel3 | 2:ab7b95fb52aa | 141 | |
jgensel3 | 2:ab7b95fb52aa | 142 | pc.printf("walking F\n\r"); |
jgensel3 | 2:ab7b95fb52aa | 143 | isWalking = true; |
jgensel3 | 1:705baf131710 | 144 | } |
jgensel3 | 1:705baf131710 | 145 | } |
4180_1 | 0:e693d5bf0a25 | 146 | } |
4180_1 | 0:e693d5bf0a25 | 147 | } |
4180_1 | 0:e693d5bf0a25 | 148 |