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
BRAKEandFLOATzero-power behavior. - Move a servo with
setPositionand 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.
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
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
| Setting | What happens at power 0 | Good for |
|---|---|---|
DcMotor.ZeroPowerBehavior.BRAKE | The motor resists turning and stops quickly | Drivetrains that need precise stops, arms and lifts that should hold still |
DcMotor.ZeroPowerBehavior.FLOAT | The motor coasts freely to a stop | Flywheels 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.
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}
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.
- 1Center firstBefore attaching the servo horn or arm, command the servo to 0.5 so the mechanism is mounted in the middle of the range.
- 2Move in small stepsUse a test OpMode where the D-pad changes the position by 0.01 and telemetry shows the current value.
- 3Find the limitsMove slowly until the claw is just open and just closed. Stop before the mechanism hits a hard stop.
- 4Record and nameWrite the values down and store them as named constants such as
CLAW_OPENinstead of typing numbers everywhere.
- Forgetting to reverse one side, so the robot spins when it should drive straight.
- Using
Servoin code when the port is configured as a continuous rotation servo, or the opposite. - Typing magic numbers like
0.47in 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
setPowerthat sets how much of the battery voltage drives the motor and in which direction. - Motor direction
- A setting (
FORWARDorREVERSE) 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
setPositionthat tells a standard servo where to move within its range. - Continuous rotation servo
- A servo that spins continuously and is controlled with
setPowerthrough theCRServoclass.
Practical exercise
Motor and servo test bench
- 1Put the robot on a box with the wheels off the ground and put on safety glasses.
- 2Write a test OpMode where holding X, Y, A and B each runs one drive motor at 0.3 power.
- 3Record which motors turn the wrong way and add
setDirection(DcMotor.Direction.REVERSE)for them, then test again. - 4Add a servo test where D-pad up and down change a position variable by 0.01 and telemetry shows the value.
- 5Find the open and closed positions of your claw or other servo mechanism and save them as named constants.
Knowledge check
Answer every question, then check your answers.