Page 123 - Hacking Roomba
P. 123

104       Part I — Interfacing




                             You can test this out with the roombacomm.Waggle test program.

                             By far the most interesting curve the Roomba does is the expanding spiral, usually when it first
                             starts up. You can implement a spiral by keeping a fixed velocity and incrementing the radius
                             value over time. In the example program Spiral.java, you can see one way to go about this.
                             Listing 5-5 shows the important parts of Spiral.java.


                               Listing 5-5: Important Parts of Spiral.java

                               int radius = 10;
                               int radius_inc = 20;
                               int pausetime = 500;
                               int speed = 250;
                               while( !done ) {
                                 roombacomm.drive(speed,radius);
                                 radius += radius_inc;
                                 roombacomm.pause(pausetime);
                               }



                             An initial radius is defined that is then incremented by radius_inc amount every
                             pausetime milliseconds.Try modifying Spiral.java to choose different values for those three
                             numbers. With the numbers in the listing, Roomba spirals out, but if you choose a large initial
                             radius and a negative radius_inc, the Roomba would spiral inward.


                     Real-Time Driving

                             The problem with the distance-based methods is the pause() method in them that causes
                             your program to wait (or block). Although it is paused, it can’t do anything else, such as check
                             for user input or read the Roomba’s sensors. A refinement you could add to RoombaComm
                             would be to implement a way to drive a certain distance without blocking the code. One way
                             to do this might be to put all Roomba control code in one thread and sensor reading or user-
                             interface checking in another thread.
                             However, if you want to drive your Roomba in real time, you don’t necessarily need to drive
                             specific distances or angles. By using the simplest methods like goForward(), spinLeft(),
                             and spinRight(), coupled with stop(), you can create a program to drive the Roomba
                             from your computer.
                             In Java the easiest way to respond to single key presses is to write a GUI program. Java makes it
                             quite easy to build GUI programs with buttons and windows and such. The toolkit one gener-
                             ally uses to make GUI Java programs is called Swing and is part of Java. Normally these Swing
                             GUI elements handle mouse clicks and key presses for you, but you can override that function-
                             ality and have your own program listen to key presses. This is done by making your code’s class
                             implement a KeyListener and having a method called keyPressed().
   118   119   120   121   122   123   124   125   126   127   128