Skip to content

dyn_bike, dyn_4w: the full (q, p) port-Hamiltonian form; the single track's side slope with gravity in H - #987

Merged
transfix merged 17 commits into
masterfrom
feat/dyn-ph-full
Nov 3, 2026
Merged

transfix merged 17 commits into
masterfrom
feat/dyn-ph-full

Conversation

@transfix

@transfix transfix commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

The vehicle plants' port-Hamiltonian form had only its momentum part: dyn_ph_rhs gave p' with the pose left out, and the single track took gravity as the power of a grade along its heading, given as data. With no slope across the heading, H carried no potential. This adds the full (q, p) form beside the momentum-only one for both plants, and the single track's slope across its heading with gravity as a potential in H. Everything is opt-in or additive; every existing output and golden is unchanged.

Follows #982 (wheel spin, merged as 4b64069) and #983 (contact, merged as 2728bca); it touches the same parts of dyn_bike.h. The diff against master is this PR's alone.

The full form (dyn_bike.h):

H(q, p) = 1/2 p^T M^-1 p + V(q),   V = m g h(centre of gravity)
q' = T(q) v,   p' = -T(q)^T dV/dq + (J(p) - R) v + G u + f_eps + f_g
T(q) = [[c, -s, lr s], [s, c, -lr c], [0, 0, 1]]
  • dyn_ph_T; dyn_ph_J_full = [[0, T], [-T^T, J(p)]]. It is skew and Poisson: T*SE(2)'s canonical structure in body momenta, with J(p) as its coadjoint block.
  • dyn_ph_rhs_full has two overloads:
    • with the slopes as data, f_g = -m g (grade, side, 0); its p' is dyn_ph_rhs's, to the bit, when side = 0;
    • on a ground, gravity is in H: -T^T dV/dq = -m g (along, across, 0). There is no moment, since V is the centre of gravity's.
  • Ground overloads take any at(x, y, mu, height) sampler (dyn_4w.h's):
    • dyn_ph_slope, dyn_ph_potential, dyn_ph_grad_q;
    • dyn_ph_hamiltonian (T + V);
    • dyn_ph_rates_at, whose grade line is -dV/dt = -m g (along vx + across vy).

The side slope.

  • dyn_plant gains an optional side (the slope across the heading). Gravity's body-y component, -m g side, joins the lateral solve's force.
  • dyn_plant_ground samples the ground under the centre of gravity at the step's start and applies gravity along and across the heading.
  • dyn_plant_books_ground books it with the grade line equal to -dV, the potential's change. dH stays the kinetic energy's, so dH - grade is the change of dyn_ph_hamiltonian(p, ground, .).
  • dyn_spin_plant_ground and dyn_spin_plant_books_ground do the same for the wheel-spin plant.

Sine or tangent. The slopes are the ground's gradient at the centre of gravity, along and across the heading: the tangents of the slope angles. The model's pose and twist are planar (projected), so the force whose work is -dV, with V = m g h(x, y), is -m g grad h at any angle. The sine is gravity's component along the surface for a speed measured along it. Over the projected pose it leaves m g (tan - sin), about m g θ³/2, unbooked, and a smaller step does not remove that (measured below). The existing path keeps nav's grade_at sine. The gradient is taken by central differences over 1e-3 (lf + lr), as dyn4_grade does.

dyn_4w had no side-slope gap: grade_force already applies dyn4_grade along and across the heading, and its terrain energy is in H. Its gap was the pose missing from the state.

  • dyn4_ph_T, dyn4_ph_coords_full (12 coordinates), dyn4_ph_grad_full, dyn4_ph_J_full, dyn4_ph_rhs_full.
  • -T^T dH/dg is dyn4_grade's force and moment, to round-off, so f_grade moves into the structure.
  • The plant's field with grade_force is dyn4_ph_rhs_full. Without grade_force it lacks -T^T dH/dg, so the vehicle gets the hill for free.

The live plant. dyn_bike_plant_options::side_slope (off by default) uses dyn_plant_books_ground and its wheel-spin twin on options.plant.ground_height. Its books' grade line becomes -dV. The driver still reads #869's sine along the heading.

Measured (MSVC 14.44 Release, Windows). New suite nav_dyn_ph_full_test, 8 tests:

Gate Result
DynPHFull.TheStructureIsSkewAndPoisson, 300 states on a hillside J_full exactly skew; e^T J e ≤ 1.1e-16 of its terms; Jacobi defect ≤ 3.1e-9 (central differences); dV/dq within 1.6e-9 m g of the exact gradient; -T^T dV/dq within 3.5e-17 of -m g (along, across, 0)
DynPHFull.ThePoseUpdateIsTv q1 - q0 = hdt T(q1) v1 to 1.8e-16 over 400 states (flat and hillside); a first-order step of q' = T(q) v: the error against T(q0) v0 is 0.176 → 0.087 → 0.043 → 0.021 for hdt 0.04 → 0.005
DynPHFull.TheVectorFieldIsTheFullFormsAtEverySlip, 400 states × mu 0.9/0.6, a 1e-5 s step worst q' 6.1e-5 of the speed, p' 1.65e-4 m g (ground and data overloads)
DynPHFull.SideSlopeBooksCloseWithGravityInH, closed loop across a 15% hillside with swells, the body slipping (up to 0.052 rad) gravity in H: mean residual per step 3.70 → 0.917 → 0.228 → 0.057 J (hdt 0.02 → 0.0025, ×4.0 per halving); |Σ|/flow 2.8e-4 → 2.8e-5. The same steps in the momentum-only books: |Σ|/flow 1.53e-2 → 1.58e-2 (does not shrink). Plant without the side slope, against −ΔV: 1.52e-3 → 1.17e-3. Slopes as sines: 2.05e-3 → 1.81e-3
DynPHFull.TheWheelSpinPlantTakesTheSlopeAcrossItsHeading mean residual 0.790 → 0.198 → 0.050 J (hdt 0.01 → 0.0025); |Σ|/flow ≤ 8.7e-5
Dyn4PHFull.TheStructureIsSkewAndPoissonAndTheGradeForceIsIts, 200 states skew; e^T J e ≤ 1.7e-16; Jacobi ≤ 9.1e-11; dH/dg within 1.6e-8 m g L of H's differences; -T^T dH/dg within 6.9e-17 of dyn4_grade
Dyn4PHFull.TheVectorFieldIsTheFullFormsWithTheGradeForce, 300 states worst 2.2e-4 of each row's scale in 12 coordinates; without grade_force the momenta miss up to 0.20 of theirs; pose step vs hdt T(q1) v1 1.6e-16
DynPHFull.Golden tests/data/dyn_ph_full_golden.json (CVC_DYN_PH_FULL_GOLDEN_WRITE=1), for GRL-SNAM's torch twin
DynBikePlant.SideSlopePullsDownhillAndBooksThePotential, 6 s across a 10% side slope, wheel straight centre of gravity 0.196 m downhill (0.187 with wheel spin); grade 515.187 J = −m g Δz exactly; residual 8.0e-5 of the flow. Off: drives straight, grade 0

Unchanged. DynBike.Golden, DynBikePH.Golden, the ext_rollout and dyn4_ext goldens, dyn_spin_ph, dyn4_spin_ph and dyn_contact all regenerate byte-identical. dyn4_golden.json passes its check at 1e-9; it is not rewritten here because of the 11 last-bit lines master's own build already drifts by (noted on #982).

GRL-SNAM's twin of the full form, with its parity test from this golden, is in CVC-Lab/GRL-SNAM#146 (with the wheel-spin and contact twins): values within 2.1e-14 relative, gradients within 3.2e-11.

dyn_spin_plant and dyn4_spin_plant give each wheel (each axle's, on the
single track) its own angular speed, driven and braked by torques with
the existing drive/brake split, and a Pacejka longitudinal tire force of
its slip ratio under the friction ellipse. The longitudinal step solves
(vx, om) implicitly with the tires and brakes linearized by their secants.

In pH form H gains 1/2 sum J om^2, R gains K d d^T with K = F_x / sigma
>= 0 along d = (-t, r_w e_wheel) (the patch's slip velocity), and the port
is (wheel torque, wheel speed). dyn_spin_plant_books and
dyn4_spin_plant_books add slip and braked lines.

dyn_bike_plant gets options.wheel_spin. dyn_plant, dyn4_plant and their
forms are unchanged by default: DynBike.Golden, DynBikePH.*, Dyn4.Golden,
Dyn4PH.* and Dyn4ExtRollout.Golden pass unchanged.
…tial and input options

plant_log records each Jolt vehicle's wheel speeds (the single track's
two axles' means and their rims' surface speeds), the pedals, and the
ledger's shaft work and brake energy per tick. cut_flights takes an
opt-in flight_layout::wide row (18 columns: attitude, wheels, pedals,
shaft and brake power); the planar 8-column layout stays the default.
cvc plant-log --wide writes it, with meta.json naming the columns.

cvc::ph::se2: se2_setup::potential world (V(x, y), the default), none,
or terrain (the logged grade and roll times two learned coefficients:
the gradient of m g z read where the vehicle is). Non-world models are
cvc-ph-se2/3. se2_flights carries the column names; select_inputs picks
the inputs by name (e.g. the wheels' rim speeds as ports). cvc ph-train
gains --inputs, --potential and --eval-runs, and reports the mean model's
clean rollout loss on train, held-out seeds and the eval runs.
… slip pointer

write_plant_log wrote the bundle path into meta.json unescaped, so a run on
a Windows bundle path could not be read back (by load_plant_log_runs or any
JSON reader). dyn4_ph_parts_at fills the slips only when given somewhere to
put them.
…rm reads it

The body's grade and roll carry the suspension's pitch and roll under
acceleration, so a terrain term fitted to them on flat ground learns the
acceleration, not the ground, and blows up on real slopes. planar_sample
gains ground_grade and ground_roll from the wheels' contact points (the
two axles' and the two sides' mean contact heights); the wide row gains
them (20 columns), and cvc ph-train --potential terrain reads them where
the runs have them. On flat ground the body tilts up to 0.027 while the
ground slope stays 0.
…te windows

An input with no variation in training (the ground's slope on flat
ground) had a scale of zero, so every window of a run where it moves
diverged and the reported loss averaged none of them. It now keeps a
unit scale, and each final loss says how many windows stayed finite.
… keeps contact windows

The dynamic plant had no world to collide with. dyn_contact.h samples the
vehicle's footprint (chassis box and wheels about the centre of gravity)
and, against a field's signed distance and between vehicles, gives each
point a barrier energy psi = 1/2 k (s - d)^2, a Hunt-Crossley damper on
the normal velocity and smoothed Coulomb friction along the obstacle. The
barrier is minus its energy's gradient in the pose (H, through T*SE(2)'s
canonical structure); the damper and friction are R_c, PSD. Each point's
force reaches the body through the external-port map; dyn_plant and
dyn_spin_plant take the wrench as a port held over the step, and their
books book its energy.

