Setting up the LeJOS project

The first part of programming any EV3 robot using LeJOS is to set up the LeJOS project. This consists of the now-familiar sequence of steps:

  1. Create an empty folder/directory that will contain the project and name it LineFollower.
  2. Create subfolders named src and ev3.
  3. Inside the ev3 subfolder, copy the DBusJava and ev3classes library folders from the LeJOS source code.
  4. Create and populate the build.gradle file.
  5. Optionally create and populate Makefile (to use make to compile-transfer-execute the project) as described in Appendix A, The Make Utility.
  6. Inside the src subfolder, create and populate the LineFollower.java file.

We will add more source files in the src subfolder as we implement various parts of the robot's functionality. At the very least, we need the src/LineFollower.java file to have the smallest possible viable LeJOS project.

The build.gradle file

We will use the by-now familiar build.gradle file with slight modifications for this specific project:

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")
            }
        }
    }
}

In the preceding code snippet, we define the name of the main class (the one whose main() method is launched when the program is executed) to be LineFollower, the name of the project.

The LineFollower.java file

The main class is defined in the src/LineFollower.java file as follows:

public class LineFollower
{
    public static void main(String[] args)
    {
        log("Hello, World");
    }


    private static void log(String msg)
    {
        System.out.println("log>	" + msg);
    }
}

To test whether the project has been set up correctly, we populate the LineFollower class with two simple methods that simply print Hello, World to the terminal when the program is executed.

With this minimal setup, you can compile the program to get the .jar file, copy it to the /home/lejos/programs folder of the EV3, and execute it from a terminal (or, simply execute make run if you have the Makefile set up) to test the project setup. You should see Hello, World printed on the terminal, indicating that the project was set up correctly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.20.57