Robot Obstacle Detection and Avoidance with the Devantech SRF05 Ultrasonic Range Finder

The Devantech SRF05 Ultrasonic Range Finder is one of the most popular distance sensors among robotic hobbyists. The module is relatively inexpensive, accurate, and easy to interface with a microcontroller. The SRF05's range of 3 centimeters to upwards of 4 meters makes it ideally suited for developing object detection and avoidance schemes for small and medium size robots. As with any sensor, in order to make best use of its capabilities it is essential to have a clear understanding of the nature of the device. This article describes the fundamentals of applying ultrasonic sonar in robot navigation while considering some of the characteristics specific to the SRF05.

The basic principle of sonar is to generate a pulse of sound and then electronically listen for the echoes created when the sound wave hits an object and is reflected back. By timing how long it takes for the echo to return, an accurate estimate can be made of the distance to the object. The sound pulse generated by the SRF05 is ultrasonic, meaning that it is above the range of human hearing. While lower frequencies could be used in this type of application, higher frequencies perform better for short range, high precision needs.

The SRF05 takes care of a lot of the heavy lifting in using sonar for range finding. The module has two 40khz ultrasonic transducers, one to transmit and one to receive. It also has an onboard PIC microcontroller which is used to generate the ultrasonic signal and listen for the echo, and a handful of other components which condition and detect the signals. The unit is triggered by generating a pulse of at least 10us on the trigger line. The SRF05 then returns a simple pulse, the duration of which is directly proportional to the distance of the object detected: the further away the object is the longer the pulse. The PICAXE has the PULSIN command designed to measure an incoming pulse, and most microcontroller systems will have such a function. For Basic Stamp the command is also PULSIN. For Arduino, Freeduino and the like it is the similar PULSEIN (note the "E").

The unit has two modes selected by configuring a mode pin on the circuit board. If the mode pin is unconnected the SRF05 uses separate data lines for the trigger and echo signals. If the mode pin is tied to ground, then the unit uses one data line for both signals. These two configurations are illustrated nearby. Using the single pin mode conserves a data line on microcontrollers that are capable of toggling pins from input to output. For more details on the two modes and other hookup topics see the SRF05 Specification Sheet.

Here is a sample subroutine for the PICAXE using the two data line mode:

   ping:
    pulsout 7,1            ; send a 10uS trigger pulse on output 7
    pulsin  1,1,w0         ; read the pulse on input 1 and store the result in w0
    pause   10             ; SRF05 required 10mS recharge period after ranging completes
   return


Calculating distance based on the duration of the pulse is a fairly straightforward affair. If the pulse is known in microseconds it can be divided by 58 to yield the approximate distance in centimeters. PULSIN on the PICAXE returns a pulse measured in 10 microsecond increments - a PULSIN result of 15 equals 150 microseconds. Therefore to make a distance calculation on a PICAXE continuing the code sample above:

  w0 = w0 * 10 / 58  ; distance in centimeters


On other microcontrollers this pulse increment will likely be different. Basic Stamps vary across the product line - BS2 uses 2 microsecond increments. Arduino conveniently returns a pulse in 1 microsecond increments.

This calculation is based on the speed of sound being roughly 343 meters per second (or .0343 centimeters per microsecond). The time in microseconds multiplied by .0343 will give distance in centimeters. Remembering the result must be divided in half because the duration of the pulse is actually a round trip for the sound wave, then the full formula in this instance would be distance = duration * .0343/2. However distance = duration/58 provides a close-enough approximation, is easier to remember, and is a friendlier equation for many microcontrollers.

With all that said, it is extremely important to note that it is almost never necessary in navigation to make this conversion. Since the pulse value is proportional you can simply use the value without converting to a specific unit of measure such as centimeters or inches. In other words, if you want to set a 30 centimeter event threshold for a PICAXE driven circuit just use a PULSIN value of 174.

The uncertainty of certain variables will make absolute precision difficult. The speed of sound is not a constant and will change slightly with temperature and humidity. The timings of pulse reading operations on any microcontroller should be considered approximate unless the manufacturer specifically states otherwise.

