Instructional Material: Exercise: Line Follower

Printer-friendly version

In this exercise we are going to program a robot to follow a line. Typically a line is created by putting tape on a surface where the tape is of a different color than surface. We want to use the Color Sensor to detect the tape and guide the robot by turning appropriately when we are over the tape or off of the tape. In this example we are going to follow the right edge of the tape. This program will not work on the left edge or if the robot starts with the Color Sensor not on the edge. When you start robot, line the Color Sensor up on the right edge of the tape.

We use the Color Sensor in red light mode. We use the ColorDemo exercise program to test the amount of light returned by the sensor when over the tape and when not over the tape. In our example, we did this color measurement and found that when over the tape the Color Sensor returned about .70 for the red light value and when off the tape it returned about .130. Further testing showed that at the edge of the tape the sensor returned about .100. So in the program we will monitor the Color Sensor red light value and when the value is less than .100 we will turn right. When greater than .100 we will turn left. We will make the turns by reducing the speed of the appropriate motor by half.

Create a new package called ev3.projects.lineFollower and create a new class in the package called LineFollower. Copy the following code into that class:

package ev3.projects.lineFollower;

import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.hardware.motor.*;
import lejos.hardware.port.*;
import lejos.robotics.Color;
import ev3.projects.library.*;

public class LineFollower 
    static UnregulatedMotor motorA = new UnregulatedMotor(MotorPort.A);
    static UnregulatedMotor motorB = new UnregulatedMotor(MotorPort.B);
    static TouchSensor        touch = new TouchSensor(SensorPort.S1);
    static ColorSensor        color = new ColorSensor(SensorPort.S3);
    public static void main(String[] args)
        float    colorValue;
        System.out.println("Line Follower\n");

        Button.LEDPattern(4);    // flash green led and 
        Sound.beepSequenceUp();  // make sound when ready.

        System.out.println("Press any key to start");
        // drive waiting for touch sensor or escape key to stop driving.

        while (!touch.isTouched() && Button.ESCAPE.isUp()) 
            colorValue = color.getRed();
            Lcd.print(7,  "value=%.3f", colorValue);

            if (colorValue > .100)
        // stop motors with brakes on.

        // free up resources.
        Sound.beepSequence(); // we are done.

This line follower example can be improved upon by controlling the rate of turn based on how far away the color sensor value is from .100. So for a value of .80 we would turn right less than for a value of .70 or .60. This determines the rate of turn proportionally to how far from the edge of the tape the robot is. Further from the edge more turn, closer to the edge less turn. This is called proportional control and should smooth out the movements of the robot. Modify the example code to do this.


Material Type: 
Lab Activity
Education Level: 
HW Platform: 
SW Platform: 
Interactivity Style: