Page 117 - Hacking Roomba
P. 117

98       Part I — Interfacing




                     Simple Tank-Like Motion


                             Roomba is capable of some fairly complex moves. The most useful (and simplest) are those like
                             that of a toy tank: forward, back, spin left, and spin right.These moves aren’t the most elegant that
                             Roomba is capable of, but they certainly do the job.

                             Moving with the send() Command
                             All movement commands are implemented by sending byte sequences. RoombaComm pro-
                             vides a direct way of sending DRIVE commands to the Roomba via the low-level send()
                             method. This method takes either a single byte or an array of bytes. All more advanced com-
                             mands ultimately use send(). The more advanced commands are what will normally be used,
                             but sometimes they cannot do what you want. So instead you can pop down to the lowest level
                             and do it by hand.
                             The most basic command sends a single byte, say for instance the CLEAN command:
                             roombacomm.send((byte)RoombaComm.CLEAN);

                             The byte is sent immediately; no queuing is performed in the RoombaComm code (although
                             there may be OS-level queuing). The casting to byte is required because Java has no way to do
                             unsigned bytes. That is perhaps the most frustrating thing about Java when using it with an
                             embedded computer system like a robot.

                             In the code samples, remember from the end of Chapters 3 and 4 that roombacomm is a
                             roombacomm.RoombaComm object that’s been initialized and connected to a serial port and
                             Roomba. All the functions described in the next few chapters are in RoombaComm (or the subclass
                             RoombaCommSerial for serial port specific functions).

                             The more commonly used variant of send() takes an array of bytes. For example, to send the
                             byte sequence that starts the Roomba spinning in place to the right:
                             byte cmd[] = {(byte)RoombaComm.DRIVE,(byte)0x01,(byte)0xf4
                                                                     (byte)0xff,(byte)0xff};
                             roombacomm.send(cmd);
                             Repeating the above to do a large sequence of movements would get really tiresome, so you will
                             create a better way.

                             The constants CLEAN, DRIVE, and many others are in the RoombaComm class to assist with
                             these kinds of tasks. Look in roombacomm/RoombaComm.java or read the Javadocs to see
                             them all.



                             Moving with the drive() and …At() Commands

                             A first step would be to encapsulate the above DRIVE command into a method like in
                             Listing 5-1.
   112   113   114   115   116   117   118   119   120   121   122