Application of an ultrasonic sensor in vehicles

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.

Prerequisites

What students should know before

  • the concept of ultrasounds, infrasounds

  • range of human hearing

  • The lesson is presented in two variants for students who do not know the concept of functions and for those who know the definition of a linear function, the notion of domain, the codomain. In addition, the teacher could expand the concept of functions onto and into a set.

  • Basic Lessons: Sound, Display, Motors

Time constraints:

  • starting from 90 min - double lesson

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.

Problems:

  • Can autonomous cars react to different obstacles before them?

Effects

Mathematics - After this lesson students will be able to prepare the mathematical model of the parking sensor.

Computer science - This lesson will tell students how to create the program using the ultrasonic sensor.

Physics - After this lesson students will understand how the ultrasonic sensor works.

Exercise

  1. Create a programme that will allow you to hear the sounds of different frequencies.

  2. Prepare a programme that will allow you to check what is the range of the ultrasonic sensor?

  3. Write a programme that will cause that the robot to ‘’see’’ any obstacle and stop before it about 10 cm. Stop programme by pressing one of the brick buttons.

  4. Modify the above programme to slow down the vehicle when it detects any obstacle and display the distance between an obstacle and the vehicle.

  5. Modify the above programme that the robot generates the warning sounds (the volume depends on the distance between the vehicle and the obstacle).

Example solution

Part 1

Physics

In this part students listen to the sounds of different frequencies. During this fragment of the lesson you can remind students of the ranges of audible sounds, recall information about infrasound and ultrasounds. You can ask about animals that use infrasound, ultrasound and echolocation. You can ask about the usage of ultrasounds, e.g. in medicine, on ships, in vehicles.

Theory

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)

Computer science

Step 1:

File here

Check out this text to create a programme that will allow you to hear the sounds of different frequencies.

1.  #!/usr/bin/env python3

2.  from ev3dev2.sound import Sound

3.  #Play a SINGLE tone for 1 seconds

4.  try:

5.    sound=Sound()

6.    sound.speak("c 0")

7.    sound.tone(16.35, 1000)

8.    sound.speak("c 4 to C 5")

9.    sound.tone(261.63, 1000)

10.   sound.tone(293.66, 1000)

11.   sound.tone(329.63, 1000)

12.   sound.tone(349.23, 1000)

13.   sound.tone(392.00, 1000)

14.   sound.tone(440.00, 1000)

15.   sound.tone(493.88, 1000)

16.   sound.tone(523.2, 1000)

17.   sound.speak("c 8")

18.   sound.tone(4186.01, 1000)

19. except Exception as e:

20.   print(e)

Part 2

Physics

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).

Computer science

Step 1:

File here

Check out this text to configure and test how the ultrasonic sensor works. Firstly, import the necessary libraries (lines 2-3). Next, create an instance of the object UltrasonicSensor (line 6) and set mode of the ultrasonic sensor into distance mode (line 9). In line 11 we create an instance of a button on the brick to finish activity. Until the button isn’t pressed, the programme makes a loop (lines 13-16). In line 15 the ultrasonic sensor measures the distance to the obstacle in front of it and converts mm to cm. In line 16 we present results on the screen. The programme uses error handling (try-except) to detect the situation of improperly connected motors or sensors (lines 5 and 17-18).

1.  #!/usr/bin/env python3

2.  from ev3dev2.sensor.lego import UltrasonicSensor

3.  from ev3dev2.button import Button

4.
5.  try:

6.    us = UltrasonicSensor()

7.    #Set mode of ultrasonic sensor

8.    # Put the US sensor into distance mode.

9.    us.mode='US-DIST-CM'

10.
11.   btn=Button()

12.   while not btn.any():

13.     # Stop program by pressing button sensor

14.     # us sensor will measure distance to the obstacle in front of it.

15.     distance = us.value()/10 # convert mm to cm

16.   print(str(distance))

17. except Exception as e:

18.   print(e)

Step 2:

File here

Check out this text to configure and test how the ultrasonic sensor works during movement of robots (the changes or new part of code are marked in yellow). After importing the library (line 3), we create instances of LargeMotors (lines 7,8) and run them at the set speed (lines 11,12). When the loop is finished, motors stop (lines 17,18). Note that we do not use buttons on the brick to finish the action of the robot. The robot will stop when the distance from the obstacle is less than 50 cm (line 13).

1.  #!/usr/bin/env python3

2.  from ev3dev2.sensor.lego import UltrasonicSensor

3.  from ev3dev2.motor import LargeMotor,OUTPUT_B,OUTPUT_C

4.
5.  try: motors to port B and port C

6.    mb = LargeMotor(OUTPUT_B)

7.    mc = LargeMotor(OUTPUT_C)

8.    us = UltrasonicSensor()

9.    #Connect two large

10.   us.mode='US-DIST-CM'

11.   mc.on(10)

12.   mb.on(10)

13.   while us.value()/10>50:

14.     distance = us.value()/10 # convert mm to cm

15.   print(str(distance))

16.
17.   mc.off()

