[LeJOS] Get EV3 sensor value remotely with Java

Introduction

This article assumes that you have a leJOS development environment in place. Please refer to this article for details.

[LeJOS] Let's program mindstorm-EV3 in Java [Environment construction first part]

[LeJOS] Let's program mindstorm-EV3 in Java [Environment construction part 2]

Overview

leJOS allows remote control of Ev3 via WiFi using the remoteEv3 class. In this article, I will explain how to get the Ev3 sensor value remotely. Please refer to this article for how to remotely control the motor.

[LeJOS] Let's remotely control the EV3 motor with Java

Sample program

The sensor value is output as standard on the console. Connect the touch sensor to port 1 of the EV3 main unit, the ultrasonic sensor to port 2, the color sensor to port 3, and the gyro sensor to port 4. IMG_8616.JPG

Execute the following program on your PC. (No need to deploy the program to Ev3)

RemoteEv3Sensor.java



import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import lejos.remote.ev3.RMISampleProvider;
import lejos.remote.ev3.RemoteEV3;

public class RemoteEv3Sensor {

	public static void main(String[] args) throws RemoteException {
		RemoteEV3 ev3 = null;
		RMISampleProvider TouchSensor = null;
		RMISampleProvider UltraSonicSensor = null;
		RMISampleProvider ColorSensor = null;
		RMISampleProvider GyroSensor = null;
		
		try {
			//Connect to EV3, EV3 IP address as an argument
			ev3 = new RemoteEV3("192.168.2.91");	
			ev3.setDefault();
		} catch (RemoteException | MalformedURLException | NotBoundException e) {
			e.printStackTrace();
		} 
		
		TouchSensor = ev3.createSampleProvider("S1", "lejos.hardware.sensor.EV3TouchSensor","Touch");
		UltraSonicSensor = ev3.createSampleProvider("S2", "lejos.hardware.sensor.EV3UltrasonicSensor","Distance");
		ColorSensor = ev3.createSampleProvider("S3", "lejos.hardware.sensor.EV3ColorSensor","RGB");
		GyroSensor = ev3.createSampleProvider("S4", "lejos.hardware.sensor.EV3GyroSensor","Angle");
		
		try{
			System.out.println("TouchSensor");
			printData(TouchSensor.fetchSample());
			System.out.println("UltraSonicSensor");
			printData(UltraSonicSensor.fetchSample());
			System.out.println("ColorSensor");
			printData(ColorSensor.fetchSample());
			System.out.println("GyroSensor");
			printData(GyroSensor.fetchSample());
		}finally{
			TouchSensor.close();
			UltraSonicSensor.close();
			ColorSensor.close();
			GyroSensor.close();
		}
	}
	
	public static void printData(float[] sample){
		for(float data: sample) {
	         System.out.println(data);
	    }
	}
}

Execution result

The data of each sensor is output to the standard output. The touch sensor outputs a value of 0 or 1, which indicates the pressed state, the ultrasonic sensor outputs the distance value (m) to the object, the color sensor outputs the R, B, G values of the object, and the gyro sensor outputs the angle value (°). Will be done.

TouchSensor
0.0
UltraSonicSensor
0.076000005
ColorSensor
0.0
0.0
0.0
GyroSensor
-0.0

Commentary

Use the createSampleProvider method to access the remote sensor. Set the connection destination port in the first argument, the sensor name in the second argument, and the sensor acquisition mode in the third argument as a String type.

		TouchSensor = ev3.createSampleProvider("S1", "lejos.hardware.sensor.EV3TouchSensor","Touch");
		UltraSonicSensor = ev3.createSampleProvider("S2", "lejos.hardware.sensor.EV3UltrasonicSensor","Distance");
		ColorSensor = ev3.createSampleProvider("S3", "lejos.hardware.sensor.EV3ColorSensor","RGB");
		GyroSensor = ev3.createSampleProvider("S4", "lejos.hardware.sensor.EV3GyroSensor","Angle");

The sensor value can be received as a float type array by using the fetchSample method.

TouchSensor.fetchSample();

Finally, be sure to use the close method to close the access to the sensor. If you get the error "Can not create SampleProvider", it's because it's not working properly.

TouchSensor.close();
UltraSonicSensor.close();
ColorSensor.close();
GyroSensor.close();

reference

https://sourceforge.net/p/lejos/wiki/Remote%20access%20to%20an%20EV3/

Summary

In addition to the above, it is now possible to remotely control the Ev3 motor and sensor.

[LeJOS] Let's remotely control the EV3 motor with Java

Next time, I would like to use these to develop a GUI application that runs on a PC.

Recommended Posts

[LeJOS] Get EV3 sensor value remotely with Java
[LeJOS] Let's remotely control the EV3 motor with Java
[LeJOS] Let's control the EV3 motor with Java
[Java] Get List / Map elements with Iterator
Get Timestamp with Azure BlobStorage Java SDK
[Java] Get the date with the LocalDateTime class
Get Value from URL parameter with GetMapping ~ LocalDate ~
Get block information with Hyperledger Iroha's Java SDK
[Java] Get multiple values from one return value
[Java] Get a random value from an array
Let leJOS ev3 communicate with Bluetooth without blocking
Get along with Java containers in Cloud Run
Experienced Java users get started with Android application development
[Java] How to get the maximum value of HashMap
Get Azure App Service for Java Configuration with System.getEnv ()
[Java] Get the date 10 days later with the Calendar class
[Java] Get the file path in the folder with List
I want to get along with Map [Java beginner]
Get started with serverless Java with the lightweight framework Micronaut!