![]() |
Java Conditional Command
Here is an idea I had recently for conditional wait commands. Thoughts?
With just a couple of static imports you could be writing CommandGroups that look like this. Code:
package org.usfirst.frc.team2363.robot.commands;Code:
package org.usfirst.frc.team2363.robot.commands;Code:
package org.usfirst.frc.team2363.robot.subsystems; |
Re: Java Conditional Command
Quote:
If you're using Java 8, you could simplify most of your code by using lambdas rather than hard-code the comparison operations. For example, your ConditionalWaitCommand can use the `java.util.function.Predicate` functional interface to define when it is completed: Code:
public class ConditionalWaitCommand extends Command {Code:
public class DriveToLine extends CommandGroup {Here's what that might look like to create a command instance that drives at 50% power while the distance is greater than 50, and once that condition has occurred then stop: Code:
Drivetrain driveTrain = ...Code:
public class ConditionalCommand extends Command { |
Re: Java Conditional Command
In this particular case of the example, I think it would be easier to build the logic into a single command.
However, the pattern is definitely useful. We had cases last year where we wanted to start a parallel command halfway through another command (for a different subsytem). We sequenced them with waits, but a way to generically peek at the progress of the first command would be much better. |
Re: Java Conditional Command
My Java is a bit rusty, but won't this command in the DriveToLine instantiator:
Code:
addSequential(waitUntil(drivetrain.getDistance(), GREATER_THAN, 50)); |
Re: Java Conditional Command
Quote:
GeeTwo, it works because getDistance() returns an object not a value. So the getValue() call on the object at the time of the comparison will get the current value. |
Re: Java Conditional Command
Quote:
That suggests to me that you would want to have polymorphic forms of the command, including one which allows both operands to be objects. This could then be used to do things like (for example) returning the original drive direction by driving faster with the left wheel than the right, then equalizing after a Code:
waitUntil(leftAxle.getDistance(), GREATER_THAN_EQUAL, rightAxle.getDistance()) |
Re: Java Conditional Command
Quote:
|
Re: Java Conditional Command
Quote:
|
Re: Java Conditional Command
Quote:
|
Re: Java Conditional Command
Quote:
|
Re: Java Conditional Command
Quote:
Code:
The method waitUntil(IProvidesValue, Operation, IProvidesValue) in the type ConditionalWaitCommand is not applicable for the arguments (double, Operation, double) |
Re: Java Conditional Command
Quote:
Code:
addSequential(waitUntil(driveTrain::getDistance, GREATER_THAN, 50));Quote:
Code:
public static <T, U> ConditionalCommand waitUntil(Supplier<T> left, BiPredicate<T, U> tester, Supplier<U> right)Code:
waitUntil(driveTrain::getDistance, GREATER_THAN, 50);Code:
GREATER_THAN = (left, right) -> left > right |
Re: Java Conditional Command
Sam,
Any chance of getting some of these types of concepts into the WPILib at some point? I'm really liking the method reference strategy. |
Re: Java Conditional Command
I would be surprised if it made it in this year with kickoff so close.
It would also need to look similar in C++ with whatever it uses for lambdas and method references |
Re: Java Conditional Command
Quote:
|
| All times are GMT -5. The time now is 12:54. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi