Skip to content
Week 4 · Lesson 3 45 min

Motors and Servos in Code

Control DC motors with power, direction and zero-power behavior, and move servos to tested positions with `setPosition`.

In this lesson you will

  • Set motor power from code and explain what the range -1 to 1 means.
  • Explain why one side of a drivetrain is usually reversed and set motor direction.
  • Choose between BRAKE and FLOAT zero-power behavior.
  • Move a servo with setPosition and find safe open and closed positions by testing.

Lesson

A DC motor in the FTC SDK is a DcMotor object. The most important method is setPower, which takes a number from -1.0 to 1.0. 1.0 is full power in one direction, -1.0 is full power in the other, and 0 means no power. Power is roughly the share of battery voltage sent to the motor, not an exact speed: a robot with a weak battery or a heavy load moves slower at the same power. Values outside the range are not useful, so keep your calculations between -1 and 1.

JAVA
1DcMotor intake = hardwareMap.get(DcMotor.class, "intake");
2intake.setDirection(DcMotor.Direction.FORWARD);
3intake.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
4
5waitForStart();
6
7intake.setPower(0.5);    // half power, one direction
8sleep(1000);             // wait 1000 ms = 1 second
9intake.setPower(-0.5);   // half power, the other direction
10sleep(1000);
11intake.setPower(0);      // stop
Running a motor forward, backward and stopping

Direction: why one side is reversed

On a drivetrain the left and right motors are usually mounted as mirror images. If you give both the same positive power, they spin the same way relative to their own shafts, which means one side drives forward and the other backward, and the robot spins in place. The fix is setDirection(DcMotor.Direction.REVERSE) on one side. Which side depends on your motors and gearboxes, so you must test it. The goal is simple: positive power on every drive motor should push the robot forward.

Zero-power behavior: brake or float

SettingWhat happens at power 0Good for
DcMotor.ZeroPowerBehavior.BRAKEThe motor resists turning and stops quicklyDrivetrains that need precise stops, arms and lifts that should hold still
DcMotor.ZeroPowerBehavior.FLOATThe motor coasts freely to a stopFlywheels and mechanisms that should spin down gently

Servos: moving to a position

A standard servo moves to a position and holds it. In code you call setPosition with a number from 0.0 to 1.0. These numbers are not degrees: 0.0 and 1.0 are the two ends of the servo's range, and how many degrees that covers depends on the servo model and how it is programmed. A servo usually stays limp until it receives its first setPosition command. You can read back the last commanded position with getPosition, but this is not a sensor reading.

JAVA
1// Class-level constants (inside the OpMode class, outside runOpMode)
2static final double CLAW_OPEN   = 0.30;   // example values: find yours by testing
3static final double CLAW_CLOSED = 0.65;
4
5// Inside runOpMode()
6Servo claw = hardwareMap.get(Servo.class, "claw");
7
8waitForStart();
9
10while (opModeIsActive()) {
11    if (gamepad1.a) {
12        claw.setPosition(CLAW_OPEN);
13    } else if (gamepad1.b) {
14        claw.setPosition(CLAW_CLOSED);
15    }
16}
Opening and closing a claw with two buttons

A continuous rotation servo spins like a small motor instead of moving to a position. Configure it as Continuous Rotation Servo and control it as a CRServo with setPower, for example spinner.setPower(1.0). Using the wrong class or the wrong configuration type is a common source of 'my servo does nothing' bugs.

  1. 1
    Center first
    Before attaching the servo horn or arm, command the servo to 0.5 so the mechanism is mounted in the middle of the range.
  2. 2
    Move in small steps
    Use a test OpMode where the D-pad changes the position by 0.01 and telemetry shows the current value.
  3. 3
    Find the limits
    Move slowly until the claw is just open and just closed. Stop before the mechanism hits a hard stop.
  4. 4
    Record and name
    Write the values down and store them as named constants such as CLAW_OPEN instead of typing numbers everywhere.
  • Forgetting to reverse one side, so the robot spins when it should drive straight.
  • Using Servo in code when the port is configured as a continuous rotation servo, or the opposite.
  • Typing magic numbers like 0.47 in five places instead of one named constant.
  • Commanding a servo past the mechanism's limit, which strains and heats the servo.

Key terms

Motor power
A value from -1.0 to 1.0 passed to setPower that sets how much of the battery voltage drives the motor and in which direction.
Motor direction
A setting (FORWARD or REVERSE) that flips which way the motor turns for positive power.
Zero-power behavior
Whether a motor actively brakes (BRAKE) or coasts (FLOAT) when its power is set to 0.
Servo position
A value from 0.0 to 1.0 passed to setPosition that tells a standard servo where to move within its range.
Continuous rotation servo
A servo that spins continuously and is controlled with setPower through the CRServo class.

Practical exercise

Motor and servo test bench

  1. 1Put the robot on a box with the wheels off the ground and put on safety glasses.
  2. 2Write a test OpMode where holding X, Y, A and B each runs one drive motor at 0.3 power.
  3. 3Record which motors turn the wrong way and add setDirection(DcMotor.Direction.REVERSE) for them, then test again.
  4. 4Add a servo test where D-pad up and down change a position variable by 0.01 and telemetry shows the value.
  5. 5Find the open and closed positions of your claw or other servo mechanism and save them as named constants.
Deliverable: A short table showing each drive motor, its final direction setting and the tested servo positions, plus the test OpMode source code.

Knowledge check

Answer every question, then check your answers.

  1. Q1You give both drive motors power 0.5 and the robot spins in place. What is the most likely cause?
  2. Q2What does claw.setPosition(1.0) mean?
  3. Q3Which zero-power behavior is best for an arm that should hold its place when the driver lets go?

Further resources

Mark lesson complete