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

3.20 Calibrate the drivetrain

The calibration autos drive the robot and measure its top speed, acceleration, braking, turn rate, command lag and wheel radius. Once the results are in the config, routes plan with the measured values in place of the ones Tempo derives from the motor, gearing, current limit and mass.

Calibrate before you tune through arrivals, because a corner planned with more acceleration than the robot has pulls it off the path.

Add the autos

Make the calibration auto once at startup, after Tempo.configure(...). If you have your own chooser of commands, add the auto’s command to it. Tempo’s chooser lists the auto without that line. This example is a CTRE Tuner X swerve drivetrain:

Java
import com.tempo.TempoCalibration;

// Runs once at startup, after Tempo.configure(...).
TempoCalibration.Routine calibrate = TempoCalibration.all()
    .modulePositions(() -> drivetrain.getState().ModulePositions);

autoChooser.addOption("Calibrate", calibrate.command());   // a SendableChooser<Command>
Python
from tempo import TempoCalibration

# Runs once at startup, after Tempo.configure(...).
calibrate = TempoCalibration.all().module_positions(
    lambda: drivetrain.get_state().module_positions)

auto_chooser.addOption("Calibrate", calibrate.command())  # a SendableChooser of commands
C++
#include <tempo/TempoCalibration.h>
#include <tempo/wpi/Setlist.h>

// Members, declared after the drivetrain.
tempo::TempoCalibration::Routine calibrate_ = tempo::TempoCalibration::all()
    .module_positions([this] { return drivetrain_.GetState().ModulePositions; });
std::optional<frc2::CommandPtr> calibrateCommand_;

// Runs once at startup, after tempo::wpi::configure(...).
calibrateCommand_ = tempo::wpi::command(calibrate_);
autoChooser_.AddOption("Calibrate", calibrateCommand_->get());  // a SendableChooser<frc2::Command*>

The autos drive the drivetrain you gave to Tempo.configure(...). The module positions are only for the wheel radius routine, and if you leave them out that routine is skipped. That routine also needs the wheel radius in the Tempo config to be the same number your drivetrain uses to turn wheel rotations into meters. A Tuner X project has the same value in both when the Tempo config reads the radius from TunerConstants.

command() works with any chooser that holds commands, and on a button with controller.a().whileTrue(calibrate.command()).

The command is tempo::wpi::command(routine). Make it after tempo::wpi::configure(...), and keep the routine and its CommandPtr as members for as long as the chooser lists them.

Tempo’s chooser

Each calibration auto registers itself under its name when it’s made, and TempoAutos.chooser() lists it with the other autos. all() is listed as Tempo Calibration: All.

Without the command scheduler

If your robot runs its autos with start(), keep the calibration auto in a field and run it the same way:

Java
public void autonomousInit() { calibrate.start(); }
public void robotPeriodic()  { Tempo.update(); }
public void teleopInit()     { calibrate.cancel(); }
public void disabledInit()   { Tempo.stop(); }
Python
def autonomousInit(self): self.calibrate.start()
def robotPeriodic(self):  Tempo.update()
def teleopInit(self):     self.calibrate.cancel()
def disabledInit(self):   Tempo.stop()
C++
void Robot::AutonomousInit() { calibrate_.start(); }
void Robot::RobotPeriodic() { tempo::Tempo::update(); }
void Robot::TeleopInit() { calibrate_.cancel(); }
void Robot::DisabledInit() { tempo::Tempo::stop(); }

Other drivetrains

modulePositions takes WPILib SwerveModulePositions. On any other drivetrain, give wheelDistances(...) with each drive wheel’s distance in meters, or wheelPositions(...) with each drive wheel’s total rotation in radians.

A mecanum config has no wheel radius, so on mecanum give wheelPositions(...) and set the config’s wheelOffsets, or the wheel radius routine is skipped.

The autos

