Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LSM303DLHC MMA8451Q PinDetect USBDevice mbed
Fork of hw3_controller by
main.cpp
00001 #include "mbed.h" 00002 #include "USBMouseKeyboard.h" 00003 #include "LSM303DLHC.h" 00004 #include "PinDetect.h" 00005 #include "math.h" 00006 00007 // define I2C Pins and address for KL25Z. Taken from default sample code. 00008 PinName const SDA = D14; 00009 PinName const SCL = D15; 00010 DigitalIn p1(D3); 00011 DigitalIn p2(D4); 00012 DigitalIn p3(D5); 00013 //PinDetect p1(D3); 00014 //PinDetect p2(D4); 00015 //PinDetect p3(D5); 00016 AnalogIn fsr(A5); 00017 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00018 00019 //serial connection to PC via USB 00020 Serial pc(USBTX, USBRX); 00021 LSM303DLHC lsm(SDA, SCL); 00022 USBMouseKeyboard mouseKey; 00023 00024 00025 float fsrVal; 00026 float fsrValPrev; 00027 int count; 00028 // acc and mag values 00029 float ax, ay, az, mx, my, mz; 00030 float theta; 00031 int mousePress; 00032 float flt_force_prev; 00033 int oldx, oldy; 00034 00035 int mouseTravelX = 0; 00036 int mouseTravelY = 0; 00037 00038 float angles[8] = {0}; 00039 float forces[8] = {0}; 00040 00041 void mouseMem(int x_, int y_) { 00042 mouseTravelX += x_; 00043 mouseTravelY += y_; 00044 } 00045 00046 float filter(float* array, int len, float value) { 00047 float mean = 0.0; 00048 for(int i = 0; i<len - 1; i++) { 00049 mean += array[i + 1]; 00050 array[i] = array[i + 1]; 00051 } 00052 mean += value; 00053 array[len - 1] = value; 00054 return mean / (float)len; 00055 } 00056 00057 00058 00059 00060 int main(void){ 00061 int x = 0; 00062 int y = 0; 00063 int mouseClickDown = 0; 00064 bool aim = false; 00065 while(1){ 00066 fsrVal = fsr.read(); 00067 lsm.read(&ax, &ay, &az, &mx, &my, &mz); 00068 00069 float cur_angle = atan(ax / az); 00070 float cur_force = fsrVal*1000; 00071 00072 float flt_force = filter(forces, 8, cur_force); 00073 float flt_angle = filter(angles, 8, cur_angle); 00074 00075 //mouseKey.printf("ax: %1.2f az: %1.2f fsrVal: %1.2f \n", ax, az, fsrVal); 00076 00077 //mouseKey.printf("fsrVal: %1.2f fsrValPrev: %1.2f flt_force: %1.2f\n", 00078 // fsrVal, fsrValPrev, flt_force); 00079 if(p1 == 0){ 00080 mouseKey.keyCode('a'); 00081 00082 } 00083 if (p2 == 0) { 00084 mouseKey.click(MOUSE_LEFT); 00085 wait(0.05); 00086 } 00087 00088 00089 //if ((flt_force > 0.2)) { 00090 00091 //mouseKey.printf("fsrVal %1.2f fsrValPrev %1.2f flt_force %1.2f \n", fsrVal, fsrValPrev,flt_force); 00092 if(p3 == 0){ 00093 if (mouseClickDown == 0) { 00094 mouseKey.press(MOUSE_LEFT); 00095 mouseClickDown = 1; 00096 } 00097 //wait(0.02); 00098 } 00099 else { 00100 if (mouseClickDown == 1) { 00101 mouseKey.release(MOUSE_LEFT); 00102 mouseClickDown = 0; 00103 } 00104 } 00105 if ((((flt_force_prev - flt_force) > 7) || flt_force == 0) && aim) { 00106 //mouseKey.keyCode('c'); 00107 aim = false; 00108 } else if ((flt_force >20)&&(!aim)) { 00109 //mouseKey.keyCode('b'); 00110 aim = true; 00111 //mouseKey.press(MOUSE_LEFT); 00112 } 00113 00114 if (ax > 0 && az > 0) { 00115 x = (int) (0.0 - cos(flt_angle) * flt_force); 00116 y = (int) (sin(flt_angle) * flt_force); 00117 } else if (ax < 0 && az > 0) { 00118 x = (int) (0.0 - cos(flt_angle) * flt_force); 00119 y = (int) (sin(flt_angle) * flt_force); 00120 } 00121 00122 //mouseKey.printf("Angle: %1.2f Force: %1.2f Move: x: %0d y: %0d x_: %0d y_ = %0d\n", 00123 // flt_angle, flt_force, x, y, mouseTravelX, mouseTravelY); 00124 00125 mouseKey.move((x-oldx), (y-oldy)); 00126 mouseMem((x-oldx),(y-oldy)); 00127 oldx = x; 00128 oldy = y; 00129 fsrValPrev = fsrVal; 00130 flt_force_prev = flt_force; 00131 wait(0.01); 00132 } 00133 } 00134
Generated on Sun Jul 17 2022 19:46:36 by
1.7.2
