-
Notifications
You must be signed in to change notification settings - Fork 0
Simple Utilities
The Simple Utilities, located in the util
and triggers
packages, make developing robot code easier and more ergonomic by using lambdas to create WPILib components (Commands
, Buttons
, etc.) without having to create an entire anonymous class.
SimpleButton
is a way to create a WPILIb Button
class based off of an arbitrary function returning a boolean
in one line of code. This is useful if you want to set up complex control schemes; for example, having a button that is only considered pressed if a joystick is in a certain range.
Method reference:
Button fooButton = new SimpleButton(this::getFoo);
Simple statement lambda:
Button fooAndBarButton = new SimpleButton(() -> getFoo() && getBar());
More complicated logic:
Button atLeast5TrueButton = new SimpleButton(() -> {
int numTrue = 0;
for (boolean b : aListOfBooleans) {
if (b) {
numTrue++;
}
}
return numTrue >= 5;
});
SimpleCommand
and SimpleLoopCommand
are wrappers that allow the creation of WPILib Commands
that run an arbitrary function when executed. SimpleCommand
runs the provided code once before finishing, while SimpleLoopCommand
will run until canceled or interrupted. Both types can have requirements just like normal commands.
Note that the constructor for SimpleCommand
requires a name; this is so that the command has a sensible name that is preserved across different program runs (as otherwise the names could be different depending on what order something is initialized statically).
SimpleCommand
in particular is very useful for putting buttons to do things on the SmartDashboard or Shuffleboard. Simply create the command and put it up using SmartDashboard.putData()
.
Hello world:
Command printHello = new SimpleCommand("Print hello", () -> System.out.println("Hello world!"));
Command with requirements:
Command openClaw = new SimpleCommand("Open claw", Robot.claw::open, Robot.claw);
Command with multiple requirements:
Command openClawAndRaiseArm = new SimpleCommand("Open Claw and Raise Arm",
() -> {
Robot.claw.open();
Robot.arm.raise();
},
Robot.claw, Robot.arm);
Looping command:
Command printTime = new SimpleLoopCommand("Print Current Time", () -> System.out.println(System.currentTimeMillis()));
SimpleConditionalCommand
is a wrapper that allows the creation of WPILib ConditionalCommand
instances based off of an arbitrary function returning a boolean
in one line of code. SimpleConditionalCommands
, like ConditionalCommands
, can run a Command
only if the provided condition is true
, or run one of two commands depending on the condition.
Run a command only if getFoo()
returns true
:
Command doSomethingIfFoo = new SimpleConditionalCommand(this::getFoo, new DoSomething());
Run the DoSomething
command if getFoo()
returns true
, otherwise run DoSomethingElse
:
Command somethingOrSomethingElse = new SimpleConditionalCommand(this::getFoo,
new DoSomething(),
new DoSomethingElse());