Remote Control of EV3 Robot using Java

Author: Gunja

Remote Control of EV3 Robot using Java

 

Have you tried accessing your EV3 robot from a java program running on your desktop or server? Have you tried controlling robot action using a program not running on your EV3? Have you tried accessing sensor data on your desktop? Have you imagined how to control your robot remotely?

 

This article will assist you implement a java program to remotely access your EV3 robot, controlling motors and access sensor data. You will need to build a simple EV3 robot with 2 motors, and 1 IR sensor. You can design a bot similar to GrabberBot (http://www.robogalaxy.com/designs/GrabberBot). You may not want to use 3rd motor in GrabberBot used to move the grabber arm, as in this sample, we are not grabbing any object.

 

Robot’s function is to move forward, and if detects any object in range of 35 cms, it will turn, and again move forward. This is a repetitive task, and robot will keep exploring the surrounding to detect an object. Programming is done in Java using leJOS APIs, and if you are new to leJOS I recommend you go through article “Program EV3 Robot using java”. This article will explain java programming on EV3, and assist you with setting up your desktop / laptop and EV3 environment.

 

This is a simple use case, however once you know how to access EV3 remotely, you can program infinite number of use cases, like navigation of robot through remote control, navigation through voice commands, storing sensor data on your server for analytics and decision making, machine to machine communication, integration with your cloud environment, robot to robot communication, and many other use cases.

 

 

Logical Flow of Program

  • Detect EV3 Brick
  • Create motor and sensor objects
  • Loop to move robot forward
  • Loop to turn robot if object is detected
  • Sample program exit after detecting 4 objects

 

How to Execute Your Program?

  • Develop the code using Eclipse IDE
  • To run the program – Right click class file -> Run As -> Java Application
  • Monitor System Outs in Eclipse Console
  • You will notice distance to object is printed in the console

//SAMPLE PROGRAM

 

package MyFirstPackage;

 

import lejos.hardware.BrickFinder;

import lejos.hardware.ev3.EV3;

import lejos.hardware.motor.EV3LargeRegulatedMotor;

import lejos.remote.ev3.*;

import lejos.utility.Delay;

import lejos.remote.ev3.RMIRegulatedMotor;

import lejos.hardware.port.*;

import lejos.hardware.sensor.*;

import lejos.robotics.SampleProvider;

import lejos.robotics.filter.MeanFilter;

import lejos.hardware.Audio;

 

public class MyDesktopClient {

 

            public static void main(String[] args) {

                       

                        // Detecting EV3 Brick

                        RemoteEV3 ev3 = (RemoteEV3) BrickFinder.getDefault();

                                   

                        // Creating objects for motor and IR sensor

                        RMIRegulatedMotor leftMotor = ev3.createRegulatedMotor("B", 'L');

                        RMIRegulatedMotor rightMotor = ev3.createRegulatedMotor("C", 'L');

                        Port p = ev3.getPort("S1");

                        EV3IRSensor ir = new EV3IRSensor(p);

                        SampleProvider distance = ir.getDistanceMode();

                        int maxlimit;

                        //Make EV3 beep                                        

                        Audio audio = ev3.getAudio();

                        audio.systemSound(0);

                        maxlimit = 0;

                        try {

                            //Sensor Data

                                    while (maxlimit <= 3) {

                                                maxlimit++;

                                    // Running average of the last 5 samples

                                    SampleProvider average = new MeanFilter(distance, 5);

                                    float[] sample = new float[average.sampleSize()];

                                    //Fetch a sample

                                    average.fetchSample(sample, 0);

                                    int dist = (int) sample[0];

                                    ,

                                    while (dist > 35) {

           

                                    // Move robot forward when sensor does not detect anything in range of 35 cms

                                    System.out.println("Robot Moving Forward");

                                    leftMotor.forward();

                                    rightMotor.forward();

                                                                                                                       

                                                average = new MeanFilter(distance, 5);

                                                sample = new float[average.sampleSize()];

                                                average.fetchSample(sample, 0);

                                                dist = (int) sample[0];

                                               

                                    }

                                                System.out.println("Objected detected at distance: " + dist);

                                    System.out.println("Robot Turning");                              

                                    leftMotor.stop(true);

                                    rightMotor.stop(true);

                                    leftMotor.backward();

                                    Delay.msDelay(2000);

                                    leftMotor.stop(true);

                                    }

                                    System.out.println("BYE BYE");

                            }

                            catch(java.lang.Exception e) {

                                    System.out.println(e.toString());

                            }

               }

}


Comments

16-06-2016

I was getting an error "unable to open port" when I run this program twice (without restarting EV3). I observed this was due to not closing the port. I inserted below code (just before closing try block and in catch block as well) and it solved the problem.

System.out.println("CLOSING PORTS");
ir.close();
leftMotor.close();
rightMotor.close();

Post a comment