Basic Communication Arduino

From FIRE Wiki
Revision as of 17:04, 30 March 2011 by Tfriez (Talk | contribs)

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

This first exercise is the “Hello World” of multi-robot communication – Being able to send a single byte of data from one controller to another wirelessly.

In this program, we will write both sides of the code – Sending a Character and Receiving a Character from the Xbee Modules. You will need two hardware setups to test both sending and receiving a character. You can also use a PC interface with the Xbee, such as a USB adapter with a Serial Terminal Program like HyperTerminal.

Hardware and Software Required

Project Files

Teacher Notes

This exercise is simple for students to execute. Student teams will be required to have two sets of hardware (2 Arduino Mega controllers and 2 Xbee Setups) or can be split up and have two teams work together with one team being the sender and the other team being the receiver.

Students are required to have a working knowledge of ROBOTC before attempting to being working with the Arduino and Multi-Robot Communication. We recommend that the students have completed the Teaching ROBOTC series curriculum for either the VEX Cortex or NXT/TETRIX systems before moving onto the Arduino platform.

Students should always take care when working with bare electronics, as static electricity and incorrect wiring may render the Arduino or Xbee non-functional.

Lesson

Setup and Wiring

Wiring for this project is very easy – only four wires required from the Xbee Explorer Adapter to the Arduino Mega.

Take a look at the Xbee Explorer and you’ll see 4 pins that need to be connected:

  • Gnd – Ground
  • 5V – 5 Volt Input
  • DOut – Digital (Serial) Out
  • DIn – Digital (Serial) In

XbeeExplorer.JPG

Using a Male to Female jumper wire, connect the 4 ports on the Xbee Explorer directly to the Arduino as shown:

  • Gnd – GND (below Digital Input bank)
  • 5V – 5V (above Digital Input bank)
  • DOut – Arduino RX1
  • DIn – Arduino TX1

ArduinoCloseUp.jpg

In the end, you setup should look like this.

Mega and Xbee.jpg

You're all set to start programming now!


Programming – Transmit

  • configureSerialPort(uartOne, uartUserControl);

This line is used to configure the Serial Port “uartOne” as a User Controller UART. Other options in ROBOTC involve setting the UART up to accept camera data, LCD data and other device specific capabilities.

  • setBaudRate(uartOne, baudRate9600);

The setBaudRate command is used to configure how fast data will be sent and how fast the Arduino should expect incoming data on this specific serial port. The Mega has 3 serial ports, and each serial port can be configured to a different baud rate. In our program, we’re setting the Baud Rate up to be 9600 Baud. This is the default speed of the Xbee Radio. The baud rate on both the transmit and receive sides must be the same or the data will be corrupted.

  • sendChar(uartOne, 'A');

The sendChar function is used to send a specific character over the Serial / Xbee connection. In this example, we’re sending the character A. ROBOTC is only able to send one character at a time with the built-in functionality, but in future lessons we’ll take a look at how to send a group of characters in succession. Once the character is sent, the program is complete.


Programming – Receive

  • char incomingChar;

We Declare a variables (type “char” for character) with the name ‘incomingChar’. This will be the placeholder variable we use to store the incoming variable over the Xbee Module and also will allow us to use ROBOTC’s debugger to view the character once it’s received.

  • configureSerialPort(uartOne, uartUserControl);

This line is used to configure the Serial Port “uartOne” as a User Controller UART. Other options in ROBOTC involve setting the UART up to accept camera data, LCD data and other device specific capabilities.

  • setBaudRate(uartOne, baudRate9600);

The setBaudRate command is used to configure how fast data will be sent and how fast the Arduino should expect incoming data on this specific serial port. The Mega has 3 serial ports, and each serial port can be configured to a different baud rate. In our program, we’re setting the Baud Rate up to be 9600 Baud. This is the default speed of the Xbee Radio. The baud rate on both the transmit and receive sides must be the same or the data will be corrupted.

  • while(getChar(uartOne) != -1);

This loop here uses the getChar command to receive the next character that is in ROBOTC’s buffer. The purpose of this loop is to continuously process any leftover characters that might be in the buffer still. ROBOTC’s “getChar” command will receive a -1 value if there are no characters left, so this loop will continue looping until it get a -1 value. This is a common technique known as "flushing the buffer".

  • while(true)

We want the following code to loop forever, so we’re going to put it inside of a “while(true)” loop.

  • incomingChar = getChar(uartOne);

The getChar command receives the next character that is in ROBOTC’s buffer. This line of code will set the ‘incomingChar’ value to the value returned by the getChar command. If this value is a -1, that means no actual character has been received yet.

  • if(incomingChar != -1)

This loop checks to see if the value of incomingChar (which was set above) is not equal to -1. If it is equal to negative one, then the If statement falls through and the “while(true)” loop will loop again. If the value is equal to something other than -1, then the break command below will execute.

  • break;

Breaks out of the current loop and continues execution of the rest of the code in the program. Since the while(true) loop is the last loop in the program, once the break is reached, the program will end.

Photos