Page 106 - Hacking Roomba
P. 106
Chapter 4 — Building a Roomba Bluetooth Interface 87
Listing 4-1 Continued
success = true;
}
}
}
}
} catch(Exception e) {
logmsg(“connect failed: “+e);
port = null;
input = null;
output = null;
}
return success;
}
public boolean send(int b) { // will also cover char or byte
try {
output.write(b & 0xff); // for good measure do the &
} catch (Exception e) { // null pointer or serial port dead
errorMessage(“send”, e);
return false;
}
return true;
}
public void startup() {
send(START);
}
public void control() {
send(CONTROL);
}
public void pause(int millis) {
try { Thread.sleep(millis); } catch(Exception e) { }
}
// Roomba ROI opcodes
// these should all be bytes, but Java bytes are signed
public static final int START = 128; // 0 bytes of data
public static final int BAUD = 129; // 1 byte of data
public static final int CONTROL = 130; // 0
// ...
The send() command takes the output stream obtained from connect and tries to send a
byte out it. The startup() and control() commands use send() to send the appropriate
ROI byte, using an easy-to-remember alias instead of the raw byte values. Finally, the pause()
command implements a simple pause function via the standard Java mechanism of Thread
.sleep().
The code in the listing puts the Roomba in safe mode ready for more ROI commands, but
doesn’t yet make the Roomba move. In Chapter 5, you’ll add to RoombaComm enough to
allow you to drive around the Roomba.