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.

 

The infrared sensors can register changes in the immediate field of vision and are often used in practice as motion detectors.

The Callibot has 2 infrared sensors. They are located on the lower side of the board. As infrared light reflects differently on light and dark surfaces, these sensors can differentiate between a light and dark surface and return a digital value of 0 (dark) or 1 (light).

 

 

EXAMPLES

 

Example 1: Following an 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 = irLeftValue().

 


Program:

from callibot import *

setSpeed(30)
repeat:
   v = irLeftValue()
   if v == 0:
      rightArc(0.1)
   else: 
      leftArc(0.1)
   delay(100)
► Copy to clipboard

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 = irRightValue()
vL =irLeftValue()

You then develop an algorithm with which the robot can follow the path as accurately as possible

 

Program:

from callibot import *

setSpeed(20)
repeat:
    vR = irRightValue()
    vL = irLeftValue()   
    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

 

Example 3: Move square

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 callibot 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 = irRightValue()
    vL = irLeftValue()
    if vL == 1 and vR == 1:
        left()
        delay(500)
    else:    
        keepOnTrack()
    delay(100)        
► Copy to clipboard

 

 

REMEMBER...

 

You can use the infrared sensors to measure the brightness of the surface. 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.

 

 

TO SOLVE BY YOURSELF

 

 


1.


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

 

 
 


2.

When following the paths with crossings, the robot often loses the track, especially if you let it drive faster. Complete the programme 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 recognises the first black stripe and continue driving (as at a bus stop). When it recognises the second stripe, it stops definitively (terminus 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 over the stripe. You have to use a variable s to remember the current state of the robot, e.g.
s = 0: first travelling 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 make a few changes here.

4.

 

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

 

Note: You have to find a similar trick as in the previous task. You can, for example, store the number of patrols travelled 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 programme 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 = 0. The state is switched in the while loop

....

s = 0
forward()
repeat:
    v = irLeft.read_digital()
    if v == 0 and s == 0:
        # Bei Haltestelle
        s = 1
        ...
    if v == 1 and s == 1:   
        # Auf weisser Zwischenfahrt
        s = 2
    if v == 0 and s == 2:
        # Am Endbahnhof
        ...