TigerJython4Kids
 robotics
infrarotsensor
Deutsch   English   

5. INFRARED SENSORS

 

 

YOU LEARN HERE......

 

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


 

HOW AN INFRARED SENSOR WORKS?

 

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: Following an edge
The robot should use its left infrared sensor to drive over a dark surface. Depending on whether it ‘sees’ dark or light, it corrects the direction of travel with a right or left turn. The sensor values are queried in an endless loop with a measuring period of 100 ms.

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

 


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 also run the programme in simulation mode. To do this, you must insert the following two lines after the line from mbrobot import *:

RobotContext.useBackground("sprites/blackarea.gif") 
RobotContext.setStartPosition(430, 350)

The first line adds the background image ‘blackarea.gif’, the second line determines the position at which the robot appears at the beginning. (The graphics window is 500 x 500 pixels, the coordinate (0,0) is top left). All images used in the examples and tasks are included in the TigerJython distribution (Image library). You can also use your own background images.

If you then want to run the programme in real mode again, you must deactivate the lines with RobotContext (precede with #). You can already see in simulation mode 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()

You then develop an algorithm that enables the robot to follow the path as accurately as possible.

 

 
from mbrobot import *
#from mbrobot_plus import *

#RobotContext.useBackground("sprites/trail.gif")

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

For the simulation, you must activate the line with RobotContest and use the background image trail.gif. In real mode, you must deactivate this line again. Depending on the size of the replicated template, you must adjust the speed and the radius of the arcs.



Example 3: Driving 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.

 

 
from mbrobot import *
#from mbrobot_plus import *

RobotContext.useBackground("sprites/field1.gif")
RobotContext.setStartPosition(380, 400)

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

Use the background image field1.gif for the simulation.

The Maquen Plus 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() bzw. irL1.read_digital()
irRight.read_digital() bzw. irR1.read_digital()
irL2.read_digital()
irR2.read_digital()
irL3.read_digital()
irR3.read_digital()


Example 4: Using infrared and ultrasonic sensors simultaneously
(only real mode)

A robot travels 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 round and drives back. You can also have two robots drive against each other. If your programme is correct, they should not collide.

 

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

The sensor values are queried every 100 milliseconds in the same repeat loop. The programme block after else corresponds to the programme code in example 2.

 

 

REMEMBER...

 

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 YOURSELF

 

 


1.


The robot should move endlessly on a square table with a white inner circle without falling off. 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.

You can use the background image circle.gif for the simulation.

 
 
RobotContext.useBackground("sprites/circle.gif")

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.

You can use the background image track.gif for the simulation:

 
RobotContext.useBackground("sprites/track.gif")

3.

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

 

You need the following context for the simulation mode:

RobotContext.useBackground("sprites/blacktapes.gif")
RobotContext.setStartPosition(10, 250)
RobotContext.setStartDirection(0)

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 strip. 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 do a little 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 recognises the fourth stripe.

 

Use the following context for the simulation:

RobotContext.setStartPosition(0, 250)
RobotContext.setStartDirection(0)
RobotContext.useBackground("sprites/panels.gif")
Note: You have to find a similar trick as in the previous task. For example, you can 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.

You can use the background image oval.gif for the simulation

 
RobotContext.useBackground("sprites/oval.gif")
RobotContext.setStartPosition(400, 400)

 

   

 

5-4
Technical information::

At the beginning, the state variable s is set to zero. 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
        ...