Skip to content

nav: dyn_bike_step, a differentiable dynamic single-track vehicle (P8a) - #714

Merged
transfix merged 3 commits into
masterfrom
feat/dyn-bike
Oct 19, 2026
Merged

transfix merged 3 commits into
masterfrom
feat/dyn-bike

Conversation

@transfix

Copy link
Copy Markdown
Owner

P8a of the vehicle-physics plan: a differentiable dynamic single-track vehicle beside bike_step (Joe: "start P8a, the dynamic bicycle model").

The split

bike_step is now bike_control (the trained controller with every governor, emitting (delta_cmd, a_cmd)) then bike_plant (the kinematic integration). Its outputs are bit for bit what they were. bike_step_bwd is a thin wrapper over the same reverse pass, and bike_control_bwd takes gradients on the two commands from any other plant. coef_train's 8 tests pass unchanged (bicycle gradcheck dir_rel 8.5e-4).

The vehicle (inc/cvc/nav/detail/dyn_bike.h)

dyn_bike_step = bike_control into diff::dyn_plant. Everything is CVC_HD.

  • State: the rear axle's (x, y), the heading, the body velocity (vx, vy) at the centre of gravity, the yaw rate, and the steering angle and drive force behind their actuators.
  • Actuators: exact first-order lags, and a smooth steering slew limit.
  • Tires: Pacejka lateral D sin(C atan(B alpha)). The drive and brake force per axle saturates smoothly at mu Fz, and the cornering peak is the friction ellipse's remainder, longitudinal first.
  • Load transfer: quasi-static, longitudinal.
  • Longitudinal: drag, rolling resistance and grade.
  • Low speed: a blend to the kinematic bicycle.

Numerics.

  • (vy, r) advance by implicit Euler, solved by three steps. Each tire is linearized by its secant through its own root in lateral velocity, -F / (w - w0).
    • That secant is positive at any slip and any speed, and the linear model keeps the tire's root.
    • So stiff tires are stable at 0.06 s, and a sliding tire never pushes its slip past zero, even crawling at full lock.
    • Slip-space secants and tangents both blew up there.
  • The speed is predicted from the longitudinal forces, then corrected with the lateral forces the implicit step applied (constraint forces in the stiff limit).

