2021-09-06 18:45:59 -07:00
|
|
|
from cereal import car
|
2024-06-14 00:08:58 -07:00
|
|
|
from openpilot.common.numpy_fast import clip
|
2024-07-15 11:14:04 -07:00
|
|
|
from openpilot.common.realtime import DT_CTRL
|
2024-06-14 00:08:58 -07:00
|
|
|
from openpilot.selfdrive.controls.lib.drive_helpers import CONTROL_N
|
2024-08-31 16:49:29 -07:00
|
|
|
from openpilot.common.pid import PIDController
|
2023-10-19 14:23:51 -07:00
|
|
|
from openpilot.selfdrive.modeld.constants import ModelConstants
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2024-06-05 08:04:50 +08:00
|
|
|
CONTROL_N_T_IDX = ModelConstants.T_IDXS[:CONTROL_N]
|
|
|
|
|
|
2021-09-06 18:45:59 -07:00
|
|
|
LongCtrlState = car.CarControl.Actuators.LongControlState
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2021-09-06 17:29:32 -07:00
|
|
|
|
2024-06-14 00:08:58 -07:00
|
|
|
def long_control_state_trans(CP, active, long_control_state, v_ego,
|
|
|
|
|
should_stop, brake_pressed, cruise_standstill):
|
|
|
|
|
stopping_condition = should_stop
|
|
|
|
|
starting_condition = (not should_stop and
|
2022-09-06 21:30:10 -07:00
|
|
|
not cruise_standstill and
|
|
|
|
|
not brake_pressed)
|
|
|
|
|
started_condition = v_ego > CP.vEgoStarting
|
2020-01-17 12:48:30 -08:00
|
|
|
|
|
|
|
|
if not active:
|
|
|
|
|
long_control_state = LongCtrlState.off
|
|
|
|
|
|
|
|
|
|
else:
|
2024-07-23 13:55:30 -07:00
|
|
|
if long_control_state == LongCtrlState.off:
|
|
|
|
|
if not starting_condition:
|
2020-01-17 12:48:30 -08:00
|
|
|
long_control_state = LongCtrlState.stopping
|
2024-07-23 13:55:30 -07:00
|
|
|
else:
|
|
|
|
|
if starting_condition and CP.startingState:
|
|
|
|
|
long_control_state = LongCtrlState.starting
|
|
|
|
|
else:
|
|
|
|
|
long_control_state = LongCtrlState.pid
|
|
|
|
|
|
2020-01-17 12:48:30 -08:00
|
|
|
elif long_control_state == LongCtrlState.stopping:
|
2022-09-06 21:30:10 -07:00
|
|
|
if starting_condition and CP.startingState:
|
|
|
|
|
long_control_state = LongCtrlState.starting
|
|
|
|
|
elif starting_condition:
|
|
|
|
|
long_control_state = LongCtrlState.pid
|
|
|
|
|
|
2024-07-23 13:55:30 -07:00
|
|
|
elif long_control_state in [LongCtrlState.starting, LongCtrlState.pid]:
|
2022-09-06 21:30:10 -07:00
|
|
|
if stopping_condition:
|
|
|
|
|
long_control_state = LongCtrlState.stopping
|
|
|
|
|
elif started_condition:
|
2020-01-17 12:48:30 -08:00
|
|
|
long_control_state = LongCtrlState.pid
|
|
|
|
|
return long_control_state
|
|
|
|
|
|
2022-04-28 00:42:52 -07:00
|
|
|
class LongControl:
|
2021-08-30 18:32:52 +02:00
|
|
|
def __init__(self, CP):
|
2022-04-28 00:42:52 -07:00
|
|
|
self.CP = CP
|
2024-06-14 00:08:58 -07:00
|
|
|
self.long_control_state = LongCtrlState.off
|
2022-04-07 11:34:45 -07:00
|
|
|
self.pid = PIDController((CP.longitudinalTuning.kpBP, CP.longitudinalTuning.kpV),
|
|
|
|
|
(CP.longitudinalTuning.kiBP, CP.longitudinalTuning.kiV),
|
2022-04-28 00:42:52 -07:00
|
|
|
k_f=CP.longitudinalTuning.kf, rate=1 / DT_CTRL)
|
2021-08-30 18:32:52 +02:00
|
|
|
self.last_output_accel = 0.0
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2024-06-14 00:08:58 -07:00
|
|
|
def reset(self):
|
2020-01-17 12:48:30 -08:00
|
|
|
self.pid.reset()
|
|
|
|
|
|
2024-06-14 00:08:58 -07:00
|
|
|
def update(self, active, CS, a_target, should_stop, accel_limits):
|
2020-01-17 12:48:30 -08:00
|
|
|
"""Update longitudinal control. This updates the state machine and runs a PID loop"""
|
2021-09-02 14:16:44 -07:00
|
|
|
self.pid.neg_limit = accel_limits[0]
|
|
|
|
|
self.pid.pos_limit = accel_limits[1]
|
2021-07-07 19:42:26 -07:00
|
|
|
|
2022-04-28 00:42:52 -07:00
|
|
|
self.long_control_state = long_control_state_trans(self.CP, active, self.long_control_state, CS.vEgo,
|
2024-06-14 00:08:58 -07:00
|
|
|
should_stop, CS.brakePressed,
|
2022-01-04 16:40:39 -07:00
|
|
|
CS.cruiseState.standstill)
|
2022-04-04 21:59:52 -07:00
|
|
|
if self.long_control_state == LongCtrlState.off:
|
2024-06-14 00:08:58 -07:00
|
|
|
self.reset()
|
2021-08-30 18:32:52 +02:00
|
|
|
output_accel = 0.
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2022-09-06 21:30:10 -07:00
|
|
|
elif self.long_control_state == LongCtrlState.stopping:
|
2024-06-14 00:08:58 -07:00
|
|
|
output_accel = self.last_output_accel
|
2022-09-06 21:30:10 -07:00
|
|
|
if output_accel > self.CP.stopAccel:
|
2022-11-04 13:21:34 -07:00
|
|
|
output_accel = min(output_accel, 0.0)
|
2022-09-06 21:30:10 -07:00
|
|
|
output_accel -= self.CP.stoppingDecelRate * DT_CTRL
|
2024-06-14 00:08:58 -07:00
|
|
|
self.reset()
|
2022-09-06 21:30:10 -07:00
|
|
|
|
|
|
|
|
elif self.long_control_state == LongCtrlState.starting:
|
|
|
|
|
output_accel = self.CP.startAccel
|
2024-06-14 00:08:58 -07:00
|
|
|
self.reset()
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2024-06-14 00:08:58 -07:00
|
|
|
else: # LongCtrlState.pid
|
|
|
|
|
error = a_target - CS.aEgo
|
|
|
|
|
output_accel = self.pid.update(error, speed=CS.vEgo,
|
|
|
|
|
feedforward=a_target)
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2022-09-06 21:30:10 -07:00
|
|
|
self.last_output_accel = clip(output_accel, accel_limits[0], accel_limits[1])
|
|
|
|
|
return self.last_output_accel
|