Saturday, November 16, 2013

HC-SR04 : using multiple ultrasonic modules





In this note we are going to illustrate how three (or more) HC-SR04 modules can be used to add further "intelligent" vision to your bots. Basic usage of HC-SR04 and the principles on which it is based have already be given in this note.

The idea is to position the three sensors at fixed angles one another. Each module will give its own measure. Then, we use the three measures to try to induce some "safe/unsafe" state for our bot. In a safe state you can, for example, keep running forward. In an unsafe state, you can turn left or right according to the informations of the two other sensors. Indeed, consider the situation illustrated in Figure 1.

Figure 1. The general situation.



How to position the sensors

It is important to dispose the two additional ultrasonic modules (HC-SR04 #1 and #3 in Figure 1) at fixed angles on an ideal circle of radius R centered in the origin. To be able to do that in practice, just place your sensor #2 and decide how much R should be. Take as the origin of the axis the point P that it is a distance R from sensor #2 on the normal wrt the sensor #2.
   Then, take a stripe of length R, fix one end of the stripe at P and turn the other end left of a fixed angle, say θ and mark this point as P1. Do the same to the right and mark the new point as P3. Finally, place sensor #1 perpendicular to the line passing through P and P1 and dock it at P1. Do the same for sensor #3 wrt the point P3.

    Clearly, the question now is: which values to choose for θ and R ? Concerning θ, recall that in our previous post (see it here) we said that HC-SR04 starts giving erratic results when the obstacle is at angles greater than 30°. Therefore, a good choice could be 30°. Indeed, it is a good choice also because sin and cos functions take well known values at this angle and this will ease and speed up our calculations.
   Concerning R we have also lots of possibilities, we should just pay attention that one sensor do not cover or interfere too much with the others. To obtain a nice visual effect and to minimize interferences, we can just that the minimal R such that sensor #1 touches the left side of sensor #2 and sensor #3 touches the right part of #2. In Figure 2 we have reproduced this situation and we have indicated all the interesting quantities that will help to compute minimal R.
Figure 2. Computation of the minimal value for R.

Call L the length of the HC-SR04 module (xx mm). Then, the segment PhH has length L/2, same as the segment P1P12.
We now need to compute the length of the segment P1Ph. To this purpose, remark that the angle $P_1\hat{P_2}H$ is 60° and therefore the angle $P_{12}\hat{P_1}P_h$ is 30°. Therefore P 1 P h = L / 2 cos 30°. Hence


Finally

Approximating, as usual, $\cos 30°$ cos 30 with 0.866 we obtain



How to extract useful informations from the three sensors

At the beginning we said that we wanted to use additional sensors to increase the "cleverness" of our bot. Hence, assume that we have our sensors operating and we get three measures D1, D2 and D3 (see Figure 3). The point is to compute the quantities H1 and H2 in order to understand if the robot can pass through obstacle #1 and #3, for example. We will also compute L1, L2 and L3 in order to decide toward which of the obstacle is better to go the robot cannot go through obstacle #1 and #3.


Figure 3. Trying to extract useful information.



The quantities we need are computed using basic algebra and some trigonometry. Recall that R is known and, of course, D1, D2, D3 are also known. Therefore, we have to solve the following system of equations:



which gives


Taking $R=42mm$ as above we obtain



all quantities are expressed in millimeters.

The test circuit

After lots of theory, let's turn to applications! Figure 3 presents the test circuit. Remark that here I used the Arduino Proto Shield just for fun! Of course, it is not necessary at all, you can use any breadboard or simply wire together cables that go towards the same Arduino pin.


Figure 4. The test circuit.
Needed material:
  1x Arduino
  3x HC-SR04 ultrasonic module
  1x Arduino Proto Shield (or any breadboard)
  Cables

The test program

The following diagram resumes the "intelligence" we are going to program in our bot. I leave the translation of the diagram to Arduino code to the reader :-)
Figure 4. Adding some intelligence...

RW = robot width
S = safety width

The quantity S is called safety width since it should be a width that we add to our measures in order to overcome possible measurement errors or to give enough time to the robot to make another measurement before knowing the obstacle. Obviously, this quantity depends on the robot speed.

Future developments

The test circuit (Figure 3) uses lots of pins on Arduino and hence we are left only with few pins for other functions/modules of our bot. In the near future I will present a general method on how to save pins. So, once more, stay tuned!

See also

Introduction to HC-SR04
HC-SR04: what is the distribution law for the frequencies of values? (Part 1)
HC-SR04: what is the distribution law for the frequencies of values? (Part 2)
HC-SR04: using a sonar sensor for motion detection
Look here for a practical implementation!

Enjoy!

2 comments :

  1. This is a really great guide nice work!
    Only thing is, in reality the HC-SR04 sensors are wide enough that 42mm doesn't work well when they are next to each other. Its a nice number in theory but isn't possible

    ReplyDelete
  2. Indeed, this is the min theoretical value. In practice one should adjust a bit. In the post, the method exposed provide hints on how to recompute adjustments...

    ReplyDelete