Page 341 - Hacking Roomba
P. 341
322 Part III — More Complex Interfacing
Treating devices as files is great for sending and receiving data, but in order to configure the
device something else is needed. For serial devices, you need to set the baud rate and other
parameters, and the standard way of setting those parameters is with the stty program. Thus,
the full set of command line commands to make a Roomba drive straight is:
% stty -F /dev/usb/tts/0 57600 raw -parenb -parodd cs8 -hupcl -
cstopb clocal
% printf “\x80” > /dev/usb/tts/0 # ROI START
% printf “\x82” > /dev/usb/tts/0 # ROI CONTROL
% printf “\x89\x00\xc8\x80\x00” > /dev/usb/tts/0 # ROI DRIVE
The stty gobbledygook sets the serial port to 57600 8N1 with no input or output data manip-
ulation (“raw” mode). The stty program is normally used to help control serial terminals
connecting to the computer, which is why there’s so much configuration to undo all that.
If you wanted, you could put those four commands in a file, call it roomba_forward.sh, and
run it whenever you wanted. That’s the beauty of shell script programming: You can find a set
of typed commands that perform a task you want to repeat and put them in a file so you only
have to remember one thing instead of four.
The stty program has slightly different syntax on different versions of Unix. For example, on
Mac OS X uses the syntax stty -f <port> instead of stty -F <port>.
Perl Script Control
But writing shell programs of any complexity is frustrating; its syntax is strange compared to
Java or C. While Java may be out of reach for now, a version of Perl called microPerl is part
of the standard OpenWrt distribution. It gives you most all of Perl’s capability but in a few
hundred kilobytes. With microPerl you can write nicely structured code but still be able to
easily modify it on the WL-HDD itself.
Listing 14-4 shows a microPerl script called roombacmd.mpl that enables you to control
Roomba from the command line. It is a microPerl program, but it’s also a normal Perl pro-
gram. Swap the two lines at the top and it will run on any system with Perl (and stty).
The roomba_init() function both executes the appropriate stty command as well as puts
the Roomba into SAFE mode so it can receive DRIVE instructions. The rest of the functions
are just implementations of various DRIVE functions you’ve seen before.
Listing 14-4: (micro)Perl Script roombacmd.mpl
#!/usr/bin/microperl
##!/usr/bin/perl
# in case we have stty in the current directory
$ENV{‘PATH’}=”$ENV{PATH}:.”;
sub usage() {