Spaces:
Sleeping
Sleeping
File size: 10,909 Bytes
5f38e95 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | """Phase 7 — three exclusive cable constitutive laws.
Verifies that:
* each mode can be selected independently via the factory,
* the factory rejects unknown / mixed names (no cross-contamination),
* Kelvin--Voigt reduces to the elastic spring at zero velocity, to a
pure damper at zero stretch, and clips to zero on slack,
* Irvine produces the same scalar tension as the per-cable
:func:`sagging_cable` helper for the same inputs,
* SQCK hybrid reduces to Irvine at zero velocity (no damping
contribution) and matches the analytic formula otherwise,
* the simulator's ``cable_model`` argument propagates the mode through
to the resulting :class:`SimulationResult`.
"""
from __future__ import annotations
import numpy as np
import pytest
from cdpr.cables import (
IrvineModel,
KelvinVoigtModel,
SQCKHybridModel,
available_modes,
cable_model_by_name,
sagging_cable,
)
from cdpr.core.frames import Pose, Twist
from cdpr.dynamics.rigid_body import PlatformState
from cdpr.dynamics.simulator import simulate
from cdpr.kinematics.inverse import cable_lengths
# ---------------------------------------------------------------------------
# Factory exclusivity
# ---------------------------------------------------------------------------
def test_available_modes_exact_set():
assert set(available_modes()) == {"kelvin_voigt", "irvine", "sqck_hybrid"}
def test_factory_builds_each_mode():
kv = cable_model_by_name("kelvin_voigt")
irv = cable_model_by_name("irvine")
hyb = cable_model_by_name("sqck_hybrid")
assert kv.mode_name == "kelvin_voigt"
assert irv.mode_name == "irvine"
assert hyb.mode_name == "sqck_hybrid"
def test_factory_rejects_unknown_name():
with pytest.raises(ValueError):
cable_model_by_name("super_hybrid")
with pytest.raises(ValueError):
cable_model_by_name("kelvinvoigt") # no underscore
def test_factory_rejects_unknown_parameters():
with pytest.raises(TypeError):
cable_model_by_name("kelvin_voigt", blender_spin=42)
# ---------------------------------------------------------------------------
# Kelvin--Voigt physics
# ---------------------------------------------------------------------------
def _zero_state(home_pose):
return PlatformState(pose=home_pose, velocity=Twist(np.zeros(6)))
def test_kv_elastic_only_at_zero_velocity(ipanema, home_pose):
"""At zero velocity, T = k * delta_L = (EA/L0) * delta_L."""
model = KelvinVoigtModel(youngs_modulus=1e9, cross_section=1e-5,
viscous_coefficient=0.0)
state = _zero_state(home_pose)
L = cable_lengths(home_pose, ipanema)
# Apply a 1% rest-length compression (rest < actual) -> tensile stretch.
rest = L * 0.99
T = model.tension(ipanema, state, rest)
expected = (1e9 * 1e-5 / rest) * (L - rest)
assert np.allclose(T, expected, rtol=1e-9)
def test_kv_slack_clips_to_zero(ipanema, home_pose):
"""When L < L0 (compression) and no inward velocity, T = 0."""
model = KelvinVoigtModel(youngs_modulus=1e9, cross_section=1e-5,
viscous_coefficient=1e6)
state = _zero_state(home_pose)
L = cable_lengths(home_pose, ipanema)
rest = L * 1.05 # rest > actual -> slack
T = model.tension(ipanema, state, rest)
assert np.all(T == 0.0)
def test_kv_damping_only_at_zero_stretch(ipanema, home_pose):
"""At delta_L = 0 (rest == actual length), T = c * dL/dt."""
eta = 5e7
A = 1e-5
model = KelvinVoigtModel(youngs_modulus=0.0, cross_section=A,
viscous_coefficient=eta)
L = cable_lengths(home_pose, ipanema)
# Move along +x at 0.1 m/s --- some cables will see positive dL/dt
# (moving away from anchor) and others negative.
state = PlatformState(
pose=home_pose,
velocity=Twist.from_parts([0.1, 0.0, 0.0], np.zeros(3)),
)
rest = L.copy() # no stretch
T = model.tension(ipanema, state, rest)
# Reconstruct expected damping term from internals.
c = eta * A / rest
dL_dt = model._length_rates(ipanema, state)
expected = np.maximum(c * dL_dt, 0.0)
assert np.allclose(T, expected, rtol=1e-9)
def test_kv_tension_jacobian_zero_when_slack(ipanema, home_pose):
model = KelvinVoigtModel(viscous_coefficient=0.0)
state = _zero_state(home_pose)
L = cable_lengths(home_pose, ipanema)
rest = L * 1.1 # slack
jac = model.tension_jacobian(ipanema, state, rest)
assert np.all(jac["dT_dDeltaL"] == 0.0)
assert np.all(jac["dT_dLdot"] == 0.0)
# ---------------------------------------------------------------------------
# Irvine physics
# ---------------------------------------------------------------------------
def test_irvine_matches_per_cable_solver(ipanema, home_pose):
"""The class-based IrvineModel must agree, per-cable, with
:func:`sagging_cable` on the same numerical inputs."""
model = IrvineModel(linear_density=0.07, youngs_modulus=1.1e11,
cross_section=1.26e-5)
state = _zero_state(home_pose)
L = cable_lengths(home_pose, ipanema)
rest = L * 0.99 # taut
T_model = model.tension(ipanema, state, rest)
# Direct call to the per-cable helper.
b_world = home_pose.rotation.apply(ipanema.attachments) + home_pose.position
expected = np.empty(ipanema.n_cables)
for i in range(ipanema.n_cables):
sol = sagging_cable(
anchor_upper=ipanema.anchors[i],
anchor_lower=b_world[i],
unstretched_length=float(rest[i]),
axial_stiffness=1.1e11 * 1.26e-5,
linear_weight=0.07 * 9.81,
)
expected[i] = 0.0 if sol.is_slack else sol.tension_lower
assert np.allclose(T_model, expected, rtol=1e-9)
def test_irvine_no_velocity_dependence(ipanema, home_pose):
"""Adding linear / angular velocity must not change the Irvine tension."""
model = IrvineModel()
L = cable_lengths(home_pose, ipanema)
rest = L * 0.99
state_static = _zero_state(home_pose)
state_moving = PlatformState(
pose=home_pose,
velocity=Twist.from_parts([0.1, -0.2, 0.05], [0.01, -0.02, 0.0]),
)
T_static = model.tension(ipanema, state_static, rest)
T_moving = model.tension(ipanema, state_moving, rest)
assert np.allclose(T_static, T_moving)
# ---------------------------------------------------------------------------
# SQCK hybrid physics
# ---------------------------------------------------------------------------
def test_sqck_reduces_to_irvine_at_zero_velocity(ipanema, home_pose):
irv = IrvineModel(linear_density=0.07, youngs_modulus=1.1e11,
cross_section=1.26e-5)
hyb = SQCKHybridModel(linear_density=0.07, youngs_modulus=1.1e11,
cross_section=1.26e-5, viscous_coefficient=1e8)
state = _zero_state(home_pose)
L = cable_lengths(home_pose, ipanema)
rest = L * 0.99
assert np.allclose(
hyb.tension(ipanema, state, rest),
irv.tension(ipanema, state, rest),
rtol=1e-9,
)
def test_sqck_adds_damping_correction_under_motion(ipanema, home_pose):
"""T_SQCK - T_Irvine should equal (eta A / L0) * dL/dt, clipped at zero."""
irv = IrvineModel(linear_density=0.07)
hyb = SQCKHybridModel(linear_density=0.07, viscous_coefficient=5e7)
L = cable_lengths(home_pose, ipanema)
rest = L * 0.99
state = PlatformState(
pose=home_pose,
velocity=Twist.from_parts([0.05, 0.0, 0.0], np.zeros(3)),
)
T_irv = irv.tension(ipanema, state, rest)
T_hyb = hyb.tension(ipanema, state, rest)
# Reconstruct expected damping component.
c = hyb._eta * hyb._A / rest if hyb._eta.size else None
dL_dt = hyb._length_rates(ipanema, state)
expected_damping = c * dL_dt
expected_total = np.maximum(T_irv + expected_damping, 0.0)
assert np.allclose(T_hyb, expected_total, rtol=1e-9)
# ---------------------------------------------------------------------------
# No cross-contamination between modes
# ---------------------------------------------------------------------------
def test_modes_dont_share_state(ipanema, home_pose):
"""Two instances of different modes built from the factory must remain
independent --- mutating one's parameters does not leak into another."""
kv = cable_model_by_name("kelvin_voigt")
irv = cable_model_by_name("irvine")
# Prime per-cable caches.
L = cable_lengths(home_pose, ipanema)
state = _zero_state(home_pose)
kv.tension(ipanema, state, L * 0.99)
irv.tension(ipanema, state, L * 0.99)
# KV has no linear_density; Irvine has no viscous_coefficient.
assert not hasattr(kv, "linear_density") or "linear_density" not in kv.parameters
assert "linear_density" in irv.parameters
assert "viscous_coefficient" not in irv.parameters
assert "viscous_coefficient" in kv.parameters
# ---------------------------------------------------------------------------
# Simulator integration
# ---------------------------------------------------------------------------
def test_simulator_records_active_mode(ipanema, home_pose):
kv = cable_model_by_name("kelvin_voigt", viscous_coefficient=1e6)
state0 = PlatformState.at_rest(home_pose)
result = simulate(
robot=ipanema, state0=state0,
duration=0.02, dt=2e-3,
reference=lambda t: home_pose,
cable_model=kv,
)
assert result.cable_model_name == "kelvin_voigt"
assert result.cable_model_parameters is not None
assert len(result.cable_diagnostics) == len(result.time)
# Every diagnostic record carries the active mode.
for d in result.cable_diagnostics:
assert d["mode"] == "kelvin_voigt"
def test_simulator_defaults_unchanged_without_cable_model(ipanema, home_pose):
"""Default path (no cable_model) leaves the result's cable_model_name None."""
state0 = PlatformState.at_rest(home_pose)
result = simulate(
robot=ipanema, state0=state0,
duration=0.02, dt=2e-3,
reference=lambda t: home_pose,
)
assert result.cable_model_name is None
assert result.cable_model_parameters is None
assert result.cable_diagnostics == []
def test_simulator_irvine_path_runs_to_completion(ipanema, home_pose):
"""A hold simulation under Irvine must produce a finite trajectory."""
irv = cable_model_by_name("irvine")
state0 = PlatformState.at_rest(home_pose)
result = simulate(
robot=ipanema, state0=state0,
duration=0.01, dt=2e-3,
reference=lambda t: home_pose,
cable_model=irv,
)
assert result.cable_model_name == "irvine"
assert np.all(np.isfinite(result.positions))
assert np.all(np.isfinite(result.cable_tensions))
|