Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, January 11, 2014

Building LeJOS programs for EV3 using gradle

With the wifi connection (including ssh) to the EV3 running and IntelliJ set up to write programs, development for the EV3 was coming along nicely. There was still an issue, however, with the time it took to make a change in the code, compile it, copy it to the EV3 (using scp), run it on the EV3 and then repeat. Since repetition is the bane of all programmers I started looking in to ways to automate these tasks. Of course the ubiquitous make tool came instantly to mind. I would just write a make script (Makefile) to automate these tasks. But how to automate the build task that creates the jar file from the source code, and in such a way that make could do it.

The solution is gradle. Gradle is a project automation tool with excellent support for Java (Google now recommends using gradle and Android Studio - a port of IntelliJ - for Android development). Though gradle has somewhat of a learning curve, it is well worth it, since only after using gradle have I finally begun to understand how a java project comes together. Furthermore with gradle one can build projects from the command line by simply issuing the command gradle build.

Gradle installation is straightforward and is explained here. Once installed the next step is to create a build.gradle file in the root folder of your LeJOS project (your source code). The build.gradle for one of my earlier projects looks like this:

apply plugin: 'java'

def main_class = "Rover"          // We define the main class of the project

// We set the Version of the Java SDK to the one on the system: 1.6
sourceCompatibility = 1.6

sourceSets {

    main {

        // We specify the source directories for the project. The first one is created by us while the latter two are part of the LeJOS source and provide
        // essential classes for controlling the EV3.
        java {
            srcDirs = ['src', '/home/abid/applications/lejos/ev3/DBusJava/src', '/home/abid/applications/lejos/ev3/ev3classes/src']
        }

        // We specify the jar library required to compile the project. This provides the com.sun.jna package
        dependencies {
            compile files('/home/abid/applications/lejos/ev3/ev3classes/lib/jna-3.2.7.jar')
        }

        // We specify manifest attributes (key-value pairs that must appear in the META-INF/MANIFEST>MF file inside the output Jar for the EV3 to run it correctly.
        // Additionally we supply the name of the Main-Class which is the starting point of the program execution.
        jar {
            manifest {
                attributes("Main-Class": main_class, "Class-Path": "/home/root/lejos/lib/ev3classes.jar /home/root/lejos/libjna/usr/share/java/jna.jar")
            }
        }
    }
}
This is pretty much a template build.gradle file for any LeJOS EV3 project. The one-time alterations needed are to change the latter two srcDirs to point to the locations in the local LeJOS git repo as well as the jna jar dependency in the same.

The only thing one needs to alter (from project to project) is the name of the main class defined in the variable main_class near the beginning. And you are good to go. Execute gradle build and the project will compile and place the results in the build/ sub-folder.

Next time I will show you how to incorporate all of this in to one epic Makefile complete with dependencies.

Using LeJOS to write write programs for the EV3

Over the years I have thought many times of getting in to Android development; buying books, reading up online tutorials and such. But every time I would be driven away by the sheer ugliness, unusability and slowness of Eclipse. It wasn't until I discovered IntelliJ that I could start making sense of Android and now that is what I do for a living.

What this means is that when I bought the Lego Mindstorms EV3 and installed LeJOS on it the next step was figuring out how to develop programs for it on IntelliJ (and not Eclipse as suggested by the guys at LeJOS, but what do they know). This post explains how to do it on Windows and since IntelliJ is multi-platform the instructions can be ported trivially to Linux.

A few clarifications to the instructions follow:

The LeJOS source code (the classes required for development - think of it as the SDK) can be acquired using:
    git clone git://git.code.sf.net/p/lejos/ev3

Once the repo has been cloned switch to the latest release (0.5.0-alpha):
    git checkout 0.5.0-alpha
This will place you in a HEAD-less state but that is alright.

Your main class doesn't need to be labelled "Main". It just needs to implement the function public static void main(String[] args). This class serves as the entry point in to your application (execution starts in the main function/method. Just remember to use the actual name of the class in the MANIFEST.MD file when specifying the Main-Class value.

Thursday, January 9, 2014

Programming Lego Mindstorms EV3 using LeJOS on Ubuntu Linux

I have been coveting a Lego Mindstorms Robotic kit ever since I learned there was one to be had and learning that the latest version, the EV3 runs Linux was the last straw. I was still hesitant because though the EV3 ran Linux there was no support for programming it in Linux, that is, from a PC running Linux. Out of the box the EV3 can only be programmed using a Windows or Mac machine, and even then only in a GUI suite ("visual programming") geared towards children.

What I needed was the power of a fully-formed language where I could program the EV3 using my Linux desktop (it is what I call my 7 year old laptop that I run only as a box). Lego had chosen to place Linux on the EV3 to make it more hackable and their foresight paid dividends when the LeJOS project was able to fork the open-source Lego kernel allowing them to place a JVM (Java Virtual Machine) and give access to the complete functionality of the EV3 using Java classes. Since I am an Android Developer by day, this was perfect.

The really great thing about the EV3 is that it comes with a micro-SD slot on which you can place your own bootable partition which allows you to change the firmware of the device without actually having to mod it. Basically, write a suitable image to a micro-SD card and the EV3 will boot from it. Remove the sd card and the EV3 will return to booting from the Lego firmware.

The LeJOS project provides a complete tutorial on how to create a micro-sd card that will run LeJOS on the EV3:

Stay tuned for more information on how to use a (supported) Wifi dongle (USB), program the EV3 using IntelliJ and automate the deployment of custom programs using ssh, make and gradle.

Happy hacking on the EV3.