Member-only story
MicroPython Tutorial XV
One of classic tasks in the world of robotics is edge detection. Edge detection being the method you would use to follow a line. This is a perfect task for the reflection mode of the colour sensor.
Edge detection as in following a line. Here’s a short script to illustrate the point.
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor, ColorSensor
from pybricks.parameters import Port, Stop
from pybricks.tools import wait
from pybricks.robotics import DriveBase
left_motor = Motor(Port.B)
right_motor = Motor(Port.C) colorScanned = ColorSensor(Port.S1) robot = DriveBase(left_motor, right_motor, 56, 114) targetLV = 27
fudge = 1.8 while True: shade = colorScanned.reflection()
direct = (targetLV — shade) * fudge
print(“shade”,shade)
robot.drive(50,direct)
What does it do. We define a colour sensor and link two motors into a drive pair. We than declare a target light reflection value and a fudge value.
We put everything into a endless loop and start scanning. But wait what are the targetLV and fudge values. We get the targetLV by placing our light sensor on the edge of the black line, so half on half off, and reading…