1.6 Commands v3 for Java
Tempo supports both Commands v2 and Commands v3. If both frameworks are in your robot code, Tempo will fail, but WPILib will probably give you a warning first.
Imports and commands
This example shows how you can return an auto as a command.
import com.tempo.Composer;
import com.tempo.wpi.Setlist;
import org.wpilib.command3.Command;
Command autoCommand(Composer route, Command shoot) {
return Setlist.named("Score")
.drive(route.runAfter(shoot))
.command();
}For Commands v2 the only change is import org.wpilib.command2.Command;.
On alpha-7 and alpha-8, command() takes its return type from whatever receives it. To use a
decorator, assign it first with Command c = route.command(); or write
route.<Command>command().withTimeout(3). var command = setlist.command() infers Object.
Inline coroutines
You can write a v3 hook inline with WPILib’s command builder or with
Tempo.coroutine(...).
This hook uses WPILib’s builder. stopShooter is a Runnable from your
robot code.
import com.tempo.Composer;
import org.wpilib.command3.Command;
import org.wpilib.command3.Mechanism;
void addShot(Composer route, Mechanism shooter, Command shoot, Runnable stopShooter) {
route.runAfter(Command.requiring(shooter)
.executing(co -> co.await(shoot))
.whenCanceled(stopShooter)
.named("Shoot"));
}This one uses Tempo.coroutine, which takes the body and the mechanisms it
requires. Write the parameter’s type out as (Coroutine co) here. An
untyped co is an Object in this call, so co.yield() doesn’t compile:
import com.tempo.Composer;
import com.tempo.Tempo;
import org.wpilib.command3.Command;
import org.wpilib.command3.Coroutine;
import org.wpilib.command3.Mechanism;
void addShot(Composer route, Mechanism shooter, Command shoot) {
route.runAfter(Tempo.coroutine((Coroutine co) -> {
co.await(shoot);
co.yield();
}, shooter));
}You can pass Tempo.coroutine anywhere a hook or step takes a command.
Canceling a coroutine doesn’t run its Java finally block. Put cleanup in whenCanceled or in
the route’s finallyDo.
Tempo’s commands can also be awaited and forked from a coroutine:
import com.tempo.Composer;
import com.tempo.wpi.Setlist;
import org.wpilib.command3.Command;
Command compose(Composer out, Composer back, Setlist score, Setlist stow, Command deadline) {
return Command.noRequirements(co -> {
co.await(out.command());
co.await(score.command());
Command child = stow.command();
co.fork(child);
while (co.scheduler().isScheduledOrRunning(child)) co.yield();
co.awaitAny(back.command(), deadline);
}).named("Auto");
}If it fails…
The drivetrain and every hook and step command have to come from the same framework. Change the imports that name the other one.
Tempo.coroutine needs the v3 vendordep. On v2, pass a v2 command instead.
Move the robot project to alpha-7 or alpha-8 and install the Tempo vendordep for that alpha.