The next step is to modify our ColorSensor
class and program it to detect the presence and absence of the path we want the line-follower robot to follow. For example, the path I constructed was made using red electrical tape on top of a gray marble floor. Thus, the ColorSensor
class needs to be capable of detecting the color red.
We modify the ColorSensor
class and add a method to it, which determines whether the robot is currently on the path or not.
The content of the ColorSensor.java
file is as follows:
import lejos.hardware.port.Port; import lejos.hardware.sensor.EV3ColorSensor; import lejos.robotics.Color; public class ColorSensor extends EV3ColorSensor { private final static int PATH_COLOR = Color.RED; public ColorSensor(Port port) { super(port); } public boolean onPath() { return getColorID() == PATH_COLOR; } }
The highlighted parts of the code (class) have been modified (since the time we last looked at it). The first change we made was to declare the PATH_COLOR
variable, an int
variable, which contains the value corresponding to the color of the path that the robot is supposed to follow. Since I am working with a path marked out using red electrical tape on a gray background, I chose the value 5, which corresponds to red.
The second change is the addition of the onPath()
method, which is supposed to return a Boolean (true
or false
) value, indicating whether the color sensor is currently on top of the path or not:
public boolean onPath() { return getColorID() == PATH_COLOR; }
This can be determined rather easily. We call the getColorID()
method (which, you might recall, returns an integer value corresponding to the color detected by the sensor) and compare the returned value to that of PATH_COLOR
(the specified color value of the path). In this case, if the value returned by getColorID()
is equal to the value stored in PATH_COLOR
(6 – white), the expression getColorID() == PATH_COLOR
evaluates to true
and that is the value returned.
It is by this mechanism that the onPath()
method is able to determine whether the robot is on the path or not. Once again, after adding this bit of functionality, we modify the src/LineFollower.java
file so that we can test it.
The content of the LineFollower.java
file is as follows:
import lejos.hardware.port.SensorPort;
import lejos.utility.Delay;
public class LineFollower
{
public static void main(String[] args)
{
log("Program starts");
ColorSensor sensor = new ColorSensor(SensorPort.S1);
for (int ii = 0; ii < 10; ii++)
{
Delay.msDelay(1500);
log("On Path: " + sensor.onPath());
}
log("Program ends");
}
private static void log(String msg)
{
System.out.println("log> " + msg);
}
}
The only change that we have made to the LineFollower
class is inside the body of the for
loop. Instead of reading and logging the integer value of the color detected, we log whether the sensor is currently detecting the path or not.
To that end, we use sensor.onPath()
. Since this method returns the Boolean value we want to log, we place the method call inside the log()
method (to avoid having to declare a redundant Boolean variable).
When the execution arrives on the log
method, it looks at the argument "On Path: " + sensor.onPath()
. The first part is already a string. It is being added to a function call that returns a string, so the function must be evaluated. The sensor.onPath()
function returns a Boolean, which is converted automatically to a string, true
or false
, and added to "On Path: "
. This combined string is then printed on the terminal.
Compile, transfer, and execute the program (or execute make run
). The robot will detect and report whether it is on the path or not 10 times before the program comes to an end. The best way to carry out the test is to set up a small segment of the path on the floor and as the program is running, push the robot back and forth across the path.
For a successful test, the Boolean value reported by onPath()
should match, irrespective of whether the robot (its color sensor) was on top of the path at that time or not. With this program, we are able to test both the modifications we have made to the ColorSensor
class as well as the robot's ability to distinguish between the path and the floor.
This is crucial; without this ability, the robot cannot possibly follow a line (it won't know where a line begins or ends). If this turns out to be the case, first make sure that the code is working, and if it is working, then use a different tape to construct the path (one that has a high contrast with the floor).
3.14.253.152