4.18 Condition
A Condition says when a hook or an aim starts. A moment happens once, like
the robot passing a point on the leg. A state is true or false on every loop,
like the robot being inside a region. The guide is
Conditions.
This example uses the REEFSCAPE 2025 pack:
import static edu.wpi.first.units.Units.Meters;
import static edu.wpi.first.units.Units.Seconds;
import com.tempo.Composer;
import com.tempo.Condition;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.wpilibj.util.Color;
Translation2d reefCenter = new Translation2d(4.489, 4.026);
var route = Composer.startAt(start)
.to(5.13, 5.14)
.run(Condition.progress(0.25), superstructure.stow())
.run(Condition.remaining(Meters.of(1)), superstructure.prepareL4())
.runWhile(Condition.near(reefCenter, Meters.of(2.0)).and(intake::hasPiece),
leds.solidCommand(Color.kGreen))
// seconds since the auto started
.run(Condition.matchTime(Seconds.of(13.0)), superstructure.release());from tempo import Composer, Condition
from wpilib import Color
from wpimath.geometry import Translation2d
reef_center = Translation2d(4.489, 4.026)
route = (Composer.start_at(start)
.to(5.13, 5.14)
.run(Condition.progress(0.25), superstructure.stow())
.run(Condition.remaining(1.0), superstructure.prepare_l4())
.run_while(Condition.near(reef_center, 2.0).and_(intake.has_piece),
leds.solid_command(Color.kGreen))
# seconds since the auto started
.run(Condition.match_time(13.0), superstructure.release()))#include <tempo/Composer.h>
#include <tempo/Condition.h>
// the hook commands are stored members; each run borrows its pointer.
const frc::Translation2d reefCenter{4.489_m, 4.026_m};
auto route = tempo::Composer::start_at(start)
.to(5.13, 5.14)
.run(tempo::Condition::progress(0.25), stow_.get())
.run(tempo::Condition::remaining(1.0), prepare_l4_.get())
.run_while(tempo::Condition::near(reefCenter, 2.0)
&& tempo::Condition::when([this] { return intake.HasPiece(); }),
green_.get())
// seconds since the auto started
.run(tempo::Condition::match_time(13.0), release_.get());Factories
Python and C++ spell matchTime as match_time.
Python takes plain numbers for every distance and time, in meters and seconds.
| Factory | Kind | True |
|---|---|---|
progress(fraction) | Moment | Once the robot has covered fraction of the leg’s planned path distance, 0 to 1. A replan back onto the planned path keeps reading along it, and a replan to a new path reads against the new path and keeps the distance already covered. |
remaining(distance) | Moment | Once the distance left along the planned path to the leg’s target is at most distance. That distance is never smaller than the straight line to the target. A leg that starts within it fires on its first loop. |
matchTime(elapsed) | State | From elapsed seconds after the auto started (not the FMS match clock), on any leg of its route until the route ends. A route that ends first never fires it. A Setlist counts from its first loop and a route run on its own counts from its own start. A time already past fires on the route’s first loop, and two triggers at the same time fire in the order they were written. |
passed(id) | Moment | Once the robot is level with the center of the pack element’s bounding box, measured along the leg’s straight line, or the leg arrives, whichever is first. An element behind the start fires on the first loop. The element can be any obstacle, terrain, region or avoid zone. |
entered(id) | Moment | On the loop the robot center crosses into a pack region. A robot that starts the leg inside doesn’t fire it until it leaves and comes back in. |
exited(id) | Moment | On the loop the robot center leaves a pack region. |
inside(id) | State | Every loop the robot center is inside a pack region, the first loop included. |
near(point, distance) | State | While the robot center is within distance of point. It stops holding beyond distance plus 5 cm or 5 percent of distance, whichever is more. |
when(check) | State | While the robot-code check returns true, read every loop. |
always() | State | From the leg’s first loop. Only aimTarget takes it. |
| Argument | Java | Python | C++ |
|---|---|---|---|
fraction | double, 0 to 1 | float | double |
distance for remaining | Distance | meters | meters |
elapsed | Time | seconds | seconds |
id | String | str, or an Enum member whose name is the id | std::string |
point for near | Translation2d or Pose2d | Translation2d, Pose2d or (x, y) | frc::Translation2d, frc::Pose2d, or x, y in meters |
distance for near | Distance | meters | meters or any length unit |
check | BooleanSupplier | zero-argument callable | std::function<bool()> |
When a leg arrives, it fires every progress, remaining and passed trigger
it hasn’t fired yet, so a remaining distance shorter than where the robot stops
fires then. If an arriveWith command ends a leg
before the robot has driven a crossing with a target on that leg, none of them fire.
passed is measured along the leg’s straight line, so an element off to the
side counts too. When that point on the line is within twice the arrival
tolerance of the leg’s target, the leg can arrive before the robot reaches it.
Tempo prints a warning for it while the route warms up or as it starts:
[Tempo] passed({id}) on the way to waypoint {n} fires on arrival if the robot has not reached it: ...near points mirror with the route for the other alliance, the same as
waypoints. entered, exited and inside take a [[regions]] entry of the
loaded pack that has no effect. A blue region’s red copy has the same id, and
the robot is inside the region when it is inside either copy. Giving them an avoid
zone’s id (effect = "avoid") throws, and passed takes any element.
Combinators
| Java | Python | C++ | True while |
|---|---|---|---|
a.and(b) | a.and_(b) or a & b | a.and_(b) or a && b | Both hold |
a.or(b) | a.or_(b) or a | b | a.or_(b) or a || b | Either holds |
a.not() | a.not_() or ~a | a.not_() or !a | a does not hold |
Java and Python also take a check in place of b, as in
.and(intake::hasPiece), and so do the C++ and_ and or_ methods, as a
std::function<bool()>. Only states combine: near, inside, matchTime,
when and their combinations.
Python raises TypeError when the and, or or not keyword is used on a
Condition, so use &, | and ~.
There’s a limit of 64 checks per loop. On each leg, the route’s distinct when
checks plus that leg’s run triggers can’t pass 64, and neither can the when
checks plus that leg’s aims. A route over the limit fails to freeze. One check object
counts once however many conditions read it, so pass the same object to each of them, or
combine several checks into one lambda.
Accepted by
| Method | Takes | Fires |
|---|---|---|
Composer.run(condition, cmd) | Every factory but always, and combinations | Once, the first time it holds on the leg. matchTime fires on any leg until the route ends |
Composer.runWhile(condition, cmd) | States | Each time the state comes to hold, from this leg until the route ends, and the command is interrupted each time it stops holding |
Composer.aimTarget(condition, target) | passed, entered, exited, inside, near, when, always, combinations | A state aims while it holds, and a moment aims from its event to the end of the leg |
run(condition, cmd) on a Setlist drive, before .at(n) | progress, matchTime, when, inside, near, combinations | Once, while the run drives |
The same run(condition, cmd) after .at(n) | Everything Composer.run takes | Once, on the drive to waypoint n |
Setlist.when(condition, step) | matchTime and when only | Checked once, when the auto reaches it |
Setlist also takes a plain check in when.
Errors
| Cause | Message |
|---|---|
progress outside 0 to 1 | progress must be a fraction in [0, 1]; got {value} |
remaining not positive and finite | remaining distance must be finite and positive; got {value} |
matchTime negative or not finite | match time must be finite and non-negative; got {value} |
near distance not positive and finite | [Tempo] a near distance must be finite and positive; got {value} |
near point not finite | [Tempo] a near point must be finite; got ({x}, {y}) |
when with no check | [Tempo] a when condition needs a predicate; got none |
| A moment in a combinator | [Tempo] and, or and not combine conditions that hold or not at any moment: near, inside, matchTime, when and their combinations; got {kind} condition |
A moment in runWhile | [Tempo] runWhile needs a condition that holds or not at any moment: near, inside, matchTime, when or a combination of them; got {kind} condition |
progress, remaining or matchTime in aimTarget | [Tempo] a {kind} condition cannot start an aim; use passed, entered, exited, inside, near, when or always, or a fraction of the way along the leg |
always in Composer.run (Java wording) | Condition.ALWAYS cannot fire a command mid-leg; use Condition.progress/remaining/matchTime/passed/entered/exited/inside/near/when or a combination here, or run(0, 1, ...) for a command that runs while the leg drives |
always in a Setlist drive’s run | [Tempo] an always condition has no moment to run a command at |
remaining, passed, entered or exited in a Setlist drive’s run before .at(n) | [Tempo] a {kind} condition is measured along the drive to one waypoint; pick the waypoint with .at(n) first |
Anything but matchTime or when in Setlist.when | [Tempo] .when(...) checks its condition once, off any path, so it takes a match time or a when condition; got {kind} condition |
Each is thrown at the call as IllegalArgumentException, ValueError or
std::invalid_argument, except a missing when check in Java, which throws
NullPointerException.
C++ drops the ; got value from the first three.
An id the loaded pack doesn’t declare, or an entered, exited or inside
id that isn’t a [[regions]] entry with no effect, throws when the hook or
aim is added: TempoException in Java, RuntimeError in Python and
std::invalid_argument in C++. The message starts as below, with
Condition::entered( in C++:
Condition.entered("{id}") can never fire; ...The rest of the message names what the id is in the pack and what to use.