TigerJython4Kids
 robotics
ultrasonic
Deutsch   English   

4. ULTRASONIC SENSOR

 

 

YOU LEARN HERE...

 

how you can measure and evaluate distances with an ultrasonic sensor.


 

HOW AN ULTRASONIC SENSOR WORKS?

 
An ultrasonic sensor has a transmitter and a receiver for the ultrasonic waves. When measuring distance, it transmits a short ultrasonic pulse and measures the time it takes for this to travel to the object and back again. From this, it can determine the distance using the known sound velocity.  

The Maqueen sensor can measure distances in the range from approx. 5 to 200 cm and returns the values in cm. If there is no object in the measuring range, it returns the value 255.



 

EXAMPLES

 


Example 1: Recognise an object

The robot moves forwards and measures the distance to the object in front of it every 200 milliseconds. If the distance is less than 20 cm, it stops..

 

from mbrobot import *
#from mbrobot_plus import *

forward()
repeat:
    d = getDistance()
    print(d)    
    if d < 20:
        stop()    
    delay(200)
Copy to clipboardn

The getDistance() function returns the distance to the object. The sensor value is queried in an infinite loop and stored in the variable d. delay(200) specifies the measurement period. Use the if structure to check the sensor values. The indented instruction stop() is only executed if the condition after if is fulfilled. If the robot is connected to the computer via USB cable, the sensor values are displayed in the terminal window with print(d).


Example 2: A control system for the distance to the object


Your programme should ensure that the robot stays at a certain distance from your hand. If it is too close, it should move backwards, otherwise forwards.

You use the if-else structure to evaluate the sensor values. If the condition after if is correct, the statement after if is executed, otherwise the statement after else is executed.


 


from mbrobot import *
#from mbrobot_plus import *

setSpeed(30)
forward()
repeat:
    d = getDistance()
    if d < 20:
        backward()
    else: 
        forward()    
    delay(100)
Copy to clipboard


Example 3: Search object
A robot with an ultrasonic sensor is to find an object and then travel to it and knock it over. At the start, the robot is facing in any direction, i.e. it must first turn until it detects the object.

 

from mbrobot import *
#from mbrobot_plus import *

def searchTarget():
   repeat:  
      right()
      delay(50)    
      dist = getDistance()
      if dist != 255:
         right()
         delay(500)
         break
 
setSpeed(20)
searchTarget()
forward()
Copy to clipboard

