Driving

Dependencies:   FSR LIS3DH USBDevice mbed

Fork of MMA8452_Demo by Ivan Rush

Files at this revision

API Documentation at this revision

Comitter:
darrenalilim
Date:
Tue Jun 27 00:59:52 2017 +0000
Parent:
8:46eab8a51f91
Commit message:
Publishing

Changed in this revision

FSR.lib Show annotated file Show diff for this revision Revisions of this file
LIS3DH.lib Show annotated file Show diff for this revision Revisions of this file
MMA8452.lib Show diff for this revision Revisions of this file
USBDevice.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 46eab8a51f91 -r cbce87f4f21f FSR.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FSR.lib	Tue Jun 27 00:59:52 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/cshao06/code/FSR/#d9520bf7eb9e
diff -r 46eab8a51f91 -r cbce87f4f21f LIS3DH.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LIS3DH.lib	Tue Jun 27 00:59:52 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/kenjiArai/code/LIS3DH/#50ac3372def2
diff -r 46eab8a51f91 -r cbce87f4f21f MMA8452.lib
--- a/MMA8452.lib	Fri Oct 17 15:40:43 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/ashleymills/code/MMA8452/#a92a632a0cc7
diff -r 46eab8a51f91 -r cbce87f4f21f USBDevice.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Tue Jun 27 00:59:52 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#c5e178adb138
diff -r 46eab8a51f91 -r cbce87f4f21f main.cpp
--- a/main.cpp	Fri Oct 17 15:40:43 2014 +0000
+++ b/main.cpp	Tue Jun 27 00:59:52 2017 +0000
@@ -1,25 +1,85 @@
 #include "mbed.h"
-#include "MMA8452.h"
+#include "LIS3DH.h"
+#include "FSR.h"
+#include "USBMouseKeyboard.h"
+#include <string>
+#include <math.h>
 //Using MMA8452 accelerometer. SDA on 28, SCL on 27. Writes gravities to the screen. 
 //Also brightens/dims LEDs 1-3 based on whether or not they are 'level'( 0 Gs)
 
 Serial pc(USBTX,USBRX);
-PwmOut led1(LED1);
-PwmOut led2(LED2);
-PwmOut led3(LED3);
-   double x, y, z;
- 
-   MMA8452 acc(p28, p27, 100000);
+USBMouseKeyboard controller;
+
+LIS3DH acc(D7, D6, LIS3DH_G_CHIP_ADDR);
+FSR fsr(PTB3, 100);
+
+DigitalIn d8(D8);
+DigitalIn d9(D9);
+
+PwmOut red(LED1);
+PwmOut blue(LED3);
+
+float data[3];
+double x, y, z;
+static const float Width = (float)(X_MAX_ABS - X_MIN_ABS);
+#ifndef M_PI
+#define M_PI           3.14159265358979323846
+#endif
+
+string translate(double x, double y, double z) {
+    double val = atan(x/y);
+    if (x < 0 && abs(val) >= 0.6)               return "Hard Left";
+    else if (x < 0 && val < 0.6 && val > 0.2)   return "Soft Left"; 
+    else if (x > 0 && abs(val) >= 0.6)          return "Hard Right"; 
+    else if (x > 0 && val > -0.6 && val < -0.2) return "Soft Right";
+    else                                        return "Straight";
+}
+
+int16_t calculate_mouse_x(double x, double y) {
+    double val = atan(x/y);
+    if (abs(val) < 0.2) return 0;
+    if (x < 0) {
+        return Width * (-abs(val)/M_PI)/256;
+    } else {
+        return Width * (abs(val)/M_PI)/256;
+    }
+}
 
-int main() {
-   
-  while(1) {
+void holdA() {
+    int i = (int) floor(fsr.readRaw()*100) ;
+    while (i > 0) {
+        controller.keyCode('a', 0);
+        i--;   
+    }
+}
 
-      acc.readXYZGravity(&x,&y,&z);
-      pc.printf("x:%lf   y:%lf z:%lf\r\n",x,y,z);
-      led1 = abs(x);
-      led2 = abs(y);
-      led3 = abs(z);
-      wait(.25);
-   }
+int main() {    
+    pc.printf("Starting program...\r\n");
+    while(1) {
+        acc.read_data(data);
+        x = data[0]; y = data[1]; z = data[2];
+        //pc.printf("x:%lf   y:%lf   z:%lf\r\n",x,y,z);
+        //pc.printf("Detect as: %s \r\n", translate(x, y, z));
+        //pc.printf("dX:%d \r\n", calculate_mouse_x(x, y));
+        /*
+        if (!d8) {
+            controller.keyCode('a', 0);
+            controller.keyCode('a', 0);
+            controller.keyCode('a', 0);
+        }
+        */
+        if (!d9) controller.move(calculate_mouse_x(x, y), 0);
+        /*
+        if (!d8) {
+            controller.keyCode('a', 0);
+            controller.keyCode('a', 0);
+            controller.keyCode('a', 0);
+        }
+        */
+        
+        holdA();
+        
+        //wait(0.01f);
+        //wait(2);
+    }
 }
\ No newline at end of file
diff -r 46eab8a51f91 -r cbce87f4f21f mbed.bld
--- a/mbed.bld	Fri Oct 17 15:40:43 2014 +0000
+++ b/mbed.bld	Tue Jun 27 00:59:52 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/8e73be2a2ac1
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/64910690c574
\ No newline at end of file