Cutebot - Line Tracking Image by Freepik

Cutebot - Line Tracking

In this series of tutorials we will be using the Python programming language to instruct an Elecfreaks Smart CuteBot robot car to perform a range of tasks including basic movements, line tracking, and light & object detection. In the previous tutorial, we instructed the Smart CuteBot robot car to detect and react to different levels of light intensity. In this tutorial, we will instruct the Smart CuteBot to automatically track and follow a black line.

Requirements

Please ensure that you have read and followed the instructions described in our CuteBot - Getting Started tutorial in order to prepare your Micro:bit, and hence your Smart CuteBot, for programming.

Line Tracking

The Smart CuteBot is equipped with two line tracking sensors that enables it to detect black lines and their edges. In this tutorial, we will instruct the Smart CuteBot to follow a racing track by automatically following a black line, as demonstrated in the animation below.

Follow a Black Line

In this section, we will develop a Python program that instructs the Smart CuteBot to automatically follow a black line.

def on_forever():

    # If the left line-tracking sensor no longer detects a black line, 
    # but the right line-tracking sensor continues to detect a black line, 
    # this means that the track is turning right.
    if cuteBot.tracking(cuteBot.TrackingState.L_UNLINE_R_LINE):

        # Turn right by controlling the speeds of the individual wheels.
        cuteBot.motors(20, 0)

    # If the left line-tracking sensor continues to detect a black line, 
    # but the right line-tracking sensor no longer detects a black line, 
    # this means that the track is turning left.
    if cuteBot.tracking(cuteBot.TrackingState.L_LINE_R_UNLINE):

        # Turn left by controlling the speeds of the individual wheels.
        cuteBot.motors(0, 20)

    # If the left line-tracking sensor continues to detect a black line, 
    # and the right line-tracking sensor also continues to detect a black line, 
    # this means that the track is continuing in a straight line.
    if cuteBot.tracking(cuteBot.TrackingState.L_R_LINE):

        # Move forwards.
        cuteBot.motors(20, 20)


# Keep running the 'on_forever' function in the background in a forever loop.
basic.forever(on_forever)

We begin the Python code above by defining a custom function called on_forever. This custom Python function instructs the Smart CuteBot to do the following:

  • The variable cuteBot.TrackingState.L_UNLINE_R_LINE stores a specific state detected by the line tracking sensors. If this variable resolves to True, this means that the left line-tracking sensor no longer detects a black line, but the right line-tracking sensor continues to detect a black line. Logically, this means that the track is turning right. In this case, we instruct the Smart CuteBot to turn right by setting the speed of the left wheel to a positive number (20 in our example) and the speed of the right wheel to a smaller number or zero.
  • The variable cuteBot.TrackingState.L_LINE_R_UNLINE stores another specific state detected by the line tracking sensors. If this variable resolves to True, this means that the right line-tracking sensor no longer detects a black line, but the left line-tracking sensor continues to detect a black line. Logically, this means that the track is turning left. In this case, we instruct the Smart CuteBot to turn left by setting the speed of the right wheel to a positive number (20 in our example) and the speed of the left wheel to a smaller number or zero.
  • The variable cuteBot.TrackingState.L_R_LINE stores another specific state detected by the line tracking sensors. If this variable resolves to True, this means that both left and right line-tracking sensors continue to detect a black line. Logically, this means that the track is continuing in a straight line. In this case, we instruct the Smart CuteBot to continue moving forwards at a constant speed by setting the speeds of the left and right wheels respectively to the same positive number (20 in our example).

We specify that our custom on_forever function should run continuously in the background. This is achieved by calling the basic.forever(body: ()) method, with the first and only parameter set as the name of our custom Python function i.e. on_forever that will be executed continuously in the background within a forever event-based loop.

To learn more about the basic functions and actions integrated with the Micro:bit, please refer to the Micro:bit Basic API documentation.

Jillur Quddus
Written by

Jillur Quddus

Computational Mathematician @ HyperLearning AI