
# Step 1 · Browse the integrated_servo models as a ladder
>>> from estimators import Model, families
>>> servo = families()["integrated_servo"]       # the type's models, organized as its ladder: the climb's search order
>>> print(servo)
integrated_servo: 14 models (a → b: b extends a; ° declared, not run here)
M0.ideal → M1.datasheet → M2.coulomb_viscous
├── M3.stribeck
│   ├── M3.lugre → M5.dcmotor°
│   └── M3.weakening_capture° → M4.drivetrain° → M5.envelope° → M5.wear° → M6.residual°
├── M3.load
└── M4.ripple°
M7.learned°
>>> ladder = list(servo.models())                # every key, in today's order: each one an unfitted Model

# Step 2 · Pick M3.stribeck: what it needs, means and falls back on; resolve an alias
>>> stribeck = servo.model("M3.stribeck")
>>> print(stribeck.describe())
M3.stribeck  Stribeck friction: breakaway falling to Coulomb with speed
  falls back  M3.stribeck → M2.coulomb_viscous → M1.datasheet → M0.ideal   (siblings: M3.load, M4.ripple)
  needs       output_inertia [kg·m²], gear_ratio [1], coulomb_friction [N·m], viscous_friction [N·m·s/rad], breakaway_torque [N·m], stribeck_velocity [rad/s]
  fits        coulomb_friction, viscous_friction, breakaway_torque, stribeck_velocity
  optional    stribeck_exponent [1] (+31 datasheet fields below it, read when stated)
  equation    tau_r = (coulomb_friction + (breakaway_torque − coulomb_friction)·exp(−(|v|/stribeck_velocity)^stribeck_exponent))·sign(v) + viscous_friction·v
  reference   Armstrong-Hélouvry, Dupont, Canudas de Wit (1994). A survey of models, analysis tools and compensation methods for the control of machines with friction. Automatica.
  reference   Olsson, Åström, Canudas de Wit, Gäfvert, Lischinsky (1998). Friction models and friction compensation. European Journal of Control.
  physics     actuators/rungs/stribeck/model.py: tau_r(v)
>>> servo.canonical("frictionloss_nm")                               # the rack twin's name for the quantity
'coulomb_friction'
>>> servo.model("M3.stribeck", frictionloss_nm=0.3).get_params()     # set by an alias, held by the canonical name
{'coulomb_friction': 0.3}

# Step 3 · The datasheet model: M1 from the stated values, then predict
>>> from fixture import datasheet, rig_log
>>> ds = datasheet()                              # the catalogue's units: rpm, kg·cm², mN·m·s/rad
>>> m1 = Model.from_datasheet(ds)                 # M1.datasheet by default: every stated value it holds, in SI
>>> print(m1)
M1.datasheet: not fitted
  stated   output_inertia 4.2e-05 kg·m², gear_ratio 10, peak_torque 3.5 N·m, rated_torque 1.05 N·m, max_output_speed 24.09 rad/s, coulomb_friction 0.3 N·m, viscous_friction 0.02 N·m·s/rad
>>> m1.predict(1.0)                               # tau_r at 1 rad/s [N·m]
0.32

# Step 4 · Fit M2 and M3 on the fit plateaus only
>>> fit, test = rig_log("A", "fit"), rig_log("A", "test")   # plateaus 1, 3, 5, 7, 9 to fit; 2, 4, 6, 8, 10 held out
>>> m2 = Model.from_datasheet(ds, "M2.coulomb_viscous").fit(fit)
>>> m3 = Model.from_datasheet(ds, "M3.stribeck").fit(fit)    # starts from the datasheet; the rest is scanned
>>> print(m2)
M2.coulomb_viscous: fitted on 250 samples, rms 0.0647 N·m
  fitted   coulomb_friction 0.5797 N·m
  fitted   viscous_friction -0.06487 N·m·s/rad
  stated   output_inertia 4.2e-05 kg·m², gear_ratio 10, peak_torque 3.5 N·m, rated_torque 1.05 N·m, max_output_speed 24.09 rad/s
>>> print(m3)
M3.stribeck: fitted on 250 samples, rms 0.00395 N·m
  fitted   coulomb_friction 0.4195 N·m
  fitted   viscous_friction 0.03528 N·m·s/rad
  fitted   breakaway_torque 0.65 N·m
  fitted   stribeck_velocity 0.249 rad/s
  stated   output_inertia 4.2e-05 kg·m², gear_ratio 10, peak_torque 3.5 N·m, rated_torque 1.05 N·m, max_output_speed 24.09 rad/s

# Step 5 · Compare on the held-out plateaus: climb to the lowest rung that reaches the noise floor
>>> report = servo.climb(ds, fit, test)           # fit each rung on `fit`, judge it on `test`, stop at the floor
>>> print(report)
climb integrated_servo: fit on 250 samples, judged on 250 held out; rms in N·m, noise floor 0.00417; climb while held out > 1.5× floor
  rung                   k  fit rms held out ÷ floor  verdict
  M1.datasheet           0  0.24200  0.22197   53.22  start: the datasheet, nothing fitted
  M2.coulomb_viscous     2  0.06469  0.08458   20.28  lowest held-out → climb
  M3.stribeck            4  0.00395  0.00418    1.00  lowest held-out → climb
  M3.load                ·        ·        ·       ·  skipped: the log has no tau_load
  M4.ripple              ·        ·        ·       ·  skipped: declared, above the M0–M3 stop line
  M3.lugre               ·        ·        ·       ·  skipped: the log has no dt
  M3.weakening_capture   ·        ·        ·       ·  skipped: declared, no physics shipped
chosen M3.stribeck: the lowest rung within 1.5× the noise floor on held-out plateaus
>>> report.chosen
<Model M3.stribeck: 9 values, 4 fitted on 250 samples>

# Step 6 · The negative control: a second servo, where the simpler rung must win
>>> fit_b, test_b = rig_log("B", "fit"), rig_log("B", "test")   # same rig, a plain Coulomb + viscous servo
>>> print(servo.climb(ds, fit_b, test_b))         # M3 fits its own plateaus a hair better, and loses on the held-out ones
climb integrated_servo: fit on 250 samples, judged on 250 held out; rms in N·m, noise floor 0.00422; climb while held out > 1.5× floor
  rung                   k  fit rms held out ÷ floor  verdict
  M1.datasheet           0  0.08662  0.09128   21.63  start: the datasheet, nothing fitted
  M2.coulomb_viscous     2  0.00404  0.00433    1.03  lowest held-out → climb
  M3.stribeck            4  0.00397  0.00662    1.57  not needed: M2.coulomb_viscous reaches the floor
  M3.load                ·        ·        ·       ·  skipped: the log has no tau_load
  M4.ripple              ·        ·        ·       ·  skipped: declared, above the M0–M3 stop line
chosen M2.coulomb_viscous: the lowest rung within 1.5× the noise floor on held-out plateaus

# run() returns: chosen M3.stribeck; held-out rms M2 0.0846, M3 0.0042 N·m
