Page 167 - Hacking Roomba
P. 167
148 Part II — Fun Things to Do
Listing 7-6: Computing Roomba On-Screen Position
float rx,ry,rangle; // roomba position and angle
float scalex = 0.4; // pixels per mm, essentially
float scaley = 0.4;
void computeRoombaLocation() {
int distance = roombacomm.distance();
float angle = roombacomm.angleInRadians();
rangle = rangle - angle ;
rangle %= TWO_PI;
float dx = distance * sin(rangle);
float dy = distance * cos(rangle);
rx = rx + (dx * scaley);
ry = ry - (dy * scalex);
// torroidial mapping
if( rx > width ) rx = 0;
if( rx < 0 ) rx = width;
if( ry > height ) ry = 0;
if( ry < 0 ) ry = height;
}
Displaying Status
Some of the important Roomba sensor data won’t fit on the Roomba icon. And displaying
connection status would be difficult as an icon. So on the top of the screen, put a few lines of
text that show the connection status, the current ROI mode, and the battery condition.
Listing 7-7 shows that status bar. It computes battery charge as an easy-to-read percentage as
well as showing you the instantaneous current draw on the battery so you can see what
Roomba actions draw more current than others.
Listing 7-7: Displaying Roomba Status
void drawStatus() {
String status = “unknown”;
int batt_percent=0, batt_charge=0, batt_volts=0, batt_mA=0,
batt_cap=0;
if( roombacomm == null )
status = “not connected. no roomba. please restart.”;
else if( ! roombacomm.connected() )
status = “not connected. no roomba. please restart.”;
else if( ! roombacomm.sensorsValid() )
status = “connected. sensors invalid. unplugged?”;
else if( roombacomm.safetyFault() )