1
\$\begingroup\$

My situation is as follows, I have a robot that has 4 motor controllers attached to an arduino that has a xbee series 1 connected. Xbee has its default settings except baudrate which is set to 57600. My communication with the arduino is one way from pc to arduino sending 5 bytes every 40 milliseconds (one byte for command 4 bytes for 4 pwms). All calculations are done on the pc, all arduino does is read these bytes and apply pwm values to pins. Robot is never more than 10 meters away from the transmitter, nothing in between.

What I am seeing is sometimes even though I'm sending a motor commands the solenoid on the robot triggers I'm guessing some packets got lost and the next byte on the stream is equal to a solenoid command.

What I am trying to do is something like UDP where I can just send commands and any command that is send while arduino is processing to be discarded basically every-time I read a command from xbee I would like to get the newest command send from the pc? Is there a way to achieve this?

\$\endgroup\$
  • \$\begingroup\$ Why are you guessing that packets got lost? Verify that that's actually the cause of your problem first! \$\endgroup\$ – Nick Johnson May 14 '12 at 3:03
1
\$\begingroup\$

It sounds like you're using your XBee in 'data mode', which simulates a serial (stream) connection. You could be observing missing bytes for a number of reasons, including actual dropouts (though the XBee should account for that with retransmits) or bugs in the transmitting or receiving code, or simply a failure to read data from the XBee serial port fast enough.

XBees also typically have a packet-based API mode, however, which lets you send individually addressed packets of data to receivers, with variable acknowledge, retransmit, etc, settings. This lower level protocol is intrinsically packet based, so there's no need to implement your own packetization, though it may be worthwhile to add an application-level checksum such as a CRC to verify the integrity of your data.

There are a number of different XBees and XBee clones out there; check the manual for your particular model for API documentation.

\$\endgroup\$
0
\$\begingroup\$

The easiest way is to implement a UDP like protocol is to just add a counter to your packet. This could be a full byte or even just a few bits if you have space in your command byte. What you would do is count and the receiving end would keep track of the number of the last packet it receives. You would then just ignore any packets that have a count less then the last packet you got.

You will have to apply some logic to allow for when you overflow/loop back to a 0 count. This shouldn't be too complex though.

Another thing that can be done is to add a CRC to your packets. This will allow you to make sure that your packets are correct.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.