Page 142 - Hacking Roomba
P. 142

Chapter 6 — Reading the Roomba Sensors                 123



                             Compile and run this program and watch the Roomba wander around the room. It displays an
                             amazing amount of agility and is able to get out of tight corners with ease. The code has only
                             three actions:

                                 Go forward.
                                 If bumped on the left, turn 90 degrees to the right.
                                 If bumped on the right, turn 90 degrees to the left.

                             It’s amazing that a robot with only three directives can be so agile. The Roomba bumper design
                             assists in making code simpler. This sounds strange, but observe how Roomba reacts when it
                             bumps things and see how even bumps along the side trigger the bumper. The bumper is sensi-
                             tive on approximately 160 degrees of its circumference. That’s over 44 percent of its body that
                             is responsive to pressure.
                             Try modifying BumpTurn to do different things when bumped, or try using different sensors.
                             Note that because the Roomba is in safe mode it will automatically go into a safety fault (turn-
                             ing off all motors) if a cliff or wheel drop is detected. If you want to get around that, you need
                             to switch Roomba to full mode by adding the line roombacomm.full() after the pause. Of
                             course, if you do this be sure to keep the Roomba away from stairs or other possible drops!


                               Listing 6-6: The main of BumpTurn.java

                               String portname = “/dev/cu.KeySerial1”;
                               RoombaCommSerial roombacomm = new RoombaCommSerial();
                               if( ! roombacomm.connect(portname) ) {
                                 System.out.println(“Couldn’t connect to “+portname);
                                 System.exit(1);
                               }
                               roombacomm.startup();
                               roombacomm.control();
                               roombacomm.pause(100);
                               roombacomm.updateSensors();
                               boolean done = false;
                               while( !done ) {
                                 if( roombacomm.bumpLeft() ) {
                                   roombacomm.spinRight(90);
                                 }
                                 else if( roombacomm.bumpRight() ) {
                                   roombacomm.spinLeft(90);
                                 }
                                 else if( roombacomm.wall() ) {
                                   roombacomm.playNote(72,10);  // beep!
                                 }
                                 roombacomm.goForward();
                                 roombacomm.updateSensors();
                                 done = keyIsPressed();
                               }
                               roombacomm.stop();
                               roombacomm.disconnect();
   137   138   139   140   141   142   143   144   145   146   147