Search requires an index. Run npm run build once, then restart dev.
TeleOp Programming Concepts
beginner45 min
Overview of TeleOp Programming
TeleOp programming is the process of writing code that allows drivers to control the robot in real time using gamepads. In this lesson, you’ll learn how to read gamepad input, map controls to robot actions, and use telemetry for feedback.
Gamepad Input Handling
The gamepad1 and gamepad2 objects provide access to all the buttons, sticks, and triggers on the controllers. Reading these values lets you control motors, servos, and other mechanisms.
Example: Reading Gamepad Input
This snippet shows how to read joystick and button values from gamepad1.
double drive = -gamepad1.left_stick_y; // Forward/backwarddouble turn = gamepad1.right_stick_x; // Turningboolean aPressed = gamepad1.a; // Button Aif (aPressed) { // Do something when A is pressed}
Mapping Gamepad Inputs to Robot Actions
You can use gamepad input to control motors, servos, and other mechanisms. For example, you might use the left stick for driving and a button to control a claw.
Telemetry lets you send data from your robot to the Driver Station for real-time feedback. Use telemetry.addData() to add information and telemetry.update() to send it. This is essential for debugging and for providing drivers with useful information during a match.
Write a TeleOp OpMode that uses the left stick to drive, the right stick to turn, and buttons A/B to open/close a claw. Add telemetry to display all relevant values. Refactor your code to use helper methods for each subsystem.
// Read joystick values for drive control
double drivePower = -gamepad1.left_stick_y; // Forward/backward
double turnPower = gamepad1.right_stick_x; // Left/right turning
// Read button values for claw control
boolean aPressed = gamepad1.a; // Open claw
boolean bPressed = gamepad1.b; // Close claw
// Display input values
telemetry.addData("Drive Power", "%.2f", drivePower);
telemetry.addData("Turn Power", "%.2f", turnPower);
telemetry.addData("Button A", aPressed);
telemetry.addData("Button B", bPressed);
telemetry.update();
Set motor and servo power based on input
Set motor and servo power based on input
// Calculate left and right motor powers
// Drive power controls forward/backward
// Turn power controls left/right turning
double leftPower = drivePower + turnPower;
double rightPower = drivePower - turnPower;
// Apply power limits to prevent motor damage
leftPower = Math.max(-1.0, Math.min(1.0, leftPower));
rightPower = Math.max(-1.0, Math.min(1.0, rightPower));
// Set motor powers
leftDrive.setPower(leftPower);
rightDrive.setPower(rightPower);
// Control claw servo based on button presses
if (aPressed) {
clawServo.setPosition(1.0); // Open claw
} else if (bPressed) {
clawServo.setPosition(0.0); // Close claw
}
Add telemetry for all controls
Add telemetry for all controls
// Display all control values and robot status
telemetry.addData("=== DRIVE CONTROLS ===", "");
telemetry.addData("Drive Power", "%.2f", drivePower);
telemetry.addData("Turn Power", "%.2f", turnPower);
telemetry.addData("Left Motor Power", "%.2f", leftPower);
telemetry.addData("Right Motor Power", "%.2f", rightPower);
telemetry.addData("=== CLAW CONTROLS ===", "");
telemetry.addData("Button A (Open)", aPressed);
telemetry.addData("Button B (Close)", bPressed);
telemetry.addData("Claw Position", "%.2f", clawServo.getPosition());
telemetry.addData("=== ROBOT STATUS ===", "");
telemetry.addData("OpMode Active", opModeIsActive());
telemetry.addData("Match Time", "%.1f seconds", getRuntime());
telemetry.update();