Page 120 - Hacking Roomba
P. 120

Chapter 5 — Driving Roomba              101



                             of time, and then stops. What is this speed value though? That’s easy. Define a few more
                             methods.
                             public void setSpeed( int s ) { speed = Math.abs(s); }
                             public int  getSpeed() { return speed; }

                             Now each Roomba has an internal speed that the distance-based At commands will work with.
                             You don’t often change the speed of the Roomba, so specifying every function is wasteful.
                             In use, the preceding new methods would be used like this:
                             roombacomm.setSpeed(500);   // 500 mm/sec, fastest possible
                             roombacomm.goForward(1000); // forward 1 meter = ~39 inches
                             roombacomm.goBackward(254); // backward 254 mm = ~10 inches
                             Rotating Specific Angles

                             So now you can move Roomba forward and backward specific distances. What about turns?
                             In order to determine how much the Roomba has rotated through a spinLeftAt() and
                             spinRightAt() command, you need to remember that when spinning in place, the velocity
                             parameter to the DRIVE command becomes the velocity of the wheels along the circumference
                             of a circle. That velocity is in mm/s and you need degrees/s. To convert it, you need to figure
                             out how many millimeters per degree in the circle the Roomba is moving around:
                             mm/degree = (258 mm × pi) / 360 = 2.2515 mm/degree
                             Then to get angular speed from Roomba speed in mm/s, you use this formula:
                             angular_speed in degrees/s = speed in mm/s / (mm/degree)

                             To figure out how long to wait to spin a specific number of degrees, you flip the equation
                             around to solve for seconds for a given Roomba speed and angle you want:
                             wait time = (mm/degree) × (angle/speed)
                             For example, to do a 180 degree spin at a speed of 300 mm/s, you spin for (2.2515) ×
                             (180/300) = 1.35 seconds.
                             In Java, these equations are implemented for spinRight() and spinLeft() as in Listing 5-3.


                               Listing 5-3: RoombaComm.spinRight() and spinLeft()

                               int wheelbase = 258;
                               float millimetersPerDegree = (float)(wheelbase * Math.PI /
                               360.0);

                               public void spinRight( int angle ) {
                                 if( angle < 0 ) return;
                                 float pausetime = Math.abs(millimetersPerDegree *
                               angle/speed);
                                 spinRightAt( Math.abs(speed) );
                                                                                              Continued
   115   116   117   118   119   120   121   122   123   124   125