博客首页 | 排行榜 |

设计我最赞的博客

个人档案
博文分类
可编程机器人(Programmable Robotics (Part 1))  2009-04-03 20:43
如果你想在机器人上工作,但是没有时间从无到有去建立,那就请考虑买一台iRobot公司的Create机器人。这个机器人混合了若干自定义的特性,包括一个为板上编程的设计的命令模型,一个用来发送独立命令的串口线,10个内置演示程序,超过30个内置传感器和一个25针的扩展接口。

Build on an Existing Robot Platform

If you want to work on a robot but don’t have to time build one from scratch, check outiRobot’s Create. The robot incorporates several customizable features, including a commandmodule for on-board programming, a serial cable to send individual commands,10 built-in demos, over 30 built-in sensors, and a 25-pin expansion port.

Occasionally, I get dragged to the nearest mall,especially around Christmas time. Few of thestores interest me. But Brookstone is different. It is anation-wide specialty retailer with an assortment of consumerproducts that are functional in purpose, distinctivein quality and design, and not widely available fromother retailers. Years ago, I was drawn in by a Frisbeeshapedgizmo wandering around on the floor near thestore’s entrance. It was an iRobot Roomba vacuumingrobot. iRobot’s literature described it as the first practicaland affordable home robot. You may have heard of iRobot,which developed robots like the PackBot that wasused to search the rubble of the World Trade Center inNew York after the September 11 terrorist attacks. It isnow used by the U.S. military overseas.

Apparently, iRobot quickly sold more than 1 millionRoomba units. This success led to a line of similar roboticappliances used for vacuuming, sweeping, washingfloors, and cleaning pools and gutters. As you can imagine,when the robots started becoming popular, hackingsites sprang up with information about ways to use theseplatforms for unintended purposes.iRobot took advantage of this growingfrenzy and developed an offshoot basedon the Roomba’s proven technology. The Create was born with two basicmodels. In this article, I will discusswhat iRobot provides as a system andhow you can use the technology.

PROGRAMMABLE ROBOT
The Create mobile robot platformis for educators and developers alike.It’s mobile out of the box. There is noneed to assemble the drive system orworry about low-level code. Thebasic unit includes a serial cable forsending individual commands from aPC or writing basic scripts of up to100 open-interface commands, whichcan be stored on the robot. Ten builtindemos perform various preprogrammedbehaviors. More than 30built-in sensors enable the robot toreact to both internal and externalevents (see Photo 1). In addition to aserial communications port, there isa 25-pin expansion port, to which youcan connect your own electronics(sensors, actuators, wireless connections,computers, cameras, or otherhardware). The cargo bay has variousthreaded holes for securely mountingyour own brand of hardware. You canadd a fourth wheel to improve thestability of larger payloads, but thisslightly reduces its ability to adapt tochanges in grade.

Photo 1—The Create’s Power, Play, and Advance buttonsand LEDs (a) and wall sensor and IR receiver (b) along withevery other sensor can be disconnected from the interfaceboard (c). Motor gearboxes with optical encoders (d) givethe Create (e) a movement resolution of 1 mm. Angle isdetermined to 1° of rotation based on the left and rightencoder values.

The advanced unit includes theiRobot Command Module for onboard(autonomous) programming. The Create is fully compatible with all iRobot Roomba accessories, including rechargeable batteries, apower supply, a home base, a remotecontrol, and virtual walls. The Command Module screws onto the 25-pin expansion port at the front of the cargo bay. It contains an Atmel ATmega168 microcontroller that communicates with the Createthrough a serial connection in the expansion port. It is programmed viaa USB port. Let’s begin with a look atthe basic unit’s array of peripheral I/O devices.

UNDER THE HOOD
The Create is preprogrammed with an operating system that enables access to all of its input and output devices by passing commands via a serial communications port (default speed of 57,600 bps). Commands include an opcode (128 – 159) followed by a number of data bytes, depending on the opcode. Table 1 lists the opcodes and data requirements of each. I color-coded the table so you can see the groups. Five commands place the Create into special modes.

