HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how the robot can recognise the brightness of the surface with its infrared sensors. |
HOW DOES AN INFRARED SENSOR WORK? |
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 |
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) 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.
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) 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
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) 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: Example 4: Using infrared and ultrasonic sensors simultaneously
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) 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 |
|
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: ...