Data corruption over XBee link

Team,

I am getting what appears to be data corruption over an XBee serial link. This is my setup...

1 - Transmitter: I am using a DF Gamepad which is based on a Leonardo board. Plugged into it is an XBee series 2, configured as a router in transparent mode running at 9600 baud.

2 - Receiver: A Makeblock Orion board, for all intents and purposes, an Uno board. With an Xbee series 2, configured as a controller in transparent mode running at 9600.

Here's my transmitter code, please note:

1 - it's sending a series of bytes
2 - the data is encapsulated with 254 at the beginning and 255 at the end.
3 - all the bytes are shaped to be no more than 250 (I've mapped the range of the analog value to site between 0-250.

int buttonState[17];
int joystick[4];
int AnalogButton[2];

 unsigned char gamePadButtons[22];

int inputCommand = 0;

#define shackMotorPin 2

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);  //Init the Serial baudrate
  InitIO();             // Initialize the inputs/outputs and the buffers
}

void InitIO(){ 
  for(int i = 0; i < 17; i++) 
  pinMode(i, INPUT); 
  pinMode(shackMotorPin,OUTPUT);
  digitalWrite(shackMotorPin,LOW);  // Stop shacking of the gamepad
}

unsigned long timer = 0;

void loop()
{
  if(millis() - timer > 500){  // manage the updating freq of all the controlling information
    DataUpdate();  //read the buttons and the joysticks data
    printData();   //print the datas and states
    timer = millis(); 
  }
  
//  if(Serial1.available()){
//    char input = Serial1.read();
    
//    switch(input){
//      case 's':
//        Serial1.println("Shack");
//        inputCommand = input;
//        digitalWrite(shackMotorPin,HIGH);
//        break;
      
//      case 'x':
//        Serial1.println("Stop");
//        inputCommand = input;
//        digitalWrite(shackMotorPin,LOW);
//        break;
        
//      default:
//        break;
//    }
//  }

}

void DataUpdate(){

  gamePadButtons[0] = 254;
  gamePadButtons[1] = map (analogRead(0), 0, 1023, 0, 250);              //"J2" (250 = released, 0 = pressed)
  gamePadButtons[2] = map (analogRead(1), 0, 1023, 0, 250);              //"J1" (250 = released, 0 = pressed)
  for(int i = 3; i < 17; i++) gamePadButtons[i] = digitalRead(i);        //"S2","S1","UP","LEFT","DOWN","RIGHT","1","4","2","3","RZ1","RZ2","LZ1","LZ2"
  gamePadButtons[17] = map (analogRead(2), 0, 1023, 0, 250);             //"RUD" (U250,D0)
  gamePadButtons[18] = map (analogRead(3), 0, 1023, 0, 250);             //"RLR" (L250,R0)
  gamePadButtons[19] = map (analogRead(4), 0, 1023, 0, 250);             //"LUD" (U250, D0)
  gamePadButtons[20] = map (analogRead(5), 0, 1023, 0, 250);             //"LLR" (L250, R0)
  gamePadButtons[21] = 255;
  
}

void printData(){
  for(int i = 0; i < sizeof(gamePadButtons); i++)  Serial1.write(gamePadButtons[i]); 
  //for(int i = 0; i < sizeof(gamePadButtons); i++)  Serial.write(gamePadButtons[i]);
  //Serial.print("  ");
  for(int i = 0; i < sizeof(gamePadButtons); i++)  Serial.print(gamePadButtons[i]),Serial.print(" "); 
  Serial.println("");
  delay(30); // delay added so gamepad is not transmitting continously.
}

Here's the function I used to receive the data (not my design, someone else on this board wrote it.

void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    byte startMarker = 0xFE;
    byte endMarker = 0xFF;
    byte rb;
   

    while (gamePad.available() > 0 && newData == false) {
        rb = gamePad.read();
        if (recvInProgress == true) {
            if (rb != endMarker) {
                Serial.print(rb),Serial.print(" ");
                receivedBytes[ndx] = rb;
                ndx++;
                if (ndx >= numBytes) {
                    ndx = numBytes - 1;
                }
            }
            else {
                receivedBytes[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                numReceived = ndx;  // save the number for use when printing
                ndx = 0;
                newData = true;
                Serial.println("EndMarker"); 
            }
        }

        else if (rb == startMarker) {
            recvInProgress = true;
        }
    }
}

