Page 211 - Hacking Roomba
P. 211
192 Part II — Fun Things to Do
The implementation is straightforward; the main hurdle is conceptual as you’re using Roomba
driving data without commanding the robot to move.
Instead of simply drawing lines as pixels onto the screen, an array of Line objects is created.
Each Line holds an array of points that define the line. Each time draw() is called (deter-
mined by framerate), the current line is added if the Spot button is being held down. Each
press and release of the Spot button creates a new Line object and thus a new line to be drawn.
Listing 10-1: RoombaSketch
Line[] lines = new Line[numlines];
int l = 0;
int strokeW = 5;
void draw() {
computeRoombaLocation(); // same as before
parseRoombaSensors();
updateRoombaState();
background(180); stroke(0);
for( int i=0; i<numlines; i++ )
lines[i].draw();
translate(rx,ry);
rotate(rangle);
image(rpic,-20,-20);
}
void parseRoombaSensors() {
if( roombacomm.powerButton() ) {
roombacomm.disconnect();
System.exit(0);
}
if( roombacomm.cleanButton() ) {
rx = width/2; ry = height/2;
rangle = 0;
strokeW = 5;
}
if( roombacomm.bumpLeft() ) {
strokeW -- ; if( strokeW<1 ) strokeW=1;
}
if( roombacomm.bumpRight() ) {
strokeW++; if( strokeW>100 ) strokeW=100;
}
if( roombacomm.spotButton() ) {
if( drawing ) {
if( rx != rxo && ry != ryo )
lines[l].addPoint((int)rx,(int)ry,strokeW);
}
else {
drawing = true;
l++; l %= numlines;
lines[l] = new Line();