Servo Control
Introduction: Servos move to a target position, which is useful for claws, linkages, and small mechanisms.
Basic theory: Servo position is usually a normalized value from 0.0 to 1.0. You set fixed positions for repeatable actions like open/close.
A/B button
→
choose position
→
armServo.setPosition()
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.Servo;
/**
* This OpMode demonstrates how to control a single servo (an arm servo)
* using the gamepad A and B buttons.
*/
@TeleOp(name="Servo Control Demo", group="Training")
public class ServoControlDemo extends LinearOpMode {
private Servo clawServo;
@Override
public void runOpMode() {
// --- Initialization Phase ---
// Initialize the arm servo from the hardware map.
// The name "clawServo" must match your robot configuration.
clawServo = hardwareMap.get(Servo.class, "clawServo");
telemetry.addData("Status", "Initialized. Press Play to start.");
telemetry.update();
// Wait for the driver to press the "PLAY" button.
waitForStart();
// --- Run Phase ---
while (opModeIsActive()) {
// If the 'A' button is pressed, move the servo to position 0.0.
if (gamepad1.a) {
clawServo.setPosition(0.0);
}
// If the 'B' button is pressed, move the servo to position 1.0.
if (gamepad1.b) {
clawServo.setPosition(1.0);
}
// Send feedback to the Driver Hub.
telemetry.addData("Servo Position", "%.2f", clawServo.getPosition());
telemetry.update();
}
}
}
Position range: 0.0 one side, 0.5 center, 1.0 other side.