Each
Motor
instances and its childs require the developer to specify a
LegoPort
instance to let the
Motor
to know which port and device is pointing to.
LegoPort port = new LegoPort(LegoPort.OUTPUT_A); //Output A
Motor motor = new Motor(port); //Creates a Motor instance
//or (it is the same as)
Motor motor2 = new Motor(new LegoPort(LegoPort.OUTPUT_B); //Motor with Output B
//or (1.0.1 or older)
Motor motor3 = new Motor(LegoPort.OUTPUT_C);
The motor can be commanded with the following methods:
motor.runForever()
- Causes the motor to run util another command is sent
motor.runToAbsPos()
- Runs to an absolute position specified by
setPosition_SP(int pos)
motor.runToRelPos()
- Runs to a a position relative to the current position value
motor.runTimed()
- Runs the motor for the amount of time specified in
setTime_SP(int ms)
motor.runDirect()
- Runs the motor at the duty cycle specified by
setDutyCycle_SP(int dutycycle_sp)
motor.stop()
- Stops any of the run commands before they are complete using the command specified by
setStopCommand(String command)
motor.reset()
- Resets all of the motor parameter attributes to their default value. This will also have the effect of stopping the motor.
An example to run a motor for 5000 ms (5 seconds):
package org.ev3dev.examples;
import org.ev3dev.hardware.LegoPort; //Necessary class LegoPort
import org.ev3dev.hardware.Motor; //Necessary class Motor
import java.util.Arrays; //Just for this example to list port's modes
public class MotorDemo{
public static void main(String[] args) throws IOException, InvalidException{
LegoPort port = new LegoPort(LegoPort.OUTPUT_A); //Output/Motor Port A
Motor motor = new Motor(port);
motor.setTime_SP(5000); //Set time to 5000 ms
motor.setDutyCycle_SP(50); //Set speed to 50
motor.runTimed(); //Send command
}
}
}