Page 365 - Hacking Roomba
P. 365

346       Part III — More Complex Interfacing




                             At this point, you can cd into the disk and re-run the spcacat command, but leave off the
                             -o argument. Then it will create image after image with names like jpeg 07:21:2006-
                             08:24:03-P0004.tif, which is the date, time, and image number of the capture.
                             For example, here’s how you might capture 100 images at a one-second interval, saving them to
                             the flash drive:
                             root@OpenWrt:~# cd /mydisk
                             root@OpenWrt:~# spcacat -d /dev/video0 -f jpg -g -p 1000 -N 100

                             When this is running, at any point you can just shut down the router, remove the USB drive,
                             and stick it into your PC to get a visual record of what the router saw as a series of time stamped
                             JPEGs. From there, you can make a time-lapse movie using QuickTime Pro or similar program.


                     Controlling Roomba from C

                             For controlling the Roomba robot, a compiled C program has a few advantages over the other
                             methods presented so far:

                                 Faster: Although hard to see on modern PCs, interpreted languages like MicroPerl are
                                 noticeably slower on these embedded systems.

                                 No need for stty: Shelling out to an external program to set serial parameters just seems
                                 inelegant. In C, it’s easy to do the equivalent without needing an external program.
                                 Self-contained: No execution environment is needed. Execution environments like the
                                 shell or Perl can add complexity. If the task is simple, use a simple implementation.

                             Also by programming in C, you have better control over the memory usage and timing of the
                             code. This isn’t critical for the simple Roomba controlling done here, but if you were analyzing
                             a lot of Roomba sensor data, using space efficiently becomes important when your machine
                             only has 64 MB of RAM.
                             The roombacmd program is divided into a set of basic Roomba commands, called roombalib.c,
                             and the command-line parsing, called roombacmd.c. The code compiles and runs not only on
                             OpenWrt Linux but on regular Linux and Mac OS X. It should also run in Windows with
                             Cygwin.

                             Listing 15-3 shows the three primary functions in roombalib.c. The roomba_init_
                             serialport() function uses standard Unix calls to accomplish what the stty line did in
                             the Perl script. The roomba_drive() function looks very similar to the drive() method in
                             RoombaComm you created in Chapter 5, and the roomba_read_sensors() function looks
                             much like the same function in Arduino from Chapter 13.
                             The roombalib library defines a Roomba data structure that just contains the serial port name
                             and file descriptor. Think of it as the data-only part of the RoombaComm object.
   360   361   362   363   364   365   366   367   368   369   370