Programming
Example Code:
@TeleOp
public class MotorExample extends LinearOpMode {
DcMotor motor;
@Override
public void runOpMode() {
motor = hardwareMap.get(DcMotor.class, "motor");
// Wait for the game to start (driver presses PLAY)
waitForStart();
// run until the end of the match (driver presses STOP)
while (opModeIsActive()) {
motor.setPower(gamepad1.left_stick_y);
}
}
}
Right after your class you need to initialize your motor using DcMotor motorName
Then after runOpMode you need to to use hardware map to set the motor's name.
Methods:
You can use any of these lines of code in your program to control your DcMotor.
setPower()
sets the speed of the motor you can use any double value between -1 and 1.
setPower(power);
setDirection()
To reverse the direction of your motor use setDirection()
. There are two options. DcMotor.Direction.FORWARD
or DcMotor.Direction.REVERSE
setDirection(direction);
setZeroPowerBehavior()
setZeroPowerBehavior()
has two options DcMotor.ZeroPowerBehavior.BRAKE
and DcMotor.ZeroPowerBehavior.FLOAT
This tells the motor what to do when you set the power to 0.
setZeroPowerBehavior(zeroPowerBehavior);
If you do not have an encoder plugged into your motor the rest of these methods will not work.
getCurrentPosition()
Gets the current position of the motor.
getCurrentPosition();
setTargetPosition()
Sets a target position that the motor will try to go to. Also see Run to Position Example
setTargetPosition(position);
setMode()
Sets the mode of the motor. There are four modes that you can set the motor to.
DcMotor.RunMode.RUN_WITHOUT_ENCODER
DcMotor.RunMode.RUN_USING_ENCODER
Turns towards the target position (setTargetPosition()) see Run to Position Example
DcMotor.RunMode.RUN_TO_POSITION
Stops the motor and resets the encoder
DcMotor.RunMode.STOP_AND_RESET_ENCODER
setMode(mode);
isBusy()
Checks if the motor is still running to it's target positions.
isBusy();
Last updated
Was this helpful?