Python Program for Line Following

Basic Line Following with cyber:bot

How many possible combinations of QTI sensor states exist from pattern? The values could range from 0b0000 (0) to 0b1111 (8+4+2+1=15) for a total of 16 different number combinations!

Is it likely that pattern would be 0b1001 where the two outside QTI sensors are seeing black? Not likely since they’re 1-½” apart and electrical tape is less than ¾” wide. Rather than check for every possible 16 cases, choose the ones which are most likely. This saves memory and creates a faster-running program. The example program has eight possibilities shown and described in the following 8 images.   

QTI condition showing a sharp left turn.
0b1000, Sharp left turn

A cyber:bot QTI condition showing a medium left turn.
0b1100, Medium left turn

A cyber:bot QTI condition showing a gentle left turn.
, Gentle left turn

A cyber:bot QTI condition showing straight, full speed.
, Straight ahead full speed

A cyber:bot QTI condition showing a gentle right turn.
, Gentle right turn

A cyber:bot QTI condition showing a medium right turn.
, Medium right turn

A cyber:bot QTI condition showing a sharp right turn.
, Sharp right turn

A cyber:bot QTI condition showing an "off the line" condition.
, Backup, turn, rotate, make a sound - or do the same command previously executed?

The cyber:bot Forward and Backward tutorial showed how the right motor must turn the opposite direction for the robot to go forward, using negative values. For example:

bot(18).servo_speed(75)             # Full speed forward

Note that the example code uses all positive values. How is that possible? At the end of the program the wR values are made opposite, allowing the use of all positive numbers as arguments.


This makes it a little bit easier to come up with drive speed values in the example program. The values work well with a gentle, curvy line formed with electrical tape. The combinations of motor speeds you use will vary based on the line you create.  

ERROR FALSE ALARM! On line 19 of the script  z = bit.get(pattern, x), you might see a syntax checker error warning: Argument does not match parameter type for parameter "v".  This is just a false alarm.  The script actually works fine, so go ahead and send it to the micro:bit.Example script is shown below.


from cyberbot import *
from qti import *
from intbits import *

wL = 0
wR = 0

bot(22).tone(500, 1000)

while True:

    # Read QTI sensors
    pattern = qti(7, 4).read()

    # Display QTI sensors
    for x in range(0,4):
        z = bit.get(pattern, x)
        for y in range (0,5):
            display.set_pixel(x, y, z*9)

    # Calculate speeds to respond to detection patterns
    if pattern == 0b1000:
        wL = 10
        wR = 100

    elif pattern == 0b1100:
        wL = 30
        wR = 100

    elif pattern == 0b0100:
        wL = 60
        wR = 100

    elif pattern == 0b0110:
        wL = 100
        wR = 100

    elif pattern == 0b0010:
        wL = 100
        wR = 60

    elif pattern == 0b0011:
        wL = 100
        wR = 30

    elif pattern == 0b0001:
        wL = 100
        wR = 10

    elif pattern == 0b0000:
        bot(22).tone(500, 20)

    # Update wheel speeds


High-speed Line Following with cyber:bot

If you are competing in a line following contest, you can speed up your cyber:bot two different ways.

  1. First, consider moving the power jumper from Vdd to Vin.
  2. Next, you could upgrade your cyber:bot with the Parallax Feedback 360° High Speed Servos.