HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
infrarotsensor
Deutsch   English   

5. INFRARED SENSORS

 

 

YOU LEARN HERE...

 

how the robot can recognise the brightness of the surface with its infrared sensors.


 

HOW DOES AN INFRARED SENSOR WORK?

 

An infrared sensor consists of a light-emitting diode (LED), which emits light in the infrared range, and a photodiode, which measures the intensity of the reflected light.

 

As the infrared light reflects differently on light and dark surfaces, the sensors can differentiate between a dark and light surface and return a digital value of 0 (dark) or 1 (light).

The mbRobot has 2 infrared sensors (irLeft, irRight). They are located on the lower side of the board. The Maqueen Plus V2 has 5 infrared sensors (irL1, irL2, irL3, irR1, irR2, irM), the Maqueen Plus even has 6 infrared sensors (irL1, irL2, irL3, irR1, irR2, irL3, irR3), where irLeft = irL1 and irRight = irR1. This means that all subsequent programmes can be used for all models.


 

EXAMPLES

 

Example 1: Follow edge
The robot is to follow a dark surface with its left infrared sensor. Depending on whether it ‘sees’ dark or light, it corrects the direction of travel with a right or left turn. The sensor values are scanned in an endless loop with a measurement period of 100 ms.

The sensor value is saved in the variable v with the instruction v = irLeft.read_digital().

 

Program:

from mbrobot import *
#from mbrobot_plus import *

repeat:
   v = irLeft.read_digital()
   if v == 0:
      rightArc(0.1)
   else: 
      leftArc(0.1)
   delay(100)
► Copy to clipboard

You can see that the robot control depends on the radius of the left or right arc. If it is too small (e.g. 0.05), the robot moves very slowly and unsteadily. If it is too large (e.g. 0.6), it often loses the track.


Example 2: Following a path
To follow a path, as self-driving cars do, you need several sensors. In a very simplified version of an autonomous vehicle, you use two infrared sensors.

You store the measured values of the right and left sensors in the variables vR and vL
vR = irRight.read_digital()
vL =irLeft.read_digital()

Then you develop an algorithm with which the robot can follow the path as accurately as possible.

 

Program:

from mbrobot import *
#from mbrobot_plus import *

setSpeed(30)
repeat:
    vR = irRight.read_digital()
    vL = irLeft.read_digital()   
    if vL == 0 and vR == 0:
        forward()  
    elif vL == 1 and vR == 0:
        rightArc(0.1)        
    elif vL == 0 and vR == 1:
        leftArc(0.1)    
    delay(100)
► Copy to clipboard

Depending on the size of the replicated template, you must adjust the speed and the radius of the arcs.


Example 3: Travelling in a square

In real mode, it is difficult to control motors in such a way that the robot remains on a square path for a longer period of time. With the help of the infrared sensors, the robot can correct its direction itself. To structure the programme better, define a keepOnTrack(), function that controls the movement of the robot on the straight sections of the path. A right-angled change of direction occurs when the robot ‘sees’ light with both sensors. It then turns 90° to the left and continues its journey on the strip.

 

Program:

from mbrobot import *
#from mbrobot_plus import *
def keepOnTrack():
    if vL == 0 and vR == 0: 
        forward()
    elif vL == 0 and vR == 1: 
        leftArc(0.1)
    elif vL == 1 and vR == 0:
        rightArc(0.1)
        
repeat:
    vR = irRight.read_digital()
    vL = irLeft.read_digital()
    if vL == 1 and vR == 1:
        left()
        delay(500)
    else:    
        keepOnTrack()
    delay(100)        
► Copy to clipboard

has six infrared sensors. You can see their designation at the bottom of the chassis. They return the values 0 (dark) or 1 (light) with the commands:
irLeft.read_digital() or irL1.read_digital()
irRight.read_digital() or irR1.read_digital()
irL2.read_digital()
irR2.read_digital()
irL3.read_digital()
irR3.read_digital()


Example 4: Using infrared and ultrasonic sensors simultaneously

A robot moves along the black path with the help of the two infrared sensors. If it ‘sees’ an obstacle closer than 20 cm in front of it, it turns around and moves back. You can also have two robots drive against each other. If your programme is correct, they should not collide.

 

Program:

from mbrobot import *

setSpeed(30)
repeat:
    vR = irRight.read_digital()
    vL = irLeft.read_digital() 
    d = getDistance()
    if d < 20:
        stop()
        left()
        delay(1500)
        stop()
    else:
        if vL == 0 and vR == 0:
            forward()  
        elif vL == 1 and vR == 0:
            rightArc(0.1)        
        elif vL == 0 and vR == 1:
            leftArc(0.1) 
        elif vL == 1 and vR == 1:
            backward()        
    delay(100)
► Copy to clipboard

Die Sensorwerte werden alle 100 Millisekunden in der gleichen repeat-Schleife abgefragt. Der Programmblock nach else entspricht dem Programmcode im Beispiel 2.

 

 

REMEMBER YOU...

 

You can use the infrared sensors to measure the brightness of the base. The irLeft.read_digital() function returns the value of the left infrared sensor as 0 if the base is dark or 1 if it is bright.

Several different sensors can be used at the same time.

 

 

TO SOLVE BY YOURSELF

 

 


1.


The robot should move endlessly on a square table with a white inner circle without falling off. It starts in the middle and moves straight ahead. If it detects the edge, it moves backwards, turns approximately 90 degrees to the left and then moves forwards again.

 
   
2.

When following the paths with intersections, the robot often loses the track, especially if you let it drive faster. Complete the program from example 2 for the case that the robot loses the track (bright-bright “sees”). Think about what it can do in this case.

 

 
3.

The robot should start on a white surface and then stop for 2 seconds when it detects the first black stripe and continue (as at a stop). When it detects the second stripe, it stops permanently (terminal station).

 

Note: The solution is not quite simple, because if the robot stops at the stop because it “sees” black, it still “sees” black, even if it continues across the strip. You have to use a variable s to remember the current state of the robot, e.g.
s = 0: first drive on white,
s = 1: stopped at the stop,
s = 2: between stop and terminus on white.
If you get stuck in the programming, you can do a little bit of spicing up here.

4.

 

A robot is to drive across five dark stripes and detect them with its infrared sensors. It should stop when it detects the fourth stripe.

 
Note: You have to find a similar trick as in the previous task. For example, you can store the number of patrols covered in the variable s. However, the value of this variable is only increased by 1 when the robot changes from light to dark. You therefore need a second variable in which you store the light or dark state.

5.

 

Your program should control the robot so that it moves along the edge as precisely as possible with the help of its two infrared sensors.

 

 

 

   

 

5-4
Technical Notes:

At the beginning, the state variable s is set to zero. In the while loop, the state is switched:

....

s = 0
forward()
repeat:
    v = irLeft.read_digital()
    if v == 0 and s == 0:

        s = 1
        ...
    if v == 1 and s == 1:   

        s = 2

    if v == 0 and s == 2:

        ...