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

3.7 Run commands on a route

Commands are attached to the to(...) above them and run before, during or after the drive to that waypoint. A command or method attached this way is called a hook. This route raises the elevator on the way to REEF_IR and scores there. It runs the intake on the way to STATION_LEFT and waits up to 1.5 s for a coral. It stows when the route ends. The poses are from Routes & waypoints, plus REEF_G from Write the first auto.

Java
import static edu.wpi.first.units.Units.Seconds;
import com.tempo.Composer;

Composer route = Composer.startAt(START_LEFT)
    .to(REEF_IR)
        .run(0.6, superstructure.prepareL4())          // from 60% of the drive, keeps driving
        .runAfter(superstructure.scoreL4())            // stopped at the reef; robot waits
    .to(STATION_LEFT)
        .run(0.5, intake.intakeCommand())
        .waitUntil(intake::hasPiece, Seconds.of(1.5))  // stopped; up to 1.5 s
    .finallyDo(() -> superstructure.setGoal(Goal.STOW));
Python
from tempo import Composer

route = (Composer.start_at(START_LEFT)
    .to(REEF_IR)
        .run(0.6, superstructure.prepare_l4())       # from 60% of the drive, keeps driving
        .run_after(superstructure.score_l4())        # stopped at the reef; robot waits
    .to(STATION_LEFT)
        .run(0.5, intake.intake_command())
        .wait_until(intake.has_piece, 1.5)           # stopped; up to 1.5 s
    .finally_do(lambda: superstructure.set_goal(Goal.STOW)))
C++
#include <tempo/Composer.h>

// prepare_, score_ and intakeCommand_ are frc2::CommandPtr members that outlive the route.
tempo::Composer route = tempo::Composer::start_at(START_LEFT)
    .to(REEF_IR)
        .run(0.6, prepare_.get())                     // from 60% of the drive, keeps driving
        .run_after(score_.get())                      // stopped at the reef; robot waits
    .to(STATION_LEFT)
        .run(0.5, intakeCommand_.get())
        .wait_until([this] { return intake.HasPiece(); }, 1.5_s)  // stopped; up to 1.5 s
    .finally_do([this] { superstructure.SetGoal(Goal::Stow); });

Python and C++ spell the hooks in snake case (run_after).

C++ takes a borrowed frc2::Command*. Keep the CommandPtr in a member that outlives the route and pass .get(), because a pointer to a temporary is left dangling when the route runs.

The hooks differ in when they run and whether the robot waits:

  • runBefore holds the robot where the drive starts until it finishes.
  • run(...) and runWhile(...) start while the robot drives and don’t slow it down.
  • runAfter and waitUntil hold the robot still at the waypoint until they finish.

A fraction is a share of the path distance to this waypoint, 0 leaving the previous waypoint and 1 arriving. Every hook is listed in the Composer reference.

Before leaving and after arriving

Java
route.to(STATION_LEFT)
        .runAfter(intake.intakeCommand())      // 1: at the station
    .to(REEF_KR)
        .runBefore(superstructure.stow())      // 2: still at the station
        .runAfter(superstructure.scoreL4())    // 3: at REEF_KR
        .runAfter(superstructure.stow());      // 4: starts as scoreL4 ends
Python
(route.to(STATION_LEFT)
        .run_after(intake.intake_command())      # 1: at the station
    .to(REEF_KR)
        .run_before(superstructure.stow())       # 2: still at the station
        .run_after(superstructure.score_l4())    # 3: at REEF_KR
        .run_after(superstructure.stow()))       # 4: starts as score_l4 ends
C++
route.to(STATION_LEFT)
        .run_after(intakeCommand_.get())  // 1: at the station
    .to(REEF_KR)
        .run_before(stow_.get())          // 2: still at the station
        .run_after(score_.get())          // 3: at REEF_KR
        .run_after(stow_again_.get());    // 4: starts as score_ ends

Hooks on one waypoint run in the order they’re written, each one starting on the loop the one before it ends. Tempo sends zero drive output while they run.

In the C++ tab stow_ and stow_again_ are two stored stow commands, one for each hook.

If the drive to a waypoint fails, its runAfter hooks and waitUntil checks are skipped. Commands for a failed drive go in runOnFailure.

While driving

run(f, cmd) starts cmd at fraction f and keeps it running past the waypoint. It runs until it finishes, a later hook takes its subsystem, or the route ends.

run(from, to, cmd) starts cmd at from and interrupts it at to, or when the drive ends before that. With two actions in place of the command, it calls the first at from and the second at to.