AutoChooser nameMeasures
all()Tempo Calibration: AllEvery routine below except topSpeed(), in this order
translation()Tempo Calibration: TranslationTop speed, acceleration at each speed, braking deceleration
lag()Tempo Calibration: Command LagHow late the drivetrain follows a command, and on swerve how late it follows a change of direction
turning()Tempo Calibration: TurningTurn rate and angular acceleration at 25%, 50%, 75% and 100% of the top speed. Skipped on a differential drive
rotation()Tempo Calibration: RotationTop turn rate and angular acceleration in place
wheelRadius()Tempo Calibration: Wheel RadiusWheel radius
topSpeed()Tempo Calibration: Top SpeedTop speed, on its own (Measuring top speed)

Each auto takes the same options. space(...) is the total length of clear floor in a straight line, and it’s 3 m if you don’t set it. The other options are under Saving on the robot, Configured limits and TempoCalibration.

Python and C++ spell every option and auto in snake_case, like module_positions, save_to_robot and top_speed().

Run them

  1. Aim the robot at clear floor. The translation routine drives straight forward and back at full speed in the direction the robot faces when the auto starts.
  2. Put the robot in the middle of the space. The drives brake to stay within half of it either way, measured at the robot’s center, so leave extra floor for the bumpers and for overshoot.
  3. Enable the robot and run the auto, from the chooser in autonomous or from its button.
  4. Leave it enabled until it prints its results. If you disable the robot it stops at once with a zero command, and the readings taken so far are kept.

all() runs the straight drives first and then the routines that turn the robot, and each of those ends with a slow turn back to the heading the auto started at. A turning routine that was stopped early doesn’t turn back, so check the heading before you start the next auto.

If the robot ends up more than 0.3 m past its half of the space, or the robot loop stalls for more than 0.25 s while the robot is sent motion, the auto stops and commands zero.

Each routine has a time limit: 90 s for translation and turning, 60 s for wheel radius and top speed and 40 s for command lag and rotation. A routine that runs past its limit ends with a zero command and a note in the result.

Floor space

The more floor the translation drives have, the faster the robot gets before it has to brake. When a drive brakes before the speed settles, the acceleration is only measured up to the fastest speed reached. Above that speed the curve and the top speed are estimated, and the report marks those points “(extrapolated)” and prints the top speed as “(estimated)”. Without a motor model in the config nothing is estimated, and the curve ends at the fastest speed reached.

The turning drives are held to the speeds the space allows, and the report notes when the faster ones were lowered. The spins turn the robot in place, so keep the floor clear as far out as its corners swing.

Measuring top speed

topSpeed() (top_speed() in Python and C++) measures the top speed in place of the estimate. It drives forward once and holds until the speed stops changing and then brakes. Add it the same way as the other autos, with the same options:

Java
autoChooser.addOption("Calibrate top speed", TempoCalibration.topSpeed().space(9.0).command());
Python
auto_chooser.addOption("Calibrate top speed", TempoCalibration.top_speed().space(9.0).command())
C++
// Members, declared after the drivetrain.
tempo::TempoCalibration::Routine topSpeed_ = tempo::TempoCalibration::top_speed().space(9.0);
std::optional<frc2::CommandPtr> topSpeedCommand_;

// Runs once at startup, after tempo::wpi::configure(...).
topSpeedCommand_ = tempo::wpi::command(topSpeed_);
autoChooser_.AddOption("Calibrate top speed", topSpeedCommand_->get());

Use it when there’s room and the report printed the top speed as “(estimated)”. It needs enough floor to reach about 98% of the motor’s free speed, hold it for about 0.6 s and brake. If the space is too short, the report prints “at least” the fastest speed reached and the floor the auto needs, and the measured top speed isn’t changed.

With saveToRobot() on, a settled top speed replaces an estimated one in the saved file, whichever of the two runs came first.

Configured limits

The routines don’t use the limits stated in LimitsConfigs, and they command more than the motor model says the robot can do. After calibration the stated limits still lower the measured ones when routes plan, so they can stay in the config while you calibrate.

If the floor is small or crowded, respectLimits() keeps every command within the stated top speed, acceleration, turn rate and angular acceleration. A reading at one of those limits is marked capped in the result and in the report, because the robot can do more than that reading shows.

If your drivetrain code caps the speed, the cap is what gets measured as the top speed. When the top speed settles well below the motor’s free speed, the report notes that a drivetrain speed cap or wheel speed desaturation may be limiting it.

What each routine does

  • Translation makes six drives, three each way, or four with 4 m of floor or more. Each one commands more than the top speed, holds once the speed stops rising or the space runs out and then brakes.
  • Command lag ramps the speed up and back down and finds how far behind the commands the robot moves. This becomes the follower latency. On a swerve drive the robot then weaves out and back to read the steering lag. The weave is left out when the space or a limit leaves it under 0.3 m/s, and the report then has no steering lag.
  • Turning makes one drive at each fraction of the top speed and ramps the turn rate up while it drives.
  • Rotation makes four spins in place, two each way.
  • Wheel radius spins slowly in place for two turns and compares the gyro’s turn with the turn the wheels report. If the radius is more than 2% off the configured one it prints a note. Fix it in the drivetrain’s odometry and in the module config and then run the other routines again, because they measure distance through it.

The routines need a drivetrain that follows the robot-relative speeds it’s sent. They don’t read the drivetrain’s gains, so WPILib’s SysId can run before or after. Run the routines again after you retune the feedforward, because that changes how the robot follows commands.

Use the results

Every report ends with a MeasuredConfigs call chain in your robot code’s language. Paste it into withMeasured(...) on your config. This report is from a Java robot:

[Tempo] Calibration finished.
  Translation: top speed 3.94 m/s, acceleration 8.10 m/s^2, deceleration 8.92 m/s^2 (configured 5.20 m/s, 10.79 m/s^2)
    Acceleration by speed: 8.10 m/s^2 at 0.95 m/s; 7.42 m/s^2 at 1.39 m/s; 6.51 m/s^2 at 1.80 m/s; ...
  Command lag: 0.078 s, steering 0.055 s
  Turning while driving: 9.13 rad/s and 34.49 rad/s^2 at 0.64 m/s; 7.53 rad/s and 28.39 rad/s^2 at 1.21 m/s; ...
  Rotation: top turn rate 11.00 rad/s, angular acceleration 37.12 rad/s^2 (configured 14.60 rad/s, 49.42 rad/s^2)
  Wheel radius: 0.0503 m (configured 0.0508 m)
  Paste this into withMeasured(...). It holds this run's readings only;
  copy another run's calls into it to keep those, or run with saveToRobot() to
  merge every run in the robot's calibration file:
    new MeasuredConfigs()
        .maxVelocity(3.943)
        .maxAcceleration(8.101)
        .accelerationAt(0.952, 8.101)
        ...
  Set this on the module: .wheelRadius(0.05031)
Java
public static final TempoConfig CONFIG = new TempoConfig()
    .withModule(SwerveModules.SDS.MK4i(SdsMk4iGear.L2, DCMotor.getKrakenX60Foc(1), Amps.of(60))
        .wheelRadius(0.05031))   // from "Set this on the module"
    // ... the rest of the config
    .withMeasured(new MeasuredConfigs()
        .maxVelocity(3.943)
        .maxAcceleration(8.101)
        // ... the rest of the calls from the report
        .accelerationAt(0.952, 8.101));
Python
CONFIG = (TempoConfig()
    .with_module(SwerveModules.SDS.MK4i(SwerveModules.SdsMk4iGear.L2, DCMotor.krakenX60FOC(1), 60.0)
        .wheel_radius(0.05031))   # from "Set this on the module"
    # ... the rest of the config
    .with_measured(MeasuredConfigs()
        .max_velocity(3.943)
        .max_acceleration(8.101)
        # ... the rest of the calls from the report
        .acceleration_at(0.952, 8.101)))
