Page 119 - Hacking Roomba
P. 119
100 Part I — Interfacing
Once you have drive(), building new commands from it comes naturally:
public void spinLeftAt(int aspeed) {
drive(aspeed, 1);
}
public void spinRightAt(int aspeed) {
drive(aspeed,-1);
}
public void goStraightAt(int velocity) {
drive(velocity,0x8000);
}
These three methods abstract away the three magic values of radius. In RoombaComm the
convection of ending a method with At indicates its first argument is a speed or velocity. And
the example DRIVE command now simply becomes:
roombacomm.spinRightAt( 500 );
To make Roomba stop at any time, issue the roombacomm.stop() command or physically lift
up the Roomba to trigger its safety feature.
Moving Specific Distances
Due to how the ROI works, all of these commands start the action going but do not say when
it stops. After issuing the spinRightAt() command in the preceding section, Roomba begins
to spin around and around and nothing stops it, even if you exit the program. If you want to go
straight for a certain distance or spin left a given angle, you issue the command at a particular
speed and then wait for a known time. For example, to go forward 300 mm at 100 mm/s, wait
three seconds and stop. The following three methods generalize that concept, for generally
moving in straight lines and going forward or backward:
public void goStraight( int distance ) {
float pausetime = Math.abs(distance/speed); // mm/(mm/sec) = sec
goStraightAt( speed );
pause( (int)(pausetime*1000) );
stop();
}
public void goForward( int distance ) {
if( distance<0 ) return;
goStraight( distance );
}
public void goBackward( int distance ) {
if( distance<0 ) return;
goStraight( -distance );
}
The goStraight() method computes how long to wait based on how fast and how far the
Roomba is supposed to go. It starts the Roomba up at a speed, waits for the right amount