2025-03-08 09:09:31 +00:00
|
|
|
import numpy as np
|
2023-09-27 15:45:31 -07:00
|
|
|
from cereal import car
|
2023-11-17 23:53:40 +00:00
|
|
|
from openpilot.common.realtime import DT_CTRL
|
2025-03-08 09:09:31 +00:00
|
|
|
from openpilot.selfdrive.controls.lib.drive_helpers import CONTROL_N
|
|
|
|
from openpilot.common.pid import PIDController
|
2023-11-17 23:53:40 +00:00
|
|
|
from openpilot.selfdrive.modeld.constants import ModelConstants
|
2024-09-03 16:09:00 +09:00
|
|
|
from openpilot.common.params import Params
|
2023-09-27 15:45:31 -07:00
|
|
|
|
2024-06-11 01:36:40 +00:00
|
|
|
CONTROL_N_T_IDX = ModelConstants.T_IDXS[:CONTROL_N]
|
|
|
|
|
2023-09-27 15:45:31 -07:00
|
|
|
LongCtrlState = car.CarControl.Actuators.LongControlState
|
|
|
|
|
|
|
|
|
2025-03-08 09:09:31 +00:00
|
|
|
def long_control_state_trans(CP, active, long_control_state, v_ego,
|
2025-04-26 13:30:45 +09:00
|
|
|
should_stop, brake_pressed, cruise_standstill, a_ego, stopping_accel, radarState):
|
2025-03-08 09:09:31 +00:00
|
|
|
stopping_condition = should_stop
|
|
|
|
starting_condition = (not should_stop and
|
2023-09-27 15:45:31 -07:00
|
|
|
not cruise_standstill and
|
|
|
|
not brake_pressed)
|
|
|
|
started_condition = v_ego > CP.vEgoStarting
|
|
|
|
|
|
|
|
if not active:
|
|
|
|
long_control_state = LongCtrlState.off
|
|
|
|
|
|
|
|
else:
|
2025-03-08 09:09:31 +00:00
|
|
|
if long_control_state == LongCtrlState.off:
|
|
|
|
if not starting_condition:
|
2023-09-27 15:45:31 -07:00
|
|
|
long_control_state = LongCtrlState.stopping
|
2025-03-08 09:09:31 +00:00
|
|
|
else:
|
|
|
|
if starting_condition and CP.startingState:
|
|
|
|
long_control_state = LongCtrlState.starting
|
|
|
|
else:
|
|
|
|
long_control_state = LongCtrlState.pid
|
2023-09-27 15:45:31 -07:00
|
|
|
|
|
|
|
elif long_control_state == LongCtrlState.stopping:
|
|
|
|
if starting_condition and CP.startingState:
|
|
|
|
long_control_state = LongCtrlState.starting
|
|
|
|
elif starting_condition:
|
|
|
|
long_control_state = LongCtrlState.pid
|
|
|
|
|
2025-03-08 09:09:31 +00:00
|
|
|
elif long_control_state in [LongCtrlState.starting, LongCtrlState.pid]:
|
2023-09-27 15:45:31 -07:00
|
|
|
if stopping_condition:
|
2024-09-03 16:09:00 +09:00
|
|
|
stopping_accel = stopping_accel if stopping_accel < 0.0 else -0.5
|
2025-04-26 13:30:45 +09:00
|
|
|
leadOne = radarState.leadOne
|
|
|
|
fcw_stop = leadOne.status and leadOne.dRel < 4.0
|
|
|
|
if a_ego > stopping_accel or fcw_stop: # and v_ego < 1.0:
|
2024-09-03 16:09:00 +09:00
|
|
|
long_control_state = LongCtrlState.stopping
|
|
|
|
if long_control_state == LongCtrlState.starting:
|
|
|
|
long_control_state = LongCtrlState.stopping
|
2023-09-27 15:45:31 -07:00
|
|
|
elif started_condition:
|
|
|
|
long_control_state = LongCtrlState.pid
|
|
|
|
return long_control_state
|
|
|
|
|
|
|
|
class LongControl:
|
|
|
|
def __init__(self, CP):
|
|
|
|
self.CP = CP
|
2025-03-08 09:09:31 +00:00
|
|
|
self.long_control_state = LongCtrlState.off
|
2023-09-27 15:45:31 -07:00
|
|
|
self.pid = PIDController((CP.longitudinalTuning.kpBP, CP.longitudinalTuning.kpV),
|
|
|
|
(CP.longitudinalTuning.kiBP, CP.longitudinalTuning.kiV),
|
|
|
|
k_f=CP.longitudinalTuning.kf, rate=1 / DT_CTRL)
|
|
|
|
self.last_output_accel = 0.0
|
|
|
|
|
2024-09-03 16:09:00 +09:00
|
|
|
|
|
|
|
self.params = Params()
|
|
|
|
self.readParamCount = 0
|
|
|
|
self.stopping_accel = 0
|
2025-04-26 13:30:45 +09:00
|
|
|
self.j_lead = 0.0
|
2024-09-03 16:09:00 +09:00
|
|
|
|
2025-03-08 09:09:31 +00:00
|
|
|
def reset(self):
|
2023-09-27 15:45:31 -07:00
|
|
|
self.pid.reset()
|
|
|
|
|
2025-04-19 13:45:09 +09:00
|
|
|
def update(self, active, CS, long_plan, accel_limits, t_since_plan, radarState):
|
2024-09-03 16:09:00 +09:00
|
|
|
|
|
|
|
soft_hold_active = CS.softHoldActive > 0
|
|
|
|
a_target = long_plan.aTarget
|
|
|
|
should_stop = long_plan.shouldStop
|
2025-05-05 09:08:33 +09:00
|
|
|
velocity_pid = True
|
2025-04-19 13:45:09 +09:00
|
|
|
long_delay = self.params.get_float("LongActuatorDelay")*0.01 + t_since_plan
|
2025-05-02 16:19:59 +09:00
|
|
|
|
2024-09-03 16:09:00 +09:00
|
|
|
speeds = long_plan.speeds
|
|
|
|
if len(speeds) == CONTROL_N:
|
2025-05-02 16:19:59 +09:00
|
|
|
t_idxs = ModelConstants.T_IDXS[:CONTROL_N]
|
|
|
|
j_target_now = long_plan.jerks[0]
|
|
|
|
a_target_now = long_plan.accels[0]
|
|
|
|
v_target_now = long_plan.speeds[0]
|
|
|
|
#v_target_ff = np.interp(long_delay, t_idxs, long_plan.speeds)
|
|
|
|
a_target_ff = np.interp(long_delay, t_idxs, long_plan.accels)
|
2024-09-03 16:09:00 +09:00
|
|
|
else:
|
2025-05-02 16:19:59 +09:00
|
|
|
a_target_now = 0.0
|
|
|
|
v_target_now = 0.0
|
|
|
|
#v_target_ff = 0.0
|
|
|
|
a_target_ff = j_target_now = 0.0
|
2024-09-03 16:09:00 +09:00
|
|
|
|
|
|
|
self.readParamCount += 1
|
|
|
|
if self.readParamCount >= 100:
|
|
|
|
self.readParamCount = 0
|
|
|
|
self.stopping_accel = self.params.get_float("StoppingAccel") * 0.01
|
|
|
|
elif self.readParamCount == 10:
|
|
|
|
if len(self.CP.longitudinalTuning.kpBP) == 1 and len(self.CP.longitudinalTuning.kiBP)==1:
|
|
|
|
longitudinalTuningKpV = self.params.get_float("LongTuningKpV") * 0.01
|
|
|
|
longitudinalTuningKiV = self.params.get_float("LongTuningKiV") * 0.001
|
|
|
|
self.pid._k_p = (self.CP.longitudinalTuning.kpBP, [longitudinalTuningKpV])
|
|
|
|
self.pid._k_i = (self.CP.longitudinalTuning.kiBP, [longitudinalTuningKiV])
|
|
|
|
self.pid._k_f = self.params.get_float("LongTuningKf") * 0.01
|
|
|
|
|
|
|
|
|
2023-09-27 15:45:31 -07:00
|
|
|
"""Update longitudinal control. This updates the state machine and runs a PID loop"""
|
|
|
|
self.pid.neg_limit = accel_limits[0]
|
|
|
|
self.pid.pos_limit = accel_limits[1]
|
|
|
|
|
|
|
|
self.long_control_state = long_control_state_trans(self.CP, active, self.long_control_state, CS.vEgo,
|
2025-03-08 09:09:31 +00:00
|
|
|
should_stop, CS.brakePressed,
|
2025-04-26 13:30:45 +09:00
|
|
|
CS.cruiseState.standstill, CS.aEgo, self.stopping_accel, radarState)
|
2024-09-03 16:09:00 +09:00
|
|
|
if active and soft_hold_active:
|
|
|
|
self.long_control_state = LongCtrlState.stopping
|
|
|
|
|
2023-09-27 15:45:31 -07:00
|
|
|
if self.long_control_state == LongCtrlState.off:
|
2025-03-08 09:09:31 +00:00
|
|
|
self.reset()
|
2023-09-27 15:45:31 -07:00
|
|
|
output_accel = 0.
|
|
|
|
|
|
|
|
elif self.long_control_state == LongCtrlState.stopping:
|
2025-03-08 09:09:31 +00:00
|
|
|
output_accel = self.last_output_accel
|
2024-09-03 16:09:00 +09:00
|
|
|
|
|
|
|
if soft_hold_active:
|
|
|
|
output_accel = self.CP.stopAccel
|
|
|
|
|
|
|
|
stopAccel = self.stopping_accel if self.stopping_accel < 0.0 else self.CP.stopAccel
|
|
|
|
if output_accel > stopAccel:
|
2023-09-27 15:45:31 -07:00
|
|
|
output_accel = min(output_accel, 0.0)
|
|
|
|
output_accel -= self.CP.stoppingDecelRate * DT_CTRL
|
2025-03-08 09:09:31 +00:00
|
|
|
self.reset()
|
2023-09-27 15:45:31 -07:00
|
|
|
|
|
|
|
elif self.long_control_state == LongCtrlState.starting:
|
|
|
|
output_accel = self.CP.startAccel
|
2025-03-08 09:09:31 +00:00
|
|
|
self.reset()
|
2023-09-27 15:45:31 -07:00
|
|
|
|
2025-03-08 09:09:31 +00:00
|
|
|
else: # LongCtrlState.pid
|
2025-04-19 13:45:09 +09:00
|
|
|
if velocity_pid == 0:
|
|
|
|
error = a_target_now - CS.aEgo
|
|
|
|
else:
|
|
|
|
error = v_target_now - CS.vEgo
|
2025-03-08 09:09:31 +00:00
|
|
|
output_accel = self.pid.update(error, speed=CS.vEgo,
|
2025-05-02 16:19:59 +09:00
|
|
|
feedforward=a_target_ff)
|
2023-09-27 15:45:31 -07:00
|
|
|
|
2025-03-08 09:09:31 +00:00
|
|
|
self.last_output_accel = np.clip(output_accel, accel_limits[0], accel_limits[1])
|
2024-09-03 16:09:00 +09:00
|
|
|
return self.last_output_accel, a_target_now, j_target_now
|