Line Following with ODS

The optical distance sensor shoots out a beam of IR light and reads the level of reflected light as a voltage, which gets converted into a value between 0 and 1024.

Begin by mounting your ODS sensor downwards to that it can sense the black line on the floor.

Plugging your ODS sensor into the A7 port, use the diagnostic tool to measure its reading on the floor, on the tape, and half on the floor, half on the tape... write these down.

Entirely on the floor

Half on the floor, half on the tape

Entirely on the tape

Use the following code to let your robot follow the black line. You will need to change the desired value to the reading you got when the sensor was half on the tape and half on the floor. You may also need to adjust the gain!

import Fusion
import time 

f = Fusion.driver()
ods = Fusion.analog(f, f.A7)

desired = 200
error = 0
gain = 2
while True:
    error = ods.read() - desired
    f.motorSpeed(f.M0, (50 - error)*gain)
    f.motorSpeed(f.M1, (50 + error)*gain)
    

Last updated

Was this helpful?