The Create begins in Off mode and remains there until a Start command is received. The Start command places the Create into passive mode ready for additional commands. From the passive mode, the Create accepts only the Baud, Safe, or Full commands.Safe and Full are similar commands that give you complete control using any of the other commands.Safe mode, as its name implies, has some automatic selfpreservation actions, which prevent it from falling off an edge, for instance. In Full mode, if you don ’t pay attention to its cliff sensor, for instance, it would attempt to fly right off a ledge (thereby needlessly testing its poor aerodynamic characteristics).

The built-in demo programs can be called through the open interface using the commands in rows 6 – 8 and row 15 (see Table 1). Note that while Cover, Cover and Dock, and Spot are available as individual commands,the Demo command (with a data byte of 0 – 9) lets you choose any of the built-in demos. For instance, the Cover and Dock demo instructs the Create to use random bounce, wall following, and spiraling to cover a room ’s floor area. If it sees an IR signal from its home base, it will zero in on home base, dock with it, and recharge its batteries.

This brings us to the real nitty-gritty. The commands in rows 2, 9–14, 16–22, and 26–29 have to do with the Create’s I/O. The Create’s outputs are left and right motors, LEDs, tone, and output bits on the 25-pin expansion connector (6 bits, three digital, and three PWM). On the input side, there are wheel drop sensors, bumpers, wall sensors, cliff sensors, virtual wall (IR) sensors, overcurrent sensors (for wheel and PWMs), buttons, distance, angle, charging state, battery voltage, system current, battery temperature, battery charge, battery capacity, and inputs from the 25-pin expansion connector (four digital bits and one 10-bit analog input).

You can refer to the “Open Interface Specification ”in the Resources section for more information on all of this, but I want to touch on it briefly. There are two ways to move the Create The drive command requires 2 data bytes, a velocity of  ±500 mm/s, and a radius of  ±2,000 mm. That ’s a maximum speed of approximately 1 MPH!This mode is similar to driving a car using the gas pedal for speed control and the steering wheel for direction control. The drive direct command requires 2 data bytes, a right velocity of  ±500 mm/s, and a left velocity of ±500 mm/s. This mode is similar to driving a bulldozer or tank using independent forward and reverse levers for each wheel.

Feedback on movement comes from the wheel encoders as values for distance (–32,768 mm to 32,767 mm) and angle (– 32,768° to 32,767°). The distance is the total distance covered by each wheel ’s encoder divided by two.If the Create is spinning in place, its distance remains at zero. The angle is a relationship of the distances traveled by each wheel. The distance between wheels is 260 mm. If only one wheel is moving, that wheel must travel 2πR, or 1,634 mm (i.e., 2×π×260 mm), to get back to where it started, so 1,634/360 = 4.5 mm/degree. For every 4.5 mm difference in travel distance between the two wheels, the orientation will change 1 °. With anything other than point contact (of thin wheels), you can guesstimate only the actual turning radius. This will vary greatly depending on which part of a wide wheel is making contact with the floor. That ’s why navigation by “dead reckoning ” loses accuracy over time. This could be corrected by using an electronic compass to determine direction instead of dead reckoning. Compasses aren ’t necessarily perfect either. So, in the big scheme of things,it’s best to not put all of your eggs in one basket. This is getting beyond the scope of this article, so let me just caution you: in most cases, you can rely on the angle for immediate results (i.e., turning around), but don’t depend on it to keep track of your direction over the long term (i.e., trying to set up parallel sweeps to cover a large area).

Let me make one last note about the commands in rows 23–25 (see Table 1). The Script commands enable you to record and play back a number of consecutive commands. When used in conjunction with the Wait commands, you can have the Create perform a preprogrammed sequence of events. Here is an example: wait distance (1,000 mm), wait angle (90), wait distance (1,000 mm), wait angle (90), wait distance (1,000 mm), wait angle (90), wait distance (1,000 mm), and wait angle (90), which when played back commands the Create to drive in a 1 m × 1 m square pattern.

SERIAL TETHER
The Create can play any of the demos via push button selections after you press the Power button. However, to get the Create (basic unit) to do your bidding using the open interface commands,you need a serial link to something that can send these commands via the link. This can be done with a terminal emulator (e.g.,RealTerm) running on your favorite computer with a serial interface.

While running Real-Term on your PC with the Create connected via the serial cable, you can use your keyboard to type in commands (in decimal with spaces separating input values). The Create will execute the commands in the order they are received. If you type  “128 132 139 2 0 0 ,” you will turn on the Play (>) LED. If you enter a drive command, support the system underneath with its wheels off the floor (full mode). The Create will most likely rip your serial cable out as it bolts across the floor unless you have a long tether.

