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

4.29 PathFollower

A PathFollower computes a leg’s velocity command, and Tempo still plans the trajectory and checks when the leg ends. Wrap it in Control.custom(...) and pass that to the route’s control(...) before the route freezes.

control(...) applies from the leg it follows on, so the example calls it on a route that has no to(...) yet and the follower drives every leg. It runs once per robot loop while the leg is in its DRIVING phase.

Example

This is a proportional position controller that keeps Tempo’s heading command. Tempo doesn’t limit a custom controller’s output, so limit it yourself before running one on a robot.

Java
import com.tempo.Control;
import edu.wpi.first.math.kinematics.ChassisSpeeds;

Control follower = Control.custom(ctx -> {
    double kx = 4.0, ky = 4.0;
    double vx = ctx.setpointVxMps() + kx * (ctx.setpointPose().getX() - ctx.robotPose().getX());
    double vy = ctx.setpointVyMps() + ky * (ctx.setpointPose().getY() - ctx.robotPose().getY());
    double c = Math.cos(-ctx.robotPose().getRotation().getRadians());
    double s = Math.sin(-ctx.robotPose().getRotation().getRadians());
    return new ChassisSpeeds(vx * c - vy * s, vx * s + vy * c,
        ctx.suggested().omegaRadiansPerSecond);
});
route.control(follower);
Python
import math
from tempo import Control
from wpimath.kinematics import ChassisSpeeds

def hold_setpoint(ctx):
    kx = ky = 4.0
    vx = ctx.setpoint_vx_mps + kx * (ctx.setpoint_pose.X() - ctx.robot_pose.X())
    vy = ctx.setpoint_vy_mps + ky * (ctx.setpoint_pose.Y() - ctx.robot_pose.Y())
    heading = ctx.robot_pose.rotation().radians()
    c, s = math.cos(-heading), math.sin(-heading)
    return ChassisSpeeds(vx * c - vy * s, vx * s + vy * c, ctx.suggested.omega)

route.control(Control.custom(hold_setpoint))
C++
#include <cmath>
#include <frc/kinematics/ChassisSpeeds.h>
#include <tempo/Control.h>
#include <tempo/wpi/Geometry.h>

auto follower = tempo::Control::custom([](const tempo::ControlContext& ctx) {
    constexpr double kx = 4.0, ky = 4.0;
    const frc::Pose2d robot = tempo::wpi::robot_pose(ctx);
    const frc::Pose2d setpoint = tempo::wpi::setpoint_pose(ctx);
    double vx = ctx.setpoint_vx_mps + kx * (setpoint.X() - robot.X()).value();
    double vy = ctx.setpoint_vy_mps + ky * (setpoint.Y() - robot.Y()).value();
    double c = std::cos(-robot.Rotation().Radians().value());
    double s = std::sin(-robot.Rotation().Radians().value());
    return frc::ChassisSpeeds{units::meters_per_second_t{vx * c - vy * s},
                              units::meters_per_second_t{vx * s + vy * c},
                              tempo::wpi::suggested(ctx).omega};
});
route.control(follower);
LanguageShape
Java@FunctionalInterface PathFollower { ChassisSpeeds compute(ControlContext ctx); }
PythonA callable, or a PathFollower subclass with compute(ctx). It may return a (vx, vy, omega) tuple in place of ChassisSpeeds
C++struct PathFollower: a compute callable plus optional initialize, reset and end

The return value is robot-frame ChassisSpeeds in m/s and rad/s. On 2027 it’s ChassisVelocities, and Java’s omegaRadiansPerSecond field is omega.

Warning Convert field velocity to robot velocity

The setpoint pose and velocities are field frame but the command you return is robot frame, so rotate a field velocity by the negative measured heading before returning it.

Stateful controllers

If your controller has state, such as PID history or a profile, pass a factory, and it builds a new instance for each leg and run. In the samples, MyFollower is your own class that implements PathFollower in Java and subclasses it in Python, and MyController is your own class that the C++ lambdas call.

