Page 81 - Hacking Roomba
P. 81

62       Part I — Interfacing




                             When connected, the rest of the buttons in RoombaCommTest are ready to use.

                             Click the Test button. RoombaCommTest will play a few notes on the robot’s beeper and move
                             Roomba in a little dance. Congratulations! You’ve just controlled Roomba with your computer.
                             If you like, play around with Roomba using the other buttons. Whenever you want, quit the
                             program.

                             When Roomba is being commanded through the ROI, the physical buttons on Roomba no
                             longer work. This means the Power button won’t turn off Roomba. To turn off Roomba, either
                             lift the cleaner to trigger the safety fault mode and then press the Power button or click the
                             power-off button in RoombaCommTest.


                     Commanding Roomba


                             The RoombaCommTest program is a Java GUI wrapper around some very simple commands.
                             Take for example the first things it does when the connect button is clicked:
                             RoombaComm roombacomm = new RoombaCommSerial();
                             if( !roombacomm.connect(portname) ) {
                               System.err.println(“Could not connect”);
                               return;
                             }
                             roombacomm.startup();
                             roombacomm.control();
                             roombacomm.pause(30);
                             roombacomm.playNote(72,10);  // C
                             roombacomm.pause(200);

                             The preceding code creates a RoombaComm object. The RoombaComm object contains all the
                             protocol-independent methods for communicating with a Roomba. It is the ROI protocol
                             embodied in code. Subclasses of RoombaComm implement a few low-level methods to send and
                             receive data. One example of such a subclass is RoombaCommSerial, for dealing with commu-
                             nicating with a Roomba over a serial port. Another example is RoombaCommTCPClient, for
                             talking to a TCP-connected Roomba cleaner.
                             When an appropriate RoombaComm object is created, the next step is to try to connect to
                             Roomba with the connect() method. In RoombaCommSerial, connect() hides all the Java
                             serial messiness that is needed.
                             If the connection succeeds, then the START and CONTROL commands are sent to the robot
                             through the startup() and control() methods. This puts Roomba in safe mode, ready to
                             be controlled.
                             The playNote() method plays a single note of a given duration. It does this by sending first a
                             SONG command with a one-note song defined and then a PLAY command to play that song.
                             This is the first, albeit simple, example of the RoombaComm API building higher-level func-
                             tions out of the basic ROI building blocks.
   76   77   78   79   80   81   82   83   84   85   86