-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_bindings.py
More file actions
143 lines (111 loc) · 5.74 KB
/
test_python_bindings.py
File metadata and controls
143 lines (111 loc) · 5.74 KB
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
# Copyright (c) 2025-present Polymath Robotics, Inc. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import pytest
from polymath_kinematics import (
ArticulatedModel,
BicycleModel,
DifferentialDriveModel,
)
class TestDifferentialDriveModel:
def test_construction(self):
model = DifferentialDriveModel(0.1, 0.5)
assert model.wheel_radius == pytest.approx(0.1)
assert model.track_width == pytest.approx(0.5)
def test_turning(self):
model = DifferentialDriveModel(0.1, 0.5)
result = model.wheel_velocities_to_body_velocity(5.0, 15.0)
assert result.linear_velocity_m_s == pytest.approx(1.0)
assert result.angular_velocity_rad_s == pytest.approx(2.0)
def test_roundtrip(self):
model = DifferentialDriveModel(0.1, 0.5)
linear = 2.5
angular = 1.0
wheel_state = model.body_velocity_to_wheel_velocities(linear, angular)
velocities = model.wheel_velocities_to_body_velocity(
wheel_state.left_wheel_velocity_rad_s,
wheel_state.right_wheel_velocity_rad_s,
)
assert velocities.linear_velocity_m_s == pytest.approx(linear)
assert velocities.angular_velocity_rad_s == pytest.approx(angular)
class TestBicycleModel:
def test_construction(self):
model = BicycleModel(2.5, 1.5, 0.3)
assert model.wheelbase == pytest.approx(2.5)
assert model.track_width == pytest.approx(1.5)
assert model.wheel_radius == pytest.approx(0.3)
def test_turning(self):
model = BicycleModel(2.5, 1.5, 0.3)
result = model.body_velocity_to_steering(5.0, 2.0)
assert result.velocity_m_s == pytest.approx(5.0)
assert result.steering_angle_rad == pytest.approx(math.pi / 4)
assert result.turning_radius_m == pytest.approx(2.5)
def test_steering_angle_from_radius_negative(self):
model = BicycleModel(2.5, 1.5, 0.3)
angle = model.steering_angle_from_radius(-2.5)
assert angle == pytest.approx(-math.pi / 4)
def test_roundtrip(self):
model = BicycleModel(2.5, 1.5, 0.3)
velocity = 3.0
steering_angle = -0.3
body_vel = model.steering_to_body_velocity(velocity, steering_angle)
steering = model.body_velocity_to_steering(body_vel.linear_velocity_m_s, body_vel.angular_velocity_rad_s)
assert steering.velocity_m_s == pytest.approx(velocity)
assert steering.steering_angle_rad == pytest.approx(steering_angle)
def test_reverse_turning(self):
model = BicycleModel(2.5, 1.5, 0.3)
# Reversing with CCW body rotation → front wheels steer RIGHT (negative delta)
result = model.body_velocity_to_steering(-5.0, 2.0)
assert result.velocity_m_s == pytest.approx(-5.0)
assert result.steering_angle_rad == pytest.approx(-math.pi / 4)
def test_reverse_roundtrip(self):
model = BicycleModel(2.5, 1.5, 0.3)
velocity = -3.0
steering_angle = -0.3
body_vel = model.steering_to_body_velocity(velocity, steering_angle)
steering = model.body_velocity_to_steering(body_vel.linear_velocity_m_s, body_vel.angular_velocity_rad_s)
assert steering.velocity_m_s == pytest.approx(velocity)
assert steering.steering_angle_rad == pytest.approx(steering_angle)
class TestArticulatedModel:
def test_construction(self):
model = ArticulatedModel(1.5, 1.2, 1.8, 1.6, 0.4, 0.5)
assert model.articulation_to_front_axle == pytest.approx(1.5)
assert model.articulation_to_rear_axle == pytest.approx(1.2)
assert model.front_track_width == pytest.approx(1.8)
assert model.rear_track_width == pytest.approx(1.6)
assert model.front_wheel_radius == pytest.approx(0.4)
assert model.rear_wheel_radius == pytest.approx(0.5)
def test_turning(self):
model = ArticulatedModel(1.5, 1.2, 1.8, 1.6, 0.4, 0.5)
result = model.body_velocity_to_vehicle_state(2.0, 0.5)
assert result.linear_velocity_m_s == pytest.approx(2.0)
assert result.articulation_angle_rad == pytest.approx(0.6435011088)
assert result.front_axle_turning_radius_m == pytest.approx(4.0)
def test_axle_velocities(self):
model = ArticulatedModel(1.5, 1.2, 1.8, 1.6, 0.4, 0.5)
result = model.articulation_to_axle_velocities(2.0, 0.3)
assert result.linear_velocity_m_s == pytest.approx(2.0)
assert result.front_axle_turning_velocity_rad_s == pytest.approx(0.2244737375)
def test_reverse_turning(self):
model = ArticulatedModel(1.5, 1.2, 1.8, 1.6, 0.4, 0.5)
# Reversing (v < 0) + CCW yaw → articulation angle negative (mirrors forward case)
result = model.body_velocity_to_vehicle_state(-2.0, 0.5)
assert result.articulation_angle_rad == pytest.approx(-0.6435011088)
def test_reverse_roundtrip(self):
model = ArticulatedModel(1.5, 1.2, 1.8, 1.6, 0.4, 0.5)
linear_velocity = -2.0
angular_velocity = 0.5
vehicle_state = model.body_velocity_to_vehicle_state(linear_velocity, angular_velocity)
axle_vel = model.articulation_to_axle_velocities(linear_velocity, vehicle_state.articulation_angle_rad)
assert axle_vel.front_axle_turning_velocity_rad_s == pytest.approx(angular_velocity, abs=1e-6)