This chapter shows a few ways to sever the connection with a controlling computer and instead replace the microcontroller brain of Roomba with one you can program.. microcontroller archi
Trang 1Listing 12-2 Continued
}else if( $cmd == ‘spinright’ ) {roomba_spin_right( $vel );
}else if( $cmd == ‘sensors’ ) {
$sensors = roomba_read_sensors();
$c = count($sensors);
for( $i=1; $i<=$c; $i++ ) {printf(“sensors[$i]: %x\n”, $sensors[$i]);}
}else if( $cmd == “drive” ) {
Figure 12-12 shows what the script looks like in the browser when run
F IGURE 12-12: Using the PHP Roomba program roombacmd.php
Trang 2As you can see, the Roomba functions seem very similar compared to the RoombaComm
func-tions The only strange statements are the pack()statements, like:
pack(“C*”, 137, $vhi,$vlo, $rhi,$rlo)
In PHP, in order to send raw bytes with no string or numeric translation (which is normally
what you want), you need to packthe bytes into a variable according to some packing rules In
the preceding statement,C*means to pack a series of unsigned bytes The asterisk means an
unknown number of bytes In the preceding case you could use C5since you’re packing five
bytes When receiving data from Roomba, you need to use a corresponding unpack()method
If you expand roombacmd.phpto more fully control Roomba, the first thing to do is to move
the Roomba functions into their own file to build a Roomba PHP library called, for example,
roombalib.php You can then use this separate file with include ‘roombalib.php’at the
top of your main PHP file
The HTML user interface is an example of using multiple Submit buttons for a single form
Each button sends a different value for cmd You could have just as easily created multiple links
and set the cmdquery argument yourself
Putting It All Together
Now that you have a working PHP page to control Roomba, you can run it anywhere you can
find a working LAMP system This includes virtually all web hosting services on the Internet
In order to run roombacmd.phpfrom one of them, you’ll need to poke a hole in your firewall
to allow computers outside your home network to connect
Most home routers have a special configuration page just for such changes Your home network
looks like a single IP address to the rest of the Internet, but that IP address can have many
dif-ferent ports What your router can do is translate one IP:port pair on the outside part of your
network to another IP:port pair on the inside of your network This is called port mapping or
port forwarding and is often used to allow online gaming or P2P file sharing to work, so you
may already have some port maps in place
For example, if your external IP address to the Internet (WAN IP address) is 12.34.56.78
and the IP address of your Roomba is 192.168.0.1, then you’d configure your router to map
port 10001 from one IP address to the other This can be written in documentation as
12.34.56.78:10001 -> 192.168.0.1:10001
If you don’t know your external WAN IP address, go to http://whatsmyip.org— you
should be able to see it
A final task is to make an enclosure for the WiMicro and carrier board Having an exposed
cir-cuit board looks cool but as the WiMicro is expensive it’s a good idea to protect it Any small
enclosure of the right size can be used Figure 12-13 shows one example using a small project
box Small cut-aways on the sides are made where the antenna and Roomba cable come out
Figure 12-14 shows the finished enclosure mounted on the Roomba
Trang 3F IGURE 12-13: Enclosure for WiMicro and carrier board
F IGURE 12-14: The completed Wi-Fi Roomba
Trang 4Going Further with LAMP
The M part of LAMP hasn’t been touched on yet The preceding PHP script shows the basics
of how to receive sensor data, but the script doesn’t do anything with the data besides printing
it With PHP and MySQL it’s pretty easy to record the sensor data to a database table With
enough sensor data you can do an analysis on it to figure out things like:
䡲 When is the house the dirtiest? Compare historical dirt sensor readings with time
and date
䡲 Approximately how big or cluttered is the living room? Average out time between
bump sensors to determine the mean free path of your rooms
䡲 How long are my walls? Analyze the wall sensor readings to find walls; then create a
histogram to find the different wall sizes
䡲 How long does it take for the dustbin to fill up? Average the time from when the
Roomba starts to when the vacuum motor over-current bit is set
Collect enough data, and you can determine a great many things about the environment within
which Roomba moves Roomba may not be a very accurate sensor platform, but its sensors
cover the same area again and again Good data can often be recovered from noise with repeated
measurements
Summary
A Wi-Fi Roomba coupled with a LAMP application is pretty powerful You can create not just
web pages to control the robot, but dynamic database-driven web pages Having the ability to
tie a database into the robot enables you to create for an entirely different level of interaction as
Roomba can now have a huge historical memory You could create complex robotic logic based
on this elephantine memory located on a server anywhere and remotely command one or
sev-eral Roombas simultaneously Or you could create a social networking site for Wi-Fi Roomba
users and collect real-time Roomba stats from all over the world
As the price of the Wi-Fi modules continues to fall due to cell phones and portable game
sys-tems having Wi-Fi, making a Wi-Fi Roomba will become even more attractive To make the
extra cost of the current devices more palatable, you can hook up the extra serial port offered to
the microcontrollers discussed in the next chapter Then you can get the best of both worlds:
localized computation backed with a huge storehouse of previous knowledge
Trang 6Giving Roomba
a New Brain
and Senses
The projects presented so far have all been based on having the new
brains of Roomba located remotely to it From an RS-232 serial
tether to a Wi-Fi connection, in all cases, Roomba is more of a
tele-presence device than a true robot The term robot has come to have a flexible
definition with the rising popularity of various telepresence/remote-control
devices The most egregious use of robot to mean remote-controlled machine
are the battle bot programs on television The contestants are billed as robots,
but they’re all controlled by humans through radio-control transmitters
Perhaps in a few years we’ll have true robot battle shows where the machines
are programmed, given a strategy, and let loose to fend for themselves
This chapter shows a few ways to sever the connection with a controlling
computer and instead replace the microcontroller brain of Roomba with one
you can program You’ll turn your Roomba into a truly autonomous
re-programmable robot Like all projects in this book, this chapter doesn’t harm
Roomba and its original brain is available when you remove the new one
you create
In theory you could reprogram the microcontroller chip inside the Roomba,
as that’s what the OSMO//hacker device does to upgrade a Roomba’s
firmware iRobot has yet to release specifications on the protocol used to
upgrade firmware They likely never will Allowing anyone to re-program
Roomba is a sure way to brick it, resulting in unhappy customers There has
been some effort in reverse engineering the upgrade protocol, but with the
release of the ROI it’s become less of an issue, since it addresses most of
what was wanted once you add your own microcontroller that speaks the
ROI protocol
Microcontroller vs Microprocessor
A microcontroller is simply a small, integrated computer system designed
for a particular purpose Microcontrollers are pervasive in your life, yet almost
totally invisible You may be unfamiliar with the term and never realized
there is a substantial difference between the computer in your laptop and
the one in your coffee maker
Understand microcontrollers
Build circuits without soldering
Make a stand-alone robot out of
Build a mobile mood light
chapter
in this chapter
Trang 7In the 1970s, when computers started to become small and cheap enough for everyday use,engineers realized that the modular architecture of microprocessor computer systems was toocomplex and cumbersome for designs that didn’t need all the processing power and input/out-put of a normal computer There were many tasks that needed computation but didn’t need apowerful microprocessor, let alone a screen, keyboard, or printer So smaller, less powerfulCPUs were joined with a few simple I/O devices onto a single chip These new creations werecalled microcontrollers.
Figure 13-1 is a diagrammatic view of the difference between a normal computer system and amicrocontroller system In a standard computer setup, different physical chips provided differentfunctions: computation, memory, timing, input, and output These separate devices were joinedtogether by a collection of wires called a bus The modern PCI bus is a descendant of this com-mon system bus In a microcontroller, reduced-functionality versions of the distinct devices wereplaced inside a single physical chip and connected by an internal bus that was usually not acces-sible to the outside The lack of this bus may seem like a limitation, but in fact it means getting amicrocontroller system working is much easier due to the so many fewer connections needed.With a single small device containing everything needed to run algorithms, microcontrollers
were embedded in all sorts of devices This practice has come to be called embedded systems, and
designing such systems is an engineering discipline in its own right Embedded systems arewhat is responsible for all the exciting advances in our everyday objects becoming smart objects.Modern home computer systems are a kind of hybrid approach The CPU is still separate, butinstead of many separate chips serving different functions, the functionality is consolidated intoone or two large bridge chips Similarly, some microcontrollers are so advanced now as to beable to run modern operating systems like Linux
The best magazine about embedded systems is Circuit Cellar, available at http:// circuitcellar.com/ It is written by engineers for engineers and can get complex quicklyfor the uninitiated But if you want to learn about the newest developments in tiny computa-tional devices, it is a great resource
Parts and Tools
There are many microcontroller development kits available that can be used with Roomba Thefirst shown here, the Basic Stamp, is a standard, easy-to-use, and well-known device The sec-ond, Arduino, is a newcomer to the scene, but is based on standard parts, is becoming as easy touse as the Basic Stamp, and is much cheaper and more powerful than the Stamp If you havethe time, try out both microcontroller styles
For both styles of projects, you’ll need:
䡲 Mini-DIN 8-pin cable, Jameco part number 10604
䡲 Several 220-ohm resistors (red-red-brown color code), Jameco part number 107941
䡲 Jumper wire kit, Jameco part number 19289
䡲 Several LEDs, Jameco part number 156961 or similar
Trang 8F IGURE 13-1: Typical computer architecture vs microcontroller architecture
For the Basic Stamp projects, you’ll need:
䡲 Basic Stamp Board of Education Full Kit, Jameco part number 283101
䡲 Two 0.1 µF capacitors, Jameco part number 545561
䡲 Two photocells, Jameco part number 202403
For the Arduino projects, you’ll need:
䡲 Arduino USB board, Sparkfun part number Arduino-USB
䡲 8mm Red LED, Quickar part number 8R4DHCB-H or similar
䡲 8mm Green LED, Quickar part number 8G4DHCB-H or similar
䡲 8mm Blue LED, Quickar part number 8B4DHCB-H or similar
䡲 Two 8-pin header receptacles, Jameco part number 70754
䡲 Single line wire wrap socket, Jameco part number 101282
䡲 Small solderless breadboard, Digikey part number 923273-ND or Sparkfun PType-BBM
䡲 General-purpose circuit board, Radio Shack part number 276-150
Typical Computer Architecture Typical Microcontroller Architecture
OUTPUT DEVICE
INPUT DEVICE
OUTPUT DEVICE
ROM
INPUT DEVICE
OUTPUT DEVICE
External bus
Trang 9Additionally, you’ll need the various tools described in Chapter 3 For the Basic Stamp projects,you’ll need the USB-to-serial adapter used in the previous projects.
Quickar Electronics (http://quickar.com) is one of several small companies that specialize
in high-power LEDs and also sell in small quantities to hobbyists Besides PC-board mount LEDs,they stock the blindingly bright LEDs used in flashlights and traffic lights as well as a wide variety
of surplus components
Solderless Breadboards
The previous construction projects were all interfaces of one sort or another and thus benefitedfrom the level of sturdiness that you get from soldering a circuit However when prototyping,it’s usually quicker to sketch out circuit ideas on a solderless breadboard before committingthem to solder If you’re unfamiliar with solderless breadboards, Figure 13-2 shows a typicalone with parts plugged in and a kit of pre-cut jumper wires behind it In a solderless bread-board, groups of five holes are connected together In the figure, each vertical line of five is con-nected together, and at the top and bottom board between the red and blue strips, eachhorizontal line of five is connected The holes are like the holes of an IC socket or header inthat they have a spring-loaded metal connection that grabs a wire or part when inserted Andlike a socket, repeated insertions wear it out If you prototype a lot using solderless bread-boards, you eventually collect them as your old ones become flaky
F IGURE 13-2: Medium-sized solderless breadboard with pre-cut jumper wire kit
Trang 10It’s tempting to skimp and not get a box of pre-cut jumper wires Don’t do it It’s possible to
cut and strip wires yourself (and necessary if you run out of pre-cut wires), but using just any
sized wire makes for an ugly and hard-to-read circuit The ill-fitting wires move around,
put-ting stress on the breadboard holes Making jumper wires takes a lot of time, which is kind of
beside the point of using a solderless breadboard Get a box of pre-cut jumper wires and you’ll
be a much happier hacker
Alternatives
Although the circuits presented in this chapter are both simpler and easier to build than the
pre-vious projects, even easier ways to re-program your Roomba are emerging RoombaDevTools.com
is working on two boards that are functionally very similar to the Basic Stamp and Arduino
boards They are:
䡲 RooStamp: A carrier board for Basic Stamp 2 that interfaces to Roomba It is
Bluetooth-optional with servo ports and GPIOs exposed to the user, and it is powered
by the Roomba battery
䡲 RooAVR: A high-end 16-bit Atmel AVR processor with plenty of I/Os, Bluetooth
onboard, A2Ds, URART, JTAG, and servo ports, also powered by the Roomba
Both of these boards can also be powered externally if your project needs to draw more current
than Roomba can supply The RooAVR uses the same type of microcontroller as Arduino, so it
should be possible to use the Arduino programming environment with it
If wiring up a microcontroller to your Roomba is not your cup of tea and you want to get
started immediately making an autonomous robot, check out RoombaDevTools.com for these
devices But hooking up a little computer to Roomba isn’t so hard really, as you’ll see
Adding a New Brain with the Basic Stamp
The Basic Stamp was created by Parallax in the early 1990s to address a persistent problem of
embedded systems design: making it easy to use Because microcontrollers are so self-contained,
you couldn’t just add a monitor and keyboard to them to see what’s going on Instead you
needed some sort of development system, usually costing thousands of dollars When you got
the development system, learning the strange non-standard languages used to program the
microcontroller was daunting It could be months before you get a working system And even if
you could get all that working, acquiring the microcontrollers themselves was quite difficult
unless you were a large company that wanted to buy 10,000 of them
Parallax solved these problems by taking a common microcontroller, placing it on a
hobbyist-friendly carrier board and writing a BASIC language interpreter for it Now anyone who could
program in BASIC could buy a single Basic Stamp and get a tiny stamp-sized microcontroller
running a program they wrote, and do it in an afternoon It was a revolutionary way of thinking
about microcontrollers and for the past decade the Basic Stamp has been the standard in
microcontrollers for hackers
Trang 11About the Basic Stamp 2
There have been a few variations of the Basic Stamp module as Parallax improved and expandedtheir product line The most common is the Basic Stamp 2, also known as BS2, shown inFigure 13-3 The module is a small PC-board containing a specially programmed MicrochipPIC microcontroller and a few support components It is in the form of a wide DIP chip, so it’simmediately familiar in shape to anyone who’s built circuits before, and it plugs easily into asocket or solderless breadboard It provides 16 input/output pins that can be used to read but-tons or knobs, power LEDs or motors The PBASIC version of the BASIC language it usescontains special keywords to help with these common tasks
F IGURE 13-3: Basic Stamp 2 module
The BS2 can run directly off a 9V battery and a few of its pins can connect directly to a PCRS-232 serial port to serve as a means of both programming it with BASIC code and watchingdebugging output Grab a solderless breadboard, some wire, a few other common parts and youcan whip up a Basic Stamp programming board in a few minutes
Trang 12Many people, however, opt for getting one of the several Basic Stamp carrier boards that Parallax
makes One of the best (and cheapest) is the Board of Education, shown in Figure 13-4 It has
jacks for power and RS-232 and has a small solderless breadboard for quickly creating projects
Alongside the breadboard, all the pins of the BS2 are brought out as headers for easy
connect-ing The Board of Education and BS2 are so popular that Parallax sells a kit containing both
along with a serial cable and power adapter, which is exactly the one listed in the “Parts and
Tools” section in this chapter
In addition to the great products Parallax has created, they also have some of the best
docu-mentation about microcontrollers in general and Stamps in particular The docudocu-mentation covers
everything from what a microcontroller is to a multitude of interfacing techniques for sensing
and controlling the environment All are available as free PDF downloads Visit http://
parallax.com, click Downloads, and look at the Documentation and Tutorials
F IGURE 13-4: Basic Stamp Board of Education development board, with BS2 inserted
Trang 13The Basic Stamp Environment
Figure 13-5 shows the Parallax Basic Stamp development environment with a typical “HelloWorld!” type of program for microcontrollers Of course, the first program you write on a nor-mal computer that verifies everything is working is to print out “Hello World!” to the screen.With a microcontroller, instead of saying “Hello World!”, the first program you create verifiesfunctionality by blinking an LED
F IGURE 13-5: Basic Stamp editor in Windows
In PBASIC, the HIGHand LOWcommands cause a pin to go to 5V or 0V, respectively InFigure 13-5, pin 0 (P0) is brought high for half a second, then low for half a second, and thenthe program loops Figure 13-6 shows the MacBS2 environment for programming BasicStamps on Mac OS X with the same sort of “Hello World!” program but with a slightly differ-ent style MacBS2 isn’t as feature-rich as the official Parallax Basic Stamp IDE, but it is veryeasy to use and works very well
MacBS2 was written by Murat N Konar and not Parallax It is free to use and can be loaded from www.muratnkonar.com/otherstuff/macbs2/
down-If you use Linux and want to program Stamps, check out the Basic Stamp Tools for Linux at
http://bstamp.sourceforge.net/
Trang 14F IGURE 13-6: MacBS2 for Mac OS X
After you’ve downloaded one of the programming environments, getting it running and
connected to the Basic Stamp is easy Plug a 9V battery or power adapter into the Board of
Education, connect the Board of Education serial port to your USB-to-serial adapter, and
click the Identify/ID Stamp button The program should beep and tell you it found the Basic
Stamp Hook up the familiar LED and resistor circuit to a pin on the Stamp and click Run
The program will be tokenized and uploaded to the Stamp and start running Figure 13-7
shows a typical setup and a running blink program (although the light looks like a simple
white circle when shown in a black-and-white photograph)
If the Stamp isn’t detected, make sure the 0-1-2 power switch on the Board of Education is set
to position 1 or 2
Trang 15F IGURE 13-7: Typical setup when programming a Basic Stamp with a Board of Education
Hooking Up a Basic Stamp to Roomba
Since any pin on the BS2 can do serial input or output and the BS2 uses the same 0–5V naling that the Roomba does, the circuit is really simple and can be done in a number of ways.One possibility is shown in Figure 13-8 To implement this circuit, the easiest thing to do iscreate (or re-use) a Roomba ROI prototyping plug (as shown in Figure 11-8) You may want toadd a little bit of hot glue around the edges of the plug where the solder is to act as strain relief,since you’ll likely be inserting and removing the plug a lot
sig-When wiring up Roomba to a microcontroller, the most common mistake is to accidentally swapthe RXD and TXD lines If your code compiles and runs yet doesn’t control Roomba, try swappingthose wires If it still doesn’t work, verify the robot still can receive ROI commands by using theserial tether from Chapter 3
The BS2 can be run from a 9V battery, but you can also power it directly from Roomba TheStamp and the BoE have standard 7805 voltage regulators that you’re familiar with from earlierprojects They’ll take the Roomba Vpwr (approx 16VDC) line and regulate it to 5 VDC