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.
0b0100
, Gentle left turn

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

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

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

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

A cyber:bot QTI condition showing an "off the line" condition.
0b0000
, 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
bot(19).servo_speed(-75)

Note that the example QTI_Follow_Line.py 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.

    bot(18).servo_speed(wL)
    bot(19).servo_speed(-wR)

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.

# QTI_Follow_Line.py

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
    bot(18).servo_speed(wL)
    bot(19).servo_speed(-wR)

 

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.