Skip to main content

Case 06: The Line-following Car

Introduction

Make Cutebotdrive along the black line.

Programming Preparation

Please refer to: Preparing Programming Environment

Sample code

from cutebot import *

#Create a sample for Cutebot category
cutebot = Cutebot()

# While true, detect the status of the line-tracking sensor
# when both detect the black line, the Cutebot is moving forward at 50% speed.
# When the right probe detects that it is off from the black line, set the speed of the left wheel to 0% and the speed of the right wheel to 100%
# When the left probe detects that it is off from the black line, set the left wheel speed to 100% and the right wheel speed to 0%
while True:
tracing = cutebot.get_tracking()
if tracing == '11':
cutebot.set_speed(50,50)
if tracing == '10':
cutebot.set_speed(0,100)
if tracing == '01':
cutebot.set_speed(100,0)

Code details

  1. Import the modules that we need for the program:cutebot module contains classes and functions that operate on the Cutebot smart car.
from cutebot import *
  1. Create a sample for Cutebot category.
cutebot = Cutebot()
  1. While true, detect the status of the line-tracking sensor, when both detect the black line, the Cutebot is moving forward at 50% speed; when the right probe detects that it is off from the black line, set the speed of the left wheel to 0% and the speed of the right wheel to 100%; when the left probe detects that it is off from the black line, set the left wheel speed to 100% and the right wheel speed to 0%.
while True:
tracing = cutebot.get_tracking()
if tracing == 11:
cutebot.set_speed(50,50)
if tracing == 10:
cutebot.set_speed(0,100)
if tracing == 01:
cutebot.set_speed(100,0)

Results

After turning on the power, the Cutebot smart car drives along the black line.

Exploration

Is it possible to use the Cutebot to make a cart that can prevent falls?