Java
route.to(STATION_LEFT)
    .run(0, 1, intake.intakeCommand())   // whole drive, ended at arrival
    .run(0.2, 0.9,                       // set a goal at 20%, clear it at 90%
        () -> intake.setGoal(Goal.DEPLOY),
        () -> intake.setGoal(Goal.STOW));
Python
(route.to(STATION_LEFT)
    .run(0, 1, intake.intake_command())  # whole drive, ended at arrival
    .run(0.2, 0.9,                       # set a goal at 20%, clear it at 90%
        lambda: intake.set_goal(Goal.DEPLOY),
        lambda: intake.set_goal(Goal.STOW)))
C++
route.to(STATION_LEFT)
    .run(0, 1, intakeCommand_.get())     // whole drive, ended at arrival
    .run(0.2, 0.9,                       // set a goal at 20%, clear it at 90%
        [this] { intake.SetGoal(Goal::Deploy); },
        [this] { intake.SetGoal(Goal::Stow); });

If the robot arrives before reaching a start fraction, that hook starts at arrival, before runAfter.

run also takes a Condition in place of the fraction. That starts the hook on the distance left, match time, a field zone or a sensor.

Wait for a mechanism

waitUntil(check) holds the robot at the waypoint until the check returns true. It runs in order with the runAfter hooks around it. This robot raises the superstructure for the net shot, waits up to 2 s for it to get there, then shoots.

Java
route.to(NET_SHOT)
    .runAfter(() -> superstructure.setGoal(Goal.NET))
    .waitUntil(superstructure::atGoal, Seconds.of(2))
    .runAfter(superstructure.shootCommand());
Python
(route.to(NET_SHOT)
    .run_after(lambda: superstructure.set_goal(Goal.NET))
    .wait_until(superstructure.at_goal, 2.0)
    .run_after(superstructure.shoot_command()))
C++
// shoot_ is a stored frc2::CommandPtr from superstructure.ShootCommand().
route.to(NET_SHOT)
    .run_after([this] { superstructure.SetGoal(Goal::Net); })
    .wait_until([this] { return superstructure.AtGoal(); }, 2_s)
    .run_after(shoot_.get());

Hitting the timeout isn’t a failure, and the next hook starts. This robot shoots after 2 s even if the superstructure isn’t at its goal. Without a timeout, a check that never comes true holds the robot until the auto ends. giveUpAfter doesn’t apply to it.

Run while a state holds

runWhile(condition, cmd) starts cmd each time the condition becomes true and interrupts it each time it stops being true. It watches from the start of this drive until the route ends. With two actions, the second one also runs if the route ends while the condition holds.

Java
import static edu.wpi.first.units.Units.Meters;
import com.tempo.Condition;

route.to(REEF_KR)
    .runWhile(Condition.near(REEF_KR, Meters.of(1.5)),
        () -> superstructure.setGoal(Goal.L4),
        () -> superstructure.setGoal(Goal.STOW))
    .to(STATION_LEFT)
    .runWhile(Condition.near(STATION_LEFT, Meters.of(1.0)), intake.intakeCommand());
Python
from tempo import Condition

(route.to(REEF_KR)
    .run_while(Condition.near(REEF_KR, 1.5),
        lambda: superstructure.set_goal(Goal.L4),
        lambda: superstructure.set_goal(Goal.STOW))
    .to(STATION_LEFT)
    .run_while(Condition.near(STATION_LEFT, 1.0), intake.intake_command()))
C++
#include <tempo/Condition.h>

// intakeCommand_ is a stored frc2::CommandPtr from intake.IntakeCommand().
route.to(REEF_KR)
    .run_while(tempo::Condition::near(REEF_KR, 1.5_m),
        [this] { superstructure.SetGoal(Goal::L4); },
        [this] { superstructure.SetGoal(Goal::Stow); })
    .to(STATION_LEFT)
    .run_while(tempo::Condition::near(STATION_LEFT, 1.0_m), intakeCommand_.get());

The condition has to be a state (near, inside, matchTime, when or a combination of them). A moment such as progress or entered throws. See Conditions.

If the command finishes while the condition still holds, or another hook interrupts it, it only starts again the next time the condition goes from false to true.

Clean up

finallyDo(action) in the first example runs once when the route ends, including when it gives up, is interrupted or a hook throws. It applies to the whole route no matter which to(...) it’s written under. Several finallyDo actions run in the order they’re written.

Hooks still running when the route ends are interrupted, except a runOnFailure command, and that one keeps running after the route.

If a hook throws while the auto runs, that auto ends and the robot stops. Tempo prints one line, [Tempo] the auto "{auto}" ended: ..., that names the hook and the error. The robot program keeps running, so teleop still works.

