Skip to content
Tempo on GitHub (opens in a new tab)

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.

Java
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.

Java
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:

Java
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:

Java
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…

A route mixes the two frameworks Above → [Tempo] Commands v2 and Commands v3 inputs conflict; got {class}; commands and drivetrain requirements must use one framework

The drivetrain and every hook and step command have to come from the same framework. Change the imports that name the other one.

A coroutine is refused on a v2 robot Above → [Tempo] Tempo.coroutine needs Commands v3

Tempo.coroutine needs the v3 vendordep. On v2, pass a v2 command instead.

v3 commands are refused on alpha-6 Troubleshooting → [Tempo] v3 support needs WPILib 2027.0.0-alpha-7 or newer

Move the robot project to alpha-7 or alpha-8 and install the Tempo vendordep for that alpha.

esc

Loading search...