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

Revision:
0:ce988e55f00d
Child:
1:bc6098f1dd1a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_DFRobot_joystick_parse.cpp	Fri Feb 06 21:04:14 2015 +0000
@@ -0,0 +1,69 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX);    //tx, rx via USB connection
+Serial xbee(p13, p14);    //tx, rx via Xbee socket
+    
+int main() {
+    char joystr[100];   //used to store the whole joystick string
+    char header[20];    //stores header - not very useful here
+    char *tok;          //pointer used for string token function
+    int buttonsAll;     //16bit representation of all buttons
+    int button[16];     //individual button states (normally high)
+    int js_lr_L;        //Left Joystick Left/Right movement
+    int js_ud_L;        //Left Joystick Up/Down movement
+    int js_lr_R;        //Right Joystick Left/Right movement
+    int js_ud_R;        //Right Joystick Up/Down movement
+    int chksum;         //checksum for error correction
+            
+    pc.baud(921600);    //crank up the PC baudrate (USB) to avoid latency between data acquisition
+    xbee.baud(9600);
+    
+    pc.printf("\r\n\r\n%s\r\n", __FILE__);
+        
+    while(1) {        
+        //read the serial string from the xbee (starts with '$', ends with \r\n
+        xbee.scanf("$%s\r\n", &joystr);
+        
+        while(xbee.readable())      //clear out the remaining characters in the buffer
+            char c = xbee.getc();
+            
+//        pc.printf("%s ", joystr);     //used for debugging
+        
+        //First pass strtok() the string address to parse, then NULLs to iterate through string
+        tok=strtok(joystr, ",");        //Extract the header
+        strcpy(header, tok);            //copy the header
+
+        tok=strtok(NULL, ",");          //Extract buttons string
+        buttonsAll=strtol(tok, NULL, 16);  //turn the 0xXXXX string into a decimal variable
+        
+        tok=strtok(NULL, ",");          //Extract Left joystick string L/R string
+        js_lr_L=atoi(tok);              //turn ASCII to integer for left joystick Left/Right
+        
+        tok=strtok(NULL, ",");          //Extract Left joystick string U/D string
+        js_ud_L=atoi(tok);              //turn ASCII to integer for left joystick Up/Down
+        
+        tok=strtok(NULL, ",");          //Extract Right joystick string L/R string
+        js_lr_R=atoi(tok);              //turn ASCII to integer for right joystick Left/Right
+        
+        tok=strtok(NULL, "*");          //Extract Right joystick string U/D string when asterisk is reached
+        js_ud_R=atoi(tok);              //turn ASCII to integer for right joystick Up/Down
+                        
+        tok=strtok(NULL, "\0");         //end of string
+        chksum = strtol(tok, NULL, 16); //turn hex string checksumn into integer
+        
+        //print the variables
+        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);                
+        
+        // iterate through buttonsAll (each bit) to assign value MSB first
+        for(int i=15;i>=0;i--){
+            if(buttonsAll & (1 << i))
+                button[i] = 1;
+            else
+                button[i] = 0;
+            pc.printf("%1d ", button[i]);
+        }
+        pc.printf("\r\n");
+        myled = !myled; //toggle LED for activity verification
+    }//while(1)
+}//main