Submitted by Angela Hsueh, Maya Mardini, Yi Tong Slingshot controller using a force sensor and accelerometer for an Angry Birds clone game.
Dependencies: LSM303DLHC MMA8451Q PinDetect USBDevice mbed
Fork of hw3_controller by
Revision 7:689f891d62cd, committed 2015-09-21
- Comitter:
- ahsueh
- Date:
- Mon Sep 21 12:14:01 2015 +0000
- Parent:
- 6:753418e86c95
- Commit message:
- Final
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mouseCtrl.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 753418e86c95 -r 689f891d62cd main.cpp --- a/main.cpp Mon Sep 21 08:21:05 2015 +0000 +++ b/main.cpp Mon Sep 21 12:14:01 2015 +0000 @@ -1,7 +1,4 @@ #include "mbed.h" -//#include "MMA8451Q.h" -//#include "USBMouse.h" -//#include "USBKeyboard.h" #include "USBMouseKeyboard.h" #include "LSM303DLHC.h" #include "PinDetect.h" @@ -10,7 +7,12 @@ // define I2C Pins and address for KL25Z. Taken from default sample code. PinName const SDA = D14; PinName const SCL = D15; -PinDetect button(D2); +DigitalIn p1(D3); +DigitalIn p2(D4); +DigitalIn p3(D5); +//PinDetect p1(D3); +//PinDetect p2(D4); +//PinDetect p3(D5); AnalogIn fsr(A5); #define MMA8451_I2C_ADDRESS (0x1d<<1) @@ -26,7 +28,8 @@ // acc and mag values float ax, ay, az, mx, my, mz; float theta; - +int mousePress; +float flt_force_prev; int oldx, oldy; int mouseTravelX = 0; @@ -51,26 +54,63 @@ return mean / (float)len; } + + + int main(void){ int x = 0; int y = 0; + int mouseClickDown = 0; + bool aim = false; while(1){ fsrVal = fsr.read(); lsm.read(&ax, &ay, &az, &mx, &my, &mz); float cur_angle = atan(ax / az); - float cur_force = fsrVal*500; + float cur_force = fsrVal*1000; float flt_force = filter(forces, 8, cur_force); float flt_angle = filter(angles, 8, cur_angle); - + //mouseKey.printf("ax: %1.2f az: %1.2f fsrVal: %1.2f \n", ax, az, fsrVal); - //mouseKey.printf("ax: %1.2f az: %1.2f fsrVal: %1.2f \n", ax, az, fsrVal); + //mouseKey.printf("fsrVal: %1.2f fsrValPrev: %1.2f flt_force: %1.2f\n", + // fsrVal, fsrValPrev, flt_force); + if(p1 == 0){ + mouseKey.keyCode('a'); + + } + if (p2 == 0) { + mouseKey.click(MOUSE_LEFT); + wait(0.05); + } - //if((FsrVal > FsrValPrev && FsrVal > 0 && FsrValPrev > 0 && count <100) || (FsrVal > 0.8 && count <70)){ - - + + //if ((flt_force > 0.2)) { + + //mouseKey.printf("fsrVal %1.2f fsrValPrev %1.2f flt_force %1.2f \n", fsrVal, fsrValPrev,flt_force); + if(p3 == 0){ + if (mouseClickDown == 0) { + mouseKey.press(MOUSE_LEFT); + mouseClickDown = 1; + } + //wait(0.02); + } + else { + if (mouseClickDown == 1) { + mouseKey.release(MOUSE_LEFT); + mouseClickDown = 0; + } + } + if ((((flt_force_prev - flt_force) > 7) || flt_force == 0) && aim) { + //mouseKey.keyCode('c'); + aim = false; + } else if ((flt_force >20)&&(!aim)) { + //mouseKey.keyCode('b'); + aim = true; + //mouseKey.press(MOUSE_LEFT); + } + if (ax > 0 && az > 0) { x = (int) (0.0 - cos(flt_angle) * flt_force); y = (int) (sin(flt_angle) * flt_force); @@ -86,9 +126,9 @@ mouseMem((x-oldx),(y-oldy)); oldx = x; oldy = y; - - - wait(0.02); + fsrValPrev = fsrVal; + flt_force_prev = flt_force; + wait(0.01); } }
diff -r 753418e86c95 -r 689f891d62cd mouseCtrl.cpp --- a/mouseCtrl.cpp Mon Sep 21 08:21:05 2015 +0000 +++ b/mouseCtrl.cpp Mon Sep 21 12:14:01 2015 +0000 @@ -1,84 +0,0 @@ -/* -#include "mbed.h" -//#include "MMA8451Q.h" -//#include "USBMouse.h" -//#include "USBKeyboard.h" -#include "USBMouseKeyboard.h" -#include "LSM303DLHC.h" -#include "PinDetect.h" -#include "math.h" - -// define I2C Pins and address for KL25Z. Taken from default sample code. -PinName const SDA = D14; -PinName const SCL = D15; -PinDetect button(D2); -AnalogIn fsr(A5); -#define MMA8451_I2C_ADDRESS (0x1d<<1) - -//serial connection to PC via USB -Serial pc(USBTX, USBRX); -LSM303DLHC lsm(SDA, SCL); -USBMouseKeyboard mouseKey; - - -float FsrVal; -float FsrValPrev; -int count; -// acc and mag values -float ax, ay, az,mx, my, mz; -float theta; - -int main(void){ - while(1){ - FsrVal = fsr.read(); - FsrValPrev=0.1; - lsm.read(&ax, &ay, &az, &mx, &my, &mz); //get acceleration - theta = atan (abs(ay)/abs(az)); - if((FsrVal > FsrValPrev && FsrVal > 0 && FsrValPrev > 0 && count <100) || (FsrVal > 0.8 && count <70)){ - // lower left - if(ay>0 && az>0){ - mouseKey.printf("forward + downward \n"); - mouseKey.printf("y %1.2f z %1.2f ",ay, az); - mouseKey.printf("theta1 %1.2f\n",theta); - mouseKey.printf("vertical %1.2f\n\n",-(floor)(sin(theta)*5)); - mouseKey.move(1,-(floor)(sin(theta)*5)); - } - // upper left - if(ay<0 && az>0){ - mouseKey.printf("forward + upward \n"); - - mouseKey.printf("y %1.2f z %1.2f ",ay, az); - mouseKey.printf("theta2 %1.2f\n",theta); - mouseKey.printf("vertica2 %1.2f\n\n",(floor)(sin(theta)*5)); - mouseKey.move(1,(floor)(sin(theta)*5)); - } - mouseKey.move(1,0); - FsrValPrev = fsr.read(); - count++; - } - else if((FsrVal < FsrValPrev && count >0) || (FsrVal < 0.2 && count >0)){ - // lower left - if(ay>0 && az>0){ - mouseKey.printf("backward + downward \n"); - - mouseKey.printf("y %1.2f z %1.2f ",ay, az); - mouseKey.printf("theta1 %1.2f\n",theta); - mouseKey.printf("vertical1 %1.2f\n\n",-(floor)(sin(theta)*5)); - mouseKey.move(-2,-(floor)(sin(theta)*5)); - } - // upper left - if(ay<0 && az>0){ - mouseKey.printf("backward + upward \n"); - mouseKey.printf("y %1.2f z %1.2f ",ay, az); - mouseKey.printf("theta2 %1.2f\n",theta); - mouseKey.printf("vertical2 %1.2f\n\n",(floor)(sin(theta)*5)); - mouseKey.move(-2,(floor)(sin(theta)*5)); - } - mouseKey.move(-2,0); - FsrValPrev = fsr.read(); - count=count-3; - } - wait(0.02); - } - } - */ \ No newline at end of file