Skip to content

Commit

Permalink
support testing integer torque
Browse files Browse the repository at this point in the history
cmt
  • Loading branch information
sshane committed Oct 24, 2023
1 parent a5f6713 commit 2c38107
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions tests/safety/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,21 @@ def _generic_limit_safety_check(self, msg_function: MessageFunction, min_allowed
should_tx = (should_tx or v == inactive_value) and msg_allowed
self.assertEqual(self._tx(msg_function(v)), should_tx, (controls_allowed, should_tx, v))

def _common_measurement_test(self, msg_func: Callable, min_value: float, max_value: float, factor: int,
meas_min_func: Callable[[], int], meas_max_func: Callable[[], int]):
def _common_measurement_test(self, msg_func: Callable[[int | float], ...], min_value: float, max_value: float,
factor: int, meas_min_func: Callable[[], int], meas_max_func: Callable[[], int],
increment: float = 1):
"""Tests accurate measurement parsing, and that it's reset on safety mode init"""
for val in np.arange(min_value, max_value, 0.5):

# Support testing float and int measurement structs using increment argument
incremented_val = increment * (MAX_SAMPLE_VALS - 1)

for val in np.arange(min_value, max_value, incremented_val):
for i in range(MAX_SAMPLE_VALS):
self.assertTrue(self._rx(msg_func(val + i * 0.1)))
self.assertTrue(self._rx(msg_func(val + i * increment)))

# assert close by one decimal place
# assert close by one decimal place if float
self.assertAlmostEqual(meas_min_func() / factor, val, delta=0.1)
self.assertAlmostEqual(meas_max_func() / factor - 0.5, val, delta=0.1)
self.assertAlmostEqual(meas_max_func() / factor - incremented_val, val, delta=0.1)

# ensure sample_t is reset on safety init
self._reset_safety_hooks()
Expand Down Expand Up @@ -511,7 +516,6 @@ def test_reset_driver_torque_measurements(self):
for t in np.linspace(-self.MAX_TORQUE, self.MAX_TORQUE, MAX_SAMPLE_VALS):
self.assertTrue(self._rx(self._torque_driver_msg(t)))

print(self.safety.get_torque_driver_max(), self.safety.get_torque_driver_min())
self.assertNotEqual(self.safety.get_torque_driver_min(), 0)
self.assertNotEqual(self.safety.get_torque_driver_max(), 0)

Expand Down Expand Up @@ -676,10 +680,12 @@ def _reset_speed_measurement(self, speed):
self._rx(self._speed_msg(speed))

def test_vehicle_speed_measurements(self):
self._common_measurement_test(self._speed_msg, 0, 80, VEHICLE_SPEED_FACTOR, self.safety.get_vehicle_speed_min, self.safety.get_vehicle_speed_max)
self._common_measurement_test(self._speed_msg, 0, 80, VEHICLE_SPEED_FACTOR,
self.safety.get_vehicle_speed_min, self.safety.get_vehicle_speed_max, increment=0.1)

def test_steering_angle_measurements(self):
self._common_measurement_test(self._angle_meas_msg, -180, 180, self.DEG_TO_CAN, self.safety.get_angle_meas_min, self.safety.get_angle_meas_max)
self._common_measurement_test(self._angle_meas_msg, -180, 180, self.DEG_TO_CAN,
self.safety.get_angle_meas_min, self.safety.get_angle_meas_max, increment=0.1)

def test_angle_cmd_when_enabled(self):
# when controls are allowed, angle cmd rate limit is enforced
Expand Down

0 comments on commit 2c38107

Please sign in to comment.