Python passes it by keyword, Control.custom(factory=...).

Java
import com.tempo.Control;

route.control(Control.custom(MyFollower::new));
Python
from tempo import Control

route.control(Control.custom(factory=MyFollower))
C++
#include <tempo/Control.h>
#include <memory>

route.control(tempo::Control::custom([] {
    auto controller = std::make_shared<MyController>();
    return tempo::PathFollower{
        [controller](const auto& ctx) { return controller->compute(ctx); },
        [controller](const auto& ctx) { controller->reset(ctx); },
        [controller](bool interrupted) { controller->end(interrupted); }
    };
}));
CallbackRuns
initialize(ctx)Before the first compute of a driving leg, never during warm-up. Defaults to reset(ctx)
reset(ctx)Before compute after a replacement plan, a restart of the leg’s trajectory clock, or a hold that has ended. The instance is kept
end(interrupted)Once. false on normal completion, true on cancel, failure or disposal
  • Callbacks are optional in Java and Python. The C++ three-callback constructor uses its reset callback for initialize too, so assign the initialize and reset members separately if they need to differ.
  • An instance passed directly is reused as is, but a factory must return a new controller on each call.
  • If a leg is canceled while its factory runs, the controller gets end(true) without initialization.
  • Callbacks run on the robot loop and must not block, make large allocations, schedule drivetrain commands, or write to the drivetrain.
  • A controller wrapped with thenFilter(...) still gets these calls.

To adapt a drive-to-pose command such as Team 6328’s DriveToPose, move its calculation into compute. Seed the profile from the measured pose in reset, and track the planned setpoint. Give the constructor a supplier of the measured velocity through a factory lambda, which in Java is () -> new MyFollower(drive::getRobotRelativeSpeeds) in place of MyFollower::new. suggested is Tempo’s command for the tick and isn’t a measured velocity. A controller that aims at the final goal leaves the planned path, and the path is what keeps the robot clear of obstacles.

ControlContext

JavaPythonC++Meaning
robotPose()robot_posetempo::wpi::robot_pose(ctx)Measured field-frame pose
setpointPose()setpoint_posetempo::wpi::setpoint_pose(ctx)Reference position and target heading
setpointVxMps(), setpointVyMps()setpoint_vx_mps, setpoint_vy_mpssame fieldsPlanned field-frame velocity, m/s
setpointOmegaRadPs()setpoint_omega_rad_pssame fieldReference angular velocity, rad/s
suggested()suggestedtempo::wpi::suggested(ctx)Tempo’s own robot-frame command this tick
elapsedS()elapsed_selapsed_ss since the leg started driving. Java also has elapsed() as a Time
timestampS()timestamp_stimestamp_sRuntime clock
dtS()dt_sdt_ss since the last tick, 0 after initialize or reset

The setpoint heading includes terrain and reactive rotation targets, so on a holonomic robot it can differ from the path tangent. WPILib’s HolonomicDriveController reads its travel direction from the trajectory pose’s rotation, so pass it a trajectory pose whose rotation is atan2(vy, vx), with hypot(vx, vy) as the speed and the setpoint heading as the desired heading.

What Tempo still does

  • Tempo plans the leg, replans it under the drift policy, and logs the path item.
  • Tempo ends the leg on arrival, settle, timeouts and early-exit conditions.
  • While Tempo holds the robot still itself, such as while it waits for a plan, the controller isn’t called, and reset runs when the hold ends. A terrain heading-alignment hold allows rotation with translation clamped to zero.
  • A non-finite output or a callback exception ends that route at zero command, and the controller gets end(true). The console prints [Tempo] auto tick failed: custom controller failed: ...; commanding zero and ending this route, and a Setlist goes on to its next step. An end() that throws after the last leg ended doesn’t change the result, and the console prints [Tempo] custom controller cleanup failed: {error}; the route's last leg had already ended.

A custom controller attached to a route stays attached when a Setlist runs that route. Control.thenFilter(op) filters the output of the control it is called on, the built-in follower’s or a custom controller’s.

esc

Loading search...