
# 1a. Name the type and take its kit: its models, datasheet class, experiments and fidelity metric in one object
>>> import numpy as np
>>> from type_kit import types
>>> from fixture import datasheet, rig_log
>>> servo = types.of(datasheet())       # Hugging Face AutoModel's move: the datasheet's class names its type
>>> servo                               # types.integrated_servo is the same kit
<integrated_servo kit> A joint in one housing: a motor, its drive, a reducer or none, and an encoder.
  models       14 on the ladder: 6 runs, 1 structure only, 7 declared
  datasheet    IntegratedServo, 35 typed fields
  experiments  chirps, reading_datasheet, velocity_plateaus, slow_plateaus, pendulum_sweeps, small_reversals, rotor_angle_sweep, measure_only_board_calibration
  fidelity     tau_r_rms on held-out plateaus over the rig floor

# 1b. Its models are its ladder; each names the rig motion that identifies what it adds (robotparts' measured_by)
>>> print(servo.models)
M0.ideal                              runs            chirps, reading_datasheet
└─ M1.datasheet                       runs            reading_datasheet
   └─ M2.coulomb_viscous              runs            velocity_plateaus
      ├─ M3.stribeck                  runs            slow_plateaus
      │  ├─ M3.lugre                  runs            small_reversals
      │  │  └─ M5.dcmotor             declared        measure_only_board_calibration
      │  └─ M3.weakening_capture      structure only  -
      │     └─ M4.drivetrain          declared        -
      │        └─ M5.envelope         declared        -
      │           └─ M5.wear          declared        -
      │              └─ M6.residual   declared        -
      ├─ M3.load                      runs            pendulum_sweeps
      └─ M4.ripple                    declared        rotor_angle_sweep
M7.learned                            declared        -

# 2. Pick M3.stribeck: what it needs in what unit, the experiment identifying each, equation, references, fallback
>>> stribeck = servo.models["M3.stribeck"]
>>> print(stribeck)
M3.stribeck  Stribeck friction: breakaway falling to Coulomb with speed
  identified by
    chirps              output_inertia [kg·m²]
    reading_datasheet   gear_ratio []
    velocity_plateaus   coulomb_friction [N·m], viscous_friction [N·m·s/rad]
    slow_plateaus       breakaway_torque [N·m], stribeck_velocity [rad/s], stribeck_exponent [] (optional, 2 when absent)
  equation    tau_r = (coulomb_friction + (breakaway_torque − coulomb_friction)·exp(−(|v|/stribeck_velocity)^stribeck_exponent))·sign(v) + viscous_friction·v
  references  Armstrong-Hélouvry, Dupont, Canudas de Wit (1994); Olsson, Åström, Canudas de Wit, Gäfvert, Lischinsky (1998)
  fallback    M3.stribeck -> M2.coulomb_viscous -> M1.datasheet -> M0.ideal
  physics     actuators/rungs/stribeck/model.py (runs)
>>> [model.key for model in stribeck.fallback]
['M3.stribeck', 'M2.coulomb_viscous', 'M1.datasheet', 'M0.ideal']
>>> servo.canonical("frictionloss_nm")  # one vocabulary per type: the rack twin's name resolves to the type's own
'coulomb_friction'

# 3. The datasheet model: M1.datasheet holding the stated values in SI, then its resisting torque at 1 rad/s
>>> m1 = servo.from_datasheet(datasheet())
>>> {name: m1.values[name] for name in ("coulomb_friction", "viscous_friction", "max_output_speed")}
{'coulomb_friction': 0.3, 'viscous_friction': 0.02, 'max_output_speed': 24.085543677521745}
>>> m1.predict(1.0)
0.32

# 4. The rig log, split by plateau: fit on plateaus 1, 3, 5, 7, 9; hold 2, 4, 6, 8, 10 out. Identify on the fit half
>>> case                                # the servo on the rig (check_scenario.py replays this session on "B" too)
'A'
>>> fit, held_out = rig_log(case, "fit"), rig_log(case, "test")
>>> servo.experiments.covered_by(fit)
('velocity_plateaus', 'slow_plateaus')
>>> found = servo.identify(fit, datasheet=datasheet())
>>> print(found)
  M0.ideal            the datasheet's 2 stated values
  M1.datasheet        the datasheet's 7 stated values
  M2.coulomb_viscous  coulomb_friction=0.5797, viscous_friction=-0.06487
  M3.stribeck         coulomb_friction=0.4195, viscous_friction=0.03528, breakaway_torque=0.65, stribeck_velocity=0.249
  not yet: M3.load: run pendulum_sweeps
  not yet: M3.lugre: run small_reversals
  not yet: M3.weakening_capture: structure only: no published physics (the type holds its terms)
  not yet: M4.ripple, M4.drivetrain, M5.dcmotor, M5.envelope, M5.wear, M6.residual, M7.learned: declared: this round runs M0-M3 only

# 5. Choose with the type's fidelity metric, on held-out plateaus only: the lowest rung within 1.5x the rig's floor
>>> servo.compare(found, fit)           # refused: a rung that nests another always fits its own plateaus as well
ValueError: the fit saw 10 of these plateaus: choose on plateaus it never saw
>>> comparison = servo.compare(found, held_out)
>>> print(comparison)
  model                fit x floor  held-out rms N·m   floor  x floor  twinned (<= 1.5x)
  M0.ideal                  135.90            0.5319  0.0042   127.54  no
  M1.datasheet               60.49            0.2220  0.0042    53.22  no
  M2.coulomb_viscous         16.17            0.0846  0.0042    20.28  no
  M3.stribeck                 0.99            0.0042  0.0042     1.00  yes
  chosen: M3.stribeck, the lowest rung the rig cannot tell from the part on plateaus the fit never saw

# 5b. The negative control: a second servo through the same calls; M3 fits its plateaus best but not the held-out ones
>>> found_b = servo.identify(rig_log("B", "fit"), datasheet=datasheet())
>>> print(servo.compare(found_b, rig_log("B", "test")))
  model                fit x floor  held-out rms N·m   floor  x floor  twinned (<= 1.5x)
  M0.ideal                   99.57            0.4120  0.0042    97.64  no
  M1.datasheet               21.57            0.0913  0.0042    21.63  no
  M2.coulomb_viscous          1.01            0.0043  0.0042     1.03  yes
  M3.stribeck                 0.99            0.0066  0.0042     1.57  no
  chosen: M2.coulomb_viscous, the lowest rung the rig cannot tell from the part on plateaus the fit never saw

# 6. What now: hand the chosen model on (SI, numpy in and out); the kit names the motion that unlocks the next rung
>>> chosen = comparison.chosen
>>> dict(chosen.values)
{'output_inertia': 4.2e-05, 'gear_ratio': 10.0, 'coulomb_friction': 0.4194633038475971, 'viscous_friction': 0.035284565922791986, 'breakaway_torque': 0.6500489870334133, 'stribeck_velocity': 0.24901459937475945}
>>> chosen.predict(np.array([-1.0, -0.1, 0.1, 1.0]))
array([-0.4547, -0.6192,  0.6192,  0.4547])
>>> found.missing["M3.lugre"]
'run small_reversals'
>>> print(servo.experiments["small_reversals"])
small_reversals: "small reversals, recorded fast"
  identifies sigma0, sigma1
  on M3.lugre
