-
Notifications
You must be signed in to change notification settings - Fork 6
Instant Commands
To make a command run when a button is pressed normally, you have to make a whole new class that extends Command and pass an instance of it as a parameter and all that yammer.
While that's great for long commands with many, many lines of code, that can end up with you having to sort through dozens of command class files to find the one you want. If only there was an easier way to pass all my short commands that only have to run like 6 lines!
InstantCommands are just like commands, but instead of creating a class and passing an instance of that as the parameter, you can just pass a java Runnable instead! It's incredible!
You aren't convinced yet, are you?
Here's an example of a normal, stinky command:
ShooterCommand.java
...
private ShooterSubsystem shooter;
public ShooterCommand(ShooterSubsystem shooter)
{
this.shooter = shooter;
}
@Override
public void initialize()
{
shooter.doThingOne();
}
@Override
public void execute()
{
shooter.doThingTwo();
}
@Override
public void end()
{
shooter.doThingThree();
shooter.doThingFour();
}
...
Not only that, we just added one to the large, increasingly smelly pile of commands that we already have! The stench reminds me a lot of Renato.
The much better way is to use an InstantCommand under the edu.wpi.first.wpilibj2.command
package. This uses Java Runnables rather than a command-extending class instance.
Here's an example of the AMAZING InstantCommand in effect!
RobotContainer.java
private void configureBindings()
{
YButton
.onTrue(new InstantCommand(() -> shooter.doThingOne()))
.whileTrue(new InstantCommand(() -> shooter.doThingTwo()))
.onFalse(new InstantCommand(() -> {
shooter.doThingThree();
shooter.doThingFour();
}));
}
That's SO much better, isn't it?
However, there ARE cases where the boring, old normal Command-extending class should just be used over InstantCommand, such as if the command is really long and should probably be put in another file. InstantCommand should only really be used if the commands are small/insignificant.
This is an officially licensed product of Team 4026. Decatur Robotics 2024 is not sponsored by any other Team, and is not responsible for any damages caused by using this product or trusting the programming team, which is by far the least most trustworthy team(Shadow owen money gang, we love coding the robot). By using this product, you are consenting to your information, and thus your identity to be stolen and first-born child taken.
- Editing Documentation & Markdown Syntax
- Code Team to-do List
- Code Standards
- Common Library Structure
- Interfaces
- General Setup
- Branching System
- How to Create Pull Requests
- How to Switch Branches
- Code Reviews
- Reverting Commits
- Singleton Pattern
- Software Installations
- Necessary IntelliJ Plugins
- Vendordeps
- Setting Up New Projects
- Autoformatter Set Up
- Showbot Requirements
- Autonomous
- Calling a Command Based on a Button Press
- CAN
- Clearing Sticky Faults
- Current Limits
- PID Config and Usage
- Robot.java, TeleopInit, DisabledInit
- RoboRio Ports
- SetDefaultCommand
- Wait for Time
- SlewRateLimiter
- LEDs
- InstantCommand
- PhotonVision
- Apriltags
- Camera Display on Shuffleboard
- Object Detection
- Raspberry Pi
- Network Tables
- List of Network Tables (2023)
Up to date as of SJ2, end of 2023 season