Spaces:
Sleeping
Sleeping
File size: 3,868 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 | """Rigid-body dynamics and time integration."""
from __future__ import annotations
import numpy as np
import pytest
from scipy.spatial.transform import Rotation
from cdpr.core.frames import Pose, Twist, Wrench
from cdpr.dynamics.integrators import rk4_step, semi_implicit_step
from cdpr.dynamics.rigid_body import PlatformState, rigid_body_acceleration
from cdpr.dynamics.simulator import simulate
from cdpr.geometry.robot import PlatformInertia
def test_acceleration_from_gravity_only():
inertia = PlatformInertia(mass=2.0)
pose = Pose(position=np.zeros(3), rotation=Rotation.identity())
state = PlatformState.at_rest(pose)
w = Wrench.gravity(mass=2.0, g=(0.0, 0.0, -9.81))
a_lin, a_ang = rigid_body_acceleration(state, w, inertia)
assert np.allclose(a_lin, [0, 0, -9.81])
assert np.allclose(a_ang, 0.0)
def test_rk4_free_fall_matches_analytic():
"""For a free body in gravity, position after t equals p0 + v0 t + 0.5 g t^2."""
inertia = PlatformInertia(mass=1.0)
pose0 = Pose(position=np.zeros(3), rotation=Rotation.identity())
state = PlatformState.at_rest(pose0)
g = -9.81
wrench_fn = lambda s, t: Wrench.from_parts([0, 0, g], np.zeros(3))
dt = 1e-3
n_steps = 1000
t_final = n_steps * dt
for _ in range(n_steps):
state = rk4_step(state, 0.0, dt, wrench_fn, inertia)
expected_z = 0.5 * g * t_final**2
assert state.pose.position[2] == pytest.approx(expected_z, rel=1e-6)
assert state.velocity.linear[2] == pytest.approx(g * t_final, rel=1e-6)
def test_semi_implicit_pure_rotation_keeps_rotation_in_so3():
"""Free spin with constant angular velocity must keep R orthogonal."""
inertia = PlatformInertia(mass=1.0, inertia=np.eye(3))
pose = Pose(position=np.zeros(3), rotation=Rotation.identity())
omega = np.array([0.0, 0.0, 1.0])
state = PlatformState(pose=pose, velocity=Twist.from_parts(np.zeros(3), omega))
wrench_fn = lambda s, t: Wrench(np.zeros(6)) # no external wrench
for _ in range(500):
state = semi_implicit_step(state, 0.0, 0.01, wrench_fn, inertia)
R = state.pose.rotation.as_matrix()
assert np.allclose(R @ R.T, np.eye(3), atol=1e-10)
assert np.linalg.det(R) == pytest.approx(1.0)
def test_simulate_holds_static_equilibrium(point_mass_robot):
"""A platform commanded to its starting pose should stay there (within ms)."""
pose0 = Pose(position=np.array([0.0, 0.0, 0.0]), rotation=Rotation.identity())
state0 = PlatformState.at_rest(pose0)
result = simulate(
point_mass_robot,
state0,
duration=0.05,
dt=1e-3,
reference_pose=lambda t: pose0,
)
# Final position should still be near origin (gravity is cancelled by cable tensions).
assert np.linalg.norm(result.positions[-1] - pose0.position) < 1e-3
assert (result.cable_tensions[-1] > 0).all()
assert not result.infeasible_steps
def test_rk4_and_semi_implicit_agree_to_first_order():
"""At small dt, RK4 and semi-implicit should agree on a smooth problem.
The position drift between the two methods for constant gravity is
g*t*dt/2 (RK4 is exact for the quadratic; semi-implicit accumulates a
half-step bias). At dt=1e-4 over t=0.01 s with g~10, the bias is ~5e-6 m,
so the allowed tolerance is comfortably above this.
"""
inertia = PlatformInertia(mass=1.0)
pose = Pose(position=np.zeros(3), rotation=Rotation.identity())
state_a = PlatformState.at_rest(pose)
state_b = PlatformState.at_rest(pose)
wrench_fn = lambda s, t: Wrench.from_parts([0, 0, -9.81], np.zeros(3))
dt = 1e-4
for _ in range(100):
state_a = rk4_step(state_a, 0.0, dt, wrench_fn, inertia)
state_b = semi_implicit_step(state_b, 0.0, dt, wrench_fn, inertia)
assert np.allclose(state_a.pose.position, state_b.pose.position, atol=1e-4)
|