Page 289 - Hacking Roomba
P. 289
270 Part III — More Complex Interfacing
Putting It All Together: BumpTurn
Listing 13-1 shows the complete BumpTurn.bs2 program in PBASIC. Structurally it’s not
much different from the BumpTurn.java you’re already familiar with from Chapter 6. Most of
the BS2 code is spent just setting up Roomba to talk at the slower speed needed by the Stamp.
If you don’t need to get sensor data, you can omit the second downshift to 2400 bps and send
commands at 19.2 kbps.
To use this program, just program the Stamp like normal and then plug it into Roomba.
Roomba will wake up and start driving around, avoiding any obstacles it finds. One of the nice
things about the Board of Education is that you can keep it connected through the serial cable
while it’s plugged into Roomba.
Listing 13-1: BumpTurn.bs2
‘ {$STAMP BS2}
‘ {$PBASIC 2.5}
‘ what pins are being used, named as in the Roomba ROI
DD PIN 12
RXD PIN 11
TXD PIN 10
LED PIN 7
LSRB PIN 2
LSRF PIN 1
INPUT TXD
‘ baudmode 32 == 19200 8N1, not inverted
BAUDFAST CON 32
‘ baudmode 396 == 2400 8N1, not inverted
BAUDSLOW CON 396
‘ define some Roomba constants
R_START CON 128
R_BAUD CON 129
R_CONTROL CON 130
R_FULL CON 132
R_POWER CON 133
R_SPOT CON 134
R_CLEAN CON 135
R_DRIVE CON 137
R_MOTORS CON 138
R_SONG CON 140
R_PLAY CON 141
R_SENSORS CON 142
‘ sensor bytes for data packet 1
SENS_BUMPWHEEL VAR BYTE
SENS_WALL VAR BYTE