In this activity, students will build a robot that uses two sonar sensors to avoid obstacles. Students will need to attach the two sensors to the robot.
<aside> 💡 You could ask the students for thoughts about how two sensors might be helpful.
A single sonar sensor gives us the distance to the nearest detected obstacle. However, this yields little information about the obstacle’s position. All we know is an obstacle is somewhere in front of the robot. Using two sensors, the distances they return can be compared. If the distance picked up by the left sonar sensor is lower, one can assume that the obstacle is more to the robot's left. Or perhaps there is an obstacle at the left that the right sensor cannot detect (which means that obstacle is either further away or smaller). In either case, turning right to avoid this obstacle makes sense. In summary, having two sensors makes it possible to decide whether to turn left or right.
</aside>
The Thingiverse page provides several brackets that can be 3D printed to facilitate mounting two sensors on the robots (two versions of the brackets are used in the image below). Alternatively, the spacing of the holes on the robot is compatible with Lego Technic. In the past, we have provided students with Lego and asked them to be creative about mounting the robot's sensors.
The red brackets were 3D printed. The STL files accompanying these lesson plans are available on the Thingiverse page.
Students could be asked to design a program for the robot that allows it to avoid obstacles using the sonar sensors. Below, we discuss the linked example program. This program assumes that the left sonar sensor is connected to port 1 and the right sensor to port 2. A screenshot of the program is provided below.
sonar_obstacle_avoidance - mBlock Community
<aside> 💡
Clicking the link to the program will open the mBlock website. To see the actual program, click Source
at the bottom left of the page that opened.
You can use the program in the online version of mBlock or download it to your computer by selecting File
and Save to your computer
. The downloaded program can then be edited using mBlock if installed on your computer.
See Step 1: Open the example program for an example and more instructions.
</aside>
The main program loop is shown first. This loop continuously goes through the same steps. It queries the sensors. If the left sonar sensor detects the smallest distance (min_side = left
), the robot turns left as long as the left sonar returns a distance smaller than the safe distance. If the right sonar detects the shortest distance, the robot turns right as long as the right sonar returns a distance smaller than the safe distance.
The safe distance is defined at the start of the program. Here, it is set to 30 cm. The result is that the robot turns left or right (on the spot) if one of the sensors returns a distance smaller than the safe distance. And the robot keeps turning until that sensor returns a large enough value.
Let's break down the program into simpler terms:
do_sensing
: This block collects data from both sonar sensors. It measures the distances to obstacles on the left and right sides of the robot. It also multiplies the values by 1.25. See here for an explanation of why this multiplication is performed.).min_distance
: This variable stores the smaller of the two distances measured. It tells us how close the nearest obstacle is, regardless of which side it's on.min_side
: This variable tracks which sensor (left or right) detected the closest obstacle. It's crucial to decide which way the robot should turn to avoid the obstacle.Here's how these elements work together:
do_sensing
.min_distance
and min_side
.