Page 145 - Hacking Roomba
P. 145

126       Part I — Interfacing




                     Spying on Roomba


                             With all these new sensor tools at your disposal, you can now create a program to spy on
                             Roomba as it goes about its business. Listing 6-7 shows the main() method of a simple
                             Spy.java program.


                               Listing 6-7: The main() Method of Spy.java

                               static int pausetime = 500;
                               public static void main(String[] args) {
                                 // standard RoombaComm setup
                                 boolean done = false;
                                   while( !done ) {
                                     roombacomm.updateSensors();
                                     printSensors(roombacomm);
                                     roombacomm.pause(pausetime);
                                     done = keyIsPressed();
                                   }
                               }
                               public static void printSensors(RoombaCommSerial rc) {
                                 System.out.println( System.currentTimeMillis() + “:”+
                                         “bump:” +
                                         (rc.bumpLeft()?”l”:”_”) +
                                         (rc.bumpRight()?”r”:”_”) +
                                         “ wheel:” +
                                         (rc.wheelDropLeft()  ?”l”:”_”) +
                                         (rc.wheelDropCenter()?”c”:”_”) +
                                         (rc.wheelDropLeft()  ?”r”:”_”) );
                               }




                             By now you know what all the bits do, and the result is pretty simple, with all the hard parts
                             hidden away. The following is what you might see if you ran the Spy program for a few sec-
                             onds, it bumped into you, and then you picked it up:
                             1148355917865:bump:_r wheel:___
                             1148355918417:bump:lr wheel:___
                             1148355918969:bump:__ wheel:___
                             1148355919529:bump:__ wheel:___
                             1148355920089:bump:l_ wheel:_c_
                             1148355920649:bump:__ wheel:___
                             1148355921202:bump:_r wheel:_c_
                             1148355921754:bump:_r wheel:_c_
                             1148355922306:bump:_r wheel:lcr
                             1148355922907:bump:_r wheel:lcr
                             1148355923459:bump:_r wheel:lcr
                             1148355924011:bump:_r wheel:lcr
   140   141   142   143   144   145   146   147   148   149   150