-
Notifications
You must be signed in to change notification settings - Fork 2
Lesson 2: Intro to WPILib
James Li edited this page May 21, 2025
·
14 revisions
- WPILib = The library/framework we use to program our robot in Java
- Built for FRC
- Contains useful classes and methods for interacting with many parts of the FRC control system
- Handles: motors, sensors, joysticks, the driver station, etc.
Our job: use WPILib to design how the robot thinks and behaves
- E.g. organizing subsystems, defining commands, linking controls
- Understand how WPILib projects are structured
- Learn about how commands are defined and scheduled
- Make a small command to test it out
- Command Sources: Responsible for starting commands
- Commands: Define robot behavior
- Subsystems: Responsible for the hardware, e.g. translating commands into motor output
- We bind Commands to Triggers (like button presses) in the
configureBindingsmethod inside theRobotContainerclass - We can also specify a default command for a Subsystem that runs continuously
Got it — here's the revised version with an Examples: section and short descriptions per example:
- We bind Commands to Triggers (like button presses) in
configureBindingsinRobotContainer - We can also set a default command for a Subsystem to run when idle
Run intakeCommand while A is held:
driverController.buttonA().whileTrue(() -> intake.intakeCommand());Set arcadeDriveCommand to run by default:
drive.setDefaultCommand(drive.arcadeDriveCommand(
() -> driverController.getLeftY(),
() -> driverController.getRightX()));- means defining an action
- DOES NOT RUN BY ITSELF
Examples:
Commands.run(() -> { ... })
Commands.runOnce(() -> { ... })
Commands.sequence(cmd1, cmd2, ...)
cmd1.andThen(cmd2)
new InstantCommand(() -> { ... })- means telling WPILib to actually run it
Examples:
button.onTrue(cmd) // binding to a Trigger
subsystem.setDefaultCommand(cmd)Commands.runOnce(() -> intake.retract(), intake)-
Commands.runOnce(...): Creates a command that runs once, then ends immediately -
() -> intake.retract(): An anonymous function (lambda) that calls theretract()method on theintakesubsystem -
intake: Specifies the subsystem this command requires (ensures only one command controls it at a time)
This does not run the command. Pass it into something like
intake.setDefaultCommand()to schedule.
- Complex commands can be built using method chaining
- Method chaining: calling methods on the return values of other methods (in this case, Command objects)
- You don’t need to memorize syntax now: just understand that complex behavior can be chained together.
Example:
Commands.runOnce(() -> shooter.spinUp(), shooter)
.andThen(Commands.waitSeconds(2))
.andThen(Commands.runOnce(() -> shooter.fire(), shooter));What this Command does:
- Starts by spinning up the shooter wheels
- Waits 2 seconds for the wheels to reach speed
- Then fires once
It uses method chaining (specifically .andThen()) to run these actions one after the other.
- Instead of creating commands inline everywhere, we often write methods that return Command objects
- These are called command factories
- Keeps code organized and reusable
- Placed inside the Subsystem class (if specific) or a class inside the
commands/folder
Example (inside a Climber subsystem):
public Command readyClimb() {
return Commands.runOnce(
// lambdas with more than one expression should be wrapped in curly braces {}
() -> {
if (climbIsReady()) climbState = ClimbState.PULL;
},
this);
}Usage (in RobotContainer):
driverController.a().onTrue(climber.readyClimb());- Represent robot parts like drivetrain, shooter, climber
- Own the hardware (motors, sensors)
- Responsible for converting abstract Commands into real motor actions
- Expose methods that Commands use to control the robot
- E.g.
elevator.goToPosition()
- E.g.
More details in the next lesson
Let’s trace through how input (button) turns into output (motor) in our wrist robot code.
See: the 971-robot-2025 repo.
- Create a command factory method inside the
Climbersubsystem - Bind it to the A button on the joystick
- The command should print
"Climb starting"when A is pressed - Test out your code in a simulation
A template project is provided for you here (TODO)