View Single Post
  #12   Spotlight this post!  
Unread 16-11-2015, 11:10
SamCarlberg's Avatar
SamCarlberg SamCarlberg is online now
GRIP, WPILib. 2084 alum
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 54
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: Java Conditional Command

Quote:
Originally Posted by GeeTwo View Post
My Java is a bit rusty, but won't this command in the DriveToLine instantiator:

Code:
        addSequential(waitUntil(drivetrain.getDistance(), GREATER_THAN, 50));
Get the distance from the drivetrain once during the instantiator, rather than sending a handle for the drivetrain.getDistance method to waitUntil()?
Yep. It would be better to pass a method reference
Code:
        addSequential(waitUntil(driveTrain::getDistance, GREATER_THAN, 50));
and call that method during each check.

Quote:
Originally Posted by GeeTwo View Post
OK, my poorly exercised OO brain gets it, but my better exercised procedural brain sees spooky action at a getDistance().

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())
completes. If you do this, you should allow either operand or both to be objects. For sanity's sake, a constructor using two numeric values could be created, but it should throw an exception telling the user that [s]he is trying to wait forever, or not at all.
This could be done with a few generic methods, i.e.

Code:
    public static <T, U> ConditionalCommand waitUntil(Supplier<T> left, BiPredicate<T, U> tester, Supplier<U> right)
    public static <T, U> ConditionalCommand waitUntil(Supplier<T> left, BiPredicate<T, U> tester, U right)
So you could just call
Code:
waitUntil(driveTrain::getDistance, GREATER_THAN, 50);
where
Code:
GREATER_THAN = (left, right) -> left > right
Reply With Quote