sadf

Dependencies:   LSM9DS1_Library_cal mbed XBee

Fork of FootSensor by Justin Gensel

Files at this revision

API Documentation at this revision

Comitter:
jgensel3
Date:
Mon Apr 24 04:05:46 2017 +0000
Parent:
2:ab7b95fb52aa
Commit message:
a

Changed in this revision

Wireless.h Show annotated file Show diff for this revision Revisions of this file
XBee.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ab7b95fb52aa -r 2d6ff72599f1 Wireless.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wireless.h	Mon Apr 24 04:05:46 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};
+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
diff -r ab7b95fb52aa -r 2d6ff72599f1 XBee.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XBee.lib	Mon Apr 24 04:05:46 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/lucasec/code/XBee/#350d308e7b77
diff -r ab7b95fb52aa -r 2d6ff72599f1 main.cpp
--- a/main.cpp	Sun Apr 23 19:26:07 2017 +0000
+++ b/main.cpp	Mon Apr 24 04:05:46 2017 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "LSM9DS1.h"
+#include "Wireless.h"
 //#include "USBKeyboard.h"
 #define PI 3.14159
 // Earth's magnetic field varies by location. Add or subtract
@@ -13,8 +14,9 @@
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
 Serial pc(USBTX, USBRX);
-DigitalIn pb1(p21);
+DigitalIn pb1(p17);
 Timeout walkingTimer;
+WirelessModule wireless(p9, p10, FOOT_STEP);
 bool isWalking = false;
 // Calculate pitch, roll, and heading.
 // Pitch/roll calculations taken from this app note:
@@ -27,6 +29,7 @@
 void printStop()
 {
     // pc.printf("stop\n\r");
+    wireless.sendDirection(DIR_NONE);
     isWalking = false;
 }
 
@@ -72,7 +75,7 @@
 int main()
 {
     //LSM9DS1 lol(p9, p10, 0x6B, 0x1E);
-    LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
+    LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
     pb1.mode(PullUp);
     IMU.begin();
     float forward;
@@ -126,20 +129,20 @@
             //Detect direction and send command to main mbed
             if((currHeading > 270 && currHeading < 360) && !isWalking) {
 
-                pc.printf("walking L\n\r");
+                wireless.sendDirection(DIR_LEFT);
 
                 isWalking = true;
             } else if((currHeading > 90 && currHeading < 180) && !isWalking) {
 
-                pc.printf("walking R\n\r");
+               wireless.sendDirection(DIR_RIGHT);
                 isWalking = true;
             } else if((currHeading > 180 && currHeading < 270) && !isWalking) {
 
-                pc.printf("walking D\n\r");
+                wireless.sendDirection(DIR_DOWN);
                 isWalking = true;
             } else if((currHeading > 360 || currHeading < 90) && !isWalking) {
 
-                pc.printf("walking F\n\r");
+                wireless.sendDirection(DIR_UP);
                 isWalking = true;
             }
         }