Sending and Parsing Command Messages

From FIRE Wiki
Revision as of 20:14, 14 December 2011 by Ipflot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Project Description

This lesson will take a step away from back and forth communication and focus on how to send, receive and then parse a command to have the receiving robot perform an action. The objective is to have Robot A send a command and then Robot B receives the command and perform the specific movement behavior requested. Robot B will have a library of commands that it will understand, and Robot A will have to send one of those known commands for the experiment to work.


Topics Covered In This Lesson:

  • Sending and Receiving Data
  • Using External Libraries
  • Parsing data using the "StringFind" command
  • Developing a Dictionary of Commands


Sending command.png

User Notes

Note: The concepts outlined in this lesson are geared towards an intermediate user of ROBOTC and C Programming languages. Users should familiarize themselves with ROBOTC before beginnging the Multi-Robot lessons.

  • Remember when using Multi-Robot communications to always start the robot that is the receiver first. Otherwise, the receiving robot will not be listening when the sending robot broadcasts it's message.



  • Have Robot A send a command ("M3") and Robot B receive the command and decide what action to perform based on the command received.
  • Example: Robot A sends “Fwd" and Robot B moves forward for a predetermined distance.

Hardware Setup

  • Each Robot should have a programmed Xbee radio attached to a NXTBee adapter. The NXTBee Adapter should be connected to sensor port 4 of the LEGO NXT.


New Concepts

In this exercise, we will be changing the data that we are sending from basic words back and forth to a message can also represent an action. It's important to keep in mind that you actions should not be robot specific, but rather in a common language for the robots in units that are universal. Each different robot can execute the same action message differently, depending on the physical size and capabilities of the robot.

Example 1:

  • Message: “Move forward for 100 counts”
    • Robot A with small wheels turns both motors for 100 encoder counts and travels 15cm.
    • Robot B with larger wheels turns both motors for 100 encoder counts and travels 30cm.

Example 2:

  • Message: “Move forward for 15 cm”
    • Robot A with small wheels turns both motors for 100 encoder counts and travels 15cm.
    • Robot B with larger wheels turns both motors for 50 encoder counts and travels 15cm.

Now sending messages like "MoveForwardFor100counts" is probably too long and complex for the robot to have to parse and understand. We can create a short-hand language for our robots with a common language in order to communicate successfully.


  • “Fwd” - move forward 10cm
  • “TurnL” - turn left 90 degrees
  • “TurnR” - turn right 90 degrees

In order to understand receive messages, we need to understand the message received. As we mentioned previous, the best method humans use to do this is by using a dictionary. Robots can do a similar thing by comparing the incoming message with a set of known strings. We can create a library of commands such as “Fwd”, “TurnL, and “TurnR” and compare the incoming message to see if we've found a match. To do this, we'll use the "StringFind" command in ROBOTC.

Commands Used

  • StringFind(stringToCompare, “substring”) - Finds a specific sub-string inside of a string. Function will returns a value of (-1) if the sub-string could not be found. If the sub-string is found, the function will return the index of where the sub-string begins (normally 0).
  • InitRS485(); - Function to initializes the XBee radio
  • SendString(message); - Sends a string to every other robot in range on the same communication channel. This function will delay the program to allow the message to properly sent (roughly 1ms per character in the string).
  • ReceiveString(message); - Receives a string that was sent from another robot. This function will delay the program until a message is received (internal loop).

Lesson Steps

1. Start with the sample programs the from One-Way Communication lesson. Since Robot B (the receiver) will not be sending a message back to the sender (Robot A), we do not need the Two-Way Communication code.

2. Start with Robot A (sender) - This program is very similar to the Robot A program from the One-Way Communication Lesson. Robot A should only initialize its radio, and then send a string corresponding to one of the behaviors we'll program into Robot B. Robot A should send the command "Fwd", which will tell the receiving robot (Robot B) to move forward for a specified amount of time.

3. Robot B should be set up in a similar program as the One-Way Communication. One you have the radio initialized and the received string stored in a variable, we can proceed to parsing/understanding the data.

4. Robot B after it has received the string should now compare the incoming string to a set of known data. Using the "StringFind" command, we will want to check each string. We can do this by asking the question of where the "sub-string" that we are looking for appears in the incoming string from Robot A. Using an "if" command, we can ask if the sub-string "Fwd" exists in incomingString we received from Robot A. Your code should look like this:

  • if(stringFind(incomingString, "Fwd") >= 0)

This line reads "If the "Fwd" sub-string is found at index 0 or greater in the string "incomingString", then run the code for this condition." We look for an index of 0 or greater to ensure that the string is anywhere inside of what we received. This condition basically says "as long as "Fwd" is somewhere, good enough" and will run the code for the condition. We say "greater than zero" because we will receive a value of (-1) if the sub-string is not found.

5. Continue this check for every condition you would like to search for. Some good ones might be "Fwd","TurnL" and "TurnR" - Check the example code below for the solution should you have difficulty understanding how to check for multiple conditions.

Robot A Source Code

Download Command Messages 1A

Robot B Source Code

Download Command Messages 1B