
## 1. Browse the integrated_servo models as a ladder
>>> from family_records import families    # robot_models.family once landed
>>> servo = families()["integrated_servo"]
>>> print(servo.tree())
integrated_servo: A joint in one housing: a motor, its drive, a reducer or none, and an encoder.
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
* runnable here: 6 of 14 (physics attached for M0-M3 only); the rest are declared records
>>> servo.children("M2.coulomb_viscous")
('M3.stribeck', 'M3.load', 'M4.ripple')

## 2. Pick M3.stribeck: parameters, units, equation, references, fallback; resolve an alias
>>> stribeck = servo["M3.stribeck"]
>>> print(stribeck)
M3.stribeck  Stribeck friction: breakaway falling to Coulomb with speed
  base      M2.coulomb_viscous
  inherits  output_inertia [kg·m²], gear_ratio, coulomb_friction [N·m], viscous_friction [N·m·s/rad]
  adds      breakaway_torque [N·m]: Static friction at the output: the torque that starts it moving from rest, the top of the Stribeck curve.
            stribeck_velocity [rad/s]: Output speed over which friction falls from breakaway_torque to coulomb_friction.
  optional  stribeck_exponent (+31 inherited, read when stated)
  equation  tau_r = (coulomb_friction + (breakaway_torque − coulomb_friction)·exp(−(|v|/stribeck_velocity)^stribeck_exponent))·sign(v) + viscous_friction·v
  refs      Armstrong-Hélouvry, Dupont, Canudas de Wit (1994)… https://doi.org/10.1016/0005-1098(94)90209-7
            Olsson, Åström, Canudas de Wit, Gäfvert, Lischinsky (1998)… https://doi.org/10.1016/S0947-3580(98)70113-X
  physics   actuators/rungs/stribeck/model.py: torque(v [rad/s]) -> tau_r [N·m]; a fit varies coulomb_friction, viscous_friction, breakaway_torque, stribeck_velocity
>>> servo.fallback("M3.stribeck")
('M3.stribeck', 'M2.coulomb_viscous', 'M1.datasheet', 'M0.ideal')
>>> servo.canonical("frictionloss_nm")
'coulomb_friction'

## 3. The datasheet model (M1) from the datasheet: the resisting torque at 1 rad/s
>>> from fixture import datasheet, rig_log
>>> values = servo.values(datasheet())
>>> values
Values(integrated_servo, 7 stated, SI):
  peak_torque        3.5          N·m
  rated_torque       1.05         N·m
  max_output_speed   24.0855      rad/s
  coulomb_friction   0.3          N·m
  viscous_friction   0.02         N·m·s/rad
  gear_ratio         10
  output_inertia     4.2e-05      kg·m²
>>> m1 = servo["M1.datasheet"].bind(values)
>>> m1(v=1.0)
0.32
>>> m1(v=np.array([-1.0, 0.0, 1.0]))
array([-0.32,  0.  ,  0.32])

## 4. Fit M2.coulomb_viscous and M3.stribeck on the fit plateaus only (1, 3, 5, 7, 9)
>>> case
'A'
>>> fit_log, test_log = rig_log(case, "fit"), rig_log(case, "test")
>>> m2 = servo["M2.coulomb_viscous"].fit(fit_log["tau"], v=fit_log["v"], values=values)
>>> m3 = stribeck.fit(fit_log["tau"], v=fit_log["v"], values=values)
>>> print(m3)
M3.stribeck fitted on 250 samples, rms 0.003953
  output_inertia     4.2e-05    kg·m²      given
  gear_ratio         10                    given
  coulomb_friction   0.41946    N·m        fitted
  viscous_friction   0.035285   N·m·s/rad  fitted
  breakaway_torque   0.65005    N·m        fitted
  stribeck_velocity  0.24901    rad/s      fitted
  + 3 optional values it reads: peak_torque, rated_torque, max_output_speed

## 5. Compare them on the held-out plateaus (2, 4, 6, 8, 10): the lowest rung that reaches the noise floor
>>> verdict = servo.compare(m1, m2, m3, observed=test_log["tau"], repeats=test_log["plateau"], v=test_log["v"])
>>> verdict
noise floor 0.004171 N·m (pooled within-repeat std of the held-out log)
model                fitted   fit rms  held-out rms  ÷ floor
M1.datasheet              0         —         0.222    53.22
M2.coulomb_viscous        2   0.06469       0.08458    20.28
M3.stribeck               4  0.003953      0.004185     1.00
chosen: M3.stribeck, the lowest rung within 1.5× the floor
>>> verdict.chosen
'M3.stribeck'

## 6. The negative control, servo B: the same calls, and the simpler rung wins
>>> fit_b, test_b = rig_log("B", "fit"), rig_log("B", "test")
>>> m2_b = servo["M2.coulomb_viscous"].fit(fit_b["tau"], v=fit_b["v"], values=values)
>>> m3_b = stribeck.fit(fit_b["tau"], v=fit_b["v"], values=values)
>>> round(m3_b.values["breakaway_torque"], 2), round(m3_b.values["stribeck_velocity"], 4)   # a knee chasing noise
(-4.73, 0.0107)
>>> servo.compare(m2_b, m3_b, observed=test_b["tau"], repeats=test_b["plateau"], v=test_b["v"])
noise floor 0.00422 N·m (pooled within-repeat std of the held-out log)
model                fitted   fit rms  held-out rms  ÷ floor
M2.coulomb_viscous        2  0.004037      0.004326     1.03
M3.stribeck               4   0.00398      0.004395     1.04
chosen: M2.coulomb_viscous, the lowest rung within 1.5× the floor

## 7. Hand on the chosen one: MuJoCo's joint attributes, a vectorized call; a declared rung says it does not run
>>> best = verdict.best
>>> best.mujoco()
{'armature': 4.2e-05, 'frictionloss': 0.6500489938172218, 'damping': 0.03528455268066556}
>>> best(v=np.array([0.05, 0.5, 2.0])).round(4)
array([0.6427, 0.4412, 0.49  ])
>>> servo["M4.drivetrain"].fit(fit_log["tau"], v=fit_log["v"], values=values)  # -> NotImplementedError
NotImplementedError: M4.drivetrain is declared, not runnable here: physics is attached for M0-M3 only (the round's stop line)
