Page 307 - Hacking Roomba
P. 307
288 Part III — More Complex Interfacing
3. Move the power switch SV1 from position 2-3 to position 1-2.
4. Plug the Arduino board into Roomba.
To switch back to coding, reverse the steps. There is a danger of ruining an Arduino board if it
is plugged into Roomba and a USB port when the SV1 switch is set to the USB position. Just
make sure both Roomba and the computer are not plugged into Arduino at the same time.
Coding BumpTurn with Arduino
After the experience of coding RoombaComm, working in Processing, and writing BumpTurn
in PBASIC, seeing BumpTurn in Arduino is bound to trigger all sorts of déjà vu. Because the
hardware serial port is used, Arduino can talk to Roomba at its default 57.6 kbps speed, so all
the bending over backward to get Roomba to speak at a speed the BS2 could deal with is not
needed.
The code should be fairly obvious. The Arduino function digitalWrite() is used to set
the state of a pin, like the PBASIC HIGH/LOW commands. The Serial.print() function
sends data out the serial port. That function can format different types of data before it is sent,
so a BYTE type is specified, which does no formatting. To read Roomba sensor data, the
serialAvailable() and serialRead() Arduino functions are used. Listing 13-3 shows
the Arduino version of BumpTurn.
Because you’re familiar now with both the ROI and how BumpTurn works, the friendly
mnemonics like R_CONTROL and R_DRIVE aren’t used here just to make the code a little
more visually compact. If you do a lot of Roomba programming with Arduino, you should cre-
ate those constants and use them. They’ll save you time and headache in the long run. Arduino
enables you to create complete libraries so you could implement all of the RoombaComm API
in C for use with Arduino.
When you have this code loaded in the Arduino IDE, upload it to the Arduino board, unplug
the board, and plug it into Roomba. In a few seconds it should light its LED and start driving
around.
If you’ve set Roomba to baud rates other than 57.6 kbps from previous projects, pull out its battery
and put it back in to reset the Roomba ROI to its default speed.
Listing 13-3: BumpTurn with Arduino
int ddPin = 2;
int ledPin = 9;
char sensorbytes[10];
#define bumpright (sensorbytes[0] & 0x01)
#define bumpleft (sensorbytes[0] & 0x02)
void setup() {