[LeJOS] Let's control the EV3 motor 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

Here are some of the APIs that control EV3 motors with leJOS. Basic motor control can be achieved by suppressing only the methods introduced. Also, I would like to show you how to use the API concretely with a simple sample program.

API Let's connect the L motor to port A of the EV3 main unit. (L motor)

EV3LargeRegulatedMotor class

EV3 Large Regulated Motor class can be used to control the L motor.

RegulatedMotor m = new EV3LargeRegulatedMotor(MotorPort.A);

Method

I will introduce the methods that are mainly used. addListener

public void addListener(RegulatedMotorListener listener)

You can register events for starting and stopping the rotation of the motor. removeListener

public void removeListener()

Delete the event.

forward

public void forward();

Rotate the motor forward. Does not stop until stop is called.

backward

public void backward()

Rotate the motor in the reverse direction. Does not stop until stop is called.

stop

public void stop()

Stop the motor.

rotate

public void rotate(int angle)

Rotates relative to the current position by the angle specified in the argument.

rotateTo

public void rotateTo(int limitAngle)

Rotates to the absolute angle specified in the argument.

close

public void close()

Closes access to the motor and frees resources.

flt

public void flt()

Set the motor to float mode. The position of the motor is not maintained because the motor stops without braking.

getSpeed

public int getSpeed()

Returns the current rotation speed. getRotationSpeed

public int getRotationSpeed()

Returns the current acceleration. getAcceleration

public int getAcceleration()

Returns the current angular velocity (degree / s / s).

getPosition

public float getPosition()

Returns the angle of the current position of the motor.

getLimitAngle

public int getLimitAngle()

Returns the maximum position angle at which the motor rotates. getMaxSpeed

public float getMaxSpeed()

Returns the maximum speed that can be set for the motor.

isMoving

public boolean isMoving()

Returns true if the motor is spinning.

isStalled

public boolean isStalled()

Returns true if the motor is stopped. lock

public void lock(int power)

The motor will try to hold the position unless it is set to float mode using the flt method. Set the strength of the force held in the argument between 1 and 100.

setSpeed

public void setSpeed(int speed)

Set the rotation speed of the motor. setAcceleration

public void setAcceleration(int acceleration)

Sets the angular velocity of the motor passed as an argument. The default is (6000degree / s / s).

M motor

The EV3 Medium Regulated Motor class is used to control the M motor. The methods that can be used are basically the same as the EV3LargeRegulatedMotor class, so explanations are omitted. (M motor)

Sample program

It is a simple sample program that just moves the motor by changing the rotation speed.

Ev3MotorSample.java


import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;

public class Ev3MotorSample {

	public static void main(String[] args) {
		RegulatedMotor m = new EV3LargeRegulatedMotor(MotorPort.A);
		
		m.forward();
		Delay.msDelay(2000);
		m.stop();
		
		m.backward();
		Delay.msDelay(2000);
		m.stop();
		
		m.setSpeed((int) m.getMaxSpeed());
		
		m.forward();
		Delay.msDelay(2000);
		m.stop();
		
		m.backward();
		Delay.msDelay(2000);
		m.stop();
		
		m.setAcceleration(3000);
		m.rotate(720);
		m.setAcceleration(6000);
		m.rotate(-720);
	}

}

Click to play video YouTube

reference

Only some of the APIs have been introduced. If you want to know more detailed information, please refer to the official document.

https://sourceforge.net/p/lejos/wiki/Motors/

http://www.lejos.org/ev3/docs/

Recommended Posts

[LeJOS] Let's control the EV3 motor with Java
[LeJOS] Let's remotely control the EV3 motor with Java
[ev3 x Java] Single motor control
Let's scrape with Java! !!
[LeJOS] Get EV3 sensor value remotely with Java
Let's experiment with Java inlining
Let's operate Excel with Java! !!
Version control Java with SDKMAN
Java version control with jenv
Make an Ev3 radio control with JavaFx and leJOS [Part 2]
Make an Ev3 radio control with JavaFx and leJOS [Part 1]
[ev3 × Java] Display, sound, LED control
Follow the link with Selenium (Java)
Image processing: Let's play with the image
Returning to the beginning, getting started with Java ② Control statements, loop statements
Now let's recap the Java lambda expression
Let's express the result of analyzing Java bytecode with a class diagram
Let's try WebSocket with Java and javascript!
Java version control with jenv on OSX
Try using the Wii remote with Java
[Java] Get the date with the LocalDateTime class
Let's make a calculator application with Java ~ Create a display area in the window
Let's create a timed process with Java Timer! !!
Organized memo in the head (Java --Control syntax)
[Java] Set the time from the browser with jsoup
Understanding the MVC framework with server-side Java 1/4 View
Understanding the MVC framework with server-side Java 3/4 Controller
Calculate the similarity score of strings with JAVA
Java starting with JShell-A peek into the Java world
Understanding the MVC framework with server-side Java 2/4 Model
Let leJOS ev3 communicate with Bluetooth without blocking
Java control syntax
Java control syntax
Let's study Java
[LeJOS] Let's program mindstorm-EV3 in Java [Environment construction part 2]
Trace the SQL executed with the application Insights java agent
CI the architecture of Java / Kotlin applications with ArchUnit
[Java basics] Let's make a triangle with a for statement
Monitor the internal state of Java programs with Kubernetes
Check the behavior of Java Intrinsic Locks with bpftrace
Quick learning Java "Introduction?" Part 2 Let's write the process
[Java] Get the date 10 days later with the Calendar class
[Java] Get the file path in the folder with List
HTTPS connection with Java to the self-signed certificate server
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Control the processing flow of Spring Batch with JavaConfig.
The story of making dto, dao-like with java, sqlite
Java desktop with the same front end as the WEB.
Replace only part of the URL host with java
Get started with serverless Java with the lightweight framework Micronaut!