dyn_bike_plant_options::contact (off by default): a contact field
callback (sdf_field builds one from nav's SDF grid; sim_world::field()
exposes sim_world's own), vehicle-vehicle contact, and books lines
contact_energy, contact_stored, contact_dissipated, contact_exchange.

cut_flights(..., keep_contact) and cvc plant-log --keep-contact keep the
contact windows with a contact feature column.

Measured (physics_dyn_contact_test): the barrier within 5e-10 of
-dPhi/dpose; books second order (residual per step 3.23 -> 0.70 -> 0.16
J at 0.01/0.005/0.0025 s, a wall impact); against Jolt at 5 m/s: rebound
0.19 vs 0.00, a parked Humvee pushed to 2.88 vs 2.28 m/s.
… the corner radius held

The contact golden's wall cases sat 0.3-0.65 m off the wall against a
0.05 m skin, so they pinned nothing; every case now has points in the
skin (checked). The step's vehicle parameters are written into the
golden so a twin needs no other file. dyn_rounded_box_sdf holds its
corner radius to the half width and half length; dyn_ph_books' comment
says where the port's energy goes.
…ope, gravity in H

The form had only its momentum part. dyn_ph_T and dyn_ph_rhs_full give
q' = T(q) v and p' = -T(q)^T dV/dq + (J(p) - R) v + G u + f_eps + f_g, the
structure [[0, T], [-T^T, J(p)]] skew and Poisson. On a ground (any
at(x, y, mu, height) sampler) the potential m g h(centre of gravity) is in H,
and dyn_ph_hamiltonian, dyn_ph_rates_at and the books take it.

dyn_plant takes an optional slope across the heading (gravity's body-y
component on the lateral solve). dyn_plant_ground and dyn_plant_books_ground
(and their wheel-spin twins) apply gravity along and across the heading from
the ground's gradient (the tangents: the planar model's potential) and book
the grade line as -dV. dyn_bike_plant_options::side_slope turns it on in the
live plant.

