A Better Line Follower

The line follower suggested in the official 'Robot Educator' exercises (called 'Switch', in the 'Beyond Basics' section) works very badly (because it's designed for simplicity rather than efficiency). Here's my improved version.

Line following is a nice little robot challenge, making use of both a sensor and motors. it is especially challenging if you only have one colour sensor, which is the case with the standard EV3 sets.

Think about it. With a single colour sensor suspended just above the white mat with a black line on it, the sensor will either see either white or black. What does it mean if the sensor detects a change from black to white? It means the sensor is no longer over the line, but it doesn't tell you whether the robot went off the left hand side of the line or the right hand side, so it's not possible to know whether the robot should turn left or right to get back on the line. I suppose at that point the robot could stop and begin scanning left and right, searching for the line, but it would need to do this very frequently so this would be a very bad method.

A better solution is not to try to follow 'the line' but to try to follow one side of the line. If the robot tries to follow the right edge of the line then when the sensor detects black it means the robot should turn right a bit and if it detects white then it's gone off the right edge of the line so it needs to turn left. The line detector program included in the education version of the EV3 software uses the crude technique just described, and it works quite badly.

You might be thinking that the sensor were directly over the edge of the line then it would see the black and white areas at the same time so the measured light intensity would be 'medium' - this would be very useful information but it's very unlikely that the sensor will actually see this if it is placed very close to the mat - the sensor is much more likely to detect pure white or pure black. A clever trick, then, would be to lift the sensor further up above the mat so that it is more likely that that the sensor will 'see' the black and white areas at the same time (remember that the sensor only sees the brightness of the reflected light, it is essentially a camera with only one pixel so it does not see an actual 'image'). We can make good use of the additional information we now have: if the sensor detects black we should turn right, if it detects white we should turn left and if it detects 'medium' we should go straight ahead. We can make the steering respond to the varying brightness of the reflected light - this is called proportional line following and it's the best way to do line following with a single sensor.

Try suspending the colour sensor about two 'Lego beam heights' above the mat, in other words about twice as high as in the standard 'driving base' configuration, and try running the attached program (download link below). This program works quite well for me on the standard NXT mat which has a black line 2cm wide and no sharp curves. You may have to adjust the program to get optimum results with your own line, of course.

BTW, if you're interested in line following then download the printable 'tiles' from this page so you can design your own course.

Here is the program, in case you don't have the possibility of downloading it and opening it in the EV3 software right now. It includes a 'My Block' which calibrates the colour sensor so that the sensor returns zero when over MY dark surface and 100 when over MY light surface. For more about calibrating the colour sensor look for exercise 14 on this page.

Here below is another solution to the 'better line follower' challenge, suggested by Blue Angels. It may run more smoothly than mine, especially if you follow the angel's advice at the end, which is basically that by running the motors more slowly the robot will advance more smoothly, and perhaps even faster!