There are a number of additional factors which can have an effect on the real world performance of any ultrasonic ranger. The size, composition, shape, and orientation of the obstacle can all play a role in accurate detection. The SRF05 might miss a particularly soft object because the echo is so dampened it gets lost. An object oriented away from the sensor might also be missed because the sound wave is reflected away from the receiving transducer.

In addition to these false negatives, false positive readings are also possible, especially in environments with multiple objects. A very common cause of false positives is insufficient time between trigger pulses resulting in the sonar detecting residual echoes from a prior trigger. A similar situation can occur when more than one sonar device is in use and trigger pulses are being either fired simultaneously or very close in time.

A proper understanding of the detection zone of the SRF05 is key to developing a successful detection and avoidance algorithm. The beam width (or beam angle) of ultrasonic range finders is typically described as being a cone of a certain angle (for the SRF05 it is about 55). This angle describes the arc at which the ultrasound pulse emanates from the transducer. The mistake is sometimes made to consider the detection zone of the sensor as being identical to this beam. The conclusion of this logic would be that the detection zone extends from the sensor in an even, steadily expanding arc until it reaches the 4 meter limit of the SRF05 range. In reality the detection zone expands at about 55 for the first meter and then the rate of expansion starts to decay. At about 2 meters distance from the sensor the angle of the detection zone is closer to 40 with the zone reaching a maximum width of about 80-100 centimeters. From this point the detection zone will begin to narrow, ultimately reaching zero when the distance from the sensor is a little more than 4 meters.

The actual detection zone will vary based on the size and shape of the object being detected. Smaller, rounder objects will have a narrower detection zone, while larger, broader objects will have a wider detection zone. However, for the sake of visualization, the elongated teardrop shape illustrated here will serve as a good general guide.

The shape of the detection zone has two significant implications. First, the zone is narrow near the sensor resulting in blind spots where it might be desirable to detect an object. Second, the zone is relatively wide - almost a meter - for much of the device's range. An object detected as 1.5 meters from the sensor could be anywhere across that 1 meter span.

With this understanding of the detection zone, an elementary avoidance strategy of aiming the sensor forward, moving forward until an object is detected at threshold, and then changing direction can be implemented. There are two constraints in this scenario to consider. If the threshold for object detection is set too close to the sensor then objects on a collision path might be in a blind spot. If the threshold is set at too great a distance from the sensor then objects will be detected which are not on a collision path (assuming that the width of the robot is less than a meter). A good strategy will strike the best balance between these constraints. At the near zones where the detection angle is 55, the width of the detection zone will be in a proportion of about 9.2 centimeters across for each 10 centimeters distance from the sensor. Therefore if the robot is 18.4 centimeters wide the threshold must 20 centimeters at an absolute minimum. (In practice the threshold should be set high enough to allow a reasonable margin for error and variation.) One common technique to reduce the potential for blind spots and achieve a greater detection width at close range is to mount the sensor some distance back from the very front of the robot.

A simple enhancement to this elementary strategy can be achieved by adding an additional SRF05 unit and mounting the two units facing forward, each angled slightly outward. If the setup is such that there is a region where the two detection zones overlap, then an algorithm can be established around four possible states according to the following truth table:

Left SensorRight SensorStatusAction
No ObstacleNo ObstacleClearMove Forward
ObstacleNo ObstacleLeft ObstacleSlight Right Turn
No ObstacleObstacleRight ObstacleSlight Left Turn
ObstacleObstacleCenter ObstacleBroad Turn (Left or Right)

Advantages of the two-sensor, four-state strategy include reduced blind spot regions and the ability to make tighter turns based on a more precise knowledge of the location of the obstacle. A disadvantage of the strategy is added cost for the sensor. The possibility of false positives from stray echoes is also increased, but this can be mitigated by ensuring that there is a delay between trigger signals.

When even greater precision is desired, a system can be engineered in which multiple readings are taken and compared. There is more than one approach to collecting multiple readings, but a common and practical solution is to mount the SRF05 on a servo and move the sensor incrementally in a sweeping pattern while taking readings and logging the results. As most hobby servos can be rotated with some degree of accuracy in 1 or 2 degree increments, quite a few consecutive readings can be taken to develop a sonar picture of the sweep area.

