Menu

Developing with leJOS (old)

Andy Shaw Lawrie Griffiths
Attachments
rse.png (96867 bytes)

Developing with leJOS


This section shows you how you can develop programs for the EV3 using leJOS. The basic steps are:
1. Install the development environment.
2. Install tools to allow access to the EV3.
3. Download the current leJOS source code.
4. Write and run your first leJOS EV3 program.

Install the development environment

leJOS uses the Eclipse development environment. Because of the tools we use make sure it is a recent version (I use Kepler - version 4.3). Eclipse can be downloaded from here:
http://www.eclipse.org/downloads/?osType=linux
The best configuration for leJOS development is "Eclipse IDE for Java Developers". Documentation on how to install Eclipse can be found here:
http://wiki.eclipse.org/Eclipse/Installation
You will also need to install Java and the Java Native Access packages. The easiest way to do this is to use the Ubuntu Software Centre. The following packages should be installed (along with any dependencies).
1. OpenJDK Java 7 Runtime (openjdk-7-jre 7u25-2.3.10-1ubuntu0.12.10.2).
2. Dynamic access of native libraries from Java without JNI (libjna-java 3.2.7-4).

Once the above have been installed you should be able to run Eclipse. Note: Do not install the version of Eclipse provided by the Ubuntu Software Centre, this version is older and not suitable for use with leJOS.

Install tools to allow access to the EV3

To allow you to download and run programs on the EV3 we make use of the Eclipse Remote System Explorer extension:
http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.rse.doc.user%2Fgettingstarted%2Fg_start.html&cp=49_0

To install RSE use the following steps:
1. Start Eclipse.
2. From the Help menu choose "Install new software..."
3. In the "Work with" drop down choose "Kepler - http://download.eclipse.org/releases/kepler".
4. Open up the "General purpose tools category", from this locate and select "Remote System Explorer End-User Runtime".
5. Select next and continue to install the feature.
6. When requested restart Eclipse.

To allow the RSE to operate with the EV3 we need to make use of an additional feature, the scp connector. This feature needs to be downloaded and installed by hand. The feature can be downloaded from:
http://download.eclipse.org/tm/downloads/drops/R-3.4.1-201209191030/
Locate the entry "RSE-scp-incubation-3.4.1.zip" and download it. Extract the files from the zip file to a temporary directory (note do not extract this file to the same location as your eclipse install). Once the files are extracted take the following steps:
1. From the Help menu choose "Install new software..."
2. Click on "Add..."
3. In the "Add repository" dialog use the local button to browse to the folder containing the extracted files.
4. Click on OK.
5. Un-check the "Group items by category" option.
6. A single item should be displayed "RSE SCP File Services (Incubation)", choose this and click on next and complete the install.
7. Restart Eclipse if required.

We now need to set things up so you have access to your EV3. First switch to the RSE perspective by clicking on the "Window" menu item choose "Open perspective" and then "Other". Locate the "Remote System Explorer" item and choose it.

Eclipse should now show a view that looks like this:

You can click on the "local" item to explore your local file system. To add an en entry for your EV3.
1. Boot your EV3 using the leJOS SD card.
2. In the "Remote" tab click on the first small icon ("Define a connection to remote system").
3. Set the system type to be "SSH Only" and click next.
4. Set the "Hostname" to the IP address of your EV3 (this should be being displayed on the EV3 screen), enter the name of your EV3 in "Connection name" I used "EV3", click next.
5. Change the selected configuration from "ssh.files" to "scp.files", click finish.

A new entry for your EV3 should now be visible in the "Remote" tab. To test it right click on the item, and choose "connect". You will have to provide credentials for the EV3 (username is root, password is empty). You may be prompted to verify the ssh kays and to allow the creation of various files, say OK to all of these. If all goes well. The icon should have expanded to show "Scp files" and "Ssh Terminals" you should be able to click on "Scp files" to browse your EV3 file system. Right clicking on "Ssh terminals" and choosing "Launch terminal" should open up a terminal window to your EV3.

Download the current leJOS source code

