Controllers in FRC#
Drivers use gamepads or joysticks plugged into the Driver Station. WPILib reads them through controller classes. The XboxController class works with the Xbox 360, Xbox One controllers, and the Logitech F310 when its switch is set to XInput ("X") mode — these are the most common FRC controllers.
For command-based code, use CommandXboxController, which adds convenient button "trigger" methods.
Reading axes and buttons (raw)#
XboxController driver = new XboxController(0); // DS USB slot 0
double forward = -driver.getLeftY(); // sticks are inverted: up is negative
double turn = driver.getRightX();
boolean shoot = driver.getAButton();
Joystick axes range from -1.0 to 1.0. A common gotcha: the Y axis is negative when pushed forward, so you usually negate it.
Triggers: the command-based way#
A Trigger represents a boolean condition (a button press, a sensor state, anything). You bind commands to triggers in a method usually called configureBindings() inside RobotContainer:
CommandXboxController driver = new CommandXboxController(0);
// Run the intake WHILE the A button is held; stop when released
driver.a().whileTrue(intake.intakeCommand());
// Run a command ONCE when the right bumper is pressed
driver.rightBumper().onTrue(arm.raiseToScore());
// Combine conditions
driver.a().and(driver.b()).onTrue(robot.specialMode());
Key binding methods:
onTrue(cmd)— schedule the command when the trigger becomes true (rising edge).whileTrue(cmd)— run while the trigger is true; interrupt when it goes false.onFalse(cmd)/toggleOnTrue(cmd)— other edge behaviors.
Because CommandXboxController has named methods (a(), b(), leftBumper(), povUp(), etc.), bindings read almost like English.
Triggers aren't just buttons#
A Trigger can wrap any boolean supplier:
new Trigger(arm::atTarget).onTrue(Commands.runOnce(() -> led.setGreen()));
This is the declarative power of command-based: instead of polling if (...) every loop, you declare "when this becomes true, do that."
Driving with default commands + triggers#
A typical teleop setup combines both ideas: the drivetrain's default command reads the sticks every loop, while triggers handle mechanism actions:
drivetrain.setDefaultCommand(
drivetrain.run(() -> drivetrain.arcadeDrive(-driver.getLeftY(), driver.getRightX())));
driver.a().whileTrue(intake.intakeCommand());
driver.b().onTrue(shooter.shoot());
Tip: deadbands#
Real sticks rarely rest at exactly 0. Apply a deadband (MathUtil.applyDeadband(value, 0.1)) so small stick drift doesn't creep the robot.
the part worth keeping
Key takeaways
- Use XboxController (raw) or CommandXboxController (command-based) to read gamepads.
- Axes are -1.0 to 1.0; the Y axis is negative when pushed forward — usually negate it.
- Triggers bind commands to conditions: onTrue, whileTrue, onFalse, toggleOnTrue.
- A Trigger can wrap any boolean, not just buttons — enabling declarative reactions.
- Apply MathUtil.applyDeadband to stick inputs to ignore resting drift.
Programming, Controls & SensorsThe Robot Program: Lifecycle and Command-Based Architecturelesson 4 of 4
Keep going
Take the quiz+10 XP with an accountMore in The Robot Program: Lifecycle and Command-Based Architecture
where this came from
Sources and corrections
This lesson is AI-assisted: drafted from primary sources, then reviewed and edited by hand. Errors still get through. When one is reported we fix it and write down what changed, in public, in the corrections log.
sources and further reading
- docs.wpilib.orgJoysticks
- docs.wpilib.orgBinding Commands to Triggers
clipped to this lesson
Articles that go further on this
The lesson gets you through the topic. These go wider on it, and they read in one sitting.
- 9 min readFRC Command-Based Programming: Subsystems, Commands, and the SchedulerA beginner-friendly guide to WPILib command-based programming in Java: subsystems, commands, the CommandScheduler, triggers, and composing commands./blogread it
- 13 min readFRC Code Structure Best Practices: Command-Based Project ArchitectureHow to structure an FRC command-based robot project the right way — subsystems, commands, RobotContainer, and Constants — verified against official WPILib docs./blogread it
- 7 min readFRC "No Robot Code" & Driver Station Won't Connect: Full Fix ChecklistA verified, step-by-step FRC troubleshooting decision tree for "No Robot Code" and a Driver Station that won't connect to the roboRIO — most common causes first./blogread it
answer sheet
Lesson quiz
All 3 right completes the lesson. Miss one and only that question comes back, anything you already answered correctly stays banked.
0 of 3 answered
01Using CommandXboxController, what does binding a command with whileTrue() do?
02What is the value range of a joystick or XboxController axis, such as getLeftY()?
03In the modern command-based API, how do you obtain a Trigger for the A button on a CommandXboxController?
Answer every question to submit.
All 51 lessons in Programming, Controls & Sensorsopenclose
01 / prerequisites
02 / foundations-tools-and-first-program
03 / robot-program-and-command-based
04 / motors-and-control
05 / autonomous-trajectories-simulation
06 / sensing-fundamentals
07 / encoders
08 / gyros-imus-orientation
09 / closed-loop-control
10 / vision-pose-estimation
11 / worked-examples-mini-projects
- Not read yet:Mini-Project: A Closed-Loop Elevator with Motion Magic
- Not read yet:Mini-Project: A Velocity-Controlled Shooter on REVLib
- Not read yet:Mini-Project: A Teleop Swerve Drive Subsystem
- Not read yet:Mini-Project: An Autonomous Routine with PathPlanner
- Not read yet:Mini-Project: Vision-Aligned Scoring with Limelight
12 / common-mistakes-troubleshooting
13 / advanced-techniques-case-studies
- Not read yet:State-Space Control and Kalman Filtering
- Not read yet:Log Replay Architecture with AdvantageKit
- Not read yet:Advanced Pose Estimation: Multi-Tag Fusion and Standard Deviations
- Not read yet:Robot Coordination, Alerts, and Operator Feedback
- Not read yet:Case Study: Hardening Software Before an Event