Programming
Example Code:
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.
setDirection()
To reverse the direction of your motor use setDirection()
. There are two options. DcMotor.Direction.FORWARD
or DcMotor.Direction.REVERSE
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.
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.
setTargetPosition()
Sets a target position that the motor will try to go to. Also see Run to Position Example
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
isBusy()
Checks if the motor is still running to it's target positions.
Last updated
Was this helpful?