2025-09-30 22:37:13 -04:00
|
|
|
from cereal import car, custom
|
2024-07-23 13:55:30 -07:00
|
|
|
from openpilot.selfdrive.controls.lib.longcontrol import LongCtrlState, long_control_state_trans
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestLongControlStateTransition:
|
|
|
|
|
|
|
|
|
|
def test_stay_stopped(self):
|
|
|
|
|
CP = car.CarParams.new_message()
|
2025-09-30 22:37:13 -04:00
|
|
|
CP_SP = custom.CarParamsSP.new_message()
|
2024-07-23 13:55:30 -07:00
|
|
|
active = True
|
|
|
|
|
current_state = LongCtrlState.stopping
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=True, brake_pressed=False, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.stopping
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=True, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.stopping
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=False, cruise_standstill=True)
|
|
|
|
|
assert next_state == LongCtrlState.stopping
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=1.0,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=False, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.pid
|
|
|
|
|
active = False
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=1.0,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=False, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.off
|
|
|
|
|
|
|
|
|
|
def test_engage():
|
|
|
|
|
CP = car.CarParams.new_message()
|
2025-09-30 22:37:13 -04:00
|
|
|
CP_SP = custom.CarParamsSP.new_message()
|
2024-07-23 13:55:30 -07:00
|
|
|
active = True
|
|
|
|
|
current_state = LongCtrlState.off
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=True, brake_pressed=False, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.stopping
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=True, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.stopping
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=False, cruise_standstill=True)
|
|
|
|
|
assert next_state == LongCtrlState.stopping
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=False, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.pid
|
|
|
|
|
|
|
|
|
|
def test_starting():
|
|
|
|
|
CP = car.CarParams.new_message(startingState=True, vEgoStarting=0.5)
|
2025-09-30 22:37:13 -04:00
|
|
|
CP_SP = custom.CarParamsSP.new_message()
|
2024-07-23 13:55:30 -07:00
|
|
|
active = True
|
|
|
|
|
current_state = LongCtrlState.starting
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=0.1,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=False, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.starting
|
2025-09-30 22:37:13 -04:00
|
|
|
next_state = long_control_state_trans(CP, CP_SP, active, current_state, v_ego=1.0,
|
2024-07-23 13:55:30 -07:00
|
|
|
should_stop=False, brake_pressed=False, cruise_standstill=False)
|
|
|
|
|
assert next_state == LongCtrlState.pid
|