Motors and Encoders
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 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.
Motor Encoders
In the realm of FTC robotics, motors connected to an encoder serve as advanced motors with an extra ability: they can communicate to the robot how much they've rotated and at what speed. Imagine you're steering a remote-control car, and you want it to travel a particular distance or turn a specific angle. Regular motors might get close, but they can't give you exact control. Encoder motors, on the other hand, offer precision.
An encoder is typically connected via a special wire that is connected to the motor. This device keeps track of rotations, and each complete rotation is divided into smaller parts called ticks. Think of a tick as a tiny mark on a ruler that measures how much the motor has turned. For instance, a motor may measure 100 ticks for every rotation of the motor shaft.
FTC teams harness the power of these encoder motors to program their robots with accurate movements. When the robot's wheels turn, the encoders count the ticks. So, if the robot's wheel turns 100 ticks, the robot knows it has moved a specific distance. It's akin to the robot reading its own mini-map that shows exactly where it is.
On the other hand, the motor's velocity is also measured by the encoder through the number of ticks per second the motor is traveling.
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.
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.
When using RUN_TO_POSITION, the power should not ever be negated as the motor will go towards whatever direction the tick count is. If the motor needs to go in the opposite direction, use a negative tick count
Finding Motor Position
When using encoders, there is a useful method that can be used to find the motors position in encoder ticks.
Velocity Control
When setting the motor to RUN_USING_ENCODER mode, setting power to a motor causes it to move at a constant velocity rather than an actual power, this means that a motor connected to an encoder will be less resistant to battery fluctuations when compared to a motor that is not connected to an encoder.
Alternatively, you can specify the exact ticks per second the motor should be spinning at:
Motor Class
Here is a convenient motor class that contains shortcuts to most of the methods listed above.
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