Mine, not yours justin

Dependencies:   HMC6352 USBHost mbed-rtos mbed

Fork of Project by Justin Eng

Committer:
ganeshsubram1
Date:
Fri Apr 25 23:08:01 2014 +0000
Revision:
4:b2a30c313297
Parent:
3:6a7620e9abd9
First Publish. Working 90 degree turning calibration and execution, just need a good power supply to fully test.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganeshsubram1 3:6a7620e9abd9 1 #include "USBHostMouse.h"
ganeshsubram1 3:6a7620e9abd9 2 #include "HMC6352.h"
ganeshsubram1 3:6a7620e9abd9 3
ganeshsubram1 3:6a7620e9abd9 4 #define PFLAG_ON 0
ganeshsubram1 3:6a7620e9abd9 5 #define PFLAG_OFF 1
ganeshsubram1 3:6a7620e9abd9 6 #define PFLAG_CALIB 2
ganeshsubram1 3:6a7620e9abd9 7 #define PTURN_SPEED (0.5)
ganeshsubram1 3:6a7620e9abd9 8
ganeshsubram1 3:6a7620e9abd9 9 float PTURN_RIGHT = 0;
ganeshsubram1 3:6a7620e9abd9 10
ganeshsubram1 3:6a7620e9abd9 11 extern Serial pc(USBTX, USBRX);
ganeshsubram1 3:6a7620e9abd9 12
ganeshsubram1 3:6a7620e9abd9 13 // updates xy position if on, does nothing if off
ganeshsubram1 3:6a7620e9abd9 14 extern char PFlag = PFLAG_ON;
ganeshsubram1 3:6a7620e9abd9 15
ganeshsubram1 3:6a7620e9abd9 16 // whenever a turn is complete, this should store
ganeshsubram1 3:6a7620e9abd9 17 // the degrees facing (0, 90, 180, 270) in system
ganeshsubram1 3:6a7620e9abd9 18 extern int t_position = 0;
ganeshsubram1 3:6a7620e9abd9 19
ganeshsubram1 3:6a7620e9abd9 20 // variables to keep track of coordinate position
ganeshsubram1 3:6a7620e9abd9 21 float x_position = 0;
ganeshsubram1 3:6a7620e9abd9 22 float y_position = 0;
ganeshsubram1 3:6a7620e9abd9 23
ganeshsubram1 3:6a7620e9abd9 24 // variables to keep track of movement during turning
ganeshsubram1 3:6a7620e9abd9 25 float pturn_x = 0;
ganeshsubram1 3:6a7620e9abd9 26 float pturn_y = 0;
ganeshsubram1 3:6a7620e9abd9 27
ganeshsubram1 3:6a7620e9abd9 28 /* mouse event handler */
ganeshsubram1 3:6a7620e9abd9 29 void onMouseEvent(uint8_t buttons, int8_t x_mickey, int8_t y_mickey, int8_t z) {
ganeshsubram1 3:6a7620e9abd9 30
ganeshsubram1 3:6a7620e9abd9 31 // calculate new position
ganeshsubram1 3:6a7620e9abd9 32 if(PFlag == PFLAG_ON) {
ganeshsubram1 3:6a7620e9abd9 33
ganeshsubram1 3:6a7620e9abd9 34 // mouse movements are in mickeys. 1 mickey = ~230 DPI = ~1/230th of an inch
ganeshsubram1 3:6a7620e9abd9 35 float temp = y_mickey / 232.6;
ganeshsubram1 3:6a7620e9abd9 36
ganeshsubram1 3:6a7620e9abd9 37 // determine direction we are facing and add to that direction
ganeshsubram1 3:6a7620e9abd9 38 if(t_position == 0)
ganeshsubram1 3:6a7620e9abd9 39 y_position += temp;
ganeshsubram1 3:6a7620e9abd9 40 else if(t_position == 90)
ganeshsubram1 3:6a7620e9abd9 41 x_position += temp;
ganeshsubram1 3:6a7620e9abd9 42 else if(t_position == 180)
ganeshsubram1 3:6a7620e9abd9 43 y_position -= temp;
ganeshsubram1 3:6a7620e9abd9 44 else
ganeshsubram1 3:6a7620e9abd9 45 x_position -= temp;
ganeshsubram1 3:6a7620e9abd9 46 } else if(PFlag == PFLAG_CALIB) {
ganeshsubram1 3:6a7620e9abd9 47 PTURN_RIGHT += x_mickey / 232.6;
ganeshsubram1 3:6a7620e9abd9 48 } else {
ganeshsubram1 3:6a7620e9abd9 49 pturn_x += x_mickey / 232.6;
ganeshsubram1 3:6a7620e9abd9 50 pturn_y += y_mickey / 232.6;
ganeshsubram1 3:6a7620e9abd9 51 }
ganeshsubram1 3:6a7620e9abd9 52 }
ganeshsubram1 3:6a7620e9abd9 53
ganeshsubram1 3:6a7620e9abd9 54 // intialize x and y variables for turning
ganeshsubram1 3:6a7620e9abd9 55 void turnInit() {
ganeshsubram1 3:6a7620e9abd9 56 pturn_x = 0;
ganeshsubram1 3:6a7620e9abd9 57 pturn_y = 0;
ganeshsubram1 3:6a7620e9abd9 58 }
ganeshsubram1 3:6a7620e9abd9 59
ganeshsubram1 3:6a7620e9abd9 60 /* positioning system thread function */
ganeshsubram1 3:6a7620e9abd9 61 void PositionSystemMain(void const *) {
ganeshsubram1 3:6a7620e9abd9 62
ganeshsubram1 3:6a7620e9abd9 63 USBHostMouse mouse;
ganeshsubram1 3:6a7620e9abd9 64
ganeshsubram1 3:6a7620e9abd9 65 while(1) {
ganeshsubram1 3:6a7620e9abd9 66 // try to connect a USB mouse
ganeshsubram1 3:6a7620e9abd9 67 while(!mouse.connect())
ganeshsubram1 3:6a7620e9abd9 68 Thread::wait(500);
ganeshsubram1 3:6a7620e9abd9 69
ganeshsubram1 3:6a7620e9abd9 70 // when connected, attach handler called on mouse event
ganeshsubram1 3:6a7620e9abd9 71 mouse.attachEvent(onMouseEvent);
ganeshsubram1 3:6a7620e9abd9 72
ganeshsubram1 3:6a7620e9abd9 73 // wait until the mouse is disconnected
ganeshsubram1 3:6a7620e9abd9 74 while(mouse.connected())
ganeshsubram1 3:6a7620e9abd9 75 Thread::wait(500);
ganeshsubram1 3:6a7620e9abd9 76 }
ganeshsubram1 3:6a7620e9abd9 77 }