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]
The sample code explains how to get the event when the motor starts or stops rotating. Implement using the addEventListener method.
Ev3Motor.java
import lejos.hardware.Button;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.RegulatedMotorListener;
public class Ev3Motor {
private static RegulatedMotor m;
public static void main(String[] args) {
m = new EV3LargeRegulatedMotor(MotorPort.A);
m.addListener(new RotateStateEvent());
m.rotate(360);
Button.waitForAnyPress();
}
}
class RotateStateEvent implements RegulatedMotorListener{
@Override
public void rotationStarted(RegulatedMotor motor, int tachoCount, boolean stalled, long timeStamp) {
System.out.println("ROTATE");
}
@Override
public void rotationStopped(RegulatedMotor motor, int tachoCount, boolean stalled, long timeStamp) {
System.out.println("STOP");
}
}
In the sample program, an instance of RotateStateEvent class that inherits the RegulatedMotorListener interface is given as an argument of the addEventListener method. The RegulatedMotorListener interface defines a rotationStarted method that fires when the motor starts rotating and a rotationStopped method that fires when the motor stops rotating. Let's implement the process you want to realize in each method. In the sample program, it is implemented to output "ROTATE" or "STOP" when an event fires on the LCD.
You can get the button press event of the EV3 main unit in the same way. For example, to register the press event of the center button of the EV3 main unit, write as follows using the addKeyListener method.
Button.ENTER.addKeyListener(new ButtonEvent(m));
In the sample program, an instance of ButtonEvent class that inherits the lejos.hardware.KeyListener interface is passed as an argument. Implement the keyPressed method that fires when the button is pressed and the keyReleased event that fires when the button is released in the ButtonEvent class.
The following is a sample program that switches the rotation direction of the motor depending on how the button is pressed.
Ev3Motor.java
import lejos.hardware.Button;
import lejos.hardware.Key;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.RegulatedMotorListener;
import lejos.hardware.KeyListener;
public class Ev3Motor {
private static RegulatedMotor m;
public static void main(String[] args) {
m = new EV3LargeRegulatedMotor(MotorPort.A);
m.addListener(new RotateStateEvent()); //Registration of motor rotation state event
Button.UP.addKeyListener(new ButtonEvent(m)); //Button press status event registration
Button.DOWN.addKeyListener(new ButtonEvent(m));
Button.ESCAPE.addKeyListener(new ButtonEvent(m));
while(true){}
}
}
class RotateStateEvent implements RegulatedMotorListener{ //Motor rotation state change event
@Override
public void rotationStarted(RegulatedMotor motor, int tachoCount, boolean stalled, long timeStamp) { //Event at the start of rotation
System.out.println("ROTATE");
}
@Override
public void rotationStopped(RegulatedMotor motor, int tachoCount, boolean stalled, long timeStamp) { //Event when rotation stops
System.out.println("STOP");
}
}
class ButtonEvent implements KeyListener{ //Button press state event
private RegulatedMotor _Motor;
public ButtonEvent(RegulatedMotor Motor) {
_Motor = Motor;
}
@Override
public void keyPressed(Key k) { //Event when the motor is pushed
switch (k.getId()){
case Button.ID_UP:
System.out.println("UP");
_Motor.forward();
break;
case Button.ID_DOWN:
System.out.println("DOWN");
_Motor.backward();
break;
}
}
@Override
public void keyReleased(Key k) { //Event when the button is released
switch (k.getId()){
case Button.ID_UP:
_Motor.stop();
break;
case Button.ID_DOWN:
_Motor.stop();
break;
case Button.ID_ESCAPE:
System.out.println("ESCAPE");
_Motor.stop();
System.exit(0);
break;
}
}
}