Line Following Tips

Line Following is a standard and useful robotics challenge — you build a robot that needs to drive around following a line that twists and turns. Typically the line is made of black electrical tape on a white surface. What do you need to know to build a line follower, especially as a LEGO Mindstorms NXT Roboticist?

Quick Start

The first thing you should do is check out nxtprograms.com, which has instructions for building and programming a line follower with both the NXT 1.0 / Educational Kit and for the NXT 2.0 kit. The instructions are simple and step-by-step, and the program is well-commented so you can understand the principles. The pages also discuss how to calibrate a light sensor, and how to work with the newer Colour sensor which can not be calibrated.

For those of you who have a light sensor, you may also want to take a look at Team Hassenplug’s Line Following Block, which can be used in your programs and is faster than if you wrote the same code in NXT-G.

Principals

Fundamentally, your robot needs to be able to tell the black apart from the white, and turn to adjust to follow the line.

Calibration

Light sensors are fairly simple. They send out a little bit of light and measure how much light is reflected. If there is a lot of sunlight on a surface, they’ll get a higher reading than if the area is dark. (You can reduce the effect somewhat by blocking out all other light so that the sensor is always reading in the dark). As lighting conditions change, your robot needs to be able to compensate for it. You do this by calibrating the sensor.

In calibrating, you record the light sensor reading above a black point (which will always be the lowest reading) and above a white point (the highest reading). Typically, you then calculate the halfway point between the readings (which is also the average of the two), and then you can say, if my current reading is greater than the halfway point, I’m looking at something white, and if it is not, I’m looking at something black.

Auto-calibration

A robot can calibrate itself by scanning an area which it knows will contain both the black and white, and remembering the largest value it sees and the darkest value it sees. These will then be the white and black points.

Sensor Placement

It helps to place the sensor close to the surface you are looking at (but not too close!). If it is too far away, it will see a large area and report that it is greyish. If it is too close, it will not be able to see the reflected light and report that everything is blackish.

Note that if your sensor is not firmly attached, it will move around and see things at different angles and distances, which will mess up your readings.

With Many Sensors

It is handy to have multiple light sensors (or colour sensors in light-sensing mode), for then you can say, “which one of my sensors sees black? Turn so that that is under the middle of the robot”. If you have two sensors, keep them so they both see white, and if either see black, turn them away from the line, all the while driving forward. If you have three sensors, try to adjust so that the middle one always sees black. If the right sensors sees black, turn right, and if the left sensor sees black, turn left.

With One Sensor, A Naive Approach

(A naive approach is what you do when you first start and don’t know better). With one light sensor, you need to follow the edge of the line. If you wanted your robot to follow the right edge of the line, you’d make it turn left when it sees white, and turn right when it sees black, all the while going forward.

With One Sensor, A Better Approach

It turns out that your light sensor or colour sensor (in light sensing mode) does not see just black or white, it sees a range of values. What you really want it to do is follow the grey that it sees between the black line and the white surface. When it sees black, it needs to turn back toward the grey, and when it sees white, it needs to turn back toward the grey. (Going one step further, if it sees light grey, it turns slowly back towards the grey, and if it sees dark grey, it turns slowly back toward the grey.)

Remember how we calibrated the sensor? The darkest value is black and the lightest value is white? Any value in between will be grey. If it is lower, it will be dark grey, and if it is higher, it will be light grey, and, away from the two extremes, it will be a neutral grey.

If your robot sees black or white, it is far from the grey area that it sees when it straddles the line, and needs to turn hard to get back to it. If it sees light grey or dark grey, it is not too far from the neutral grey it needs to follow and can adjust more gradually. Turning harder the farther you are from the line is called proportional control, and it helps your robot follow the line smoothly.

Where To Go From Here

You can go one step better than proportional control. See this excellent article A PID Controller For Lego Mindstorms Robots.

Posted by Clinton Blackmore - Tuesday February 2, 2010.
Posted in .

Comment