This is what the data looks like being sent out of the transmitter (gamepad)...

** **254 250 250 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 125 122 120 255 254 250 250 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 125 122 120 255 254 250 250 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 125 122 120 255 254 249 250 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 125 122 120 255 254 249 250 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 125 122 120 255 etc...** **

and this is what I'm receiving on the other end...

** **250 250 1 1 1 [color=blue]129[/color] 1 1 1 1 [color=blue]129[/color] 1 1 1 1 1 124 125 [color=blue]250[/color] 120 EndMarker 250 250 1 1 1 1 1 1 1 [color=blue]129[/color] 1 1 1 1 1 1 124 125 122 120 EndMarker 250 250 1 [color=blue]129[/color] 1 1 1 1 1 1 1 1 1 1 [color=blue]129[/color] 1 124 125 122 120 EndMarker 250 250 1 1 [color=blue]129[/color] 1 1 1 1 1 1 1 [color=blue]129[/color] 1 1 1 124 125 122 120 EndMarker 250 250 1 [color=blue]129[/color] 1 1 1 1 1 1 1 [color=blue]129[/color] 1 1 1 1 [color=red]252[/color] 125 122 120 EndMarker 250 250 1 1 [color=blue]129[/color] 1 1 1 1 1 1 1 [color=blue]129[/color] 1 1 1 124 125 122 120 EndMarker 250 250 1 1 1 1 1 1 [color=blue]129[/color] 1 1 1 1 1 1 1 124 125 122 120 EndMarker** **

The data marked blue is erroneous, but what is more telling is the data marked in red which in this example = 252, the big problem with this is the transmitter is programmed to only send data up to 250 (excluding the start and end tags).

I added a delay in the transmitter code, and have been increasing it by 10ms to see if that helps, I'm currently up to 30ms, and I see no change.

Any ideas?

regards
Michael

Team,

I just realized I put the delay in the wrong place, so I've change this...

void printData(){
  for(int i = 0; i < sizeof(gamePadButtons); i++)  Serial1.write(gamePadButtons[i]);

to this...

void printData(){
  for (int i = 0; i < sizeof(gamePadButtons); i++) {
    Serial1.write(gamePadButtons[i]);
    delay(20);
  }

...but I still get the corrupted data coming through.

Any ideas?

regards
Michael

Team,

I've just had another thought. The data I'm sending and receiving are gamepad output i.e. buttons and joystick positions to control a robot tank.

Since the frequency of the output is pretty high, missing 1 or 2 instructions that are repeated 10s-100s times a second isn't going to affect gameplay, so here's my idea...

I send each byte twice, so this...

254 249 250 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 125 122 120 255

becomes this...

254 249 249 250 250 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 124 124 125 125 122 122 120 120 255

In the receiver, I compare each pair of bytes - possible outcomes....

byte#1 not corrupted, byte#2 not corrupted = good.

byte#1 not corrupted, byte#2 corrupted = bad - replace with neutral value.

byte#1 corrupted, byte#2 not corrupted = bad - replace with neutral value.

byte#1 corrupted, byte#2 corrupted = bad - replace with neutral value.

Any ideas?

regards
Michael

1 - Transmitter: I am using a DF Gamepad which is based on a Leonardo board. Plugged into it is an XBee series 2, configured as a router in transparent mode running at 9600 baud.

2 - Receiver: A Makeblock Orion board, for all intents and purposes, an Uno board. With an Xbee series 2, configured as a controller in transparent mode running at 9600.

The terms are router/end device and coordinator.

Why so slow?

Instead of sending each byte twice, reduce the amount of data. There is no reason to send 14 ones (or zeros presumably), pack them all into an int, using bitWrite().

Then, use the XBee library to package, and send, in API mode, API packets. The packets will include a checksum, and the library will ask that the packet be resent if the contents and the checksum don''t agree, which would mean that corruption occurred.