In another command file, if I write:
new ConditionalCommand(new DriveTimed(.5, -.25, .25), new DriveTimed(.5, .25, -.25), Robot.getTurn()::get)
the DriveTimed() calls are underlined and it says that the constructor for ConditionalCommand() only takes (Command, Command, boolean). Any idea why DriveTimed() doesn’t count as a command?
Why do you not just put the two DriveBase(leftPower,rightPower).withTimeout(timeout)
command into the ConditionalCommand
?
You can’t mix the old command framework with the new one. You need to delete all references to the old command framework from your code; your command is probably implemented on top of the v1 base class.
just found out drivetimed isn’t the problem, but the condition is. When it says the 3rd param needs to be a “condition”, can’t I just put in a boolean or put ::get next to a boolean?
You can’t just put in a boolean, because the condition has to be evaluated at scheduling time, not at command construction. You have to give it a method that returns a boolean.
Unfortunately, Java is not such a powerful language that you can do boolean::get
(if only!). You’ll have to write a lambda expression.
According to the documentation, the last parameter needs to be a BooleanSupplier
. A BooleanSupplier
is a function having no argument as input, and returning a boolean
as output.
If Robot.getTurn()
is the condition you want to use, the conditional command can be written like this:
new ConditionalCommand(new DriveTimed(.5, -.25, .25), new DriveTimed(.5, .25, -.25), Robot::getTurn);
Key points:
-
Robot
is the robot instance for whichgetTurn
is going to be invoked on -
Robot::getTurn
is called a method reference
If you need additional logic, a BooleanSupplier
can be created using a lamba expression (e.g. if the condition is Robot.getTurn() must be greater than 5, then this could do the trick:
new ConditionalCommand(new DriveTimed(.5, -.25, .25), new DriveTimed(.5, .25, -.25), () -> { return Robot.getTurn() > 5; });
The last parameter is called a lambda expression. It can be used to declare a BooleanSupplier
:
e.g. BooleanSupplier supplier = () -> { return Robot.getTurn() > 5; };
I hope this helps.