I know what you are thinking:  “Not user friendly, this tether.” Well, there is of course a better way. You can eliminate the tether by supplying an RF link. I want to stop from going down this path for the remainder of this column. Instead, I want to focus on the Create ’s advanced unit, adding the command module to the 25-pin expansion connector in the forward end of the cargo bay.

COMMAND MODULE
You can use a PC to enter open interface commands that the Create can receive via its serial port and execute in its operating system. The command module uses an ATmega168 flash memory microcontroller to send commands to the Create. This is accomplished by writing a control program and programming it into the microcontroller.The control program uses the open interface commands to access the Create ’s functions.How is this any different than typing commands to the Create through the tethered serial port? It ’s not!However, the complicated answer is much more satisfying.

The most obvious difference is that the tether disappears because the Command Module rides piggyback on the Create.Second, I don ’t know about your typing speed (or accuracy for that matter), but the microcontroller can send commands at blazing speeds.Here comes the most important fact. Even if I could type a drive and read sensor commands fast enough, I would not be able to check all of the important sensor states and decide to stop before I hit the wall on the other side of the room (or ripped out the tether). The Command Module has plenty of flash memory storage space to hold a useful control program.

Before I get into the programming of the Command Module, I need to mention that the Command Module passes the I/Os available on the 25-pin expansion connector to the user along with a bevy of extra I/O controlled directly from the microcontroller. These are accessed via four ePorts (DB9F). In addition to the extra I/Os, there is a RESET button, a User button, and a USB device port. The USB port provides a programming tether to a PC. The RESET button jump-starts the Command Module. If a USB cable is plugged in, the microcontroller goes into Bootload mode to accept code via USB. If RESET is pushed without the USB cable plugged in, it will begin executing the downloaded code.

COMMANDING THE SYSTEM
The Command Module ’s CD installs a development system on your PC, which enables you to develop programs in C using the provided programmer ’s notepad IDE (editor), GNU GCC (compiler),and AVRDude (downloader). After installing this software, you can explore the three example programs on the CD. The first example,DRIVE, shows how to make the Create move around and react to an object when its bumper signals come in contact with it. The second example,INPUT, teaches you how to interact with the Create ’s I/O and the Command Module ’s extra I/O. The third example, LIGHT, introduces you to the process of adding external sensors. In this example, an added CDS light sensor (to the command module) is monitored while the Create wanders the area. When a significant increase in light is detected, the Create stops and plays a song.

This might be where we part company.As you probably know, I like getting down and dirty, so I ’m going to finish up this column by discussing using assembly language with the Command Module, which I couldn ’t find much about on any of the Create-related sites or forums I visited. Although I think you get a great little development system here for the Create with the Command Module, there is a missing piece that is important to me when I develop something: debugging. A debugging connector could have been added to the Command Module, which would have made in-circuit debugging simple.There are a few mentions of how to use the USB port for feedback, but there is conflicting information and there aren ’t any examples. I ’m going to show you how to do this.Although I ’ll be working in assembler,this can be done in C as well.

AVR Studio
I’ve used AVR Studio (IDE) in the past for development work with Atmel parts. It includes a simulator (which I use from time to time to check routines) and other tools for debugging (i.e., in-circuit emulation).Because I didn ’t have access to the debug port on the ATmega168 used in the Command Module (I’m sure they wouldn ’t want the user to accidentally wipeout the bootloader), I couldn’t use this tool.


Figure 1—This is the idle flowchart I wrote by reviewing the code in the example DRIVE.C on the CD that came with the Create.

Using the C code as a guide, I went through the DRIVE example and came up with a flowchart. Figure 1 shows the initialization and idle loop waiting for a User button push. The movement routine in Figure 2 then takes over until a second push or any safety issues, such as a cliff sensor,are detected. Items in round bubbles are calls made to other routines.These can easily become a library of calls used over and over by your programs (just like a library in C).

Figure 2—This is how the example DRIVE.C code handles the movement of Create. I based my assembly code on this flowchart.

