Code to detect hand movement for use in video game
Dependencies: LSM9DS1_Library_cal XBee mbed
Revision 0:fa587f2eec11, committed 2017-04-24
- Comitter:
- jgensel3
- Date:
- Mon Apr 24 04:14:25 2017 +0000
- Commit message:
- Code to detect hand movement for game
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LSM9DS1_Library_cal.lib Mon Apr 24 04:14:25 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/jgensel3/code/LSM9DS1_Library_cal/#2c98369b783c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Wireless.h Mon Apr 24 04:14:25 2017 +0000 @@ -0,0 +1,81 @@ +#ifndef WProtocol_h +#define WProtocol_h + +#include "XBee/XBee.h" +#include <mbed.h> + +#define HOST_ADDRESS 0x0003 + +enum Direction {DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT, DIR_NONE, A_BUTTON, B_BUTTON}; +enum WMessageType {FOOT_STEP, HAND_GESTURE}; + +typedef struct WMessage { + WMessageType type; + Direction direction; +} WMessage_t; + +class WirelessModule { +public: + WirelessModule(PinName tx, PinName rx, WMessageType type) : + uart(tx, rx), xbee(uart), + tx16req(HOST_ADDRESS, (uint8_t *) &message, sizeof(message)) + { + uart.baud(9600); + message.type = type; + } + + int sendDirection(Direction dir) { + message.direction = dir; + xbee.send(tx16req); + return 0; + } +private: + RawSerial uart; + XBee xbee; + WMessage_t message; + Tx16Request tx16req; +}; + +class WirelessHost { +public: + WirelessHost(PinName tx, PinName rx) : uart(tx, rx), xbee(uart) {} + + int waitForMessage(WMessage_t *store) { + xbee.readPacket(5000); + + if (xbee.getResponse().isError()) { + // API error + return -99; + } + + if (xbee.getResponse().isAvailable()) { + if (xbee.getResponse().getApiId() != RX_16_RESPONSE) { + // Unexpected API message + return -5; + } + + // Retrieve response + xbee.getResponse().getRx16Response(rx16resp); + + // Validate packet length + if (rx16resp.getDataLength() == sizeof(WMessage_t)) { + // Store the payload + memcpy(store, rx16resp.getData(), sizeof(WMessage_t)); + return 0; + } else { + // Unexpected payload format + return -10; + } + } else { + // No Response + return -1; + } + } + +private: + RawSerial uart; + XBee xbee; + Rx16Response rx16resp; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XBee.lib Mon Apr 24 04:14:25 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/lucasec/code/XBee/#350d308e7b77
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 24 04:14:25 2017 +0000 @@ -0,0 +1,82 @@ +#include "mbed.h" +#include "LSM9DS1.h" +#include "Wireless.h" +#define PI 3.14159 +// Earth's magnetic field varies by location. Add or subtract +// a declination to get a more accurate heading. Calculate +// your's here: +// http://www.ngdc.noaa.gov/geomag-web/#declination +#define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA. + +DigitalOut myled(LED1); +Serial pc(USBTX, USBRX); +InterruptIn pb(p21); +InterruptIn pb2(p5); +WirelessModule wireless(p28, p27, HAND_GESTURE); + +void pb_hit_interruptA() { + pc.printf("pressed\n\r"); + wireless.sendDirection(A_BUTTON); +} +void pb_hit_interruptB(){ + wireless.sendDirection(B_BUTTON); + pc.printf("pressedB\n\r"); +} +int main() +{ + + LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); + IMU.begin(); + if (!IMU.begin()) { + pc.printf("Failed to communicate with LSM9DS1.\n"); + } + IMU.calibrate(1); + //IMU.calibrateMag(0); + pb.mode(PullUp); + pb2.mode(PullUp); + wait(.01); + pb.fall(&pb_hit_interruptA); + pb2.fall(&pb_hit_interruptB); + double inX,inY,inZ; + int pos=0; + while(1) { + while(!IMU.accelAvailable()) {pos = 0;} + IMU.readAccel(); + + inX = IMU.calcAccel(IMU.ax); + inY = IMU.calcAccel(IMU.ay); + inZ = IMU.calcAccel(IMU.az); + if (inY >= .5) + { + pc.printf("left\n\r"); + pos = 1; + wireless.sendDirection(DIR_LEFT); + } + else if( inY <= -.5) + { + pc.printf("right\n\r"); + pos = 2; + wireless.sendDirection(DIR_RIGHT); + } + else if (inX >= .5) + { + pc.printf("up\n\r"); + pos = 3; + wireless.sendDirection(DIR_UP); + } + else if (inX <= -.5) + { + pc.printf("down\n\r"); + pos = 4; + wireless.sendDirection(DIR_DOWN); + } + else + { + //pc.printf("error"); + pos = 0; + wireless.sendDirection(DIR_NONE); + } + wait(0.5); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Apr 24 04:14:25 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/97feb9bacc10 \ No newline at end of file