The information gleaned from these multiple readings opens up a world of possibilities. A very simple approach would be to compare all the trajectories, choose the trajectory that shows the longest unobstructed course, and head the robot in that direction. The robot would need to be maneuvered to the new heading which may present its own challenges. Once aligned in this "ideal" trajectory the robot could move forward while continuing to scan for near range obstructions. When an obstruction is encountered it can search for a new ideal trajectory. This basic strategy would allow for a robot that makes longer continuous runs and becomes trapped less often than those considered earlier.

Also with multiple readings, strategies can be built upon the understanding that in addition to detection zones revealing where an object is, the detection zone can also provide information about where an object is not. By combining these two pieces of information a significantly more accurate assumption can be made about the actual location of the object. In the nearby illustration you can see a graphic representation of the results of multiple sonar readings. As expected, the object being detected creates an echo response across a wide swath of readings. Taken in isolation each of of these "positive" readings shows a close range object, but it is not known whether or not travelling along that trajectory would result in a collision. It is only known that a collision is possible. However, when the image of the detection zone is superimposed along the path of a trajectory that registered as unobstructed, it reveals that neighboring trajectories which indicated possible obstructions are in fact clear.

The idea that nearby trajectories can be examined for the possibility that the current trajectory is safe, even if the reading shows a nearby obstacle, is certainly empowering. While taking full advantage of the information provided in the sonar sweep requires delving into the mathematics of triangles, a reasonably simple detection and avoidance algorithm can be developed using rules of thumb. Remember that for about the first 2 meters, as detected objects are further away, the detection zone is getting wider. This means that the further away an object is, the more likely it is that an echo does not indicate a collision course. At the same time, the further away the object is, the wider across is the "safety zone" of a neighboring trajectory which indicates no obstruction. If rather than 55, we consider a fairly conservative 40 detection zone at ranges of up to about one meter, it can be assumed that a trajectory which registers no obstruction is clear for about 3.4 centimeters in each direction for every 10 centimeters out from the sensor. For the sake of this discussion, consider a robot 15 centimeters wide.

If a measurement along the current trajectory shows an obstacle at less than 30 centimeters, then treat the trajectory as obstructed.

If a measurement along the current trajectory shows an obstacle between 30 centimeters and 50 centimeters, then a neighboring trajectory as far away as 5 to either side could indicate a safe zone.

If a measurement along the current trajectory shows an obstacle at between 50 centimeters and 70 centimeters then a neighboring trajectory as far away as 11 to either side could indicate a safe zone.

If a measurement along the current trajectory shows an obstacle at between 70 centimeters and 90 centimeters then a neighboring trajectory as far away as 13 to either side could indicate a safe zone.

If a measurement along the current trajectory shows an obstacle at between 90 centimeters and 110 centimeters then a neighboring trajectory as far away as 14 to either side could indicate a safe zone.

A scheme such as this one has several improvements over the earlier examples. It can search for obstacles at a much greater range and should enable a robot to travel much more smoothly with fewer required course corrections.

Hopefully the information provided here will be of some use in understanding and getting started with the SRF05. Although the examples provided in this article are notional, they should give you some sense of the great possibilities of the sensor even when used with very basic designs. Beyond these basics, these ideas can be built upon and applied to significantly more intricate and precise mathematical analysis and integrated with correspondingly sophisticated navigation and goal seeking behaviors for robotic projects.



Other Articles You Might Find Enjoyable

Introduction to the PICAXE Microcontroller

Design and Build Your Own Robot

K'nexapod - A Hexapod Robot Built With K'nex and PICAXE

Guide to PICAXE Selection

Setting Up A Differential Drive For Your PICAXE Project

Adding Memory with i2c EEPROMs

PICAXE USB to Serial Conversion

Basic PICAXE Servo Interfacing

Calculating Right Triangles with PICAXE BASIC

Understanding Variables and Symbols in PICAXE BASIC

Flexinol and other Nitinol Muscle Wires

Analog Sensors and the MSP430 Launchpad


Things to Do Here

All PICAXE Articles
Home
Links



Sully Station Technologies

Parts, Tools and Supplies for the Electronics Experimenter