You define the search process in the searchTarget(). function. The robot turns a small angle to the right and measures the distance. If it detects an object (the sensor no longer returns the value 255 it rotates a little further so that it is directed towards the centre of the object. You can cancel the repeat loop with break to end the search process. The searchTarge() function is called in the main programme and the robot moves to the object after the successful search process. Solve task 3 and complete the programme so that the robot stops in front of the object.

A modern industrial plant without sensors is hardly conceivable today. We are also surrounded by sensors in our everyday lives. Modern cars have 50 - 100 different sensors: Distance sensors, rpm and speed sensors, petrol tank fill level sensors, temperature sensors, etc.

When parking, distance sensors are used that work in a similar way to those of our small mbRobot.

 

 

 

 

REMEMBER...

 

The getDistance() instruction returns the value of the ultrasonic sensor. The sensor values are measured periodically in a repeat loop, delay(100) determines the measurement period. This instruction is important because otherwise the values are queried too frequently, which can lead to the microprocessor being overloaded.

 

 

TO SOLVE YOURSELF

 

 

1.

Das Regelungssystem im Beispiel 2 ist noch nicht optimal. Wenn du die Hand ganz wegnimmst, reagiert der Roboter oft verwirrt, da er entweder weit entfernte Gegenstände oder gar nichts detektiert. Optimiere das Programm so, dass der Roboter im Fall, dass die Distanz grösser als 50 cm ist, stehen bleibt.

 
if d < 20:
   ... 
elif d >= 20 and d < 50:
   ... 
else:
   ...    
  Dazu verwendest du die if-elif-else-Struktur, mit welcher du eine mehrfache Auswahl programmieren kannst.

2.

The control system in example 2 is not yet optimal. If you remove your hand completely, the robot often reacts in a confused manner, as it either detects distant objects or nothing at all. Optimise the program so that the robot stops if the distance is greater than 50 cm.


 
3.

A robot should find an object in the same way as in example 3 by turning in place and analysing the values from its ultrasonic sensor. When it detects an object, it moves towards it and stops at a distance of 10 cm from the object.

4.
A robot with an ultrasonic sensor tries to find the exit from a confined space. It first turns slowly in place and if it does not detect a wall, it moves off.  

 

 

ADDITIVE: ULTRASONIC SENSOR IN SIMULATION MODE

 

Distance measurement with an ultrasonic sensor can also be carried out in simulation mode. To do this, you must enter the coordinates of the object (target) in the graphics window.

Example 4: Ultrassonic sensor simulation
The robot should travel to the obstacle. If the distance is less than 30 cm, it moves backwards a short distance and then forwards again.

For the simulation, use a horizontal bar as the obstacle, which you create with the following context:

 
target = [[200, 10], [-200, 10], [-200, -10], [200, -10]]
RobotContext.useTarget("sprites/bar0.gif", target, 250, 100)

The first line contains the coordinates of the corner points of the object (a rectangle with the centre at (0,0)). In the graphics window, the object is displayed at the position (250, 100) with the image bar0.gif. The images are included in the TigerJython distribution (Image library).

from mbrobot import *
#from mbrobot_plus import *

target = [[200, 10], [-200, 10], [-200, -10], [200, -10]]
RobotContext.useTarget("sprites/bar0.gif", target, 250, 100)

forward()
repeat:
    d = getDistance()
    if d < 30:
        backward()
        delay(2000)
    else: 
        forward()    
    delay(100)
Copy to clipboard

In the simulated EV3, the ultrasonic sensor is placed on top of the brick and not at the front. This is why the robot moves a little closer to the beam.

Example 5: Search object in simulation mode
A robot with an ultrasonic sensor rotates slowly around the location. When it detects an object, it moves towards it and stops briefly in front of it.
You write a searchTarget() function that defines the search process: The robot turns to the right in small steps, measuring the distance dist. If it detects an object (dist != -1), it turns a little further and interrupts the search process witht break.

 

Danach fährt er mit Hilfe einer zweiten repeat-Schleife so lange vorwärts, bis die Distanz zum Objekt kleiner als 30 ist. Mit dem Befehl setBeamAreaColor() kannst du den Suchvorgang besser veranschaulichen.

from mbrobot import *

target = [[50,0], [25,43], [-25,43], [-50,0],[-25,-43], [25,-43]] 
RobotContext.useTarget("sprites/redtarget.gif", target, 400, 400)

def searchTarget():
   repeat:  
      right()
      delay(50)    
      dist = getDistance()
      if dist != -1: 
         right()
         delay(600)
         break
 
setBeamAreaColor(Color.green) 
setSpeed(10)
searchTarget()
forward()

repeat:
   dist = getDistance()
   print(dist)
   if dist < 20:
      stop()
      break
Copy to clipboard

It then moves forwards with the help of a second repeat loop until the distance to the object is less than 30. You can use the setBeamAreaColor() function to better illustrate the search process..


 

REMEMBER...

 

You can also simulate the ultrasonic sensor. You must describe the obstacle with the coordinates of the corner points. If the simulated sensor does not detect an object, it returns the value -1 (the real robot returns 255). For real mode, you must deactivate the lines with RobotContext.


 

TO SOLVE YOURSELF

 
5.

A robot with an ultrasonic sensor is to find and knock over three tall, light objects one after the other. It rotates slowly in place and when it detects an object, it moves towards it a little faster until it has brought it down. It then travels back a short distance and searches for the next object.

In simulation mode you use the following RobotContext:

 

 
mesh = [[-30, -30], [-30, 30], [30, -30], [30, 30]]  
RobotContext.useTarget("sprites/squaretarget.gif", mesh, 350, 250)  
RobotContext.useTarget("sprites/squaretarget.gif", mesh, 100, 150)  
RobotContext.useTarget("sprites/squaretarget.gif" ,mesh, 200, 450)  
RobotContext.setStartPosition(40, 450)
RobotContext.setStartDirection(250)

6.

The robot should travel along a rectangular area that is bounded on all sides as if it were mowing it like a lawnmower. If it detects a boundary with its ultrasonic sensor, it turns 90 degrees, travels a short distance parallel to the boundary, turns another 90 degrees and travels forwards again until it reaches the opposite boundary. It repeats this movement until it has mowed the entire field.

In simulation mode, you can use the following field boundary

 

 
target = [[200, 10], [-200, 10], [-200, -10], [200, -10]]
RobotContext.useTarget("sprites/bar0.gif", target, 250, 20)
RobotContext.useTarget("sprites/bar0.gif", target, 250, 480)
RobotContext.setStartPosition(430, 480)

The task is not so simple, as the robot must rotate alternately left and right. This can be solved with the following trick: you define a function turn(n), which causes a turn to the left for even n and a turn to the right for odd n. You must set n to 0 at the beginning.

def turn(n):
if n % 2 == 0:
left()
else:
right()
delay(540))
: