Code to detect hand movement for use in video game
Dependencies: LSM9DS1_Library_cal XBee mbed
main.cpp@0:fa587f2eec11, 2017-04-24 (annotated)
- Committer:
- jgensel3
- Date:
- Mon Apr 24 04:14:25 2017 +0000
- Revision:
- 0:fa587f2eec11
Code to detect hand movement for game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jgensel3 | 0:fa587f2eec11 | 1 | #include "mbed.h" |
jgensel3 | 0:fa587f2eec11 | 2 | #include "LSM9DS1.h" |
jgensel3 | 0:fa587f2eec11 | 3 | #include "Wireless.h" |
jgensel3 | 0:fa587f2eec11 | 4 | #define PI 3.14159 |
jgensel3 | 0:fa587f2eec11 | 5 | // Earth's magnetic field varies by location. Add or subtract |
jgensel3 | 0:fa587f2eec11 | 6 | // a declination to get a more accurate heading. Calculate |
jgensel3 | 0:fa587f2eec11 | 7 | // your's here: |
jgensel3 | 0:fa587f2eec11 | 8 | // http://www.ngdc.noaa.gov/geomag-web/#declination |
jgensel3 | 0:fa587f2eec11 | 9 | #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA. |
jgensel3 | 0:fa587f2eec11 | 10 | |
jgensel3 | 0:fa587f2eec11 | 11 | DigitalOut myled(LED1); |
jgensel3 | 0:fa587f2eec11 | 12 | Serial pc(USBTX, USBRX); |
jgensel3 | 0:fa587f2eec11 | 13 | InterruptIn pb(p21); |
jgensel3 | 0:fa587f2eec11 | 14 | InterruptIn pb2(p5); |
jgensel3 | 0:fa587f2eec11 | 15 | WirelessModule wireless(p28, p27, HAND_GESTURE); |
jgensel3 | 0:fa587f2eec11 | 16 | |
jgensel3 | 0:fa587f2eec11 | 17 | void pb_hit_interruptA() { |
jgensel3 | 0:fa587f2eec11 | 18 | pc.printf("pressed\n\r"); |
jgensel3 | 0:fa587f2eec11 | 19 | wireless.sendDirection(A_BUTTON); |
jgensel3 | 0:fa587f2eec11 | 20 | } |
jgensel3 | 0:fa587f2eec11 | 21 | void pb_hit_interruptB(){ |
jgensel3 | 0:fa587f2eec11 | 22 | wireless.sendDirection(B_BUTTON); |
jgensel3 | 0:fa587f2eec11 | 23 | pc.printf("pressedB\n\r"); |
jgensel3 | 0:fa587f2eec11 | 24 | } |
jgensel3 | 0:fa587f2eec11 | 25 | int main() |
jgensel3 | 0:fa587f2eec11 | 26 | { |
jgensel3 | 0:fa587f2eec11 | 27 | |
jgensel3 | 0:fa587f2eec11 | 28 | LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
jgensel3 | 0:fa587f2eec11 | 29 | IMU.begin(); |
jgensel3 | 0:fa587f2eec11 | 30 | if (!IMU.begin()) { |
jgensel3 | 0:fa587f2eec11 | 31 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
jgensel3 | 0:fa587f2eec11 | 32 | } |
jgensel3 | 0:fa587f2eec11 | 33 | IMU.calibrate(1); |
jgensel3 | 0:fa587f2eec11 | 34 | //IMU.calibrateMag(0); |
jgensel3 | 0:fa587f2eec11 | 35 | pb.mode(PullUp); |
jgensel3 | 0:fa587f2eec11 | 36 | pb2.mode(PullUp); |
jgensel3 | 0:fa587f2eec11 | 37 | wait(.01); |
jgensel3 | 0:fa587f2eec11 | 38 | pb.fall(&pb_hit_interruptA); |
jgensel3 | 0:fa587f2eec11 | 39 | pb2.fall(&pb_hit_interruptB); |
jgensel3 | 0:fa587f2eec11 | 40 | double inX,inY,inZ; |
jgensel3 | 0:fa587f2eec11 | 41 | int pos=0; |
jgensel3 | 0:fa587f2eec11 | 42 | while(1) { |
jgensel3 | 0:fa587f2eec11 | 43 | while(!IMU.accelAvailable()) {pos = 0;} |
jgensel3 | 0:fa587f2eec11 | 44 | IMU.readAccel(); |
jgensel3 | 0:fa587f2eec11 | 45 | |
jgensel3 | 0:fa587f2eec11 | 46 | inX = IMU.calcAccel(IMU.ax); |
jgensel3 | 0:fa587f2eec11 | 47 | inY = IMU.calcAccel(IMU.ay); |
jgensel3 | 0:fa587f2eec11 | 48 | inZ = IMU.calcAccel(IMU.az); |
jgensel3 | 0:fa587f2eec11 | 49 | if (inY >= .5) |
jgensel3 | 0:fa587f2eec11 | 50 | { |
jgensel3 | 0:fa587f2eec11 | 51 | pc.printf("left\n\r"); |
jgensel3 | 0:fa587f2eec11 | 52 | pos = 1; |
jgensel3 | 0:fa587f2eec11 | 53 | wireless.sendDirection(DIR_LEFT); |
jgensel3 | 0:fa587f2eec11 | 54 | } |
jgensel3 | 0:fa587f2eec11 | 55 | else if( inY <= -.5) |
jgensel3 | 0:fa587f2eec11 | 56 | { |
jgensel3 | 0:fa587f2eec11 | 57 | pc.printf("right\n\r"); |
jgensel3 | 0:fa587f2eec11 | 58 | pos = 2; |
jgensel3 | 0:fa587f2eec11 | 59 | wireless.sendDirection(DIR_RIGHT); |
jgensel3 | 0:fa587f2eec11 | 60 | } |
jgensel3 | 0:fa587f2eec11 | 61 | else if (inX >= .5) |
jgensel3 | 0:fa587f2eec11 | 62 | { |
jgensel3 | 0:fa587f2eec11 | 63 | pc.printf("up\n\r"); |
jgensel3 | 0:fa587f2eec11 | 64 | pos = 3; |
jgensel3 | 0:fa587f2eec11 | 65 | wireless.sendDirection(DIR_UP); |
jgensel3 | 0:fa587f2eec11 | 66 | } |
jgensel3 | 0:fa587f2eec11 | 67 | else if (inX <= -.5) |
jgensel3 | 0:fa587f2eec11 | 68 | { |
jgensel3 | 0:fa587f2eec11 | 69 | pc.printf("down\n\r"); |
jgensel3 | 0:fa587f2eec11 | 70 | pos = 4; |
jgensel3 | 0:fa587f2eec11 | 71 | wireless.sendDirection(DIR_DOWN); |
jgensel3 | 0:fa587f2eec11 | 72 | } |
jgensel3 | 0:fa587f2eec11 | 73 | else |
jgensel3 | 0:fa587f2eec11 | 74 | { |
jgensel3 | 0:fa587f2eec11 | 75 | //pc.printf("error"); |
jgensel3 | 0:fa587f2eec11 | 76 | pos = 0; |
jgensel3 | 0:fa587f2eec11 | 77 | wireless.sendDirection(DIR_NONE); |
jgensel3 | 0:fa587f2eec11 | 78 | } |
jgensel3 | 0:fa587f2eec11 | 79 | wait(0.5); |
jgensel3 | 0:fa587f2eec11 | 80 | } |
jgensel3 | 0:fa587f2eec11 | 81 | } |
jgensel3 | 0:fa587f2eec11 | 82 |