A couple of the routines are worth mentioning. When a bumper is detected, a turn is requested with the direction based on the bumper so Create turns away from the collision.My random angle routine uses the 7 least significant bits of the Timer1 register counter (which increments rapidly with no prescaler). The constant 53 is added to this value (0 – 127)to allow angles between 53 ° and 180 °(or  – 53 ° and  – 180 ° degrees when rotating clockwise). A drive command is then issued using a velocity of 200 mm/s with either radius of 0xFFFFmm (for clockwise rotation) or 0x0001mm (for counterclockwise rotation). The random angle value becomes the requested angle and is compared to the angle returned by Create to determine when the turn is completed.

Table 1—The Create’s basic unit runs an operating system that enables you to manipulate behavior by controlling and monitoring its I/O devices through its serial port. The commands listed here can be sent by any device capable of serial communications.

The update routine requests a full complement of data to be returned by Create. Refer back to the Sensors command in Table 1. This command requires 1 data byte (0 – 42). There are in fact only 36 sensor values that you can ask for, so why isn ’t the data byte (0 – 35)? The first seven values (0 – 6)request groups of sensors to be sent.Value 0 requests the first 20 sensors,1 requests the first 16, 2 requests sensors 11 – 14, 3 requests sensors 15 – 20,4 requests sensors 21 – 28, 5 requests sensors 29 – 36, and 6 requests all 36 sensors. This makes using this command quick. Because the commands going to and sensor data being returned from Create happen asynchronously (i.e., your program may execute numerous instructions while the sensor data is being received), you must be cautious about when you rely on new data. For this reason,once all the sensor data has been received, it is copied into a working bank of registers for you to access.This approach assures each time you access sensor data it is complete.

There is sensor data received (e.g., distance and angle) that must be used appropriately each time it is received.These items indicate a change since the last request and are cleared internally (within Create’s OS) when sent. If you use the drive command to rotate counterclockwise, you must continually add the sensor angle values to determine when you’ve rotated the proper amount. You must then use the drive command to halt the motors (velocity = 0). Distance works the same way. While the value of every sensor available is not used in this program, I ask for it all any way, just as an exercise. It also makes the next section more interesting, as you will see shortly.

DEBUGGING
You don ’t have a way of getting inside the microcontroller and singlestepping execution or looking at register values, but you can get the next best thing. The single UART in the ATmega168 is actually directed by external circuitry toward either the USB port for bootloading your program or Create ’s serial port, via the 25-pin expansion connector, to respond to your program ’s command requests. The control that does this switching is an output bit on the ATmega168. You have access to this,which means from your program you can flip back and forth between destinations.Because the microcontroller is set up to go into bootload mode whenever a USB cable is plugged into the Command Module ’s USB port,swapping destinations is impossible unless you know the secret. Note that holding down the User button while resetting the microcontroller can deactivate this function.

For debugging purposes, I can write a routine to send the sensor data out of the USB port each time the sensor data is copied into the working registers.Note that even though you can check the UART to see if any characters remain in the transmitter, you should add a delay of at least one character before switching the serial output destination to ensure that you are not interfering with a transmission in process.

To view the debugging data, you can use a terminal emulator like RealTerm. Because you are now anchored again with tether, you will want to place the Create up on a support to keep its drive wheels off the ground. You will also need to comment out the safety checks within the program so the Do Movement loop isn ’t exited when one of the cliff sensors is triggered. I use conditional assembly with a Debug mode that eliminates certain code. I started by just sending the data bytes as received. While RealTerm enables you to see the data in various formats such as ASCII, binary, or HEX, there are a few problems with this, as you might expect. Not all of the data makes sense in any one format. And some sensor values are multibyte representations of a word value. It makes sense to format each sensor value appropriately and send the value as ASCII characters. For bytes used as bit flags, binary might be appropriate. For signed word values, a + or  –  sign would be appropriate, and so on. You can get as fancy as you wish. A name or an alias that helps to identify it would also be useful.You don ’t have to limit the data to just the Create ’s sensor values. You can display any variables your program is calculating.

Be careful what you choose to display.Too much data can be just as bad as too little. Your screen can fill quickly. Logging to a file will be helpful in some instances. The idea is to help you narrow down where to look for a bug in your code. When the robot backs up and rotates forever,it might not be so obvious where to start looking in your code. I filled my debug output routine with every possible piece of data to export. I added a jmp instruction at the beginning of each section of code for outputting that particular piece of data (see Listing 1).


