Convenience Features
Using FTCLib-provided Commands to Enhance Your Program
FTCLib offers convenience features to make your paradigm program more compact. The idea is that it improves the overall structure of your program. Some of these convenience commands are just that: for convenience. This does not mean they are exceptional. Plenty are simplified for minimal competitive use, such as the PurePursuitCommand
.
Framework Commands
Framework commands exist to decrease the amount of program needed for simplistic tasks, like updating a number. Rather than having the user create an entire command for one simplistic task (which when done for multiple menial tasks builds up and makes the structure fairly disheveled), the user can utilize framework commands.
InstantCommand
InstantCommand
is a versatile framework command that on initialization runs some task and then ends on that same iteration of the CommandScheduler's run()
method. This is especially useful for button-triggered events.
You should actually use subsystems here instead of the motor object. That way we can add the subsystem's requirements to the InstantCommand
. Below is a proper example.
And then we can set up our command bindings as such:
This removes a lot of unnecessary clutter of commands since in a custom implementation the user would have to define a command for both running the intake and stopping it. With InstantCommand
, the amount of code on the user-side is dramatically reduced.
RunCommand
As opposed to an InstantCommand
, a RunCommand
runs a given method in its execute phase. This is useful for PerpetualCommand
s, default commands, and simple commands like driving a robot.
ConditionalCommand
ConditionalCommand
has a wide variety of uses. ConditionalCommand
takes two commands and runs one when supplied a value of true, and another when supplied a value of false.
One use is for making a toggle between commands without using the inactive or active states of the toggleWhenPressed()
binding. Let's update our intake to have two more methods we can use for this toggling feature:
We can then use a conditional command in a trigger binding to produce a toggling effect once pressed:
An example of usage could be in Velocity Vortex, where the beacons were either red or blue. Using a color sensor, we can detect the color and then perform some action based on whether it was red or blue.
As you can see, conditional commands are very useful for switching between states with a certain state. We will see later that we would want to use a SelectCommand
when working with several states and not a simple command that switches between two.
ScheduleCommand
Does exactly as the name suggests: schedules commands. You can input a variable number of command arguments to schedule, and the command will schedule them on initialization. After this, the command will finish. This is useful for forking off of command groups.
So far we've been using the convenience commands we've learned in tandem and how they can be used together to produce more efficient paradigm utility. This is no exception for the ScheduleCommand
. We can use a conditional command to schedule a desired command.
It is important to note that the schedule command will finish immediately. Which means any following commands in the sequential command group will be run. The point of the schedule command is to fork off from the command group so that it can be run separately by the scheduler.
SelectCommand
The select command is similar to a conditional command but for several commands. This is especially useful for a state machine. Let's take a look at the ring stack from the 2020-2021 Ultimate Goal season.
A select command is finished when the selected command also finishes. An alternative use of the select command is to run a command given a supplier instead of a map. Below is a comparable version to the one above:
PerpetualCommand
As opposed to an instant command, a perpetual command swallows a command and runs it perpetually i.e. it will continue to execute the command passed in as an as argument in the constructor and ignore that command's isFinished
condition. It can only end if it is interrupted. This makes them useful for default commands, which are interrupted when another command requiring that subsystem is currently being run and is not scheduled again until that command ends.
Let's take a look back at the command bindings for when we learned InstantCommand
, Instead of doing whileHeld
and whenReleased
binding, a more idiomatic method is to use a default command to stop the intake when the button is released instead (which cancels the command once the trigger/button is inactive, allowing the default command to be scheduled).
Note that a perpetual command adds all the requirements of the swallowed command.
WaitUntilCommand
A WaitUntilCommand
is run until the boolean supplied returns true. This is useful for when you have forked off from a command group. Let's expand upon the example from the ScheduleCommand
but with a single schedule command.
The following commands in the auto command group are only run after that forked command is finished due to that WaitUntilCommand
.
StartEndCommand
The StartEndCommand
is essentially an InstantCommand
with a custom end function. It takes two Runnable
parameters an optional varargs of subsystems to require. The first parameter is run on initialize and the second is run on end.
FunctionalCommand
The last framework command we will discuss is the FunctionalCommand
. It is useful for doing an inline definition of a complex command instead of creating a new command subclass. Generally, it is better to write a class for anything past a certain complexity.
The FunctionalCommand
takes four inputs and a variable amount of subsystems for adding requirements. The four inputs determine the initialization, execution, and end actions, followed by the boolean supplier that determines if it is finished.
A good use of this is inside of a sequential command group. Let's use the previous command group and add a functional command.
Command Decorators
Decorators are methods that allow you to make command bindings and logic without having to create new commands.
withTimeout
Returns a ParallelRaceGroup
with a specified timeout in milliseconds.
interruptOn
Returns a ParallelRaceGroup
that ends once a specified condition is met.
whenFinished
Returns a SequentialCommandGroup
that runs a given Runnable after the calling command finishes.
beforeStarting
Returns a SequentialCommandGroup
that runs a given Runnable before the calling command is initialized.
andThen
Returns a SequentialCommandGroup
that runs all the given commands in sequence after the calling command finishes.
deadlineWith
Returns a ParallelDeadlineGroup
with the calling command as the deadline to run in parallel with the given commands.
alongWith
Returns a ParallelCommandGroup
that runs the given commands in parallel with the calling command.
raceWith
Returns a ParallelRaceGroup
that runs all the commands in parallel until one of them finishes.
perpetually
Swallows the command into a PerpetualCommand
and returns it.
asProxy
Swallows the command into a ProxyScheduleCommand
and returns it. This is similar to a ScheduleCommand
except it ends when all the commands that it scheduled are finished rather than immediately.
Last updated