Adjoint. The plant is written once over a scalar type, and dyn_plant_vjp runs it on forward-mode duals (doc 19's suggestion). dyn_bike_step_bwd then hands the command gradients to bike_control_bwd.

Parameters from the spec sheet

cvc::nav::dyn_bike_si (SI), converted by length exponent with dyn_bike_params_of(si, frame).

cvc::physics::nav_plant::dyn_bike_si_of(vehicle_desc) derives:

  • mass, and the yaw inertia of the chassis box;
  • the axles relative to the centre of gravity;
  • Pacejka B and C from the tire's peak slip angle and sliding fraction;
  • the drive and brake splits, drag, rolling resistance and the steering rate.

front_stiffness (0.85) stands in for the front's load sensitivity and steering compliance (understeer).

Gates (nav_dyn_bike_test, 9 tests; the Humvee from its vehicle_desc)

Gate Result
Plant gradient vs central differences, double, 6 regimes (cruise, hard corner, braking in a turn, past the peak, backing up, creeping) worst relative error 6e-6
Whole step through the controller, 6-step rollout, float dir_rel 6e-4 (limit 5e-2)
Kinematic limit: bike_step's commands through the plant, 200 ticks, stiff tires, no lag, mu g 40 vs 1.5, mass and drive/brakes at the rear axle within 0.011 of its path (0.07 rr); under the controller 0.017 apart, ending 1.110 vs 1.111 from the goal
Understeer growing with lateral g (15 m/s) 0.0003 rad at 0.07 g to 0.0029 rad at 0.53 g, faster toward the limit
Oversteer braking in a turn (0.4 g, then 0.4 g of brake) path tightens 6%
Steady lateral acceleration never above mu g 0.97 mu g (mu 0.4), 0.95 mu g (mu 0.9)
Stopping within 10% of v^2/(2 mu g) -4% to -6%

The kinematic limit is exact only for the vehicle bike_step assumes. A real body differs physically from its v' = a:

  • it puts kinetic energy into yaw and sideways motion as it turns;
  • a steered front wheel's brake force acts along the wheel, about F / cos(delta) on the body.

The test reports that case: up to 1.7 apart open loop, and 1.122 vs 1.111 from the goal under the controller.

DynBike.Golden pins the plant's values for GRL-SNAM's torch twin (tests/data/dyn_bike_golden.json).

Not in this PR

  • The torch twin (GRL-SNAM).
  • rollout_kind::dynamic in the trainers (P8b).
  • A CUDA build of the plant.
  • Training scenes in physical units: the trainer's city scene is in units where vmax is 0.9 and a_max is 1.5 against a 0.035 wheelbase, so P8b needs a physically sized scene.

…er bike_control (P8a)

bike_step splits into bike_control (the controller, emitting delta_cmd and a_cmd) and
bike_plant (the kinematic integration), its outputs bit for bit unchanged; bike_control_bwd
takes the commands' gradients from another plant. diff::dyn_plant: Pacejka tires within a
friction ellipse, quasi-static load transfer, actuator lags, drag, rolling resistance and
grade, a low-speed blend; (vy, r) by implicit Euler with each tire linearized by its secant
through its root in lateral velocity. Its adjoint runs it on forward-mode duals. Parameters
are SI from the spec sheet (dyn_bike_si), and nav_plant::dyn_bike_si_of derives them from a
vehicle_desc. nav_dyn_bike_test gates the gradients, the kinematic limit against bike_step,
the skid modes and the stopping envelope, and pins golden values for the torch twin.
…ers in the golden

A double plant is now double throughout (the lags' exp, the slew limit, the determinant floor
and the friction ellipse's factor were float), so GRL-SNAM's torch twin reproduces it to 1e-9:
next state and VJP on all seven golden cases (tests/test_dyn_bike_parity.py there).
@transfix
transfix merged commit 1e3ed0b into master Oct 19, 2026
23 of 33 checks passed
transfix added a commit that referenced this pull request Oct 19, 2026
…s (fixes macOS)

DynBike.KinematicLimitFollowsBikeStep failed on macOS (#717's CI, and master since #714): its
32 routes come from sample_starts_goals, whose uniform_int_distribution differs by standard
library, and under the controller (dyn_bike_step) the worst agent parted by 1.3 against 0.017
here. Over 40 sets of routes, 0 to 11 of 32 agents part by more than 0.2 rr in closed loop (the
controller amplifies the plant's small differences along some routes), while the median gap is
at most 0.0081 and the median end-distance difference at most 0.41%. The closed loop is now
gated by those medians (0.1 rr, 2%) and its worst agent reported; the open-loop gate, at most
0.0151 over the 40 sets, moves from 0.1 rr to 0.2 rr.
transfix added a commit that referenced this pull request Oct 19, 2026
…Jolt reach (P8b) (#717)

* nav: rollout_kind::dynamic in coef_train, the policy trained through the dynamic vehicle (P8b)

coef_trainer drives bike_control into dyn_plant (dyn_bike_step / dyn_bike_step_bwd) when
train_config::rollout is dynamic. loss_and_grad carries the vehicle's further state (vy, r,
delta, a) through optional x / x_out, and train() keeps it across windows. train_config::dyn is
the vehicle; a dynamic run without one whose wheelbase is veh_L throws.
nav_plant::dyn_bike_params_at(desc, wheelbase) builds it from a vehicle_desc: the trainer's
city reads as a RoboRacer at about 0.108 normalized units per metre. The CUDA trainer refuses
the dynamic rollout for now. Gradcheck dir_rel 2.3e-2; the horizon loss falls; a policy
trained at 5e-5 keeps 71.9% reach; an lr sweep is in docs/NAV_TRAINING.md.

* nav: material_integrator::dynamic, the material trainer through the dynamic vehicle (P8b)

integrate_dynamic_material drives integrate_bicycle_material's controller (force law, pure
pursuit, governors, d_cap) into diff::dyn_plant in place of the kinematic update; arc length and
cumulative risk follow the dynamic path. Its VJP joins the plant's dual-number VJP to the
bicycle's hand-written controller reverse, which gains a controller-only mode (the kinematic
default adds 0, so its sums are unchanged). multi_start_penalty_dynamic runs L_multi on it.
material_loss_config::dynamic is the vehicle; L_vel compares the rear axle's world velocity.
Gradchecks: rollout VJP on 40 coefficients (worst 1.5e-2, directional 3e-6 at eps 1e-3),
L_multi worst 4.8e-4, the full loss-to-weights chain dir_rel 7.0e-4 over 64 weights.

* nav_plant_test: P8b's gate on the Jolt plant, the dynamic-trained policy against the point-mass one

NavPlantConvergence.DynamicTrainedPolicyAgainstPointMassOnJolt trains both alike on the city and
drives each on Jolt RoboRacers (the city as a RoboRacer's, its blocks static boxes) over 20
paired seeds, with a paired-bootstrap 95% CI. At lr 2e-4 / 300 steps the dynamic-trained policy
reaches 44.0% against 35.0% (+9.0, CI [+6.7, +11.4]), but both fall below the untrained seed's
51.7%; at 5e-5 / 150 they tie (+0.4, CI [-1.8, +2.8]). Recorded in NAV_TRAINING.md and the
roadmap.

* nav: checkpoint selection on Jolt reach (P8b)

coef_trainer::train(scene, verbose, fitness) scores the untrained policy, every
train_config::checkpoint_every steps and the last, and ends on the best, so it never ends
below where it started by that score (checkpoints(), selected_step()).

cvc::physics::reach_on_plant (nav_eval.h) is that score on the physics plant: a coef_train
scene's blocks as static boxes, read at a vehicle's scale, `agents` vehicles driven through a
nav_plant per seed. The P8b gate test now uses it. It repeats exactly (single-threaded,
cross-platform determinism) but is chaotic: passing veh_L's 0.035f instead of 0.035 moves a
seed's reach by up to 0.1, so the tests pin the exact scale.

NavPlantConvergence.CheckpointSelectionOnJoltReach: at lr 2e-4 / 300 steps, selected every 30
steps on 8 validation seeds, held out over the gate's 20 seeds the dynamic-trained policy
reaches 52.7% and the point-mass-trained one 50.1% (+2.6, CI [+0.3, +4.7]); the seed 51.7%
(dynamic - seed +1.0, CI [-1.2, +3.4]). With 4 validation seeds: +3.8 [+0.9, +6.7] and +1.3
[-0.6, +3.3]. Recorded in NAV_TRAINING.md and the roadmap.

* nav: reach_on_plant scores seeds in parallel; lower-lr, denser-checkpoint sweep (P8b)

plant_reach_options::threads (0 = the hardware's threads) runs seeds at once, each in its own
world, sim_world and plant with its agents on the worker's thread. NavPlant.
ReachOnPlantIsRepeatable checks that parallel scores equal serial ones; 8 validation seeds now
score in about the time of one, so a 31-checkpoint selection run takes about 9 minutes.

The next P8b lever, lower peak rates with denser checkpoints (8 validation seeds, 31
checkpoints, held out on the gate's 20 seeds; the seed reaches 51.7%):
  1e-4 / 300 steps / every 10: dynamic 53.5%, point mass 51.1%; +2.4 [+0.3, +4.4], dynamic -
    seed +1.9 [-0.8, +4.4]
  5e-5 / 600 / every 20: 50.5% vs 48.9%; +1.7 [-1.2, +4.5], -1.1 [-4.0, +1.6]
  2e-4 / 150 / every 5: 51.7% vs 50.5%; +1.1 [-0.8, +3.0], +0.0 [-1.9, +2.1]
No setting beats the seed outside the CI. At the lower rates validation reach stays flat and
denser checkpoints mostly add selection noise, so the next levers act on the signal: more
validation seeds, or Jolt reach as the training signal. Recorded in NAV_TRAINING.md and the
roadmap.

* nav_dyn_bike_test: gate the closed-loop kinematic limit by its medians (fixes macOS)

DynBike.KinematicLimitFollowsBikeStep failed on macOS (#717's CI, and master since #714): its
32 routes come from sample_starts_goals, whose uniform_int_distribution differs by standard
library, and under the controller (dyn_bike_step) the worst agent parted by 1.3 against 0.017
here. Over 40 sets of routes, 0 to 11 of 32 agents part by more than 0.2 rr in closed loop (the
controller amplifies the plant's small differences along some routes), while the median gap is
at most 0.0081 and the median end-distance difference at most 0.41%. The closed loop is now
gated by those medians (0.1 rr, 2%) and its worst agent reported; the open-loop gate, at most
0.0151 over the 40 sets, moves from 0.1 rr to 0.2 rr.

* test(state): poll pump_cycles instead of a fixed 50ms sleep

StateSessionStatusTest.StatusReportsPumpCycles joined a session with
pump_interval_ms = 5, slept a fixed 50ms and then expected
pump_cycles > 0. On 2026-10-19 it failed on recipe / windows-2022 /
Release of #717 (job 118144550758), a PR that does not touch state:

    state_session_status_test.cpp(53): error: Expected:
      (s.pump_cycles) > (0u), actual: 0 vs 0
    [  FAILED  ] StateSessionStatusTest.StatusReportsPumpCycles (1002 ms)

The assertion fired ~220ms after [ RUN ], and stop() then took another
~560ms to join the pump thread: under ctest -j load the pump thread had
not finished its first cycle. Nothing is wrong with the pump; the window
is just too short to guarantee one scheduling slice.

Poll session->status().pump_cycles until it is > 0, with a 5s
steady_clock deadline, the same remedy cc45c55 applied to
ConflictAutoPublish. The common case still leaves within a few
milliseconds, and a timeout now says the pump completed no cycle in 5s.

The file's other tests do not depend on wall-clock timing:
StatusReportsRunning and the accessor tests read state set before
join() returns, StatusReportsLocalSequence drives pump_all() itself,
WaitForDataTimeoutReturns asserts nothing about the result, and
ConflictAutoPublish already polls (cc45c55).

Verified on Windows (MSVC 19.38, Release, deps from a local cvcpkg
prefix), on a box already at 100% CPU from other work, running 16
copies of the test binary at once:
  * old test: 1 failure in 16 x 50 runs, the same "actual: 0 vs 0";
  * new test: 0 failures in 16 x 50 + 16 x 150 runs (3200); median 30ms,
    p99 114ms, max 419ms per test including stop();
  * full suite 8/8, and ctest -R StateSessionStatusTest -j 8
    --repeat until-fail:5 passes.

(cherry picked from commit 4933281)
@transfix
transfix deleted the feat/dyn-bike branch October 29, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant