Page 141 - Hacking Roomba
P. 141

122       Part I — Interfacing




                             With the updateSensors() manner of observing the world, there’s always going to be big
                             chunks of time where your program just sits and waits. For example, in the loop:
                             while( !done ) {
                                roombacomm.pause(1000);
                                roombacomm.updateSensors();
                             }
                             the actual time through the loop varies between 1050 milliseconds and 2000 milliseconds,
                             depending on whether or not the Roomba is plugged in. At the expense of having little com-
                             plexity, you can have an optimally responsive loop that is able to do both things not related to
                             Roomba and Roomba sensor-related things as soon as the sensor data is available. Listing 6-5
                             shows an example of such a loop.


                               Listing 6-5: Non-blocking Periodic Sensor Reading

                               int ts = 1000;
                               int t1 = System.currentTimeMillis();
                               roombacomm.sensors();
                               while( !done ) {
                                 t2 = System.currentTimeMillis();
                                 if( (t2 - t1) > ts && roombacomm.sensorsValid() ) {
                                     actOnSensors();
                                     roombacomm.sensors();
                                     t1 = t2;
                                 }
                                 doNonSensorThings();
                               }




                             The variable ts defines the time interval that sensors should be checked. Variables t1
                             and t2 are used to measure when ts has occurred. Every time through the loop, it will
                             execute doNonSensorThings(). But if ts amount of time has happened, it will execute
                             actOnSensors(), resend the sensors command, and reset the time pointers. Just to be
                             sure, before acting on the sensors, it makes sure that sensors are valid.
                             This non-blocking will be used in Chapter 7 as part of the RoombaView application.


                     BumpTurn: Making an Autonomous Roomba


                             Armed with the techniques from above, you can now start creating some programs that make
                             an autonomous Roomba. By reading sensors, you can make Roomba know about its environ-
                             ment, and by commanding its motors, you can make it move around. Listing 6-6 shows the
                             entirety of the main() method for BumpTurn.java, a program that turns Roomba into a
                             simple object-avoidance robot. (The keyPressed() method isn’t shown in the listing but
                             implements a Java-standard way of detecting a keypress.)
   136   137   138   139   140   141   142   143   144   145   146