C++
inline const tempo::TempoConfig kConfig = tempo::TempoConfig{}
    .with_module(tempo::SwerveModules::SDS::MK4i(
            tempo::SwerveModules::SdsMk4iGear::L2, tempo::DcMotor::kraken_x60_foc(), 60.0)
        .wheel_radius(0.05031))   // from "Set this on the module"
    // ... the rest of the config
    .with_measured(tempo::MeasuredConfigs{}
        .max_velocity(3.943)
        .max_acceleration(8.101)
        // ... the rest of the calls from the report
        .acceleration_at(0.952, 8.101));

If you run one routine the chain only has that routine’s readings, so keep the calls from an earlier run’s chain when you paste a later one.

The wheel radius isn’t in the chain. It prints on its own Set this on the module line, and it goes on the module with wheelRadius(...) (wheel_radius(...) in Python and C++).

Saving on the robot

saveToRobot() (save_to_robot() in Python and C++) makes each run merge its readings into a file on the robot, and the chain it prints then holds every reading in that file. Without it nothing is written.

Load the file with MeasuredConfigs.saved() in place of the pasted chain:

Java
public static final TempoConfig CONFIG = new TempoConfig()
    // ... the rest of the config
    .withMeasured(MeasuredConfigs.saved());
Python
CONFIG = (TempoConfig()
    # ... the rest of the config
    .with_measured(MeasuredConfigs.saved()))
C++
inline const tempo::TempoConfig kConfig = tempo::TempoConfig{}
    // ... the rest of the config
    .with_measured(tempo::MeasuredConfigs::saved());

The file is /home/lvuser/tempo/calibration.json on a roboRIO and /home/systemcore/tempo/calibration.json on a SystemCore, and it stays there across deploys. In simulation it’s tempo/calibration.json in the project folder.

To keep the readings with the robot code, for simulation and for a new controller, copy the file into src/main/deploy/tempo/.deploy/tempo/ beside robot.py. saved() reads the file on the controller first and the deployed copy when the controller has none or its file can’t be read. With neither file it prints a line and the config keeps its derived limits.

A saved file isn’t removed when the controller moves to another robot or the drivetrain is regeared, so Tempo reads it only when the config calls saved().

The measured values replace the ones Tempo derives, and they are ceilings. A limit stated in LimitsConfigs can lower them but not raise them. The measured lag becomes the follower latency unless withLag states one. What each reading does to a route is listed on Measured limits.

Tempo prints a warning when a measured limit is more than 25% away from what the motor model gives. A large gap usually means the gearing, current limit or mass in the config is wrong.

The results also go to TempoLogging.calibration(...), and TempoLogging.everything(...) writes them under Calibration/ (TempoLogging).

If it fails…

Only "[Tempo] Calibration finished." shows in the console Above →

The rest of the report prints on the lines right after it. Scroll the Driver Station console back to it, or read /var/local/natinst/log/FRC_UserProgram.log on the roboRIO, which keeps the robot program’s console. Run with saveToRobot() (save_to_robot() in Python and C++) to keep every reading in the robot’s calibration file, or log them with TempoLogging.everything(...), which writes them under Calibration/.

The routine stops right away with "the robot left the 1.50 m space" Above →

The robot braked too late, or the pose jumped. Give it more space, and check that the pose getter given to Tempo.configure(...) reads meters and doesn’t jump during the run. The length in the message is half of space(...), the distance the robot may drive each way from where it started.

The top speed prints with "(estimated)" Above →

The space ended every drive before the speed settled, so the curve above the fastest speed reached comes from the motor model. Run topSpeed() where there’s room for a measured top speed.

The wheel radius routine is skipped Above →

Give the auto the module positions with modulePositions(...), or the wheel distances or rotations.

A reading is listed as at a configured limit Above →

The auto ran with respectLimits(). Run it without that option to measure the robot.

A routine ends with "it did not finish within" Above →

The drivetrain didn’t follow the commands well enough to finish the routine in its time limit. Check that the drivetrain drives when it’s sent robot-relative speeds.

esc

Loading search...