The leJOS source code is stored in a Sourceforge git repository. In this section we will obtain a copy of this for local use. If you are not familiar with git you may want to read the following:
http://git-scm.com/book
http://wiki.eclipse.org/EGit/User_Guide

To import the leJOS files first switch from the RSE perspective to the Java perspective (Use the "Java" button displayed near the top right of the Eclipse Window). Then:
1. Clock on the "File" menu and choose "Import...".
2. For the import source choose "git" and then "projects from git" and click next.
3. Choose "URI" and click next.
4. To obtain a read only copy of the repository enter the following URI:
git://git.code.sf.net/p/lejos/ev3
click next.
5. Ensure that the branch is set to "master" and click next.
6. Check that you are happy with the chosen local destination directory (probably best not to change this unless you know what you are doing) and click next.
7. After a short while the git clone should be complete and you can click next.
8. You should now see a list of projects that you can import. Deselect any projects that you do not require, you must have the "ev3classes" project, others are optional. Click finish.
9. All of the projects (except for ev3classes) should import without any errors (there may be warnings), if there are any errors, resolve these before proceeding. There are currently errors with some of the classes in the ev3classes project mainly due to issues with older NXT sensor drivers. I hope to resolve these shortly, but for now you can ignore them.

Write and run your first leJOS EV3 program

Now we get to create your first EV3 program. First we need to create a new project:
1. Click on "File" and choose "new" then "Java project".
2. Set the project name to be "EV3FirstProgram" then click next.
3. Click the "Projects" tab and then click on "Add", check "ev3classes" then click OK.
4. Ensure that ev3classes is now shown in the "Projects" tab and click Finish.

We now need to add a simple class to the project:
1. In "package explorer" click on "EV3FirstProgram" right click on src and choose "new" then "class".
2. Set the "Name" to be "EV3FirstProgram" check the "public static void main(String[] args)" option then click finish.
3. In the editor window change things until the code looks like this:

~~~~~~~~~~~~~~~~~~~~~~~
import lejos.nxt.Button;
import lejos.nxt.LCD;

public class EV3FirstProgram {

public static void main(String[] args) {
    LCD.clear();
    LCD.drawString("First EV3 Program", 0, 5);
    Button.waitForAnyPress();
    LCD.clear();
    LCD.refresh();

}

}
~~~~~~~~~~~~~~~~~~~~~~~~

Click the "File" menu and then "Save".

To make the program easier to move to the EV3 we now need to package it as a jar file to do this:
1. In the "Package Explorer" tab right click on "EV3FirstProgram" and choose "Export..."
2. Click on "Java" and then "jar file", click next.
3. Ensure that EV3FirstProgram is selected in the resources list.
4. Set the "JAR File" entry to be "EV3FirstProgram/bin/EV3FirstProgram.jar", this will store the jar file in the bin folder of your project. The project will be located in the workspace location you chose when first starting Eclipse (probable a directory called "workspace" in your home dir).
5. Click finish.

We now need to download the program to the EV3 and run it:
1. Switch to the "Remote System Explorer" perspective by clicking on the button located towards the top right of the Eclipse Window.
2. If you are not currently connected to your EV3, start it and follow the instructions in the previous section to connect to the EV3.
3. In the "Remote" tab click on the "Local" item and browse to the directory containing the EV3FirstProgram.jar file.
4. Right click this file and choose copy.
5. Click on the "EV3" item and then on "Scp files" and "My Home" to browse the EV3 file system. Right click on "My Home" and choose paste.
6. The file will now be copied to the EV3, you may see an error saying that "setLastModified() is not supported" you can ignore this error.
7. Open a terminal window to your EV3. Typing ls should show that the jar file has been transferred.
8. To run your program type:

jrun -cp EV3FirstProgram.jar EV3FirstProgram
  1. If all is well after a short while a message should appear on the EV3 screen. Press any button to end the program.
  2. To shutdown your EV3 type "halt" in the terminal window.
  3. Make yourself a cup of tea (or a beverage of your choice!) to celebrate!

Congratulations you can now create and run Java programs on the EV3. Be sure to post on the forum and let us know how you are using leJOS!