This is the first rev of HW3 for IDD

Dependencies:   MMA8451Q USBDevice mbed nRF24L01P

Committer:
shunshou
Date:
Mon Sep 29 17:14:42 2014 +0000
Revision:
3:99ed0483f868
Parent:
2:c3b748d86642
Child:
4:7012a2af25f6
Better mouse/keyboard interface; motor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antoniorohit 0:d89cdf3d29f8 1 //NOTE: NORDIC BOARD ONLY WORKS W/ MBED 84!
antoniorohit 0:d89cdf3d29f8 2 //Chose pin ordering from https://mbed.org/questions/1360/Using-nRF24L01-Module-with-FRDM-Board/ -- unused pins
antoniorohit 0:d89cdf3d29f8 3
antoniorohit 0:d89cdf3d29f8 4 #include "mbed.h"
antoniorohit 0:d89cdf3d29f8 5 #include "nRF24L01P.h" // nordic library
antoniorohit 0:d89cdf3d29f8 6
shunshou 3:99ed0483f868 7 #define DEBUG 0 // code debug mode
shunshou 3:99ed0483f868 8 #define BASE 0 // is base station
antoniorohit 0:d89cdf3d29f8 9
antoniorohit 0:d89cdf3d29f8 10
antoniorohit 0:d89cdf3d29f8 11 #include "USBMouseKeyboard.h" // for the sword - mouse/keyboard combo
antoniorohit 0:d89cdf3d29f8 12 #if(BASE == 1)
shunshou 3:99ed0483f868 13 USBMouseKeyboard MK; //Default is REL_MOUSE, bout could use ABS_MOUSE too
antoniorohit 0:d89cdf3d29f8 14 #endif
antoniorohit 0:d89cdf3d29f8 15
antoniorohit 0:d89cdf3d29f8 16 #define SCALING 50 // factor to multiply the accelerometer reading by (usually it is in the scale of g's. Decides sensitivity of mouse. Keep it less 100 (mouse is assigned an int8)
antoniorohit 0:d89cdf3d29f8 17
antoniorohit 0:d89cdf3d29f8 18 // Accelerometer includes
antoniorohit 0:d89cdf3d29f8 19 #include "MMA8451Q.h"
antoniorohit 0:d89cdf3d29f8 20 #define MMA8451_I2C_ADDRESS (0x1d<<1)
antoniorohit 0:d89cdf3d29f8 21
antoniorohit 0:d89cdf3d29f8 22 // define I2C Pins and address for KL25Z. Taken from default sample code.
antoniorohit 0:d89cdf3d29f8 23 PinName const SDA = PTE25;
antoniorohit 0:d89cdf3d29f8 24 PinName const SCL = PTE24;
antoniorohit 0:d89cdf3d29f8 25
antoniorohit 0:d89cdf3d29f8 26 // Base station TX/RX
antoniorohit 0:d89cdf3d29f8 27 #define RX_NRF24L01P_ADDRESS ((unsigned long long) 0xABABABABAB )
antoniorohit 0:d89cdf3d29f8 28 #define TX_NRF24L01P_ADDRESS ((unsigned long long) 0xCDCDCDCDCD )
antoniorohit 0:d89cdf3d29f8 29
antoniorohit 0:d89cdf3d29f8 30 // The nRF24L01+ supports transfers from 1 to 32 bytes
shunshou 3:99ed0483f868 31 // To base station: roll | cycle 1 2 3 | space = jump | left click = attack | right click = defend | w | s | d | a | 8-bit Y mouse movement | 8-bit X mouse movement
shunshou 3:99ed0483f868 32 // WASD = forward, left, back, right
shunshou 3:99ed0483f868 33 // From PC, only need (zero padded) 1 bit flag indicating if hit
shunshou 3:99ed0483f868 34 #define TRANSFER_SIZE 4
antoniorohit 0:d89cdf3d29f8 35
antoniorohit 0:d89cdf3d29f8 36 Serial pc(USBTX, USBRX); // PC communication
antoniorohit 0:d89cdf3d29f8 37
antoniorohit 0:d89cdf3d29f8 38 PwmOut motor(D2); // Only specific pins have PWM capability
antoniorohit 0:d89cdf3d29f8 39
antoniorohit 0:d89cdf3d29f8 40 nRF24L01P nordic(PTD2, PTD3, PTC5, PTD0, PTD5, PTA13); // mosi, miso, sck, csn, ce, irq
antoniorohit 0:d89cdf3d29f8 41
shunshou 3:99ed0483f868 42 DigitalIn modeSW(D15); // base station or sword mode can be selected w/ jumper
shunshou 3:99ed0483f868 43
shunshou 3:99ed0483f868 44 DigitalOut greenLED(LED_GREEN);
antoniorohit 0:d89cdf3d29f8 45
antoniorohit 0:d89cdf3d29f8 46 // Accelerometer
antoniorohit 0:d89cdf3d29f8 47 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
antoniorohit 0:d89cdf3d29f8 48
antoniorohit 0:d89cdf3d29f8 49 void Calibrate(void);
antoniorohit 0:d89cdf3d29f8 50 void Acc_GetXY(void);
antoniorohit 0:d89cdf3d29f8 51
shunshou 3:99ed0483f868 52 int16_t x,y; // variables to hold acceleration data after call to Acc_Get_All
shunshou 3:99ed0483f868 53 float x_b, y_b; // acc bias
antoniorohit 0:d89cdf3d29f8 54
antoniorohit 0:d89cdf3d29f8 55 // Debug
antoniorohit 1:b58cf7dd20d7 56 //DigitalOut greenLED(LED_GREEN);
antoniorohit 1:b58cf7dd20d7 57 //DigitalOut redLED(LED_RED);
antoniorohit 0:d89cdf3d29f8 58
shunshou 3:99ed0483f868 59 AnalogIn ATTACK(A0); // attack FSR
shunshou 3:99ed0483f868 60 AnalogIn SELECT(A1); // defense/mode FSR
shunshou 3:99ed0483f868 61 AnalogIn XJOY(A2); // joystick x axis
shunshou 3:99ed0483f868 62 AnalogIn YJOY(A3); // joystick y axis
antoniorohit 0:d89cdf3d29f8 63
antoniorohit 0:d89cdf3d29f8 64 DigitalIn JOYSEL(D3);
antoniorohit 0:d89cdf3d29f8 65
antoniorohit 0:d89cdf3d29f8 66 bool isBaseStation;
shunshou 3:99ed0483f868 67 bool motorOnFlag;
shunshou 3:99ed0483f868 68 int motorCycle = 0;
antoniorohit 0:d89cdf3d29f8 69
antoniorohit 0:d89cdf3d29f8 70 int main() {
antoniorohit 0:d89cdf3d29f8 71 int8_t mouse_x, mouse_y;
shunshou 3:99ed0483f868 72 Calibrate(); // Calibrate accelerometer
antoniorohit 0:d89cdf3d29f8 73
shunshou 3:99ed0483f868 74 modeSW.mode(PullUp); // Configure pull up to minimize wire on sword
antoniorohit 0:d89cdf3d29f8 75
shunshou 3:99ed0483f868 76 isBaseStation = (bool) !modeSW; // Detect device via jumper connection
shunshou 3:99ed0483f868 77
antoniorohit 0:d89cdf3d29f8 78 wait(5);
antoniorohit 0:d89cdf3d29f8 79
antoniorohit 0:d89cdf3d29f8 80 // Power up wireless
antoniorohit 0:d89cdf3d29f8 81 nordic.powerUp();
antoniorohit 0:d89cdf3d29f8 82
antoniorohit 0:d89cdf3d29f8 83 // Display + change the (default) setup of the nRF24L01+ chip
antoniorohit 0:d89cdf3d29f8 84
antoniorohit 0:d89cdf3d29f8 85 // Addresses 5 bytes long
antoniorohit 0:d89cdf3d29f8 86
antoniorohit 0:d89cdf3d29f8 87 if (isBaseStation){
antoniorohit 0:d89cdf3d29f8 88 nordic.setTxAddress(TX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 89 nordic.setRxAddress(RX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 90 }
antoniorohit 0:d89cdf3d29f8 91 else{
antoniorohit 0:d89cdf3d29f8 92 nordic.setRxAddress(TX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 93 nordic.setTxAddress(RX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 94 }
antoniorohit 0:d89cdf3d29f8 95
antoniorohit 0:d89cdf3d29f8 96 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", nordic.getRfFrequency() );
antoniorohit 0:d89cdf3d29f8 97 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", nordic.getRfOutputPower() );
antoniorohit 0:d89cdf3d29f8 98 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", nordic.getAirDataRate() ); // 1Mbps
antoniorohit 0:d89cdf3d29f8 99 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nordic.getTxAddress() );
antoniorohit 0:d89cdf3d29f8 100 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nordic.getRxAddress() );
antoniorohit 0:d89cdf3d29f8 101
antoniorohit 0:d89cdf3d29f8 102 // Data packet length
antoniorohit 0:d89cdf3d29f8 103 nordic.setTransferSize( TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 104
antoniorohit 0:d89cdf3d29f8 105 nordic.setReceiveMode();
antoniorohit 0:d89cdf3d29f8 106 nordic.enable();
antoniorohit 0:d89cdf3d29f8 107
antoniorohit 0:d89cdf3d29f8 108 // Set motor PWM period
antoniorohit 0:d89cdf3d29f8 109 motor.period(0.001f); // 1ms period
antoniorohit 0:d89cdf3d29f8 110 motor.write(0.0f); // initially not on
antoniorohit 0:d89cdf3d29f8 111
antoniorohit 0:d89cdf3d29f8 112 // Status flags
antoniorohit 0:d89cdf3d29f8 113 bool motorON = false;
shunshou 3:99ed0483f868 114 bool mouse1 = false;
shunshou 3:99ed0483f868 115 bool mouse2 = false;
shunshou 3:99ed0483f868 116 bool mouse3 = false;
shunshou 3:99ed0483f868 117 bool mouse4 = false;
shunshou 3:99ed0483f868 118 bool mouse5 = false;
shunshou 3:99ed0483f868 119 bool attackflag = false;
antoniorohit 0:d89cdf3d29f8 120
antoniorohit 0:d89cdf3d29f8 121 int rxDataCnt = 0;
antoniorohit 0:d89cdf3d29f8 122
antoniorohit 0:d89cdf3d29f8 123 // counting the mode
antoniorohit 0:d89cdf3d29f8 124 uint8_t mode_count = 0;
antoniorohit 0:d89cdf3d29f8 125
antoniorohit 0:d89cdf3d29f8 126 while (1) {
shunshou 3:99ed0483f868 127 // Only reads 1 byte from PC + sends to other mcu (pads w/ bytes) if base station
antoniorohit 0:d89cdf3d29f8 128 if (isBaseStation){
antoniorohit 0:d89cdf3d29f8 129
antoniorohit 0:d89cdf3d29f8 130 // If we've received anything over the host serial link...
antoniorohit 0:d89cdf3d29f8 131 if ( pc.readable() ) {
antoniorohit 0:d89cdf3d29f8 132 char txData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 133 // ...add it to the transmit buffer -- only care about first byte
antoniorohit 0:d89cdf3d29f8 134 txData[0] = pc.getc();
antoniorohit 0:d89cdf3d29f8 135 txData[1] = 100;
shunshou 3:99ed0483f868 136 txData[2] = 100;
shunshou 3:99ed0483f868 137 txData[3] = '\n';
antoniorohit 0:d89cdf3d29f8 138
antoniorohit 0:d89cdf3d29f8 139 // Send the transmitbuffer via the nRF24L01+
antoniorohit 0:d89cdf3d29f8 140 nordic.write( NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 141
antoniorohit 0:d89cdf3d29f8 142 // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
antoniorohit 0:d89cdf3d29f8 143 /*if (txData[0] == 66)
antoniorohit 0:d89cdf3d29f8 144 greenLED = !greenLED;*/
antoniorohit 0:d89cdf3d29f8 145 }
antoniorohit 0:d89cdf3d29f8 146
antoniorohit 0:d89cdf3d29f8 147 // If we've received anything in the nRF24L01+... = sword
antoniorohit 0:d89cdf3d29f8 148 if ( nordic.readable() ) {
antoniorohit 0:d89cdf3d29f8 149 char rxData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 150 // ...read the data into the receive buffer
antoniorohit 0:d89cdf3d29f8 151 rxDataCnt = nordic.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 152
shunshou 3:99ed0483f868 153 //Get data and convert to mouse x,y and key entry
shunshou 3:99ed0483f868 154 // To base station: roll | cycle 1 2 3 | space = jump | left click = attack | right click = defend | w | s | d | a | 8-bit Y mouse movement | 8-bit X mouse movement
antoniorohit 0:d89cdf3d29f8 155
shunshou 3:99ed0483f868 156 int8_t dx = rxData[0]; // change in x
shunshou 3:99ed0483f868 157 int8_t dy = rxData[1]; // change in y
antoniorohit 1:b58cf7dd20d7 158
shunshou 3:99ed0483f868 159 bool mode_stat = (rxData[2] >> 7) & 1; // checking flag bits
antoniorohit 0:d89cdf3d29f8 160 bool joy_but = (rxData[2] >> 6) & 1;
shunshou 3:99ed0483f868 161 bool def_stat = (rxData[2] >> 5) & 1;
antoniorohit 0:d89cdf3d29f8 162 bool attack_stat = (rxData[2] >> 4) & 1;
antoniorohit 0:d89cdf3d29f8 163 bool key_w = (rxData[2] >> 3) & 1;
antoniorohit 0:d89cdf3d29f8 164 bool key_s = (rxData[2] >> 2) & 1;
antoniorohit 0:d89cdf3d29f8 165 bool key_d = (rxData[2] >> 1) & 1;
antoniorohit 0:d89cdf3d29f8 166 bool key_a = (rxData[2] >> 0) & 1;
antoniorohit 1:b58cf7dd20d7 167
antoniorohit 1:b58cf7dd20d7 168 bool roll = (rxData[3] >> 0) & 1;
antoniorohit 1:b58cf7dd20d7 169
shunshou 3:99ed0483f868 170 #if(BASE == 1) // Only if base station
shunshou 3:99ed0483f868 171 MK.move(dy, dx); // move mouse relative
shunshou 3:99ed0483f868 172
shunshou 3:99ed0483f868 173 if(joy_but) // do keyboard events
shunshou 3:99ed0483f868 174 MK.putc(' ');
shunshou 3:99ed0483f868 175
shunshou 3:99ed0483f868 176 if(attack_stat)
shunshou 3:99ed0483f868 177 {
shunshou 3:99ed0483f868 178 MK.press(MOUSE_LEFT);
shunshou 3:99ed0483f868 179 mouse1 = true;
shunshou 3:99ed0483f868 180 }
shunshou 3:99ed0483f868 181 if(!attack_stat && mouse1)
shunshou 3:99ed0483f868 182 {
shunshou 3:99ed0483f868 183 MK.release(MOUSE_LEFT);
shunshou 3:99ed0483f868 184 mouse1 = false;
shunshou 3:99ed0483f868 185 }
shunshou 3:99ed0483f868 186 if(key_a) // move left
antoniorohit 0:d89cdf3d29f8 187 {
shunshou 3:99ed0483f868 188 MK.press(MOUSE_RIGHT);
shunshou 3:99ed0483f868 189 mouse2 = true;
shunshou 3:99ed0483f868 190 }
shunshou 3:99ed0483f868 191 if(!key_a && mouse2)
shunshou 3:99ed0483f868 192 {
shunshou 3:99ed0483f868 193 MK.release(MOUSE_RIGHT);
shunshou 3:99ed0483f868 194 mouse2 = false;
shunshou 3:99ed0483f868 195 }
shunshou 3:99ed0483f868 196 if(key_d) // move right
antoniorohit 0:d89cdf3d29f8 197 {
shunshou 3:99ed0483f868 198 MK.press(MOUSE_MIDDLE);
shunshou 3:99ed0483f868 199 mouse3 = true;
shunshou 3:99ed0483f868 200 }
shunshou 3:99ed0483f868 201 if(!key_d && mouse3)
shunshou 3:99ed0483f868 202 {
shunshou 3:99ed0483f868 203 MK.release(MOUSE_MIDDLE);
shunshou 3:99ed0483f868 204 mouse3 = false;
shunshou 3:99ed0483f868 205 }
shunshou 3:99ed0483f868 206 // mouse 4, 5 have issues on windows
shunshou 3:99ed0483f868 207 if(key_w) // move forward
shunshou 3:99ed0483f868 208 {
shunshou 3:99ed0483f868 209 //MK.press(16); // mouse5
shunshou 3:99ed0483f868 210 //mouse5 = true;
antoniorohit 0:d89cdf3d29f8 211 MK.putc('w');
antoniorohit 0:d89cdf3d29f8 212 }
shunshou 3:99ed0483f868 213 /*if(!key_w && mouse5)
antoniorohit 0:d89cdf3d29f8 214 {
shunshou 3:99ed0483f868 215 MK.release(16);
shunshou 3:99ed0483f868 216 mouse5 = false;
shunshou 3:99ed0483f868 217 }*/
shunshou 3:99ed0483f868 218 if(key_s) // move backward
antoniorohit 0:d89cdf3d29f8 219 {
shunshou 3:99ed0483f868 220 //MK.press(8); // mouse4
shunshou 3:99ed0483f868 221 //mouse4 = true;
antoniorohit 0:d89cdf3d29f8 222 MK.putc('s');
antoniorohit 0:d89cdf3d29f8 223 }
shunshou 3:99ed0483f868 224 /*if(!key_s && mouse4)
antoniorohit 0:d89cdf3d29f8 225 {
shunshou 3:99ed0483f868 226 MK.release(8);
shunshou 3:99ed0483f868 227 mouse4 = false;
shunshou 3:99ed0483f868 228 }*/
shunshou 3:99ed0483f868 229
shunshou 3:99ed0483f868 230 // Single push commands
shunshou 3:99ed0483f868 231 if(def_stat)
shunshou 3:99ed0483f868 232 MK.putc('d');
shunshou 3:99ed0483f868 233
antoniorohit 0:d89cdf3d29f8 234 if(mode_stat)
antoniorohit 0:d89cdf3d29f8 235 {
antoniorohit 0:d89cdf3d29f8 236 MK.putc('1'+mode_count);
antoniorohit 0:d89cdf3d29f8 237 mode_count = (mode_count+1)%3;
antoniorohit 0:d89cdf3d29f8 238 }
antoniorohit 1:b58cf7dd20d7 239 if(roll)
shunshou 3:99ed0483f868 240 MK.putc('r');
shunshou 3:99ed0483f868 241
antoniorohit 0:d89cdf3d29f8 242 #endif
antoniorohit 0:d89cdf3d29f8 243
shunshou 3:99ed0483f868 244 //pc.printf("x: %d y: %d \r\n",dx,dy);
antoniorohit 0:d89cdf3d29f8 245
antoniorohit 0:d89cdf3d29f8 246 // Display the receive buffer contents via the host serial link
antoniorohit 0:d89cdf3d29f8 247 /*for ( int i = 0; i < TRANSFER_SIZE; i++ ) {
antoniorohit 0:d89cdf3d29f8 248
antoniorohit 0:d89cdf3d29f8 249 pc.putc( rxData[i] );
antoniorohit 0:d89cdf3d29f8 250 }*/
antoniorohit 0:d89cdf3d29f8 251 }
antoniorohit 0:d89cdf3d29f8 252 }
antoniorohit 0:d89cdf3d29f8 253
antoniorohit 0:d89cdf3d29f8 254 // sword
antoniorohit 0:d89cdf3d29f8 255 else{
shunshou 3:99ed0483f868 256 int ATTACKVAL = ATTACK.read_u16(); // attack FSR
shunshou 3:99ed0483f868 257 int SELECTVAL = SELECT.read_u16(); // mode/defense FSR
shunshou 3:99ed0483f868 258 int XJOYVAL = XJOY.read_u16(); // joystick x
shunshou 3:99ed0483f868 259 int YJOYVAL = YJOY.read_u16(); // joystick y
shunshou 3:99ed0483f868 260 int JOYSELVAL = (int) JOYSEL; // joystick select value
antoniorohit 0:d89cdf3d29f8 261
shunshou 3:99ed0483f868 262 // if joystick push/pulled all the way on axis, do roll/move faster in direction
shunshou 3:99ed0483f868 263 //bool roll = ((XJOYVAL > 64000) || (XJOYVAL < 1000) || (YJOYVAL > 64000) || (YJOYVAL < 1000)) ? true: 0;
shunshou 3:99ed0483f868 264 // Disable backward roll due to lack of continuous MOUSE buttons
shunshou 3:99ed0483f868 265 bool roll = ((XJOYVAL > 64000) || (XJOYVAL < 1000) || (YJOYVAL > 64000)) ? true: 0;
antoniorohit 1:b58cf7dd20d7 266
shunshou 3:99ed0483f868 267 // thresholding
shunshou 3:99ed0483f868 268 bool mode_stat = (SELECTVAL > 45000) ? true: 0; // 1, 2, 3 rotated (defense/mode FSR pressed all the way)
antoniorohit 0:d89cdf3d29f8 269 bool joy_but = JOYSEL == 0? true: 0;
shunshou 3:99ed0483f868 270 bool def_stat = (SELECTVAL > 10000 && SELECTVAL < 45000) ? true: 0; // defend if defense/mode FSR slightly pressed
shunshou 3:99ed0483f868 271 bool attack_stat = ATTACKVAL > 10000? true: 0; // attack if attack FSR pressed
shunshou 3:99ed0483f868 272 bool key_d = XJOYVAL > 35000 ? true: 0; // if joystick moves in direction, direction key should be pressed
antoniorohit 0:d89cdf3d29f8 273 bool key_a = XJOYVAL < 30000 ? true : 0;
antoniorohit 0:d89cdf3d29f8 274 bool key_w = YJOYVAL > 35000 ? true: 0;
antoniorohit 0:d89cdf3d29f8 275 bool key_s = YJOYVAL < 30000 ? true: 0;
antoniorohit 0:d89cdf3d29f8 276
shunshou 3:99ed0483f868 277 // turn on motor at start of attack
shunshou 3:99ed0483f868 278
shunshou 3:99ed0483f868 279 if (attack_stat == true)
shunshou 3:99ed0483f868 280 attackflag = true;
shunshou 3:99ed0483f868 281 if (attackflag == true){
shunshou 3:99ed0483f868 282 if (motorCycle < 250){
shunshou 3:99ed0483f868 283 motorCycle++;
shunshou 3:99ed0483f868 284 motor.write(0.80f); // 40% duty cycle, relative to period
shunshou 3:99ed0483f868 285
shunshou 3:99ed0483f868 286 }
shunshou 3:99ed0483f868 287 else {
shunshou 3:99ed0483f868 288 motorCycle = 0;
shunshou 3:99ed0483f868 289 attackflag = false;
shunshou 3:99ed0483f868 290 motor.write(0.00f);
shunshou 3:99ed0483f868 291 }
shunshou 3:99ed0483f868 292 }
antoniorohit 0:d89cdf3d29f8 293
antoniorohit 0:d89cdf3d29f8 294
shunshou 3:99ed0483f868 295
shunshou 3:99ed0483f868 296 // make byte 2 of packet
shunshou 3:99ed0483f868 297 uint8_t key_stat = mode_stat<<7|joy_but<<6|def_stat<<5|attack_stat<<4|key_w<<3|key_s<<2|key_d<<1|key_a<<0;
shunshou 3:99ed0483f868 298
shunshou 3:99ed0483f868 299 //pc.printf("F1: %d \t F2: %d \t X: %d \t Y: %d \t SEL: %d \r\n", ATTACKVAL, SELECTVAL, XJOYVAL, YJOYVAL, JOYSELVAL);
antoniorohit 0:d89cdf3d29f8 300
antoniorohit 0:d89cdf3d29f8 301 // Let serial read catch up on base station/PC side
antoniorohit 0:d89cdf3d29f8 302 wait_us(150);
antoniorohit 0:d89cdf3d29f8 303
antoniorohit 0:d89cdf3d29f8 304 char swordData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 305
shunshou 3:99ed0483f868 306 Acc_GetXY(); // Get X, Y acceleration
antoniorohit 0:d89cdf3d29f8 307
shunshou 3:99ed0483f868 308 mouse_x = x; // x, y assigned in prev function
antoniorohit 0:d89cdf3d29f8 309 mouse_y = y;
antoniorohit 0:d89cdf3d29f8 310
shunshou 3:99ed0483f868 311 char lowX = char(mouse_x & 0x00FF); // only use lowest 8 bits of data
shunshou 3:99ed0483f868 312 char lowY = char(mouse_y & 0x00FF);
shunshou 3:99ed0483f868 313
shunshou 3:99ed0483f868 314 //pc.printf("x: %f \t y: %f \r\n", mouse_x,mouse_y);
antoniorohit 0:d89cdf3d29f8 315
antoniorohit 0:d89cdf3d29f8 316 // left A, right D, back S, up W
antoniorohit 0:d89cdf3d29f8 317
shunshou 3:99ed0483f868 318 // MSB -- LSB send packet
antoniorohit 0:d89cdf3d29f8 319 swordData[0] = lowX;
antoniorohit 0:d89cdf3d29f8 320 swordData[1] = lowY;
antoniorohit 0:d89cdf3d29f8 321 swordData[2] = char(key_stat);
shunshou 3:99ed0483f868 322 swordData[3] = char(roll); // cast bool into char
antoniorohit 0:d89cdf3d29f8 323
antoniorohit 0:d89cdf3d29f8 324 // Send the transmitbuffer via the nRF24L01+
antoniorohit 0:d89cdf3d29f8 325 nordic.write( NRF24L01P_PIPE_P0, swordData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 326
antoniorohit 0:d89cdf3d29f8 327 // If we've received anything from base station
shunshou 3:99ed0483f868 328 if ( nordic.readable() ) {
antoniorohit 0:d89cdf3d29f8 329
shunshou 3:99ed0483f868 330 greenLED = !greenLED;
shunshou 3:99ed0483f868 331
antoniorohit 0:d89cdf3d29f8 332 char rxData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 333 // ...read the data into the receive buffer
antoniorohit 0:d89cdf3d29f8 334 rxDataCnt = nordic.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 335
antoniorohit 0:d89cdf3d29f8 336 #if DEBUG == 1
antoniorohit 0:d89cdf3d29f8 337 // Display the receive buffer contents via the host serial link
antoniorohit 0:d89cdf3d29f8 338 for ( int i = 0; i < TRANSFER_SIZE; i++ ) {
antoniorohit 0:d89cdf3d29f8 339 pc.putc( rxData[i] );
antoniorohit 0:d89cdf3d29f8 340 }
antoniorohit 0:d89cdf3d29f8 341 #endif
antoniorohit 0:d89cdf3d29f8 342
shunshou 3:99ed0483f868 343 // From PC, only need (zero padded) 1 bit flag indicating if hit
antoniorohit 0:d89cdf3d29f8 344 // In first byte
shunshou 3:99ed0483f868 345 motorON = (rxData[0] >> 0) & 1; // Motor ON when sword contact made
shunshou 3:99ed0483f868 346 if (motorON)
shunshou 3:99ed0483f868 347 motorOnFlag = true;
antoniorohit 0:d89cdf3d29f8 348 }
shunshou 3:99ed0483f868 349
shunshou 3:99ed0483f868 350 // if flag to turn motor on sent, run motor for a little while and then stop
shunshou 3:99ed0483f868 351 // period not extended if another motor on command sent in the middle of the count
shunshou 3:99ed0483f868 352
antoniorohit 0:d89cdf3d29f8 353 }
antoniorohit 0:d89cdf3d29f8 354 }
antoniorohit 0:d89cdf3d29f8 355 }
antoniorohit 0:d89cdf3d29f8 356
shunshou 3:99ed0483f868 357 // perform initial accelerometer calibration to zero stuff
antoniorohit 0:d89cdf3d29f8 358 void Calibrate(void)
antoniorohit 0:d89cdf3d29f8 359 {
antoniorohit 0:d89cdf3d29f8 360 unsigned int count1;
antoniorohit 0:d89cdf3d29f8 361 count1 = 0;
antoniorohit 0:d89cdf3d29f8 362 float sstatex = 0;
antoniorohit 0:d89cdf3d29f8 363 float sstatey = 0;
antoniorohit 0:d89cdf3d29f8 364
antoniorohit 0:d89cdf3d29f8 365 do{
shunshou 3:99ed0483f868 366 sstatex += acc.getAccX(); // Accumulate Samples
antoniorohit 0:d89cdf3d29f8 367 sstatey += acc.getAccY();
antoniorohit 0:d89cdf3d29f8 368 count1++;
shunshou 3:99ed0483f868 369 }while(count1!=0x0400); // 1024 times
shunshou 3:99ed0483f868 370 x_b = sstatex/1024.0; // division between 1024
antoniorohit 0:d89cdf3d29f8 371 y_b = sstatey/1024.0;
antoniorohit 0:d89cdf3d29f8 372 }
antoniorohit 0:d89cdf3d29f8 373
shunshou 3:99ed0483f868 374 // remove offset from calibration + scale for sensitivity when getting accelerometer data
antoniorohit 0:d89cdf3d29f8 375 void Acc_GetXY(void)
antoniorohit 0:d89cdf3d29f8 376 {
antoniorohit 0:d89cdf3d29f8 377 x = (int16_t)((acc.getAccX()- x_b)*SCALING);
antoniorohit 0:d89cdf3d29f8 378 y = (int16_t)((acc.getAccY()- y_b)*SCALING);
antoniorohit 0:d89cdf3d29f8 379 }
antoniorohit 0:d89cdf3d29f8 380
shunshou 3:99ed0483f868 381 /* Processing code
shunshou 3:99ed0483f868 382
shunshou 3:99ed0483f868 383 //sikuli-java.jar needed to keep track of HP/score for rumble + serial RX/TX
shunshou 3:99ed0483f868 384
shunshou 3:99ed0483f868 385 import org.sikuli.script.*;
shunshou 3:99ed0483f868 386 import processing.serial.*;
shunshou 3:99ed0483f868 387
shunshou 3:99ed0483f868 388 Serial myPort; // Serial port
shunshou 3:99ed0483f868 389 int lastTime = 0; // Time counter
shunshou 3:99ed0483f868 390
shunshou 3:99ed0483f868 391 Region myHPReg = new Region(1,1,500,500); // HP watch regions x, y, w, h
shunshou 3:99ed0483f868 392 Region otherHPReg = new Region(500,500,500,500);
shunshou 3:99ed0483f868 393
shunshou 3:99ed0483f868 394 // Specify event handler for detecting changes
shunshou 3:99ed0483f868 395 class hpChangeClass implements SikuliEventObserver {
shunshou 3:99ed0483f868 396 @Override
shunshou 3:99ed0483f868 397 void targetChanged(SikuliEventChange evnt1) {
shunshou 3:99ed0483f868 398 println( "changed!" );
shunshou 3:99ed0483f868 399 for (int i = 0; i <100; i++){
shunshou 3:99ed0483f868 400 myPort.write(3); // Rumble motor (LSB high)
shunshou 3:99ed0483f868 401 }
shunshou 3:99ed0483f868 402 //lastTime++;
shunshou 3:99ed0483f868 403 }
shunshou 3:99ed0483f868 404
shunshou 3:99ed0483f868 405 void targetAppeared(SikuliEventAppear evnt2) {
shunshou 3:99ed0483f868 406 println("test");
shunshou 3:99ed0483f868 407 }
shunshou 3:99ed0483f868 408
shunshou 3:99ed0483f868 409 void targetVanished(SikuliEventVanish evnt3) {
shunshou 3:99ed0483f868 410 println("test");
shunshou 3:99ed0483f868 411 }
shunshou 3:99ed0483f868 412
shunshou 3:99ed0483f868 413 }
shunshou 3:99ed0483f868 414
shunshou 3:99ed0483f868 415 hpChangeClass regionChange = new hpChangeClass();
shunshou 3:99ed0483f868 416
shunshou 3:99ed0483f868 417 void setup(){
shunshou 3:99ed0483f868 418 size(1,1); // Don't care (out of focus)
shunshou 3:99ed0483f868 419 myPort = new Serial(this, "/dev/tty.usbmodem1422", 9600); // Serial setup
shunshou 3:99ed0483f868 420 myPort.clear();
shunshou 3:99ed0483f868 421 //lastTime = millis();
shunshou 3:99ed0483f868 422 myHPReg.highlight(5); // Show regions watched for 5 seconds
shunshou 3:99ed0483f868 423 otherHPReg.highlight(5);
shunshou 3:99ed0483f868 424
shunshou 3:99ed0483f868 425 myHPReg.onChange(10, regionChange); // num of pixels for change detection, event handler
shunshou 3:99ed0483f868 426 myHPReg.observeInBackground((int)Float.POSITIVE_INFINITY); // watch forever
shunshou 3:99ed0483f868 427
shunshou 3:99ed0483f868 428 otherHPReg.onChange(10, regionChange); // num of pixels for change detection, event handler
shunshou 3:99ed0483f868 429 otherHPReg.observeInBackground((int)Float.POSITIVE_INFINITY); // watch forever
shunshou 3:99ed0483f868 430 }
shunshou 3:99ed0483f868 431
shunshou 3:99ed0483f868 432 void draw() {
shunshou 3:99ed0483f868 433
shunshou 3:99ed0483f868 434 Wait
shunshou 3:99ed0483f868 435 if ( millis() - lastTime > 5000 ) {
shunshou 3:99ed0483f868 436 myPort.write(68);
shunshou 3:99ed0483f868 437 lastTime = millis();
shunshou 3:99ed0483f868 438 }
shunshou 3:99ed0483f868 439
shunshou 3:99ed0483f868 440 }
shunshou 3:99ed0483f868 441
shunshou 3:99ed0483f868 442 */
antoniorohit 0:d89cdf3d29f8 443
antoniorohit 0:d89cdf3d29f8 444