Application of ultrasonic sensor¶
The purpose of the lesson is to show the application of the ultrasonic sensor. We suggest to the student to write a programme simulating the operation of the reversing (parking) sensor installed in the vehicles. We will use simple mathematical modelling for this purpose.
Preparing For This Tutorial:¶
The LEGO Mindstorm EV3 Robot that coincides with this tutorial comes from building specific sections found in the LEGO Mindstorm Education Core Set building instructions. Students will construct the Robot Educator base model, and then add the Ultrasonic Sensor according to the building instructions.
They could also use the Touch Sensor that could be added somewhere. Instead of Touch Sensor it could be used the Brick Button.
Time constraints:
Starting from 90 min - double lesson
Problems:
Can autonomous cars react to different obstacles before them?
Exercise:
Create a programme that will allow you to hear the sounds of different frequencies.
Prepare a programme that will allow you to check what is the range of the ultrasonic sensor?
Write a programme that will cause the robot to ‘’see’’ any obstacle and stop before it about 10 cm. Stop programme by pressing one of the brick buttons.
Modify the above programme to slow down the vehicle when it detects any obstacle and display the distance between an obstacle and the vehicle.
Modify the above programme that the robot generates the warning sounds (the volume depends on the distance between the vehicle and the obstacle).
Theory¶
Part 1¶
Create a programme that will allow you to hear the sounds of different frequencies.
Sound can propagate through a medium such as air, water and solids as waves. Sound that is perceptible by humans has frequencies from about 20 Hz to 20,000 Hz. Below, we’ll put a table containing the values of the notes and their frequency. If you are interested in this topic, please refer to the website https://pages.mtu.edu/~suits/notefreqs.html
Note |
C0 |
C4 |
D4 |
E4 |
F4 |
G4 |
A4 |
B4 |
C5 |
C8 |
---|---|---|---|---|---|---|---|---|---|---|
Frequency |
16.35 |
261.63 |
293.66 |
329.63 |
349.23 |
392.00 |
440.00 |
493.88 |
523.2 |
4186.01 (Hz) |
Part 2¶
The purpose of this part of the lesson is to use an ultrasonic sensor to measure the distance between the sensor and any obstacles. Create the program that will allow you to check the sensor range (3 – 2550 mm).
Writing the programme follow the instruction:
Import the necessary libraries.
Create an instance of the object UltrasonicSensor and set mode of the ultrasonic sensor into distance mode.
Create an instance of a button on the brick to finish activity.
Until the button isn’t pressed, the programme makes a loop.
The ultrasonic sensor measures the distance to the obstacle in front of it and converts mm to cm.
Print results on the screen.
You could use error handling (try-except) to detect the situation of improperly connected motors or sensors.
Test your programme.
Add robot movement to your program.
Import the library
Create instances of LargeMotors.
Run them at the set speed. Try modify your programme to obtain the results: The robot will stop when the distance from the obstacle is less than 50 cm .
Stop programme by pressing one of the brick buttons.
Part 3¶
(for primary school students)
Mathematics
The purpose of this part of the lesson is to see the relationship between the distance from the obstacles and the robot’s velocity (specifically the relationship between the distance from the obstacles and the power of motors). Note that for the EV3 motors variable speed isn’t really the speed of the vehicle but it is x% of the rated maximum speed (The real speed is about 33 cm/s when the battery is charged). To shorten it we will use the word speed instead power of motors. The robot stops when the speed is 0. When the speed (v) is equal to the distance (d) the robot will reach the obstacle (v = d).
Use the following table to discover the mathematical formulae between velocity and distance in the described situation: robot stops 10 cm before the obstacle.
d |
v=d |
New v1= |
---|---|---|
50 |
50 |
|
40 |
40 |
|
30 |
30 |
|
20 |
20 |
|
10 |
10 |
0 |
3 |
3 |
Add modificatication to the programme.
Part 3¶
(for secondary school students)
Mathematics
The purpose of this part of the lesson is to see the relationship between the distance from the obstacles and the robot’s velocity (specifically the relationship between the distance from the obstacles and the power of motors). Note that for the EV3 motors variable speed isn’t really the speed of the vehicle but it is x% of the rated maximum speed (The real speed is about 33 cm/s when the battery is charged). To shorten it we will use the word speed instead power of motors. The robot stops when the speed is 0. When the speed (v) is equal to the distance (d) the robot will reach the obstacle (v = d).
Use the function notation.
\(f:\lbrack 3,255\rbrack \rightarrow \lbrack - 100,100\rbrack.\ \)The domain of this function is the range of the ultrasonic sensor. The codomain is the range of servo motors. Now we put the restrictions:
\(f(10) = 0\)(to stop 10 cm in front of the obstacle) and \(f(255) = 100\) to have maximal speed when not having the obstacle. We will investigate the linear function \(f(x) = ax + b\), so we obtain the system of equations
\({}_{255a + b = 100}^{10a + b = 0}\).
The solutions is \(a = 0.408,\ b = - 4.08\) and the function is \(f(x) = 0.408x - 4.08\)
We can plot a graph of this function and analyze it.
Part 4¶
(for primary school students)
Modify the above programme to hear sound when the robot is near the obstacle. Note that the sound should be very short. A sound called Bip.wav (taken from LEGO materials) has been prepared especially for this exercise.
Mathematics
The purpose of this part of the lesson is to see the relationship between the distance from the obstacles and the sound volume (in the range [0,100]). In the table presented above you could add new column and denote the sound volume by volume.
d |
v=d |
v1=d-10 |
volume |
---|---|---|---|
50 |
50 |
40 |
0 |
40 |
40 |
30 |
10 |
30 |
30 |
20 |
|
10 |
10 |
0 |
|
3 |
3 |
-7 |
Part 4¶
(for secondary school students)
We would like to find the function from [3,255] to [100, 0] (exactly into). Why we have [100,0] not [0, 100] - The loudest when we’re closest. So we have:
\(f(3) = 100,\ f(255) = 0\)and we will investigate linear function \(f(x) = ax + b\). We obtain the equations
\(3a + b = 100\)
\(255a + b = 0\)
and \(a = 0,3968 \approx 0.4,\ b = 98.8\)
\(f(x) = 0.4x + 98.8\)
Short help on programming¶
Commands/functions needed for the exercise
for sounds (for details see Module Sounds) from ev3dev2.sound import Sound
Import library sound=Sound()
Create an instance | tone(frequency in Hz, time in milliseconds) | speak(“Text”)
Function
Example:
#!/usr/bin/env python3
sound=Sound()
sound.speak("c 0")
sound.tone(16.35, 1000)
Import the library and create an instance of class UltrasonicSensor Plug a sensor into any sensor port
#!/usr/bin/env python3
from ev3dev2.sensor.lego import UltrasonicSensor
Implicit : port 4 = ultrasonic
us = ev3.UltrasonicSensor()
Set mode of ultrasonic sensor.
us.mode='US-DIST-CM'
Put the US sensor into distance mode.
Function of UltrasonicSensor class allow you to measure the distance between the sensor and the obstacle (output 0 – 2550mm). value()
Example:
us.value()
Import the library and create an instance of class Button
from ev3dev2.button import Button
Function any - Checks if any button is pressed.
btn=Button()
Example:
while not btn.any():
pass
Import the library and create an instance of class LargeMotor
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor,OUTPUT_B,OUTPUT_C
R**otate the motor at **speed** 'forever'**
mb = LargeMotor(OUTPUT_B)
Turn off the motor
on(speed, brake=True, block=False)
off()