193 Chapter 10 — Using Roomba as an Input DeviceFigure 10-2 shows how you might hold Roomba and draw with it, while Figure 10-3 shows a drawing made with RoombaSketch.. Granted, as Figur
Trang 1The implementation is straightforward; the main hurdle is conceptual as you’re using Roombadriving data without commanding the robot to move.
Instead of simply drawing lines as pixels onto the screen, an array of Lineobjects is created.Each Lineholds 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 Eachpress and release of the Spot button creates a new Lineobject and thus a new line to be drawn
Listing 10-1: RoombaSketchLine[] lines = new Line[numlines];
int l = 0;
int strokeW = 5;
void draw() {computeRoombaLocation(); // same as beforeparseRoombaSensors();
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();
Trang 2193 Chapter 10 — Using Roomba as an Input Device
Figure 10-2 shows how you might hold Roomba and draw with it, while Figure 10-3 shows a
drawing made with RoombaSketch The ability to change the pen stroke width while drawing
enables a much more fluid line than is possible with a normal mouse You can create very
organic drawings Granted, as Figure 10-2 shows, using Roomba as a mouse requires a bit
more physical movement than with a normal mouse, but with some people complaining that
computer users don’t get enough exercise, you can now point to the Roomba and say, “That’s
my mouse.”
F IGURE 10-2: Using Roomba as a mouse
Trang 3F IGURE 10-3: A drawing made with RoombaSketch
Using Roomba as a Theremin
The theremin is a strange and unique musical instrument It’s played without even touching it;placing your hands in front of it in particular ways adjusts its pitch and loudness The BeachBoys song “Good Vibrations,” old Star Trek episodes, and countless horror movies have usedthe theremin to good effect, so you have undoubtedly heard it It produces a clear pure sinewave tone that glides between notes and sounds vaguely human-like
Invented in 1919 by Leon Theremin, the theremin was the result of research into electrostaticproximity sensors Figure 10-4 shows the inventor playing his instrument The theremin con-sists of two antennae, one for controlling pitch and one for controlling volume The pitch antenna
is usually vertical and on the right-hand side of the player The circuitry inside the thereminmeasures the varying capacitance between the player’s body and the antennae and adjusts theloudness and pitch accordingly If you’ve ever used your body to get better reception on your
TV, you’ve experienced how the theremin works
Making a Theremin Out of Roomba
The two Roomba dirt sensors are apparently capacitive-based and thus a likely candidate to use
as a theremin input Unfortunately, they do not seem to be tuned to picking up human-sizedvariations But the other downward-facing sensors offer an alternative for the pitch control By
Trang 4195 Chapter 10 — Using Roomba as an Input Device
tilting Roomba left and right you can get a few bits of resolution by combining the cliff sensors
with the wheeldrop sensors Figure 10-5 shows how Roomba can be used to detect variations
in tilt The scale of the tilt is a bit exaggerated in the diagram to demonstrate the effect Since
the theremin glides from one pitch to the next, having relative pitch adjustments via tilting
actually works pretty well
F IGURE 10-4: Leon Theremin
and his musical instrument
F IGURE 10-5: Tilting Roomba for pitch control
No sensors triggered Pitch change = 0
Left cliff sensor triggered Pitch change = +1
Left cliff & front left cliff sensors triggered Pitch change = +2
Left cliff & front left cliff & left wheeldrop triggered Pitch change = +3
Trang 5Listing 10-2 shows the core of a Processing sketch called RoombaTheremin As before, the
draw()method calls parseRoombaSensors()and draws a growing vertical line showingpitch change Figure 10-6 shows what the sketch looks like after running for a while and per-forming with it Figure 10-7 shows the typical way you hold and use the robot when playing itwith RoombaTheremin
Listing 10-2: RoombaThereminint pitch = 70;
int dur = 6;
int pshift = 0;
void draw() {parseRoombaSensors();
line( x,y, x,y+2);
}void parseRoombaSensors() {if( roombacomm.cliffLeft() ) pshift++;
if( roombacomm.cliffFrontLeft() ) pshift++;
if( roombacomm.cliffRight() ) pshift ;if( roombacomm.cliffFrontRight() ) pshift ;if( roombacomm.wheelDropLeft() ) pshift++;
if( roombacomm.wheelDropRight() ) pshift ;if( roombacomm.wheelDropLeft() &&
Trang 6197 Chapter 10 — Using Roomba as an Input Device
F IGURE 10-6: RoombaTheremin tracking tilt changes to modify pitch
F IGURE 10-7: The three main positions when performing with RoombaTheremin
Better Sound with the Ess Library
The necessary time delays between each note sent to Roomba makes for a less-than-convincing
theremin simulation Instead of a smooth tone characteristic of a theremin, the best that can be
done on Roomba is a repetitive beep-beep-beep that is limited by the time it takes the robot to
execute the SONGand PLAYcommands
An optional library for Processing called Ess makes it really easy to load, play, and manipulate
sounds on your computer It is built on JavaSound (the standard Java sound API), but is much
Trang 7easier to use than JavaSound normally is For example, the following snippet is a completeProcessing sketch that loads an MP3 file, pitch shifts it up five semitones, and plays it.
Ess is available at www.tree-axis.com/Ess/and, like any Processing library, is installed byunzipping its downloaded zip file into the librariesfolder of the main Processing applica-tion After restarting Processing, Ess is visible when you use the Sketch ➪ Import Library menucommand
Touchless Sensing
Another way of triggering pitch changes that is perhaps more theremin-like is to flip Roombaover and use the cliff sensors as hand proximity sensors Figure 10-8 shows how this works Byholding your hand over one or more of the cliff sensors, you un-detect a cliff The cliff sensors
are barrier sensors just like the wall sensor, but its logical sense is flipped and it’s called a cliff
sensor instead of a floor sensor It’s more logical (and marketable) to say cliff sensor instead of
floor sensor
But flipped over and used as a theremin, these cliff sensors work well detecting hands, as long
as your hand has its fingers together and is mostly parallel to the surface of Roomba Listing 10-3shows a variation of the previous code, called RoombaThereminToo It includes the Ess librarymethod of making sound and has a modified version of parseRoombaSensors()to deal
with the fact that it should change pitch when not detecting a cliff The other features of that
method are that by pressing both wheels down, it mutes the audio Pressing the bump sensorstarts the audio back up again
You’ve probably noticed that parseRoombaSensors()doesn’t actually change the pitch Itsets the pshiftvariable but doesn’t act on it That’s because changing the pitch of a soundwhile it’s playing causes an audible glitch By waiting for the sound to finish playing throughand changing its pitch before it loops again, the pitch transitions are smooth In Ess, if themethod channelLoop()is declared in your sketch, it will be called when a sound is finishedplaying By adding that method and a check to see if pshiftis actually set, the pitch (actu-ally the ratehere since it’s a faster computation and yields the same result) can be changedseamlessly
Trang 8199 Chapter 10 — Using Roomba as an Input Device
F IGURE 10-8: Alternate playing method, used in RoombaThereminToo
Listing 10-3: RoombaThereminToo, Using Ess and Hand-Waving
if( !roombacomm.cliffLeft() ) pshift++;
if( !roombacomm.cliffFrontLeft() ) pshift++;
if( !roombacomm.cliffRight() ) pshift ;
if( !roombacomm.cliffFrontRight() ) pshift ;
if( !roombacomm.wheelDropLeft() ) pshift++;
if( !roombacomm.wheelDropRight() ) pshift ;
if( !roombacomm.wheelDropLeft() &&
Trang 9Turning Roomba into an Alarm Clock
Roomba is now so familiar that it’s almost like a pet Why not have it sleep at your feet like afaithful dog? Except unlike a dog, with Roomba you can set the exact time when it will wakeyou up And you can even make it turn on the radio for you
Listing 10-4 shows the basics of the Processing sketch RoombAlarmClock, an alarm clockimplemented on Roomba It has the following features:
䡲 Alarm: Beep and make noise at a particular wakeup time.
䡲 Snooze button: Must hit both the left and light bump sensors.
䡲 Turn off alarm: Pick up Roomba and press the Power button.
䡲 Turn on or off radio on iTunes: Press the Clean button to play or pause.
The alarm and snooze times are determined by using Java Dateand DateFormatobjects.Alter the initial time for the alarm to go off (wakeupTime) and the length of the snooze(snoozeSecs) and run the sketch to start the alarm When the alarm goes off, the method
playAlarm()is called, which plays a tune and vibrates Roomba back-and-forth Pressingboth bumpers snoozes the alarm, while picking up Roomba and pressing the Power buttonturns off the alarm until the next day
The radio is turned on and off with the runRadioCmd()method This method uses
Runtime.exec()to execute a system command The particular command shown uses
osascript, the Mac OS X command-line tool for running Applescript statements TheApplescript statement tell app “iTunes” to playpausewill tell iTunes to play if it’spaused or pause if it’s playing So before using RoombAlarmClock, set iTunes to the playlist
or Internet radio station you’d like to wake up with Then, when it’s running, at any time pressthe Clean button to turn on the radio
For other operating systems, change the radioCmdvariable to any command-line commandthat you fancy You could even add additional commands that are triggered for the other but-tons or sensors Perhaps the Max button runs a command that talks to an X-10 controller toturn on the lights If you encapsulate these various commands into a shell script (or batch file
on Windows), you can change it all you want without re-exporting the sketch in Processing
In Chapter 13 you’ll learn how to make Roomba fully autonomous When fully stand-alone,having it be an alarm clock would be even more interesting For example, it could run awayfrom you as you try to turn off the alarm
If you use RoombAlarmClock to wake you up, don’t put Roomba on your nightstand table Itmay very well fall off Roomba is quite sturdy, but why take the chance? Place Roomba on thefloor
Trang 10201 Chapter 10 — Using Roomba as an Input Device
You probably want to use the simpler serial tether rather than the Bluetooth adapter for the
alarm clock Some computers may power down their Bluetooth interface if it’s idle Regular serial
ports don’t have that problem
Listing 10-4: RoombAlarmClock
String wakeupTime = “6/20/06 07:48 am”;
Date wakeupDate = null;
boolean alarm = false;
int snoozeSecs = 60 * 9; // nine minutes
String radioCmd[] = {“osascript”, “-e”,
“tell app \”iTunes\” to playpause”};
try { wakeupDate = df.parse(wakeupTime); }
catch( Exception e ) { println(“error:”+e); }
Date now = new Date();
if( now.compareTo( wakeupDate ) > 0 )
roombacomm.createSong(5,song);
roombacomm.playSong(5); // play rude song
for( int i=0; i<5; i++ ) { // and shudder a little
Trang 11Listing 10-4 Continued
Process p = Runtime.getRuntime().exec(radioCmd);
p.waitFor();
} catch(Exception e) { println(“exception:”+e); }}
void parseRoombaSensors() {if( alarm ) {
if( roombacomm.bumpLeft() &&
roombacomm.bumpRight() ) { // snoozewakeupDate.setTime(wakeupDate.getTime()+snoozeSecs*1000);
println(“snooze until “+wakeupDate);
alarm = false;
}else if( roombacomm.wheelDropLeft() &&
roombacomm.wheelDropRight() &&
roombacomm.wheelDropCenter() &&
roombacomm.powerButton() ) {println(“alarm off! (until tomorrow)”);
wakeupDate.setTime(wakeupDate.getTime() +(60*60*24)*1000);
alarm = false;
}}if( roombacomm.cleanButton() )runRadioCmd();
}
Summary
Even though the Roomba’s sensors are primitive, they can be put to some interesting uses.These uses need not be vacuum-related or even robotics-related The cliff sensors are one ofthe best examples of this, becoming non-contact proximity sensors for hands or other movableobjects when Roomba is turned upside down While upside down, the wheeldrop sensors becomebuttons to trigger actions You could even turn the wheels when upside down and register themovement as a variable function
By combining the sensors into more complex aggregations, you can create a novel way to ure something Roomba normally cannot measure Combining cliff and wheeldrop sensors gives
meas-a sense of tilt, but you cmeas-an immeas-agine other combinmeas-ations meas-as well Perhmeas-aps meas-a combinmeas-ation of tons and driving motor over-current sensor could make a weight sensor and turn Roomba into
but-a scbut-ale Or using the distbut-ance but-and but-angle sensors but-and but-a little cbut-alibrbut-ation could yield but-a Roombbut-ayardstick Try making Roomba a DJ input device and “scratch” audio files with it by rotating itback and forth like a record The number of possible sensor combinations is huge and many ofthem produce useful results Even those that produce non-optimal results are fun and instructive
Trang 12Chapter 16
Other Projects
part
in this part
Trang 14Connecting Roomba
to the Internet
The objects in our homes are becoming smart Not just the obvious
ones like the TV and stereo, but also the more mundane ones like the
stove and vacuum cleaner The “smart home” movement of a decade
ago with its centralized house computer is giving way to the emergent
phe-nomena of all the little parts of our homes becoming smart
If you have a Roomba robotic vacuum cleaner, you’re already aware of this
Roomba has more computing power than large corporations could afford in
the 1960s Imagine what bits of disposable computing will be present in our
everyday devices a few decades from now
A smart object is useful, but as anyone who has used the Internet can attest
to, connecting smart objects with each other leads to entirely new and
higher-level interactions This network effect characteristic is so important,
it’s considered a field of study in and of itself The effect has long been
recognized (no one will go to a stock exchange with only a few traders, and
a phone company with 10 users isn’t nearly as useful as one with 10,000
users), but it took computers and the Internet to bring it into sharp focus
Network effects can apply to any aspect of a group that becomes more
effi-cient or useful when a higher percentage of the group participates
We do not yet live in a world where a large percentage of the objects in our
lives are smart and networked, but we are on the brink Often with network
effects, once a critical mass of participants has been reached, all others in
the group quickly follow suit A recent example of this is e-mail: Before
1990 it seemed like no one had it, and then just a few years later suddenly
everyone did
Right now, only certain household objects are smart, and even fewer are
networked Recent advances in networking allow even the simplest smart
device to communicate with others and to do it cheaply This chapter
dis-cusses two of those types of devices that enable networking via Ethernet
Why Ethernet?
Both RS-232 and Bluetooth turn Roomba into a kind of networked object,
but a subservient one Objects communicating through simple serial
proto-cols like those two require a computer translator to convert between the
TCP/IP protocol used on the Internet and the simpler serial port protocol
About Ethernet
Choosing the right embedded Net device
Building an Ethernet adapter
Using the SitePlayer Telnet
Using the Lantronix XPort
Updating RoombaComm for network use
chapter
in this chapter
Trang 15The object is beholden to a single computer and cannot function without it Ethernet is thesimplest and most pervasive physical protocol that can handle TCP/IP as a peer, rather thanbeing subservient to a larger computer However, even though it’s the simplest, dealing withEthernet and TCP/IP requires a great deal more processing power than a simple serial port.
䡲 Ethernet breaks all transmission streams into frames, a kind of data packet.
䡲 Ethernet supports multiple devices on the same cable
The packetization of data was an important and crucial step in the creation of the Internet.Before packets, computers were connected with serial cables or phone lines These dedicatedcircuits joined just two computers together If you wanted your computer to talk to anothercomputer, you had to get another serial port or phone line It wasn’t a very scalable design, soEthernet was created to allow multiple computers to use the same wire In order to keep onecomputer from monopolizing the wire, data transmission was divided into frames and sent oneafter the other Between frames other computers could interrupt to transmit their own data.Ethernet is a physical layer protocol and thus is only valid on a local area network (LAN) Tocommunicate between LANs, a higher-level protocol is needed, and the one everyone uses now
is the Internet Protocol (IP) IP provides a common, open language for all networked devicesand is the reason why the Internet works If you then want to recreate a virtual direct connec-tion between two computers on the Internet, you can use the Transmission Control Protocol(TCP), which exists on top of IP the way IP does on top of Ethernet TCP and IP are so com-monly used together that they’re often called TCP/IP TCP/IP doesn’t require Ethernet towork In fact, there are many alternatives to Ethernet The most common alternative available
to consumers is Wi-Fi (also known as 802.11b/g)
To create a device that exists on the Internet like any other, you need to implement TCP/IPand Ethernet (if using Ethernet) These are complex protocols, not something you can quicklywhip up by hand For Ethernet, there are several chips that provide that protocol’s functional-
ity For TCP/IP, there are several tiny stacks (collections of interlocking software) that fit within
modern microcontrollers and talk to these Ethernet chips
In the open-source realm, one of the most popular TCP/IP stacks is uIP available from
www.sics.se/~adam/uip/ This is a great stack that has been ported to many different types of microcontrollers The downside of rolling your own embedded TCP/IP system is that the Ethernet chips are all surface-mounting and hard to solder for a hobbyist
Fortunately, several companies have done the hard work of putting together an Ethernet chip,
a microcontroller, and a TCP/IP stack and making it all function To the user of these devices,