Writing Commands
Right now the mechanism has two private void methods, and nothing can reach them. This lesson converts them into commands that control the mechanism.
- Branch
mech-1-Mechanismsbuilding clean, with a privatesetVoltageandstopMotoronArmandFlywheel. - Lambdas and
privatefrom Java Basics.
What mechanism are you working on?
The lesson below is written for the one you pick. Switch back any time to read it for the other.
The armflywheel has a working motor and no way yet to ask it for anything. A command is the way in, and it wraps that private setter. The scheduler hands the mechanism to one command at a time, so two of them can never fight over the same motor.
One import
Open src/main/java/first/robot/mechanisms/Arm.javasrc/main/java/first/robot/mechanisms/Flywheel.java. One line goes in at the top, alphabetically, just above Mechanism.
import com.ctre.phoenix6.signals.NeutralModeValue;import org.wpilib.command3.Command;import org.wpilib.command3.Mechanism;Three commands
Now convert the void methods into commands. There are three, and all of them use the two methods you already made.
// Each command uses runRepeatedly, which runs its action every loop while the // command is scheduled. Every one of them is a hold: it never finishes on its own. /** Push the arm at 3 volts and keep pushing. Never finishes. */ public Command runSlow() { return runRepeatedly(() -> setVoltage(3.0)).named("runSlow"); } /** Push the arm at 6 volts and keep pushing. Never finishes. */ public Command runFast() { return runRepeatedly(() -> setVoltage(6.0)).named("runFast"); } /** Stop the arm motor and keep it stopped. Never finishes. */ public Command stop() { return runRepeatedly(() -> stopMotor()).named("stop"); } // Same setup as Arm. runRepeatedly runs the action every loop while the command // is scheduled. Every one of these is a hold: it never finishes on its own. /** Spin the flywheel at 3 volts and hold it there. Never finishes. */ public Command runSlow() { return runRepeatedly(() -> setVoltage(3.0)).named("runSlow"); } /** Spin the flywheel at 6 volts and hold it there. Never finishes. */ public Command runFast() { return runRepeatedly(() -> setVoltage(6.0)).named("runFast"); } /** Stop the flywheel and keep it stopped. Never finishes. */ public Command stop() { return runRepeatedly(() -> stopMotor()).named("stop"); }Three things happen on the runSlow line.
() -> setVoltage(3.0)is a lambda. It does not callsetVoltagehere. It hands that call torunRepeatedly, which makes it for you every loop.runRepeatedlycomes fromMechanism, the interface the class implements..named(...)gives the command a name. Names show up in logs and on the dashboard.
() -> stopMotor() in stop() works the same way, calling the private helper from last lesson. runRepeatedly has no exit, so these run until something else claims the mechanism. Even stop() holds, sending zero every loop rather than once. It exists because canceling a command does not stop the motor.
A hold never finishes
So never make anything wait for one to end. A routine waits on a question about the mechanism instead, such as whether it has arrived. Conditions and Logging writes that question.
Check your work
Run WPILib: Build Robot Code. You should see BUILD SUCCESSFUL. If it does not, check for these two errors.
| Error | Cause | Fix |
|---|---|---|
NeedsNameBuilderStage cannot be converted to Command | A .named(...) is missing from that method. | Name the command. |
cannot find symbol: class Command | The import is missing. | Add import org.wpilib.command3.Command;. This stack is never edu.wpi.first. |
Check yourself
setVoltage is private on both mechanisms. What does that achieve?
In runRepeatedly(() -> setVoltage(3.0)).named("runSlow"), what does runRepeatedly(...) return before .named(...) runs?
runSlow() is built with runRepeatedly. When does it end on its own?