Example program of decoding the DF Robotics xbee joystick serial output

Dependencies:   mbed

Weapons and Systems Engineering mbed DFRobotJoystick Interface

/media/uploads/jebradshaw/dfjoystick_xbee.jpg

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).

/media/uploads/jebradshaw/df_robot_data_stream.jpg

Committer:
jebradshaw
Date:
Fri Feb 06 21:04:14 2015 +0000
Revision:
0:ce988e55f00d
Child:
1:bc6098f1dd1a
Example program for using the mbed to decode the DFRobotics Joystick serial output from the xbee

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 0:ce988e55f00d 1 #include "mbed.h"
jebradshaw 0:ce988e55f00d 2
jebradshaw 0:ce988e55f00d 3 DigitalOut myled(LED1);
jebradshaw 0:ce988e55f00d 4 Serial pc(USBTX, USBRX); //tx, rx via USB connection
jebradshaw 0:ce988e55f00d 5 Serial xbee(p13, p14); //tx, rx via Xbee socket
jebradshaw 0:ce988e55f00d 6
jebradshaw 0:ce988e55f00d 7 int main() {
jebradshaw 0:ce988e55f00d 8 char joystr[100]; //used to store the whole joystick string
jebradshaw 0:ce988e55f00d 9 char header[20]; //stores header - not very useful here
jebradshaw 0:ce988e55f00d 10 char *tok; //pointer used for string token function
jebradshaw 0:ce988e55f00d 11 int buttonsAll; //16bit representation of all buttons
jebradshaw 0:ce988e55f00d 12 int button[16]; //individual button states (normally high)
jebradshaw 0:ce988e55f00d 13 int js_lr_L; //Left Joystick Left/Right movement
jebradshaw 0:ce988e55f00d 14 int js_ud_L; //Left Joystick Up/Down movement
jebradshaw 0:ce988e55f00d 15 int js_lr_R; //Right Joystick Left/Right movement
jebradshaw 0:ce988e55f00d 16 int js_ud_R; //Right Joystick Up/Down movement
jebradshaw 0:ce988e55f00d 17 int chksum; //checksum for error correction
jebradshaw 0:ce988e55f00d 18
jebradshaw 0:ce988e55f00d 19 pc.baud(921600); //crank up the PC baudrate (USB) to avoid latency between data acquisition
jebradshaw 0:ce988e55f00d 20 xbee.baud(9600);
jebradshaw 0:ce988e55f00d 21
jebradshaw 0:ce988e55f00d 22 pc.printf("\r\n\r\n%s\r\n", __FILE__);
jebradshaw 0:ce988e55f00d 23
jebradshaw 0:ce988e55f00d 24 while(1) {
jebradshaw 0:ce988e55f00d 25 //read the serial string from the xbee (starts with '$', ends with \r\n
jebradshaw 0:ce988e55f00d 26 xbee.scanf("$%s\r\n", &joystr);
jebradshaw 0:ce988e55f00d 27
jebradshaw 0:ce988e55f00d 28 while(xbee.readable()) //clear out the remaining characters in the buffer
jebradshaw 0:ce988e55f00d 29 char c = xbee.getc();
jebradshaw 0:ce988e55f00d 30
jebradshaw 0:ce988e55f00d 31 // pc.printf("%s ", joystr); //used for debugging
jebradshaw 0:ce988e55f00d 32
jebradshaw 0:ce988e55f00d 33 //First pass strtok() the string address to parse, then NULLs to iterate through string
jebradshaw 0:ce988e55f00d 34 tok=strtok(joystr, ","); //Extract the header
jebradshaw 0:ce988e55f00d 35 strcpy(header, tok); //copy the header
jebradshaw 0:ce988e55f00d 36
jebradshaw 0:ce988e55f00d 37 tok=strtok(NULL, ","); //Extract buttons string
jebradshaw 0:ce988e55f00d 38 buttonsAll=strtol(tok, NULL, 16); //turn the 0xXXXX string into a decimal variable
jebradshaw 0:ce988e55f00d 39
jebradshaw 0:ce988e55f00d 40 tok=strtok(NULL, ","); //Extract Left joystick string L/R string
jebradshaw 0:ce988e55f00d 41 js_lr_L=atoi(tok); //turn ASCII to integer for left joystick Left/Right
jebradshaw 0:ce988e55f00d 42
jebradshaw 0:ce988e55f00d 43 tok=strtok(NULL, ","); //Extract Left joystick string U/D string
jebradshaw 0:ce988e55f00d 44 js_ud_L=atoi(tok); //turn ASCII to integer for left joystick Up/Down
jebradshaw 0:ce988e55f00d 45
jebradshaw 0:ce988e55f00d 46 tok=strtok(NULL, ","); //Extract Right joystick string L/R string
jebradshaw 0:ce988e55f00d 47 js_lr_R=atoi(tok); //turn ASCII to integer for right joystick Left/Right
jebradshaw 0:ce988e55f00d 48
jebradshaw 0:ce988e55f00d 49 tok=strtok(NULL, "*"); //Extract Right joystick string U/D string when asterisk is reached
jebradshaw 0:ce988e55f00d 50 js_ud_R=atoi(tok); //turn ASCII to integer for right joystick Up/Down
jebradshaw 0:ce988e55f00d 51
jebradshaw 0:ce988e55f00d 52 tok=strtok(NULL, "\0"); //end of string
jebradshaw 0:ce988e55f00d 53 chksum = strtol(tok, NULL, 16); //turn hex string checksumn into integer
jebradshaw 0:ce988e55f00d 54
jebradshaw 0:ce988e55f00d 55 //print the variables
jebradshaw 0:ce988e55f00d 56 pc.printf("%s %7d %5d %5d %5d %5d 0x%02X ", header, buttonsAll, js_lr_L, js_ud_L, js_lr_R, js_ud_R, chksum);
jebradshaw 0:ce988e55f00d 57
jebradshaw 0:ce988e55f00d 58 // iterate through buttonsAll (each bit) to assign value MSB first
jebradshaw 0:ce988e55f00d 59 for(int i=15;i>=0;i--){
jebradshaw 0:ce988e55f00d 60 if(buttonsAll & (1 << i))
jebradshaw 0:ce988e55f00d 61 button[i] = 1;
jebradshaw 0:ce988e55f00d 62 else
jebradshaw 0:ce988e55f00d 63 button[i] = 0;
jebradshaw 0:ce988e55f00d 64 pc.printf("%1d ", button[i]);
jebradshaw 0:ce988e55f00d 65 }
jebradshaw 0:ce988e55f00d 66 pc.printf("\r\n");
jebradshaw 0:ce988e55f00d 67 myled = !myled; //toggle LED for activity verification
jebradshaw 0:ce988e55f00d 68 }//while(1)
jebradshaw 0:ce988e55f00d 69 }//main