Program EV3 Robot Using Java

Author: Gunja

Program Your EV3 Robot using Java

 

Java is a veteran technology in software industry and usage has expanded from a small web application to advance robotics industry. There are multiple programming platform available to program your robots including graphical user interface environment wherein you need to drag and drop the components and stitch together to design behavior of your robots to micro controller programming. You can program your robots using Java, Java provides a unique advantage, as it is an industry standard language, supports object oriented programming, and skill set is widely available.

 

In this article, I am going to touch upon how you can develop a Java program and run on your LEGO MINDSTORMS EV3 Robots. I am using leJOS, a Java Virtual Machine ported on EV3 brick. leJOS for the EV3 includes all the classes in the EV3 API and a full Java run time system.

 

What you need to try this out?

- LEGO MINDSTORMS EV3 Brick

- Micro SD card (at least 2GB and no more than 32GB. SDXC cards are not supported by the EV3 hardware)

- List of Software (as listed in below article)

 

You need to setup your EV3 and Desktop / Laptop to run Java program on EV3. Here are the broad steps to setup your environment, and get you going to develop your first Java program on EV3 -

 

1. Format SD Card (https://www.sdcard.org/downloads/formatter_4/) - SD Card is required to install LeJOS on EV3

2. Install JDK 7 on desktop (http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html) - JDK is required to setup Java environment

3. Install EV3 Oracle JRE 7 http://java.com/legomindstorms - This is required to setup java run time on EV3

4. Install LeJOS (https://sourceforge.net/projects/lejos/files/lejos-EV3/) - This is required to program and run your Java program

5. Install Eclipse - Programming IDE

6. Install LeJOS EV3 Eclipse Plugin (From Eclipse tool) - This is required to compile and build leJOS program for your EV3

7. Insert SD Card into EV3 - This will install LeJOS on your EV3

 

Once you have setup the environment, you need to open the Eclipse and write your program using LeJOS APIs. LeJOS API provides classes and interfaces to your robot components like motor, sensors, etc. You need to write your program using standard Java API and LeJOS API. Once you write the program, you need to run this program. As you have already installed Eclipse plugin, you really do not need to worry about compilation and running. Simply go to package explorer, and then to class file you just developed. You have to right click on the class file, then Run As, and then run it as LeJOS EV3 program. This will compile your package, load the program on EV3, and execute it. Please ensure while compiling, using "Run As" your EV3 is connected to your development environment (desktop / laptop).

 

I have developed a sample program using a design published on RoboGalaxy site (http://www.robogalaxy.com/designs/TunnelBot). This model is using 2 servo motors and I have implemented a basic control for moving robot forward and backward. You will notice there is a commented piece of code. You can un-comment this code. Commented code will run your robot forward indefinitely till you press any button.

 

You can refer to LeJOS APIs from your installation. Typically these are available in \Program Files\leJOS EV3\docs\ev3. However path may change basis your local environment.

 


//SAMPLE PROGRAM

 

package MyFirstPackage;

 

import lejos.hardware.BrickFinder;

import lejos.hardware.lcd.GraphicsLCD;

import lejos.hardware.motor.Motor;

import lejos.utility.Delay;

import lejos.robotics.RegulatedMotor;

import lejos.hardware.motor.*;

import lejos.hardware.port.*;

import lejos.utility.Delay;

import lejos.hardware.Button;

 

public class MyFirstClass {

 

            GraphicsLCD LCD = BrickFinder.getDefault().getGraphicsLCD();

            static RegulatedMotor leftMotor = new EV3LargeRegulatedMotor(MotorPort.B);

    static RegulatedMotor rightMotor = new EV3LargeRegulatedMotor(MotorPort.C);

   

    void moverobotfwd(){

           

            LCD.clear();

            // Display on robot screen

            LCD.drawString("Moving Forward", 100, 20, GraphicsLCD.BASELINE|GraphicsLCD.HCENTER);

            // This code will set the speed and move the robot forward for 5 seconds

            leftMotor.setSpeed(400);

            rightMotor.setSpeed(400);

            leftMotor.forward();

            rightMotor.forward();

            Delay.msDelay(5000);

            // This code will stop the motors after 5 seconds

            leftMotor.stop();

            rightMotor.stop();

 

            /*while (true) {

                                     leftMotor.setSpeed(400);

                                     rightMotor.setSpeed(400);

                     leftMotor.forward();

                     rightMotor.forward();

                     if (Button.readButtons() != 0) {

                         leftMotor.stop();

                         rightMotor.stop();

                         System.exit(1);

                     }

                         }*/

        

            }

            void moverobotbkw() {             

                        LCD.clear();

                        // Display on robot screen

                        LCD.drawString("Moving Backward", 100, 20, GraphicsLCD.BASELINE|GraphicsLCD.HCENTER);

                        // This code will set the speed and move the robot backward for 5 seconds        

                        leftMotor.setSpeed(400);

                        rightMotor.setSpeed(400);

                leftMotor.backward();

                rightMotor.backward();

                Delay.msDelay(5000);

                // This code will stop the motors after 5 seconds

                leftMotor.stop();

                rightMotor.stop();

                                   

            }

            public static void main(String[] args) {

                        MyFirstClass sample = new MyFirstClass();

                        //Robot will start when you press a button

                        Button.waitForAnyPress();

                        sample.moverobotfwd();

                        sample.moverobotbkw();

            }

 

}


Comments

chendra.sekhar

17-02-2017

Hi...how can i run this program on my Lego ev3 kit.? tell me the procedure

18-02-2017

First you need to setup your EV3 brick and LeJOS environment (you may refer step 1 to 7 above), and then you can run your program using Eclipse (Run As) feature. Steps are provided above in this article.

Post a comment