Page 103 - Hacking Roomba
P. 103

84       Part I — Interfacing




                     Making RoombaComm


                             RoombaComm is a Java API library you’ll be creating to talk to Roombas. It will contain a
                             large number of functions to make talking to the Roomba easy. It will also have a bunch of
                             higher-level commands to let you do hard things simply. It wraps the ROI protocol and allows
                             you to program the Roomba without needing to know hexadecimal, bit manipulation, or all
                             the details of the ROI. These details will be discussed in the next few chapters as you build
                             up RoombaComm. To get started using it right away, you can download the latest version of
                             RoombaComm from http://roombahacking.com/.
                             RoombaComm is open source, a continual work in progress by several people, and commu-
                             nity support is appreciated. So far RoombaComm has been run successfully on Mac OS X,
                             Windows 2000/XP, and Linux on desktop computers and embedded systems. If you like, you
                             can add new functionality to RoombaComm to become part of the official release.
                             The basic RoombaComm idea was introduced in Chapter 2. In this chapter you’ll begin actu-
                             ally writing RoombaComm by creating the foundation methods to communicate with any
                             Roomba over a serial port (RS232 or Bluetooth).

                             Code Structure

                             Java requires all code to be in a package. The package is a namespace for all code with a similar
                             concept. The Java package name for all RoombaComm code can be just roombacomm. The
                             library then can consist of a base class called roombacomm.RoombaComm. In this base class you
                             can put all the communication protocol-independent functions. You can then make subclasses
                             of it, like roombacomm.RoombaCommSerial and roombacomm.RoombaCommTCPClient, to
                             implement the additional functionality needed to talk to a Roomba over a serial cable or a net-
                             work cable, respectively.
                             In the roombacomm.RoombaComm base class, you can translate the entire ROI specification
                             into Java code. Such methods as startup(), control(), and drive() can map directly to
                             the START, CONTROL, and DRIVE ROI commands. Then you write Java code as if they
                             were ROI commands like this:
                             RoombaComm roombacomm = new RoombaComm();
                             roombacomm.startup();
                             roombacomm.control();
                             roombacomm.drive(0x8000,0x0200);

                             With even just a few ROI commands implemented and a communication-specific subclass, an
                             infinite number of programs can be created to interact with Roomba in all sorts of fun ways.
                             The RoombaCommTest GUI program from the previous chapter is one simple example. You
                             can create many others. But you first must get Java to talk to serial ports.

                             RXTX Serial Port Library

                             Most of the projects here that use RoombaComm will use the RoombaCommSerial class to
                             connect to the Roomba over a serial port. Java doesn’t have a way to talk to serial ports so an
                             external library is needed.
   98   99   100   101   102   103   104   105   106   107   108