Following the Right Track | Huskylens Playground with micro:bit EP06

0 4173 Medium

Nowadays robots are getting more and more powerful. They can do almost anything high on top of the mountain and deep into the sea. Look at them! Some of them serve well as waiters in restaurants. Some work hard as "couriers" in factory workshops. Some work as safety inspectors in power grid hubs with a high sense of responsibility...


If you look closely, you will find that there are lines on the ground where these robots work, and they are following the lines. Line tracking? How do they do it? Let's DIY a cute car robot to find out!

Function Introduction:

This project uses HuskyLens line tracking function to make Maqueen Plus follow the line track on the ground.



Knowledge Field:

If we want Maqueen Plus to move according to the lines on the ground, we need some sensors to identify these lines. According to the different sensors, there are several kinds of line tracking methods. This project uses the line tracking function of the visual sensor——HuskyLens to implement the line tracking effect.

1. What Is Line-tracking

Line tracking refers to the process of object moving along a designated route. A fully functional line tracking robot uses a mobile robot as a carrier, a visible light camera, an infrared thermal imager and other detection instruments as a load system, a multi-field information fusion of machine vision, electromagnetic field, GPS and GIS as a navigation system for autonomous movement and tracking of the robot, and an embedded computer as a software and hardware development platform for the control system.

2. Comparison of Two Commonly Used Line-tracking Methods


3. The Principle of HuskyLens Sensor - Line-tracking Function

HuskyLens line tracking function is based on Pixy, an open source project of Carnegie Mellon University.


Pixy's algorithm can recognize the color of pictures. Its basic idea is to use the color space to remove the background that all users are not interested in and extract the foreground(such as lines).


In this way, after learning the color of the line, HuskyLens will recognize the color line within the field of view.

4. Demonstration of HuskyLens Sensor – Line-tracking Function

Object Learning:

This function can track lines of specified colors and make path predictions. The default setting is to track lines of only one color. This project will be explained by taking line tracking of one color as an example.

Operation and Setting:

(1) Dial the function button to the left or right until the word "Line Tracking" is displayed at the top of the screen.

(2) Long press the function button to enter the parameter setting of the line tracking function.

(3) Dial the function button right or left until "Learn Multiple" is selected, then short press the function button, and dial it to the left to turn off the "Learn Multiple" switch, that is, the square icon on the progress bar is turned to the left. Then short press the function button to complete this parameter.

(4) You can also turn on the LEDs by setting "LED Switch". This is very useful in a dark environment.


(5) Dial the function button to the left until "Save & Return" is selected, and short press the function button to save the parameters and it will return automatically.

Learning and Prediction:

(1) Line Learning

Point the "+" symbol at the line, then point the orange frame at the background area. It is recommended that within the view field of HuskyLens, just remain lines to learn and no any cross lines. Try to keep HuskyLens parallel to the target line, then HuskyLens will automatically detect the line and a white arrow will appear. Then, short press the "learning button", the arrow will turn blue.


(2) Line Prediction

When HuskyLens detects the line which has been learned(the same colored lines), a blue arrow will appear automatically on the screen. The direction of the arrow indicates the predicted direction of the line.



1. When learning the line, we need to adjust the position of HuskyLens to be parallel to the line.

2. HuskyLens can learn multiple lines according to the color of lines, but these lines must be monochrome lines with obvious color that differs from the background.

3. In most cases, the color of tracking line is only one. Therefore, in order to ensure stability, it is recommended to track the single line.

4. The color of the lines has a lot to do with the ambient light. When patrolling the line, please try to keep the ambient light as stable as possible.

5. Maqueen Plus

Maqueen Plus from DFRobot is a Robot car product designed for STEAM, with rich functions, extensions, and the following features:

- Compatible with micro:bit

- 18650 large-capacity lithium battery power supply, onboard charging circuit, long time of endurance

- The onboard encoder speed sensor can obtain the current vehicle condition and vehicle speed in real time, for example, the current vehicle speed of Maqueen Plus, whether it is stopped, moving forward, backward, left or right, and the size of the turn can also be obtained.

- PID motor speed regulation for decreasing the motor speed error

- Expansion ports: 8 GPIO, 3 I2C, 3 servo ports

- 6-channel line-tracking sensor, support one-key calibration of the sensor, support analog output, and can realize more complex patrol logic, suitable for expansion training and competition scenes.

- Support programming by Python

- Equipped with metal expansion top plate, adjustable angle bracket.


6. The Practicing Logic of HuskyLens Line-tracking

How to make Maqueen Plus follow the black line on the white background? In fact, we only need to know where Maqueen Plus is relative to the black line, which can be divided into the following three situations:

(1) When Maqueen Plus is on the right side of the black line, control Maqueen Plus to turn left;

(2) When Maqueen Plus and the black line are relatively centered, control Maqueen Plus to go straight;

(3) When Maqueen Plus is on the left side of the black line, control Maqueen Plus to turn right.


How should it be implemented specifically? We stripped out the information displayed on the screen of HuskyLens during the line-tracking process and abstracted it into the geometric mathematical model shown in the figure below.


