Lesson 9.1: Using setPosition() for Accurate Servo Movement
Technical Context
Positional servos are useful for claws, latches, and gates that need repeatable target positions. If a linkage blocks the servo before it reaches the command, the servo can stall, heat up, draw excess current, and damage its gears or the mechanism.
How setPosition() Maps to Real Servo Motion
The setPosition(double position) method sends a specific pulse-width modulation (PWM) signal to the Control Hub to move the servo to a fraction of its total range. It accepts a double parameter between 0.0 and 1.0, where:
0.0: one endpoint of the configured logical range0.5: the midpoint of the configured logical range1.0: the other endpoint of the configured logical range
A standard servo's internal controller continues trying to hold its commanded position while PWM output is enabled. You do not need to resend the same command every loop, but the servo may keep drawing current if the mechanism is loaded or blocked.
Annotated Code
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.Servo;
@TeleOp(name="SetPosition_Demo")
public class SetPositionDemo extends OpMode {
private Servo claw;
@Override
public void init() {
// Lookup in configuration registry
claw = hardwareMap.get(Servo.class, "claw_servo");
// Move to a known safe position at initialization
claw.setPosition(0.5);
telemetry.addData("Claw", "Initialized to center (0.5)");
}
@Override
public void loop() {
if (gamepad1.a) {
claw.setPosition(1.0); // Fully closed
telemetry.addData("Claw", "Closed");
} else if (gamepad1.b) {
claw.setPosition(0.0); // Fully open
telemetry.addData("Claw", "Open");
} else {
telemetry.addData("Claw", "Holding position");
}
}
}
Coordinating Two Servos in BunyipsLib
A dual-servo mechanism stores separate targets, then sends both targets during its periodic update:
@Override
protected void periodic() {
left.setPosition(leftServoPosition);
right.setPosition(rightServoPosition);
logger.leftTarget = leftServoPosition;
logger.rightTarget = rightServoPosition;
logger.leftServoTarget = left.getPosition();
logger.rightServoTarget = right.getPosition();
}
Separating the desired state from the hardware write makes it easier for commands such as open and close to update a pair consistently. Excerpted from BunyipsLib's DualServos.java at a pinned commit under the MIT License.
Fill-in-the-Blank Practice
- The
setPosition()method requires a__________data type to specify the target location. - In the SDK, a value of
__________typically represents the midpoint of a servo's travel. - To access servo methods, the hardware must be instantiated using the
__________class.
Show answers
double0.5Servo
Simulator Challenge
Use the simulator below to practice mapping a "gate" servo, moving it with gamepad1.y and gamepad1.a, and reporting the current position through telemetry.
Ready to move on?
Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.