Command 140 lets you define a song by sending duration
and MIDI note numbers for a sequence of notes. Then, you
send command 141 to play one of the four stored songs.
Our robotics students who are in band love the music
system. We challenge them to enter
the school’s fight song from their
You read the robot’s sensor by
sending the Read Sensor command:
opcode 142 followed by the sensor
packet you want to read. You can
then read the data values from the
serial interface. There are around 50
different sensor packets you can read:
cliffs, walls, wheel encoder counts,
motor currents, etc.
There are several sensor groups
you can read. When you request a
sensor group, you get back the data
from multiple related sensors at once.
For instance, group 3 includes six
different readings for the battery:
charging state, voltage, current,
temperature, battery charge, and
The IO protocol is binary. You
can’t use a simple terminal to type
commands directly to the robot.
Instead, you need a code library that
converts parameters to data bytes as
needed by the robot.
The library must convert the
multiple bytes of sensor data into a
form your code can use. The library
is conceptually easy; it’s just a big mapping function from
code objects to the OI byte protocol. You can write the
library yourself. In fact, it’s a great learning exercise for your
robotics team. Or, you can use one of the many libraries
shared on the iRobot site. The community writes them in all
You can also get my own Python implementation of
the library from the article downloads. My library is heavily
documented with information from the Open Interface
spec so that users can read about the parameters without
having to open the spec itself. This is great for when you’re
working in an editor that pops up completion hints and
documentation as you type.
I created several code challenges for the students in
robotics clubs. You’ll find the GitHub URI with the Resources.
Clone the repo for yourself and submit tweaks and new
challenges back to the repo for all to share.
Each challenge has a README description of the
problem, and there’s a solutions directory for each challenge
(we are learning here — no sense being stuck). Each
challenge has a “badge” image the students can print and
proudly display when they complete
the challenge. Gamification keeps the
students motivated to learn.
Figure 4 shows my solution to
the simple “Out and Back” challenge.
The code gives you an idea of how
easy it is to write Python for the
First, the code connects to the
Roomba’s serial port. Then, it drives
straight for a couple of seconds, turns
around clockwise, and drives back. It
sounds easy, but the students learn
how frustrating it is to tune the times
and motor speeds for such a simple
task. They learn how different driving
surfaces affect the timing values and
how small errors along the way add
up to big errors at the end.
Other challenges combine reading
sensors and controlling motors to
make a reactive robot. The students
learn about wheel encoders and
counting pulses for precise distances.
These skills carry forward to the FIRST
robot they build later in the season.
The “What’s Up Dock” exercise is
particularly challenging. The students
write code to read the IR beams from
the base station and drive the robot
Figure 5. The web remote control has
two modes. In DRIVE mode, you press
the arrows to control the robot manually.
In PROGRAM mode, you drag the
arrows into a script of commands at the
bottom of the page.
Figure 4. My Python library lets you focus on your robot’s
algorithms. Simply create a robot object and script its
commands in friendly Python code.
40 SERVO 09/10.2018