3.19 Test an auto
SimRun drives a route on a simple robot model in a unit test, with no HAL or WPILib simulation
running. SimAssert fails the test when the route gets slower or less accurate.
Red-alliance runs and cancellation need the real
command and the WPILib scheduler.
Run a route headless
This test configures Tempo for a blue robot and drives a route with one waypoint.
import com.tempo.Alliance;
import com.tempo.Composer;
import com.tempo.Tempo;
import com.tempo.sim.SimRun;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@BeforeEach
void setUp() {
Tempo.resetForTesting();
Tempo.configure(CONFIG, () -> false);
}
@Test
void drivesOneLeg() {
Composer route = Composer.startAt(new Pose2d(1.5, 6.5, new Rotation2d(0.0)))
.originAlliance(Alliance.BLUE)
.to(3.0, 6.0);
SimRun run = SimRun.of(route).go();
}from tempo import Alliance, Composer, Tempo
from tempo.sim import SimRun
from wpimath.geometry import Pose2d
def setup_function(_):
Tempo.reset_for_testing()
Tempo.configure(CONFIG, lambda: False)
def test_drives_one_leg():
route = (Composer.start_at(Pose2d(1.5, 6.5, 0.0))
.origin_alliance(Alliance.BLUE)
.to(3.0, 6.0))
run = SimRun.of(route).go()#include <utility>
#include <gtest/gtest.h>
#include <tempo/Tempo.h>
#include <tempo/sim/SimRun.h>
class AutoTest : public ::testing::Test {
protected:
void SetUp() override {
tempo::Tempo::reset_for_testing();
tempo::Tempo::configure(kConfig, [] { return false; });
}
};
TEST_F(AutoTest, DrivesOneLeg) {
auto route = tempo::Composer::start_at(1.5, 6.5, 0.0)
.origin_alliance(tempo::Alliance::Blue)
.to(3.0, 6.0);
auto run = tempo::SimRun::of(std::move(route));
run.go();
}CONFIG (kConfig in C++) is the config from robot setup.
Save the tests in a tests folder next to robot.py and run them with
python -m robotpy test. robotpy deploy runs the same tests before it uploads.
The alliance check after the config keeps the test on blue whatever the simulated Driver Station
reports. The examples on this page use the
REEFSCAPE 2025 pack. Save it in the test project
as src/main/deploy/tempo/game.toml.deploy/tempo/game.toml, with deploy next to robot.py. Without
it Tempo loads its default pack.
resetForTesting() (reset_for_testing() in Python and C++) clears Tempo’s configuration,
pack, registered autos and logging before each test
(what it clears). Tempo’s configuration is global to the process, so
don’t run these tests in parallel in one process.
SimRun doesn’t run your commands. A command the robot waits on, such as a runBefore,
finishes immediately. Where an arriveWith command would take over,
the robot is placed at the waypoint and the route continues from there.
go() returns when the route finishes or after 60 s of simulated time, and it doesn’t throw
if it hits that limit. A route built with Tempo.to(...) has no start pose, so give it one with
startingAt(pose) (starting_at).
route is moved into SimRun and can’t be used afterward, so the later C++ examples on this
page each start from a newly built route.
The run has the final pose, the simulated time, the largest tracking error and one Frame per
tick, and writeCsv(path) (write_csv) writes the frames to a CSV file
(Simulation).
The run also records each command the route would have scheduled, in order:
List<SimRun.Fired> fired = run.fired(); // in scheduling order
boolean spun = run.firedAny("spinUp");
double at = run.firstFiredAt("spinUp"); // NaN if it never firedrun.fired lists them, run.fired_any("spinUp") says whether one fired, and
run.first_fired_at("spinUp") says when, or NaN if it never did.
run.fired() lists them, run.fired_any("spinUp") says whether one fired, and
run.first_fired_at("spinUp") says when, or NaN if it never did.
A Fired shows that the route scheduled the command, but not that the command finished. Its
phase tells a runBefore apart from a runAfter on the same waypoint.
Assert on it
import com.tempo.sim.SimAssert;
import static edu.wpi.first.units.Units.Meters;
import static edu.wpi.first.units.Units.Seconds;
import static org.junit.jupiter.api.Assertions.assertTrue;
SimRun run = SimAssert.that(route)
.finishesWithin(Seconds.of(5)) // simulated duration at most 5 s
.staysWithin(Meters.of(0.15)) // tracking error at most 15 cm
.endsWithin(Meters.of(0.05)) // within 5 cm of the last target driven to
.check();
var end = run.finalPose(); // compared with the authored end, (3.0, 6.0)
assertTrue(Math.hypot(end.getX() - 3.0, end.getY() - 6.0) <= 0.05);import math
from tempo.sim import SimAssert
run = (SimAssert.that(route)
.finishes_within(5.0)
.stays_within(0.15)
.ends_within(0.05)
.check())
end = run.final_pose
assert math.hypot(end.X() - 3.0, end.Y() - 6.0) <= 0.05#include <tempo/sim/SimRun.h>
#include <tempo/wpi/Geometry.h>
auto run = tempo::SimAssert::that(std::move(route))
.finishes_within(5.0)
.stays_within(0.15)
.ends_within(0.05)
.check();
const frc::Pose2d end = tempo::wpi::final_pose(run);
EXPECT_LE(end.Translation().Distance(frc::Translation2d{3.0_m, 6.0_m}).value(), 0.05);The bounds are plain numbers, in seconds for finishes_within and meters for the other two.
check() runs one simulation and returns the SimRun. It throws on the first bound that’s
broken, with a message like tracking error peaked at 0.182 m, bound was 0.150 m. Bounds
you don’t set aren’t checked.
endsWithin measures from the last target the simulation drove toward. A waypoint the planner
can’t find a route to is skipped, so endsWithin can pass on the wrong endpoint, and the
example checks the authored end separately. For a longer auto, also check each point it has to
visit on the way.
SimAssert can also check the commands fired() lists. These go in the
same chain as the bounds, before check():
fires(name)fails if no command with that name fired.neverFires(name)fails if one did.firesInOrder(names...)checks the relative order and lets other commands fire in between.
Python and C++ spell them fires, never_fires and fires_in_order. C++ takes the names as
a list, fires_in_order({"intake", "shoot"}).
The names are the commands’ own (getName()), so give each command you assert on a fixed
name. A hook made from a plain function is named runOnce,
waitUntil or startEnd.
Realism
import com.tempo.sim.Realism;
SimRun run = SimRun.of(route).withRealism(Realism.typical()).go();
Realism custom = Realism.none()
.motorLagTau(0.10) // first-order lag on commanded speeds, s
.accelLimit(6.0) // max translational accel, m/s^2
.odometryNoise(0.002, 0.001) // per-tick random-walk sigma (m, rad)
.loopJitter(0.002) // sigma on the 20 ms loop period, s
.seed(1234); // same seed, same noisefrom tempo.sim import Realism
run = SimRun.of(route).with_realism(Realism.typical()).go()
custom = (Realism.none()
.motor_lag_tau(0.10) # first-order lag on commanded speeds, s
.accel_limit(6.0) # max translational accel, m/s^2
.odometry_noise(0.002, 0.001) # per-tick random-walk sigma (m, rad)
.loop_jitter(0.002) # sigma on the 20 ms loop period, s
.with_seed(1234)) # same seed, same noiseauto run = tempo::SimRun::of(std::move(route));
run.with_realism(tempo::Realism::typical()).go();
auto custom = tempo::Realism::none()
.motor_lag_tau(0.10)
.accel_limit(6.0)
.odometry_noise(0.002, 0.001)
.loop_jitter(0.002)
.with_seed(1234);Realism.none() is the default, with no lag, acceleration limit, noise or jitter.
Realism.typical() adds all four, with the values listed in
Simulation.
On one machine the same seed and inputs give the same frames in Java, Python and C++. Another
machine can give slightly different numbers, so assert that values stay inside a range. Keep
one test on none() and one seeded test with noise.
Check for obstacles
SimRun records only the robot’s center. This check tests that center against the pack’s
obstacles in each frame, and it doesn’t cover the bumpers.
for (SimRun.Frame f : run.frames()) {
var p = f.pose().getTranslation();
assertFalse(Tempo.pack().pointInObstacle(p, Alliance.BLUE), "obstacle at " + p);
}for frame in run.frames:
p = frame.pose.translation()
assert not Tempo.pack().point_in_obstacle(p, Alliance.BLUE), f"obstacle at {p}"Loop over run.frames() and call
tempo::Tempo::pack().point_in_obstacle(tempo::wpi::pose(frame).Translation(), tempo::Alliance::Blue)
on each frame.
Test the red run
SimRun always starts the robot at the authored start and never mirrors it, so it can’t test a
red run. A red test runs the route command through the scheduler with HAL simulation, the
drivetrain methods given to Tempo.configure(...) and no alliance check. Before it schedules
the command, the test sets the simulated Driver Station to red and places the simulated robot
at the mirrored start.
import edu.wpi.first.hal.AllianceStationID;
import edu.wpi.first.wpilibj.simulation.DriverStationSim;
DriverStationSim.setAllianceStationId(AllianceStationID.Red1);
DriverStationSim.notifyNewData();
// place the simulated robot at Field.mirror(authoredStart) before schedulingimport hal
from wpilib.simulation import DriverStationSim
DriverStationSim.setAllianceStationId(hal.AllianceStationID.kRed1)
DriverStationSim.notifyNewData()
# place the simulated robot at Field.mirror(authored_start) before schedulingCall frc::sim::DriverStationSim::SetAllianceStationId(HAL_AllianceStationID_kRed1) and
then frc::sim::DriverStationSim::NotifyNewData(). Place the simulated robot at
tempo::Field::mirror(authored_start) before scheduling.
After the command initializes, the first pose Tempo logs (the pose item of
TempoLogging) should be the mirrored start. With
resetOdometry(true) the pose setter should also have received it. Moving the pose estimator
doesn’t move the simulated robot, so place both.
To check whether the robot drove to and arrived at every waypoint, read
Composer.handle().
To check whether the robot drove to and arrived at every waypoint, log the arrivals and
giveUps items (give_ups) and read them in the test
(Log Tempo state).
Under the scheduler
These tests run the real WPILib command after the
Tempo.configure(...) setup, with the pose, time and sensor
inputs controlled by the test. endWhen, giveUpAfter, runOnFailure and stalls are explained
on Failure handling.
| Scenario | Expected result |
|---|---|
A command the robot waits on (a runBefore or runAfter) never finishes | The robot stays stopped until a whole-auto time limit, if one is set, interrupts it. Cleanup runs. |
The sensor a leg’s endWhen reads changes | The endWhen ends the drive to that waypoint once, and the route moves on through its runAfter commands. |
| A leg can’t be planned | The leg is skipped, the skipped-leg counts go up and the leg’s runOnFailure command starts. |
| The robot stops making progress | The drive ends by its giveUpAfter or a stall, and its runAfter scoring command doesn’t run. |
| Teleop interrupts the auto | The command, its running hooks and any running runOnFailure command stop, and the drive gets zero output. A runOnFailure command that already outlived the auto ends when the robot is disabled between modes, unless it was built with ignoringDisable(true). |
A runOnFailure command outlives its route | Under the Commands v2 scheduler it keeps running after the auto’s steps finish, in a command of its own that holds only its subsystems. A Commands v3 auto waits for it instead. |
| The auto is scheduled again | Elapsed time and route state start over. Tempo doesn’t reset state your own code keeps, such as a latched sensor reading. |
If it fails…
The test project has no src/main/deploy/tempo/game.toml (deploy/tempo/game.toml in Python), so save the pack there.
Tempo’s configuration is global to the process. Call resetForTesting() before each test and don’t run these tests in parallel in one process.
A waypoint that couldn’t be planned to was skipped, and endsWithin measured from the last target driven toward. Check the authored end separately.