I can quickly REM out the jmp instruction of any sections I want to include in the debug display. Photo 2 shows power-related data I monitored from the Create. Note that the number of variables you choose to display (total characters sent) will impact the execution loop speed of the program.

Photo 2—RealTerm is displaying some of the internal register values that have to do with power. OC = 0 (no overcurrents), CS = 0 (not charging), V = 14,973 (battery = 14.973 V), mA = –1,231 (batteries supplying 1.231 A), BT = 18 (battery temperature = 18°C), CRG = 2,698 (2.698 A/h remain), CAP = 272 (battery capacity is down by 0.272 A), and CA = 00000000 (no source).

In AVR Studio, I created a project with three files. The first file,m168def.inc, is the register/bit definitions file for the ATmega168. The second file, CreateCmdDef.inc, is used to hold the definitions for all of the items directly relating to the Create.I also placed the callable routines I made to support the Create ’s commands and my Debug code. This kept the ugliness out of the main file. The last file, iRobotCreateTest1.asm, is the main file. This is the control program.It is the only file that must be written for a new project. Assuming all of the commands have working code in CreateCmdDef.inc, the main code has includes that link the first two files.

One of the nice things about using AVR Studio is that it contains a simulator.I often use the simulator to calculate routine timing using the integrated stopwatch. I even use it to learn a routine I ’ve copied from somewhere else.After assembling the code, I use AVRDude (installed with the Create CD)to download my .HEX file to the Create through the command module ’s USB port. If I have the Create up on blocks, I can execute from here by holding down the User button while pressing the Reset button. This can be done when the Create is off, because the Command Module has its own power switch. When the Command Module begins execution, it can check the status of the Create (through the 25-pin expansion connector) and turn it on, if necessary!

In this state, you can run it all while the wheel spins without floor contact,pressing buttons and bumpers and watching for proper responses. Of course, you ’ll want to pull out the tether and try it on the floor even if it is misbehaving. But use caution. If you aren ’t paying attention to safety issues, the Create may try to run over the cat if the bumper routine doesn ’t work. Or it might continue backing up if you aren ’t paying attention to distance because there  “are no bumpers in the rear.”

Now I need to think about what kind of external sensors I might want to add to support a mapping project. This should be interesting.

ARE YOU CREATIVE?
If you want to check out some of the projects others are working on, visit the Create projects link in the Resources section. You will find a bevy of third-party add-ons that were developed for the Create. Refer to http://forums.irobot.com/irobothome and www.avrfreaks.net for discussions about iRobot and Atmel-related things.

If you want to work with an affordable robotic platform without having to deal with building and testing hardware from scratch, this is an ideal vehicle. If you want to begin with a proven design and add your own sensors, this is an ideal platform. While the top-of-the-line Create with more stuff than I’ve discussed here costs $300, the basic unit begins at approximately $100. It can really help jump-start your robotics research, product development, or technical education!

Next month, I will explain how to replace the Command Module with a microcontroller. You are well on your way to developing your first application.

Jeff Bachiochi (pronounced BAH-key-AH-key) has been writing for Circuit Cellar since 1988. His background includes product design and manufacturing. You can reach him at jeff.bachiochi@imaginethatnow.com or at www.imaginethatnow.com.

RESOURCES
iRobot Corp., “iRobot Create Open Interface Specification,” 2006, www.irobot.com/filelibrary/pdfs/hrd/create/Create%20Open%20Interface_v2.pdf.
———, “Command Module Owner’s Manual,” 2007, www.irobot.com/filelibrary/pdfs/hrd/create/Command%20Module%20Manual_v2.pdf.
———, Create projects, http://store.irobot.com/family/index.jsp?categoryId=2591511&ab=CMS_IRBT_CreateSuperCat_LearnMore_110408.

SOURCES
ATmega168 Microcontroller
Atmel Corp. | www.atmel.com
Create programmable robot
iRobot | www.irobot.com
RealTerm Terminal emulation software
RealTerm | http://realterm.sourceforge.net
类别:机器人 |
上一篇:一个非常简单的计划(A Really Simple Plan) | 下一篇:FAT 文件系统的查看(FAT File System Review (Part 1))
以下网友评论只代表其个人观点,不代表本网站的观点或立场