The Devantech SRF05 sensor doesn’t require so much time
between each reading. Its time delay is set at just one millisecond.
Note that readings from the ultrasonic and IR sensors
are not taken while the turret servo is in motion. The servo
is commanded to a new position and stops to take the
reading. This is to help reduce measurement errors.
Likewise, the ultrasonic sensor is not read when the
robot is turning. Lateral motion while taking an ultrasonic
measurement can result in wild inaccuracies, though this
greatly depends on the specific sensor you’re using. You
get the best results when the robot is still or moving at a
reasonable speed forward or backward.
As noted above, the ultrasonic sensor continually “pings”
for objects straight ahead while the robot is traveling
forward. If an object is detected to within three inches, the
servos are slowed down (by adjusting their values close to
center, which is a value of 90). If an object is detected to
within an inch, the robot stops, backs up, spins for half a
second to reorient itself, and then continues forward again.
Adjusting Timing Delays
The sketch relies heavily on delays to steer the robot, and
to position the turret servo before taking a sensor reading. For
the purpose of programming, the length of these delays is
determined empirically. The delay values are based on the speed
of the servos when operated from a fresh battery supply. Some
servo motors are particularly fast; some are downright sluggish.
Bear in mind that servo motors slow down as the
battery voltage decreases. A one second delay when the
batteries are fresh might turn the robot one complete circle.
But when the batteries are about dead, the same one
second delay might turn the robot only half way.
In the ArdBot, most timing delays are set directly in
code. An exception is the T_DELAY value, specified in the
declaration area of the sketch. This value determines the
delay used when rotating the sensor turret. The delay must
be long enough to transit the servo from one extreme to
the other, plus the time it takes to read the IR sensor
(which is about 270 ms for all five samples).
You should strive to set delays based on average speeds.
The ArdBot as presented lacks sensors for true navigation
(compass, gyro, and wheel encoders for odometry), so the motion
routines are intended only to demonstrate basic maneuverability.
In a more elaborate ArdBot setup, you could use wheel encoders
and/or a compass to more precisely determine how far the
robot has turned. (It’s best to use both, if you can; wheels slip,
resulting in errors in the wheel encoders. The compass is used
to reorient the position of the robot to a known reference.)
You may also wish to explore ways to avoid using delays, as the
delay() statement literally stops the sketch until the specified
time period has elapsed. The MsTimer2 library is a good
example of using other techniques to time events. A number
of ready-made libraries provide this kind of functionality,
and allow you to easily create multiple timing objects.
As an example, try Metro, also available from the
Arduino Libraries page. Metro allows you to create multiple
timing objects, with each object set to “expire” after a
certain length of time. With some crafty coding, you could
replace most or all the delay() statements in the sketch with
Metro timers, allowing the program to be more multi-tasking and responsive to sensor events.
ArdBot Line Following Mode
When the Mode Select header is placed so that pin A2
is brought HIGH, upon startup the ArdBot is put into line
following mode. As described in Part 6, I’ve provided two
variations of line following sensors:
1. Basic two-sensor detector, using a pair of infrared
emitters/detectors side by side. The two detectors are meant
to straddle the 3/4” line of a length of black electrical tape. Use
the mode_line_follow method with the two-sensor detector.
The sketch assumes the detectors are wired as shown in the
figures, and are connected to Arduino pins A3 and A4.
2. Enhanced multi-sensor array, using (in this case) a
Pololu QTR-8RC eight-detector array. These units are very
compact and cost less than when purchasing separate
ready-made infrared emitter/detector modules. Use the
mode_line_follow_array method with the QTR-8RC. The ArdBot
uses five of the eight available emitters/detectors, and these
are connected to digital pins D4, D5, D6, D11, and D12.
Listing 1 includes the code for both line following
variations. When using an array — like the QTR-8RC — it is
also possible to detect a “finish line” in the line following
course. See the ArdBot Construction Notes site (check the
Sources box) for more information.
For More Arduino Coverage
Though this is the final article in this series, there is
plenty more to share about combining the Arduino
microcontroller with robots. After all, there’s so much more
to talk about. So, stay tuned! Be sure to check out the
ArdBot Project updates, free code, and additional examples
at www.robotoid.com/servomag/. SV
infrared sensors for line
following, and components:
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:
AdaFruit Industries
www.adafruit.com
Prefabricated ArdBot body
pieces with all construction
hardware:
Sensor Turret Budget Robotics
ArdBot Construction Notes:
Downloadable code, updates,
additional sketches, and
examples.
www.robotoid.com/servomag
Arduino Resources:
Arduino
Fritzing
www.fritzing.org
Online Retailers of Arduino,
All Electronics
Digi-Key
www.digikey.com
Jameco
www.jameco.com
Lynxmotion
www.lynxmotion.com
Parallax
www.parallax.com
Pololu Robotics & Electronics
www.pololu.com
RobotShop
www.robotshop.com
SparkFun
www.sparkfun.com
SERVO 05.2011 53