Actions and checks

run, runBefore, runAfter, runWhile and runOnFailure also take plain methods, as the examples above do. An action (a Java Runnable, a Python callable, a C++ callable returning void) runs once and requires no subsystem.

On runBefore and runAfter, a lambda that returns boolean (bool in C++) is a check, and it holds the robot until it returns true. On the other hooks the returned value is ignored.

() -> intake.hasPiece() waits for a piece, and () -> { intake.start(); } runs once. A lambda that returns some other value, like () -> motor.setControl(request), runs once and the value is dropped.

A method or lambda that returns a command is called when the hook starts, and every hook except runWhile takes one. List the subsystems its command needs after it, as in .run(0.8, superstructure::shootCommand, superstructure). If the command needs a subsystem that isn’t listed, the auto ends and the [Tempo] line says which subsystem to declare.

Python treats every plain callable as an action, even one that returns a bool. Write a check as run_after(wait_until=check) or wait_until(check).

A callable that returns a command is called when the hook starts, and every hook except run_while takes one. List the subsystems its command needs, as in .run(0.8, superstructure.shoot_command, requirements=[superstructure]). If the command needs a subsystem that isn’t listed, the auto ends and the [Tempo] line says which subsystem to declare.

An action can be bound to a robot extension with Tempo.commands().bind(...). The binding matches that exact object, so store the action in a field and pass the same one to the route.

In Java, writing a method reference such as arm::deploy twice isn’t guaranteed to give the same object.

In C++, make a reusable handle with tempo::hooks::once(f).

A Setlist as a hook

A Setlist with no drive can be passed in place of the command to run, runBefore, runAfter and runWhile. One built with new Setlist() (Setlist() in Python, tempo::Setlist() in C++) doesn’t show up on the auto chooser and can be reused.

Java
import com.tempo.wpi.Setlist;

Setlist shot = new Setlist()
    .then(() -> superstructure.setGoal(Goal.NET))
    .waitUntil(superstructure::isShooting)
    .wait(Seconds.of(1.5))
    .finallyDo(() -> superstructure.setGoal(Goal.STOW));

route.to(NET_SHOT).runAfter(shot)    // stopped at the net
    .to(REEF_G).run(0.8, shot);      // from 80% of the next drive
Python
from tempo.wpi import Setlist

shot = (Setlist()
    .then(lambda: superstructure.set_goal(Goal.NET))
    .wait_until(superstructure.is_shooting)
    .wait(1.5)
    .finally_do(lambda: superstructure.set_goal(Goal.STOW)))

(route.to(NET_SHOT).run_after(shot)  # stopped at the net
    .to(REEF_G).run(0.8, shot))      # from 80% of the next drive
C++
#include <tempo/wpi/Setlist.h>

tempo::Setlist shot = tempo::Setlist()
    .then([this] { superstructure.SetGoal(Goal::Net); })
    .wait_until([this] { return superstructure.IsShooting(); })
    .wait(1.5_s)
    .finally_do([this] { superstructure.SetGoal(Goal::Stow); });

route.to(NET_SHOT).run_after(shot)   // stopped at the net
    .to(REEF_G).run(0.8, shot);      // from 80% of the next drive

Each time the hook starts, the Setlist runs from its first step. A window, runWhile or the end of the route interrupts it, and its finallyDo runs.

A Setlist that drives can’t be a hook. If you pass one, the route throws when it freezes on its first use. Make the driving Setlist its own step with .then(...).

What a hook may require

A hook can’t require the drivetrain because the route already requires it, and the route fails to freeze if one does. To drive, use a Setlist .then(...) step, or hand the last stretch to a drivetrain command such as a vision align with arriveWith.

A hook also can’t require a subsystem that a concurrent Setlist step (parallel, race or deadline) uses. A subsystem that a later step uses is fine. So is one that another hook on the same route uses, because the later hook takes it over when it starts.

A route requires the drivetrain and every hook’s subsystems. A Setlist requires all of them for the whole auto, so default commands on those subsystems don’t run until the auto ends.

If it fails…

The route fails to freeze because a hook requires the drivetrain Above → [Tempo] {hook} requires the drivetrain, which its route already drives

Run the drive as its own Setlist .then(...) step, or hand off the arrival with arriveWith.

A check ran once and the robot didn't wait Above → [Tempo] the run_after action returned True and ran once; a check that waits is written ...

Python treats a plain callable as an action, so write the check as run_after(wait_until=check) or wait_until(check).

The robot waits at a waypoint until the auto ends Above →

A waitUntil check never came true, and giveUpAfter doesn’t apply to it. Give the check a timeout.

esc

Loading search...