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]
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)
EV3 Large Regulated Motor class can be used to control the L motor.
RegulatedMotor m = new EV3LargeRegulatedMotor(MotorPort.A);
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).
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)
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);
}
}
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