18.   mb.off()

19. except Exception as e:

20.   print(e)

Summary

We have just created a program that gives us the opportunity to check how the ultrasonic sensor works while motion.

Part 3

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). Students should consider changing the above relationship so that the robot stops 10 cm before the obstacle (v=d-10).. In this exercise we put the variable speed equal value of the distance from the obstacle but the maximum value for speed will be 100 (not 255 because it couldn’t be greater than 100).

Computer science

Step 1:

File here

Check out this text to programme the relationship between the distance from the obstacles and the power of motors. Note that we use one brick button again to finish the program (lines 9 and 13). Lines 16-18 satisfies the condition v=d (when v is less than 100).

1.  #!/usr/bin/env python3

2.  from ev3dev2.motor import LargeMotor,OUTPUT_B,OUTPUT_C

3.  from ev3dev2.button import Button

4.  from ev3dev2.sensor.lego import UltrasonicSensor

5.
6.  try:

7.    mb = LargeMotor(OUTPUT_B)

8.    mc = LargeMotor(OUTPUT_C)

9.    btn=Button()

10.   us = UltrasonicSensor()

11.   us.mode='US-DIST-CM'

12.
13.   while not btn.any():

14.     distance = us.value()/10 # convert mm to cm

15.   print(str(distance) + " " )

16.   if distance>100:

17.     distance=100

18.     mc.on(distance)

19.     mb.on(distance)

20.
21.     mc.off()

22.     mb.off()

23. except Exception as e:

24.   print(e)

Step 2:

File here

Mathematics

Modify the above programme to obtain better results i.e. the robot stops at least 10 cm before the obstacle. With younger students, you can use the table presented below to help them discover the mathematical formulae. What happens when the obstacle “approaches” the robot. Why? To get the desired result, just change 16-19 lines in the above programme.

d

v=d

New v1=?

50

50

40

40

30

30

20

20

10

10

0

3

3

#!/usr/bin/env python3

if distance>110:

  distance=110

  mc.on(distance-10)

  mb.on(distance-10)

Step 3:

File here

If you are working with older students who are familiar with the concept of functions, 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.

image0

To get the desired result, just change lines in the above programme (note that we have not if condition)

#!/usr/bin/env python3

while not btn.any():

   distance = us.value()/10 # convert mm to cm

   print(str(distance) + " " )

   mc.on(0.408*distance-4.08)

   mb.on(0.408*distance-4.08)

Note If you would like to obtain the same result as in table put \(\ f(50) = 100\ \)instead \(\ f(255) = 100.\)

Summary

We have just created a programme that generates a relationship between the distance from the obstacles and the power of motors. Moreover, our robot stops before the obstacle.

Part 4

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.

File here

Mathematics

Step 1:

File here

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 (volume=50-d or better volume=(50-d)*2). However, we would like us to hear the sound when the obstacle is no more than 50 cm.

#!/usr/bin/env python3

from ev3dev2.sound import Sound

from ev3dev2.motor import LargeMotor,OUTPUT_B,OUTPUT_C

from ev3dev2.button import Button

from ev3dev2.sensor.lego import UltrasonicSensor

try:

  sound=Sound()

  mb = LargeMotor(OUTPUT_B)

  mc = LargeMotor(OUTPUT_C)

  btn=Button()

  us = UltrasonicSensor()

  us.mode='US-DIST-CM'

  while not btn.any():

    distance = us.value()/10 # convert mm to cm

  print(str(distance) + " " )

  if distance<50:

    sound.play_file('Bip.wav',volume=int((50-distance)*2))

  if distance>110:

    distance=110

    mc.on(distance-10)

    mb.on(distance-10)

  mc.off()

  mb.off()

except Exception as e:

  print(e)

d

v=d

v1=d-10

volume

50

50

40

0

40

40

30

10

30

30

20

10

10

0

3

3

-7

Step 2

File here

If you use function notation - 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\)

Computer science

Check out this text to programme the relationship between the distance from the obstacles and the robot’s power of servomotors and the sound volume. To the program from file we add lines marked in yellow.

#!/usr/bin/env python3

from ev3dev2.sound import Sound

from ev3dev2.motor import LargeMotor,OUTPUT_B,OUTPUT_C

from ev3dev2.button import Button

from ev3dev2.sensor.lego import UltrasonicSensor

try:

  sound=Sound()

  mb = LargeMotor(OUTPUT_B)

  mc = LargeMotor(OUTPUT_C)

  btn=Button()

  us = UltrasonicSensor()

  us.mode='US-DIST-CM'

  while not btn.any():

    distance = us.value()/10 # convert mm to cm

    print(str(distance) + " " )

    mc.on(0.408*distance-4.08)

    mb.on(0.408*distance-4.08)

    sound.play_file('Bip.wav',volume=int(0.4*distance+98.8))

  mc.off()

  mb.off()

except Exception as e:

  print(e)

Summary

We have just created a program that allows us to simulate the operation of the parking sensor.

All source codes presented above are placed in the file

Next Section - Circle and its applications