113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
"""Generic measured torque envelope validation and runtime limits."""
|
|
|
|
import math
|
|
from dataclasses import asdict
|
|
|
|
import pytest
|
|
|
|
torch = pytest.importorskip("torch")
|
|
yaml = pytest.importorskip("yaml")
|
|
|
|
from mjlab.actuator import ActuatorCmd
|
|
from mjrl.tasks.actuator import ServoCurveActuatorCfg, load_servo_curve, servo_torque_limit
|
|
|
|
CURVE = {
|
|
"model": "exp_decay_with_cutoff",
|
|
"plateau_torque_nm": 6.0,
|
|
"corner_speed_rpm": 60.0,
|
|
"decay_speed_rpm": 120.0,
|
|
"cutoff_speed_rpm": 420.0,
|
|
"thermal_continuous_torque_nm": 4.0,
|
|
"peak_duration_s": 0.3,
|
|
}
|
|
|
|
|
|
def _write(tmp_path, curve):
|
|
path = tmp_path / "motors.yaml"
|
|
path.write_text(yaml.safe_dump({"motors": {"test_motor": {"torque_speed": curve}}}))
|
|
return path
|
|
|
|
|
|
@pytest.fixture
|
|
def servo_curve(tmp_path):
|
|
return load_servo_curve(_write(tmp_path, CURVE), "test_motor")
|
|
|
|
|
|
@pytest.mark.parametrize("field,value", [
|
|
("model", "linear_decay"), ("corner_speed_rpm", None),
|
|
("plateau_torque_nm", True), ("cutoff_speed_rpm", math.inf),
|
|
("decay_speed_rpm", 0.0), ("thermal_continuous_torque_nm", 6.0),
|
|
("peak_duration_s", -1.0),
|
|
])
|
|
def test_malformed_curve_is_rejected(tmp_path, field, value):
|
|
with pytest.raises(ValueError, match=field):
|
|
load_servo_curve(_write(tmp_path, {**CURVE, field: value}), "test_motor")
|
|
|
|
|
|
def test_envelope_matches_the_scalar_reference(servo_curve):
|
|
curve = servo_curve
|
|
assert curve.corner_speed == pytest.approx(2 * math.pi)
|
|
speeds = torch.linspace(-50, 50, 1001, dtype=torch.float64)
|
|
expected = [
|
|
0.0 if abs(speed) >= curve.cutoff_speed else
|
|
curve.plateau_torque * math.exp(-max(abs(speed) - curve.corner_speed, 0) / curve.decay_speed)
|
|
for speed in speeds.tolist()
|
|
]
|
|
got = servo_torque_limit(
|
|
speeds, curve.plateau_torque, curve.corner_speed, curve.decay_speed, curve.cutoff_speed,
|
|
)
|
|
assert got.tolist() == pytest.approx(expected)
|
|
assert got.max().item() == pytest.approx(curve.plateau_torque)
|
|
assert got[0] == got[-1] == 0
|
|
|
|
|
|
def test_thermal_time_constant_reproduces_the_declared_peak_budget(servo_curve):
|
|
c = servo_curve
|
|
assert (c.plateau_torque / c.continuous_torque) ** 2 * (
|
|
1 - math.exp(-c.peak_duration / c.thermal_time_constant)
|
|
) == pytest.approx(1.0)
|
|
|
|
|
|
def test_runtime_derates_and_resets_on_the_native_backend(neutral_robot, servo_curve):
|
|
from mjlab.envs import ManagerBasedRlEnv
|
|
from mjrl.backend import resolve
|
|
from mjrl.backend.select import use_backend
|
|
from tasks.templates.Biped_Template.env_cfg import env_cfg
|
|
|
|
use_backend(resolve(backend="native", device="cpu", num_envs=2))
|
|
cfg = env_cfg(neutral_robot, play=True)
|
|
cfg.scene.num_envs = 2
|
|
articulation = cfg.scene.entities["robot"].articulation
|
|
c = servo_curve
|
|
curve = asdict(c)
|
|
curve.pop("peak_duration")
|
|
curve["thermal_time_constant"] = c.thermal_time_constant
|
|
articulation.actuators = tuple(
|
|
ServoCurveActuatorCfg(
|
|
target_names_expr=act.target_names_expr, stiffness=act.stiffness,
|
|
damping=act.damping, effort_limit=c.plateau_torque, **curve,
|
|
)
|
|
for act in articulation.actuators
|
|
)
|
|
|
|
env = ManagerBasedRlEnv(cfg, device="cpu")
|
|
try:
|
|
motor = env.scene["robot"].actuators[0]
|
|
pos = torch.zeros(2, 1)
|
|
cmd = ActuatorCmd(
|
|
position_target=torch.full_like(pos, 20.0), velocity_target=pos,
|
|
effort_target=pos, pos=pos, vel=pos,
|
|
)
|
|
peak = c.plateau_torque
|
|
assert motor.compute(cmd)[:, 0].tolist() == pytest.approx([peak, peak])
|
|
for _ in range(math.ceil(c.peak_duration / env.physics_dt) + 1):
|
|
torque = motor.compute(cmd)
|
|
assert torque[:, 0].tolist() == pytest.approx([c.continuous_torque] * 2)
|
|
|
|
motor.reset(torch.tensor([0]))
|
|
assert motor.compute(cmd)[:, 0].tolist() == pytest.approx([peak, c.continuous_torque])
|
|
cmd.vel = torch.full_like(pos, 2 * c.cutoff_speed)
|
|
assert torch.count_nonzero(motor.compute(cmd)) == 0
|
|
finally:
|
|
env.close()
|