dyn_4w had no side-slope gap (grade_force is -T^T dH/dg to round-off); its
full form adds the pose: dyn4_ph_T, dyn4_ph_grad_full, dyn4_ph_J_full,
dyn4_ph_rhs_full, the plant's field with grade_force.

nav_dyn_ph_full_test: the structure (skew, Jacobi), the pose update
q1 = q0 + hdt T(q1) v1, the full vector field, closed-loop side-slope books
(second order with gravity in H; the momentum-only books drift), and
DynPHFull.Golden for GRL-SNAM's twin. Every path is opt-in: the existing
goldens regenerate byte-identical.
Q = -R dH/dx + G u + f_eps - m g grade e_x on the five momenta, so
dyn_spin_ph_rhs is J dH/dx + Q and dH/dx^T Q is dyn_spin_ph_rates_at's
balance: the hook a discrete-gradient step evaluates at its midpoint.
DynSpinPH.TheForcesAreTheFieldLessTheStructure checks both at 500 states
(2.1e-15 of m g, 1.3e-14 of the terms).
An optional trailing wrench (F_x, F_y, M_z), as dyn_plant_core's, joins Q on
the body's momenta, so a discrete-gradient step takes the contact as the
plant does. DynSpinPH.TheForcesAreTheFieldLessTheStructure checks it adds
exactly W on the body and nothing on the wheels.
@transfix
transfix changed the base branch from feat/dyn-contact to master November 3, 2026 02:57
@transfix
transfix enabled auto-merge (squash) November 3, 2026 04:32
@transfix
transfix merged commit b89992e into master Nov 3, 2026
11 of 28 checks passed
@transfix
transfix deleted the feat/dyn-ph-full branch November 3, 2026 05:20
transfix added a commit that referenced this pull request Nov 3, 2026
… ground (#1002)

pycvc.dyn_bike_plant takes the plant's opt-in options from #982, #983 and #987:
wheel_spin; side_slope; and contact, with contact_field (a sim_world handle,
whose signed distance on contact_plane is copied as it is now), contact_vehicles
and contact_params (a dict of dyn_contact_params' fields). ground, a
pycvc.height_field in world metres (copied), gives the plant a ground height,
which the vehicle climbs and the side slope reads; before, Python had only a flat
ground_z. contact with a world raises, as do contact_field without contact and a
contact setting that does not exist.

The books gain slip, contact_energy, contact_stored, contact_dissipated and
contact_exchange. options() reports the new settings. wheel_speed(i, axle) and
contact_now(i) (the barrier's energy, the damper's power, the points touching and
the body wrench) read an agent.

test_pycvc_physics.py's test_the_dynamic_plant_options:
- wheel spin on grip 0.3 books 1.75 kJ of slip, and the axles' wheels part;
- a 5% ramp costs 51.5 kJ;
- a 10% side slope pulls the vehicle 0.23 m downhill, where the plain ground
  leaves it on its line;
- driven at a wall in another sim_world's map, it stops with its rear axle
  4.55 m short, pressed with 4 points at 23.5 kN, and the damping books 81.6 kJ.
Every run's books close to 1e-9 of the flow.

The contact run uses a 0.005 s substep. At the default 0.01 s the damper, held
over the substep, overshoots this 7.8 m/s impact: the books show
contact_dissipated at -1.5e5 J (the contact supplying energy), the limiter takes
2.4e5 J, and the residual is 3.1e-2 of the flow. At 0.005, 0.0025 and 0.00125 s
the residual is 2.4e-3, 1.3e-3 and 2.6e-4 of the flow. physics_dyn_contact_test
impacts at 5 m/s only.
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