Motors
Programming Motors
DC Motors in FTC are used for movement of large mechanisms and can be used for fast and continuous rotation, or precise movement for things like an arm. It is very important to learn how to efficiently program motors.
Motor Initialization
The first step in using a motor is to initialize it as a variable in the code. This is done through the use of an object named hardwareMap that is used for easy initialization of FTC Objects.
In this example, first the driveMotor
variable is created through the use of the DcMotor
object. Then the hardwareMap is used to initialize and name the motor, this should be the same name used in the configuration of the Motor on the phone.
DcMotor Setup Usage
There are many methods that for the DcMotor that can be used before the motor is powered to change how it behaves when it runs.
Set Direction
setDirection
is used to change the way a motor rotates when it is set to a power. set Direction should be used on drive train motors to ensure that all motors are spinning the same direction when sent to an equal power.
Run Mode
RUN WITHOUT ENCODER
This mode causes a motor to not use the plugged in encoder values to do things like control its position or speed.
RUN USING ENCODER
This causes the motor to use a motor encoder(if plugged in), to help control its position or speed. When a motor is set to RUN_USING_ENCODER
it automatically uses the encoder to help keep the motor consistently running at any speed set to the motor.
STOP AND RESET ENCODER
This will reset the encoder tick value the motor has to 0. It will ensure that the encoder ticks are consistent per each run, and causes the motor to work as expected each run. If using the encoder ticks as a measurement, STOP_AND_RESET_ENCODER
should be used at the beginning of your code.
RUN TO POSITION
Run To Position is a very useful mode for motors. It allows for a motor to run to a specific target tick value that is set, and the motor will go that position and hold it. It uses an in-built PID Control Loop to accomplish this.
Note
In some cases, RUN_TO_POSITION, might not be the best method for moving a motor to a position, and it can be better to create and use a custom PID loop in some cases. Instructions for a custom PID Loop can be found here.
Motor Encoders
All motors have built in encoder ports, where you can plug an encoder wire into. These encoders give information about the motor's rotational movement, by returning a value in ticks. Different motors will have different tick counts per rotation. Encoders are very useful to efficiently control the output speed and position of the motor. The modes outlined above show different settings you can give the motor encoder.
Moving a Motor to a Position Using Encoders
To move a motor to a target encoder position(given in ticks), we will first be introducing two more commands. setTargetPosition()
takes in a tick value for the motor that becomes its target position. setPower()
sets a [-1,1] value for the motor speed to run at. A power below 0 runs the motor in the opposite direction.
In the example the motor is set to run to a target position of 600 ticks, the amount of rotations this is will depend on the ticks per rotation of that specific motor. After setting the target position the motor is set to the RUN_TO_POSITION
mode, then set to a power of 1, or full power, to run to its target position.
First, initialize a new motor that has a variable name of arm, but has a configuration name of armMotor. Then, reverse the motor from its current direction. After that, set the motor to run using an encoder. Next, move the motor to a position of 950 ticks at 70% speed.
Last updated
Was this helpful?