FTC Code
  • Welcome!
  • Page 7
  • Getting Started
    • Page 1
    • Choosing an IDE
    • Creating an OpMode
    • Motors
    • Servos
  • Common Module Applications
    • Linear Slides
    • Sweeper
    • Kicker
    • Claw
    • Turret
    • Cantilever
    • Four Bar
    • Flywheel
  • Vision
    • Page 2
    • Page 6
  • Odometry
  • Efficient Path Following
    • Page 4
  • Efficient Tele-op Control
    • Page 5
Powered by GitBook
On this page
  • Motor Initialization
  • DcMotor Setup Usage
  • Set Direction
  • Run Mode
  • Motor Encoders
  • Moving a Motor to a Position Using Encoders

Was this helpful?

  1. Getting Started

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.

DcMotor driveMotor;       

driveMotor = hardwareMap.get(DcMotor.class, "Drive Motor");  

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

driveMotor.setDirection(DcMotor.Direction.Forward);
driveMotor.setDirection(DcMotor.Direction.Reverse);

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

driveMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
driveMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
driveMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
driveMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);

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.

TARGET_TICK_VALUE = 600;      
driveMotor.setTargetPosition(TARGET_TICK_VALUE);    //Sets Target Tick Position
driveMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION); 
driveMotor.setPower(1);           //Sets Motor to go to position at 1 power.

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

Exercise

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.

DcMotor arm;
TARGET_TICK_POSITION = 950;
arm = hardwareMap.get(DcMotor.class,"armMotor");

arm.setMode(DcMotor.RunMode.RUN_USING_ENCODER);

arm.setTargetPosition(TARGET_TICK_POSITION);
arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
arm.setPower(0.7);
PreviousCreating an OpModeNextServos

Last updated 1 year ago

Was this helpful?