The resolution of the HuskyLens screen is 320×240, the O point at the upper left corner of the screen is the origin of the screen coordinates (0, 0), the horizontal right direction is the positive direction of the X axis, and the vertically downward direction is the positive direction of the Y axis, so the coordinates at the lower right corner are (320, 240). The orange dotted line is the central axis of the screen, and the abscissa value of this line is 160. The black line is the line of the tracking map "seen" by the HuskyLens camera. The blue arrow is the line direction calculated by HuskyLens. The start point of the blue arrow is (x1, y1) and the end point is (x2), y2).

In summary, so we only need to judge the position of the starting point of the blue arrow relative to the central axis to achieve line tracking.

Project Practice:

We will implement the project in accordance with the line-tracking logic, and continuously optimize the line-tracking effect in several steps, so that Maqueen Plus can pass the map quickly and steadily. First, we will learn to use the line tracking function of HuskyLens, read the abscissa data of the line, write a simple (two intervals and one line) program to adjust the motion state to meet the line tracking requirements, and then improve the project program according to the debugging effect.

Task 1: Start line-tracking

Divide the horizontal axis into two sections (left and right) and a line (central axis) to achieve the line-tracking effect.

Task 2: Line Tracking Optimization 1

Based on task 1, increase the linear motion adjustment interval, speed up the line tracking, and improve the problems that appear in the result of task 1.

Task 3: Line Tracking Optimization 2

Based on task 2, add more motion adjustment intervals, speed up the line tracking, and continue to improve the problems that appeared in the previous results.

Line Tracking Map:


Task 1: Start Line Tracking

1. Structure Construction and Hardware Connection

Use screws to fix HuskyLens on Maqueen Plus. It should be noted that in order to track the line, we need to adjust the camera obliquely downwards, so that we can see the black line closer to Maqueen Plus, and the line tracking effect will be better.


HuskyLens and Maqueen Plus communicate via I2C, the connection diagram is as follows:


2. Program Design

When HuskyLens detects that the black line is on the left side of the screen, that is, the abscissa value of the starting point of the blue arrow x1160, it will control Maqueen Plus to turn right; when the black line is in the middle of the screen, at this time, x1=160, it will control Maqueen Plus to go straight.


3. Sample Program


4. Operating Effect

Maqueen Plus can complete basic line tracking tasks, but there are still problems:

(1) Maqueen Plus swayed obviously from side to side during the forward movement. The speed change was inconsistent, which cannot stabilize the straight movement.

(2) The speed cannot be set too fast, or it will deviate at the turning point;

(3) Different turning angles require different turning speeds. Maqueen Plus deviates at several turning points in the line tracking map.

*If Maqueen Plus deviates from the track during a turn, it needs to be adjusted continuously by modifying the speed of the left and right motors.

Task2: Line Tracking Optimization 1

1. Structure Construction and Hardware Connection

The same as task 1.

2. Program Design

In task 1, Maqueen Plus swayed from side to side during the forward movement, and could not move forward stably. Why is this? Because the horizontal axis section of straight movement is just a line, and there is inertia during the movement, it is difficult to achieve straight travel at x=160 point.

The optimization idea is to expand the straight movement interval, as shown in the figure below, we set the interval [150, 170] as the straight movement interval, when the starting point coordinate value x1 is within this interval, control Maqueen Plus to move straight; when x1170, control Maqueen Plus to turn right.


3. Sample Program


4. Operating Effect

Through adjustment, the line tracking speed of Maqueen Plus is faster, and the speed change is also smoother on the straight track, but it is still easy to deviate at different turning angles.

Task3: Line Tracking Optimization 1

1. Structure Construction and Hardware Connection

The same as task 1

2. Program Design

The problem of deviation in the previous tasks is because the same turning speed cannot adapt to all turning angles. Once the turning speed is set too fast or too slow, it will cause deviation. According to our previous optimization ideas, since the screen can be divided into 3 motion adjustment intervals, why not continue to be divided into 5? The closer the black line is to the two sides, the greater the turning speed; the closer the black line is to the central axis, the lower the turning speed.


Or even further, divide it into 7 motion adjustment intervals?


3. Sample Program


4. Operating Effect

The line tracking speed of Maqueen Plus can be faster, whether it is turning or going straight, the speed changes are much smoother, and the time of deviation is also reduced.

Project Summary:

Project Review:

In this project, we continuously optimize the line tracking algorithm to make the line tracking faster and more stable. In practice, the robot needs to track the line in a more complex environment, which may require both visual line tracking and ordinary line patrol to choose the best route.

Knowledge Review:

1. Learned the main realization ideas of line tracking

2. Algorithm optimization in line tracking process

Project Development:

After completing the non-crossing line tracking, you may need to deal with the crossing line tracking as shown in the figure below. Can you use the tag recognition learned before to let the Maqueen Plus to choose the correct path at the intersection?



The line tracking interval can be divided into 2, 3, 5, and 7. Can it continue to be divided into more? 9? 11? ...Until infinite. According to the actual effect of this project, the more the speed regulation interval, the better the effect, but the program will become longer and longer. What is the solution?

PID speed regulation algorithm can help us to solve this problem. PID stands for proportional, integral, and derivative control. It is a closed-loop control system. Closed-loop control is a control method that correct according to the feedback output of the controlled object. According to the error between the measured actual value and the planned value. It can correct the error within a certain standard.

Students who interested can search for relevant information on the Internet and continue to optimize the line tracking effect.

Next Class: Face Mask Reminder | Huskylens Playground with micro:bit EP07 >

All Rights