In this section, we will present the code for the entire project with the exception of Controller.java
. All the presented code has been explained either in this chapter (in the case of LineFollower.java
) or in the previous two chapters (DifferentialDrive
in Chapter 8, The Line-follower Robot, and ColorSensor
in Chapter 9, Sensing the Path) and is presented here for the sake of consolidating it in one place.
The remainder of this chapter will be dedicated to developing the Controller
class (inside Controller.java
), so I will only present revisions to that one class with the understanding that the rest of the code in the project remains unchanged.
The ColorSensor.java
program implements the ColorSensor
class that is used to detect the color of the spot immediately below the color sensor. This information is used to determine whether the robot is currently on the path or not. Chapter 7, Sensors and Motors – the LeJOS
way, is dedicated to this class:
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 DifferentialDrive.java
program implements the DifferentialDrive
object that directly controls the left and right Large Motors to carry out tasks assigned to differential drive, such as moving forward (in a straight line) and rotating in place in both directions. This is discussed in considerable detail in Chapter 8, The Line-follower Robot:
import lejos.hardware.motor.EV3LargeRegulatedMotor; import lejos.hardware.port.Port; public class DifferentialDrive { private EV3LargeRegulatedMotor mLeftMotor; private EV3LargeRegulatedMotor mRightMotor; private final static int SPEED = 200; public DifferentialDrive(Port left_port, Port right_port) { mLeftMotor = new EV3LargeRegulatedMotor(left_port); mRightMotor = new EV3LargeRegulatedMotor(right_port); mLeftMotor.setSpeed(SPEED); mRightMotor.setSpeed(SPEED); } public void forward() { mLeftMotor.forward(); mRightMotor.forward(); } public void stop() { mLeftMotor.stop(); mRightMotor.stop(); } public void rotateClockwise() { mLeftMotor.forward(); mRightMotor.backward(); } public void rotateCounterClockwise() { mLeftMotor.backward(); mRightMotor.forward(); } }
The LineFollower.java
program is the main class of the project. It contains the main()
method, which is where the execution of the program begins. After extensive use over the three earlier chapters, we have massively simplified this class. Its only task now is to create the Controller
object and call its run()
method. Controller
then takes over and carries out the actual task of following the line:
import lejos.hardware.port.MotorPort; import lejos.hardware.port.SensorPort; public class LineFollower { public static void main(String[] args) { Controller controller = new Controller(SensorPort.S1, MotorPort.B, MotorPort.C); controller.run(); } }
The build.gradle
file is the configuration file used by Gradle to compile the project and create an executable .jar
file that EV3 can execute. Its key features are that it defines LineFollower
as main_class
(where execution starts) and it defines the folders where one can find the source code for the project, including the location of the LeJOS library classes that are imported in our own code.
With the build.gradle
file correctly set up, one can use the gradle build
command to compile the project. This generates the LineFollower.jar
file in the build
/libs
subfolder:
apply plugin: 'java' def main_class = "LineFollower" sourceSets { main { java { srcDirs = ['src', 'ev3/DBusJava/src', 'ev3/ev3classes/src'] } dependencies { compile files('ev3/ev3classes/lib/jna-3.2.7.jar') } jar { manifest { attributes("Main-Class": main_class, "Class-Path": "/home/root/lejos/lib/ev3classes.jar" + " /home/root/lejos/libjna/usr/share/java/jna.jar") } } } }
The Makefile
is an optional configuration file used by the make utility. Its use and usefulness is elucidated in Appendix A, The Make Utility. The same Makefile
with a single line changed can be used for every LeJOS project. For the Line-follower robot, the only change that need be made is to set NAME=LineFollower
.
18.221.165.246