Example program of decoding the DF Robotics xbee joystick serial output
Weapons and Systems Engineering mbed DFRobotJoystick Interface
This page provides an example of reading an ASCII comma separated variable string from the DR Robot Wireless Gamepad using the mbed micro-controller. The DF Robbot Wireless Gamepad is an Arduino programmable game controller with an on-board xbee wireless transceiver module. The gamepads used in the Weapons and Systems Engineering Department have been reprogrammed with the arduino sketch listed below.
/media/uploads/jebradshaw/joes_dfrobot_gamepad_example_20141021_mode.ino
The output of the joystick follows the GPS NMEA 0183 protocol as an ASCII string preceded by an "$JOYSTK", and terminated with an asterisk and a two character checksum (hex representation).
Below is a screen shot of the data streaming in TeraTerminal when the gamepad is connected directly to the PC (through a virtual COM port).
mbed_DFRobot_joystick_parse.cpp@1:bc6098f1dd1a, 2015-02-09 (annotated)
- Committer:
- jebradshaw
- Date:
- Mon Feb 09 13:42:35 2015 +0000
- Revision:
- 1:bc6098f1dd1a
- Parent:
- 0:ce988e55f00d
mbed WSE TSD xbee DF Robotics Joystick parse example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jebradshaw | 1:bc6098f1dd1a | 1 | // Example of parsing the serial string from the DF Robot Joystick with the mbed |
jebradshaw | 0:ce988e55f00d | 2 | #include "mbed.h" |
jebradshaw | 0:ce988e55f00d | 3 | |
jebradshaw | 0:ce988e55f00d | 4 | DigitalOut myled(LED1); |
jebradshaw | 0:ce988e55f00d | 5 | Serial pc(USBTX, USBRX); //tx, rx via USB connection |
jebradshaw | 0:ce988e55f00d | 6 | Serial xbee(p13, p14); //tx, rx via Xbee socket |
jebradshaw | 0:ce988e55f00d | 7 | |
jebradshaw | 0:ce988e55f00d | 8 | int main() { |
jebradshaw | 0:ce988e55f00d | 9 | char joystr[100]; //used to store the whole joystick string |
jebradshaw | 0:ce988e55f00d | 10 | int buttonsAll; //16bit representation of all buttons |
jebradshaw | 0:ce988e55f00d | 11 | int button[16]; //individual button states (normally high) |
jebradshaw | 0:ce988e55f00d | 12 | int js_lr_L; //Left Joystick Left/Right movement |
jebradshaw | 0:ce988e55f00d | 13 | int js_ud_L; //Left Joystick Up/Down movement |
jebradshaw | 0:ce988e55f00d | 14 | int js_lr_R; //Right Joystick Left/Right movement |
jebradshaw | 0:ce988e55f00d | 15 | int js_ud_R; //Right Joystick Up/Down movement |
jebradshaw | 0:ce988e55f00d | 16 | int chksum; //checksum for error correction |
jebradshaw | 0:ce988e55f00d | 17 | |
jebradshaw | 0:ce988e55f00d | 18 | pc.baud(921600); //crank up the PC baudrate (USB) to avoid latency between data acquisition |
jebradshaw | 0:ce988e55f00d | 19 | xbee.baud(9600); |
jebradshaw | 0:ce988e55f00d | 20 | |
jebradshaw | 0:ce988e55f00d | 21 | pc.printf("\r\n\r\n%s\r\n", __FILE__); |
jebradshaw | 0:ce988e55f00d | 22 | |
jebradshaw | 1:bc6098f1dd1a | 23 | while(1) { |
jebradshaw | 0:ce988e55f00d | 24 | while(xbee.readable()) //clear out the remaining characters in the buffer |
jebradshaw | 0:ce988e55f00d | 25 | char c = xbee.getc(); |
jebradshaw | 0:ce988e55f00d | 26 | |
jebradshaw | 1:bc6098f1dd1a | 27 | //read the serial string from the xbee (starts with '$', ends with \r\n |
jebradshaw | 1:bc6098f1dd1a | 28 | xbee.scanf("$%s\r\n", &joystr); |
jebradshaw | 1:bc6098f1dd1a | 29 | |
jebradshaw | 1:bc6098f1dd1a | 30 | // pc.printf("%s\r\n", joystr); //used for debugging |
jebradshaw | 1:bc6098f1dd1a | 31 | |
jebradshaw | 1:bc6098f1dd1a | 32 | if(!strncmp(joystr, "JOYSTK", 6)){ |
jebradshaw | 1:bc6098f1dd1a | 33 | if (sscanf(joystr, "JOYSTK,%X,%d,%d,%d,%d*%X", &buttonsAll, &js_lr_L, &js_ud_L, &js_lr_R, &js_ud_R, &chksum) >= 1) { |
jebradshaw | 1:bc6098f1dd1a | 34 | |
jebradshaw | 1:bc6098f1dd1a | 35 | // iterate through buttonsAll (each bit) to assign value MSB first |
jebradshaw | 1:bc6098f1dd1a | 36 | for(int i=15;i>=0;i--){ |
jebradshaw | 1:bc6098f1dd1a | 37 | if(buttonsAll & (1 << i)) |
jebradshaw | 1:bc6098f1dd1a | 38 | button[i] = 1; |
jebradshaw | 1:bc6098f1dd1a | 39 | else |
jebradshaw | 1:bc6098f1dd1a | 40 | button[i] = 0; |
jebradshaw | 1:bc6098f1dd1a | 41 | pc.printf("%1d ", button[i]); |
jebradshaw | 1:bc6098f1dd1a | 42 | } |
jebradshaw | 1:bc6098f1dd1a | 43 | |
jebradshaw | 1:bc6098f1dd1a | 44 | //pc.printf("%7 %5d %5d %5d %5d 0x%02X\r\n", buttonsAll, js_lr_L, js_ud_L, js_lr_R, js_ud_R, chksum); |
jebradshaw | 1:bc6098f1dd1a | 45 | pc.printf("%5d %5d %5d %5d\r\n", js_lr_L, js_ud_L, js_lr_R, js_ud_R); |
jebradshaw | 1:bc6098f1dd1a | 46 | } |
jebradshaw | 1:bc6098f1dd1a | 47 | else{ |
jebradshaw | 1:bc6098f1dd1a | 48 | pc.printf("BAD parse %s\r\n", joystr); |
jebradshaw | 1:bc6098f1dd1a | 49 | } |
jebradshaw | 1:bc6098f1dd1a | 50 | } |
jebradshaw | 0:ce988e55f00d | 51 | myled = !myled; //toggle LED for activity verification |
jebradshaw | 0:ce988e55f00d | 52 | }//while(1) |
jebradshaw | 0:ce988e55f00d | 53 | }//main |