yincheng.zhong
2 天以前 567085ead3f6adaabd884f16ab4b17c62e8f0403
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
import math
 
import pytest
 
from hitl.dynamics import DifferentialDriveModel
 
 
def test_straight_motion_updates_position():
    model = DifferentialDriveModel()
    model.reset()
    state = model.step(target_linear=1.0, target_angular=0.0, dt=1.0)
    assert state.east == pytest.approx(1.0, rel=0.05)
    assert state.north == pytest.approx(0.0, abs=0.05)
    assert state.heading == pytest.approx(0.0, abs=1e-3)
 
 
def test_in_place_rotation_changes_heading():
    model = DifferentialDriveModel()
    model.reset()
    state = model.step(target_linear=0.0, target_angular=math.radians(90.0), dt=1.0)
    assert math.degrees(state.heading) == pytest.approx(90.0, abs=1.0)
    assert abs(state.linear_velocity) < 1e-6
 
 
def test_velocity_limits_respected():
    model = DifferentialDriveModel(max_linear_speed=0.5, max_linear_accel=0.5)
    model.reset()
    state = model.step(target_linear=2.0, target_angular=0.0, dt=0.2)
    assert state.linear_velocity <= model.max_linear_speed + 1e-6
 
 
def test_body_acceleration_contains_gravity():
    model = DifferentialDriveModel()
    model.reset()
    state = model.step(target_linear=0.0, target_angular=0.0, dt=0.1)
    ax, ay, az = state.body_accel_g
    assert -1.05 < az < -0.95  # 重力方向
    assert abs(ax) < 0.05
    assert abs(ay) < 0.05