Making Robots With The Arduino part 5.pdf

(901 KB) Pobierz
Servo 2011-03.pdf
With The
Part 5 -Adding Sensors To Explore The World
A RDUINO
By Gordon McComb
You’ve spent hours designing and building your latest robot creation.
You bring it into your living room, fire it up, and step back. Behold!
Your beautiful new robot smashes into the fireplace and scatters itself
into tiny pieces over the living room rug. You remembered things like
motor speed controls, colorful blinky lights, even a synthetic voice, but
you forgot to provide your robot with the ability to look before it leaps.
your ArdBot (or other Arduino-based automaton) the
sense of touch and light. Augmenting these basic
senses are methods to detect objects in proximity to
your robot — seeing what’s there without having to actually
bump against them.
Proximity detection forms the basis of collision
avoidance — how to keep your bot from crashing into
things in the first place. Collision avoidance takes many
forms. Some of these techniques are designed to detect
objects very close to the robot, while others are made to
detect objects several feet away.
In this installment, you’ll learn about two popular forms
of proximity detection — ultrasound and infrared — and
how these low cost sensors are interfaced and used with
the Arduino.
The State of the ArdBot
differentially-steered robot based on the popular Arduino
Uno and compatible microcontrollers. Here’s what past
installments covered:
• Part 1 (Nov ‘10) introduced the ArdBot project, along
This article series has been on the construction and use
of the ArdBot (see Figure 1 ) — an inexpensive two-wheeled
FIGURE 1. The ArdBot Arduino-based expandable robot,
shown with this month’s enhancements: a rotating turret,
ultrasonic distance ranger, and infrared proximity detector.
SERVO 03.2011 43
Making Robots
I n last month’s installment, you learned how to give
527809971.022.png 527809971.023.png 527809971.024.png 527809971.025.png 527809971.001.png
www.servomagazine.com/index.php?/magazine/article/march2011_McComb
Where Have All the Sharp
Distance Sensors Gone?
switches with polling and hardware
interrupts to detect physical contact
with objects, and ways to use
photoresistors to steer the robot by
light.
some models of their infrared distance
sensors, allowing the stock that remains
“in the channel” (still held in reseller
inventory) to be depleted. New versions
are in production, but they may not be
universally available.
You may have noticed that several of
the Sharp infrared distance sensors have
become hard to get from some sources —
most particularly from Digi-Key, who at
one time was a major seller of the things.
These sensors are discontinued and/or
not available as new stock in retail
channels.
However, most of the sensors can
still be purchased from a number of
specialty online shops, such as SparkFun
and Pololu, both of whom carry large
inventories of several Sharp models.
What remains of the current stock may
not be useful for creating a new mass-
market product, but there’s plenty still
around for us robo-tinkerers.
Sharp has introduced a relatively
new style of sensor — the GP2Y0D805
and GP2Y0D810 — that are smaller and
less expensive. The ‘05 has a set 5 cm
proximity range; the ‘10 has a set 10 cm
range. Output is a digital LOW or HIGH.
This new class of sensor comes in a
DIP-size package, but it requires some
external components. Online retailers
such as Pololu (see the Sources box)
offer the sensors on a breakout board for
easy use in your projects. Even with the
addition of the breakout board, these
sensors are roughly half the cost of their
predecessors. They also have a much
improved response time: over 350 Hz
(350 updates per second), as opposed to
about 25 Hz of the older sensors.
Be sure to check out the
previous four issues of SERVO
Magazine for more details. This
installment describes three
important robotic functions:
programming an ultrasonic
transducer to accurately measure
the distance to objects; how to use
a Sharp infrared distance module
for monitoring proximity; and ways
to add a rotating turret so the
ArdBot can scan the room to look
for things nearby.
In the next issue, we’ll conclude
with putting all the pieces together,
combining what you’ve learned to
create an autonomous room
wanderer that’s able to seek things out, investigate its
environment, and follow the direction of its master —
that’s you.
with the popular Arduino microcontroller and basic
programming fundamentals of this powerful controller.
• Part 2 (Dec ‘10) detailed the construction of the ArdBot,
using common materials including PVC plastic and aircraft
grade plywood.
• Part 3 (Jan ‘11) covered the Arduino in more detail. It
also examined the ins and outs of programming R/C
servo motors that provide the locomotion for the ArdBot.
• In Part 4 (Feb ‘11), we learned about using bumper
About Non-Contact
Near-Object Detection
Last time, we learned how leaf switches are used as a
form of touch sensor to detect contact with objects.
Contact detection provides an immediate signal that
something looms directly in the way.
Non-contact detection senses objects without
having to hit them first. Near-object detection does
just what its name implies: It senses objects that are
close by, from perhaps just a breath away to as
much as eight or 10 feet. These are objects that a
robot can consider to be in its immediate space;
objects it may have to deal with, and soon. These
objects may be people, animals, furniture, or other
robots.
By detecting them, your robot can take
appropriate action which is defined by the
programming you give it. Your bot may be
programmed to come up to people and ask for their
name. Or, it might be programmed to run away
whenever it sees movement. In either case, it won’t
be able to accomplish either behavior unless it can
detect the objects in its neighborhood.
There are two common methods of achieving
near-object detection: proximity and distance.
Sources
If you’d like to build the
ArdBot, be sure to start with
the November ‘10 issue of
SERVO Magazine for Part 1 of
this series. Also check out the
following sources for parts:
Acroname
www.acroname.com
AdaFruit Industries
www.adafruit.com
HVW Tech
www.hvwtech.com
Prefabricated ArdBot body
pieces with all construction
hardware; 360 degree rotation
sensor turret:
Jameco
www.jameco.com
Budget Robotics
www.budgetrobotics.com
Lynxmotion
www.lynxmotion.com
Arduino Resources:
Mark III Robot Store
www.junun.org
Arduino
www.arduino.cc
Pololu Robotics & Electronics
www.pololu.com
Fritzing
www.fritzing.org
Robotshop
www.robotshop.com
Online Retailers of Arduino,
Sharp sensors, and/or
ultrasonic sensors:
SparkFun
www.sparkfun.com
• Proximity sensors care only that some object is
within a zone of relevance. That
44 SERVO 03.2011
F rom time to time, Sharp discontinues
527809971.002.png 527809971.003.png 527809971.004.png
is, if an object is near enough to be
considered important. Objects beyond the
proximal range of a sensor are effectively
ignored because they are not seen. Out of
view, out of mind.
Distance measurement sensors determine
the space between the sensor and
whatever object is within detection range.
Distance measurement techniques vary;
almost all have notable minimum and
maximum ranges. Few yield accurate data
if an object is smack-dab next to the
robot, or very far away.
FIGURE 2. The Sharp infrared distance
modules rely on the displacement of
reflected light across a linear plane to
detect variations in distance.
Collectively, these sensor types are often
referred to as rangefinders, though only a
device that actually measures and reports
the distance of the covered range is a true
rangefinder.
Among the most common proximity and
distance measurement detectors used in
robotics are ultrasonic transducers, and
specialty infrared sensors made by Sharp.
Depending on the design of the specific
sensor, either can be used for proximity or
distance measurement. In practice however,
the Sharp IR sensors are best suited for
proximity, and ultrasound sensors are the
ideal choice for measuring distance. That’s
how these two detectors are used in the
ArdBot.
FIGURE 3. The analog output of the
Sharp GP2D120 sensor is a non-linear
voltage of between (approximately)
0.25 volts and 2.55 volts.
Using the Sharp
GP2D120 Infrared
Detector
The Sharp GP2D120 is among a series
of remarkable distance measuring devices
originally intended for industrial control.
They’re common finds in amateur robot
projects. These sensors rely on the
displacement of reflected light across a linear
sensor (see Figure 2 ).
Here’s how they work: A beam of
modulated infrared light from the sensor
illuminates an object. The beam reflects off
the object and bounces back into the sensor.
The reflected beam is focused onto what’s known as a
position sensitive device, or PSD. The PSD has a surface
whose resistance changes depending on where light strikes
it. As the distance between sensor and object changes, so
does the linear position of the light falling on the PSD.
Circuitry in the sensor monitors the resistance of the PSD
element, and calculates the distance based on this
resistance.
Among the Sharp detectors currently available through
FIGURE 4. Connection schematic for the Sharp GP2D120,
showing a transistor used to turn the sensor on and off.
retail channels are two general types:
Distance judgment sensors provide a simple digital
(LOW/HIGH) signal that represents whether an object is
within detection range. That range is set at the factory,
and depends on the specific model of the sensor.
Common distances are five, 10, 24, and 80 centimeters.
SERVO 03.2011 45
527809971.005.png 527809971.006.png 527809971.007.png 527809971.008.png 527809971.009.png 527809971.010.png
FIGURE 5. Breadboard view of
the Sharp GP2D120-to-Arduino
connection. Note the pinout and
wiring order of the GP2D120 sensor.
Important! The wiring diagram relies
on existing connections on the
breadboard. See Part 2 of Making
Robots with the Arduino (Dec ‘10)
for details.
closer than a few inches and up to
several feet.
For example, with the GP2Y0D810 sensor, the output is a
digital LOW when an object is within its 10 cm proximity
range, and HIGH otherwise.
• Distance measurement sensors provide an analog voltage
that’s more or less proportional to the distance from the
sensor to the detected object. The voltage output is non-
linear, as shown in Figure 3 . These detectors work over a
span of minimum and maximum distance, usually no
Distance judgment sensors
are ideal for interfacing with
simple electronics, as they don’t
require analog-to-digital
conversion. Since the Arduino Uno
is equipped with a six-input ADC,
we can use either type. For the
ArdBot project, I’ve selected a
GP2D120 which has a range of 4
cm to 30 cm (about 1.5 inches to
12 inches).
The distance is reported as a
varying voltage, from approximately 0.25 volts (no
detection) to 2.55 volts (detection at minimum distance).
That’s according to the spec sheet, but know that there can
be a normal variation of a few tenths of a volt from one
sensor to another.
While the GP2D120 is capable of reporting distance
with acceptable accuracy, for the ArdBot I’ve elected to use
it as a “multi-zone” proximity detector. That is, instead of
hassling with converting its analog voltage to
some quasi-precise distance measurement, for
the ArdBot the GP2D120 will instead simply
indicate when an object is within preset zones.
The ArdBot relies on a separate ultrasonic
rangefinder for accurate distance measuring
and as a secondary near-object detection
check. More about the ultrasound rangefinder
in a bit.
See Figure 4 for a schematic diagram for
connecting the GP2D120 to the Arduino.
Figure 5 has the same circuit, but in
breadboard view. See that I’m being clever
here, and I’ve added a small 2N2222 NPN
type transistor in order to turn the GP2D120
on and off.
The transistor is an optional enhancement,
but there are a couple of reasons for it. First,
like all of the Sharp sensors, the GP2D120
takes constant measurements — about 25 a
second — as long as the device is powered.
Current consumption can go as high as 50
milliamps when no object is detected. While
that’s not a huge current demand, it’s
unnecessary power consumption when the
sensor is not actually being used.
Listing 1 - SharpIR.pde.
const int irCtrl = 12; // Digital pin D12
const int irSense = A5; // Analog pin A5
int distance = 0;
void setup() {
pinMode(irCtrl, OUTPUT);
digitalWrite(irCtrl, LOW);
Serial.begin(9600);
// Use Serial Monitor window
}
void loop() {
Serial.println(irRead(), DEC);
}
int irRead() {
int averaging = 0;
// turn on, wait 250 ms to completely stabilize
digitalWrite(irCtrl, HIGH);
delay(250);
// Get a sampling of 5 readings from sensor
for (int i=0; i<5; i++) {
distance = analogRead(irSense);
averaging = averaging + distance;
delay(55);
// Wait 55 ms between each read
}
distance = averaging / 5; // Average out readings
digitalWrite(irCtrl, LOW); // Turn sensor off
return(distance);
}
46 SERVO 03.2011
527809971.011.png 527809971.012.png 527809971.013.png 527809971.014.png
Second, each time the sensor takes a
new reading there’s extra line noise
induced into your robot’s power supply. By
turning the sensor off when it’s not
needed, the noise is completely removed.
(You can also help filter the noise by
adding some decoupling capacitors across
the +V and ground power connections, as
close to the sensor as possible. Try a 47 µF
electrolytic capacitor and a .1 µF ceramic
capacitor. Be sure to observe correct
polarity of the electrolytic capacitor.)
Listing 1 shows a sketch that
demonstrates how to use the GP2D120
with the Arduino. This is an example sketch
only; in the next installment, you’ll see how
the GP2D120 can be implemented for
object seeking and avoidance, as the
ArdBot is set loose in your living room and
left to discover what’s around it.
A couple of things to note in this
sketch:
FIGURE 6. Connection schematic
for the Devantech SRF05 ultrasonic
ranging module.
• To make a reading, the sensor is turned
on and then allowed to settle for 250
milliseconds (ms) before taking a
reading. The datasheet for the GP2D120
indicates a much shorter delay upon
startup, but I’ve found the longer period
is often necessary to avoid spurious
reads.
• The sketch takes five “samples,” each
separated by a delay of 55 ms. The five
samples are averaged to remove possible
incorrect readings due to momentary
glitches.
• After the sensor is read, it’s turned back off again.
FIGURE 7. Breadboard view of the
Devantech SRF05-to-Arduino connection.
the wiring order is “corrected” to place the +V power lead
in the center. In this arrangement, damage is less likely to
occur if the connection is flipped. I have shown such a
cable in the breadboard wiring view in Figure 5 . Regardless
of whether you use a cable type that re-arranges the wiring
order, be absolutely sure to observe correct polarity . Or
poof goes your sensor.
Open the serial monitor window to observe the actual
values reported by the sensor, converted from an analog
voltage to 10-bit digital values (0 to 1023) by the Arduino’s
ADC. Note that because the sensor does not output a full
five volts when an object is closest, you won’t get the full
1,024 steps. Minimum values are about 40 to 60; maximum
values are in the 625 to 675 range, depending on the
sensor and the reflectivity of the object (dark colors tend to
produce slightly lower values). Anything outside these
ranges can indicate a spurious reading.
Important! Take note of the connection diagram for
the GP2D120. Sharp uses a polarized JST connector where
the +V power lead is on the outside and ground is in the
middle. This is potentially dangerous when the other end of
the wiring is non-polarized, as is often the case with
connectors on general-purpose microcontrollers.
Numerous sources (such as Lynxmotion) sell adapter
cables that go from the JST locking connector to a standard
three-pin 0.100” female header. On many of these cables,
Using the Devantech
SRF05 Ultrasonic Ranger
Ultrasonic distance measurement —also called ultrasonic
ranging — is now an old science. Polaroid used it for years
as an automatic focusing aid on their instant cameras. To
measure distance, a short burst of ultrasonic sound (usually
at a frequency of around 40 kHz) is sent out through a
transducer; in this case, the transducer is a specially built
ultrasonic speaker. The sound bounces off an object and
the echo is received by the same or another transducer (this
one is an ultrasonic microphone). A circuit then computes
the time it took between the transmit pulse and the echo,
SERVO 03.2011 47
527809971.015.png 527809971.016.png 527809971.017.png 527809971.018.png 527809971.019.png 527809971.020.png 527809971.021.png
Zgłoś jeśli naruszono regulamin