Sending and Receiving Messages (Two-Way)
The second lesson it to have the original receiving robot (Robot B) send a reply message to the original sending robot (Robot B). In the previous lesson Robot A (the sender) sent a single message to Robot B (the receiver). In this program, we want Robot B to send a reply message once it has received the original message. Robot A should then receive the message and display it to its LCD screen for a few seconds so the user can confirm the message was received.
Topics Covered In This Lesson:
- Sending and Receiving Data
- Using the NXT LCD screen
- Using External Libraries
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.
- Send a message (“M2”) from Robot A to Robot B
- Have Robot B send back a message (“M2”) to Robot A, but only after it receives (“M1”) from Robot A.
- 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.
- 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).
1. Start with the previous lesson's program. We will build this program on top of the previous program.
2. With Robot A's program we already have the SendString command successfully working. We now need to add in the code to receive a string and then display it to the LCD screen. Create a new string variable to store the incoming message and add the "ReceiveString" command using the newly created variable.
3. Lastly (with Robot A) add the "nxtDisplayString" command to view the received string on the NXT's LCD screen. Remember to give your program a few seconds of "wait1Msec" time at the end to keep the message displayed on the screen.
4. Robot B is even easier. Since we already have the code to receive a string, all we have to add is a "SendString" command to the end of the program. It's up to you if you would like this to be delayed (by having the message Robot B received displayed for a few seconds and then sending the message back to Robot A), or if you want to have both Robot A and Robot B displaying their messages together. You can make this choice by moving the "wait1Msec" command before or after the "SendString" command.
Robot A Source Code
Download Sending and Receiving 2A
Robot B Source Code
Download Sending and Receiving 2B