Files
carrotpilot/selfdrive/controls/lib/longcontrol.py
carrot 0423d12c9c Nuggets In Dijon model, C3xLite, livePose (#221)
* Update carrot_functions.py

* fix.. atc..

* TR16 model.

* fix.. atc

* Adjust interpolation value for t_follow calculation

* fix safeMode..

* fix safeMode2

* fix.. v_cruise

* model_turn_speed..

* fix..

* fix..

* fix.. cruise.py

* fix.. modelTurn..

* fix stopped car safe mode.

* model turn 120%

* remove model turn speed..

* paramsd...

* Revert "remove model turn speed.."

This reverts commit 564e9dd609.

* model_turn_speed...  120 -> 115%

* starting achange cost 30 -> 10

* fix..

* aChangeCostStarting

* fix..

* gwm v7

* Adjust traffic stop distance parameter

* Update carrot_functions.py

* update gwm 250929

* trafficStopDistance adjust

* localizer_roll_std

* scc13

* fix...

* fix.. scc13

* scc14

* bypass scc13

* fix scc13

* TheCoolPeople's Model

* North Nevada Model

* Revert "model_turn_speed...  120 -> 115%"

This reverts commit e842a7e99f.

* Reapply "remove model turn speed.."

This reverts commit 544ac16811.

* for c3x lite (#218)

add hardware c3x lite

* NNV(North Nevada) v2

* fix..

* Nuggets In Dijon Model

* toyota accel pid long

* for c3xlite fix (#219)

* LatSmoothSec

* Revert "Reapply "remove model turn speed..""

This reverts commit 2c10aae495.

* apply livePose

* releases 251017

---------

Co-authored-by: 「 crwusiz 」 <43285072+crwusiz@users.noreply.github.com>
2025-10-17 12:34:13 +09:00

137 lines
5.2 KiB
Python

import numpy as np
from cereal import car
from openpilot.common.realtime import DT_CTRL
from openpilot.selfdrive.controls.lib.drive_helpers import CONTROL_N
from openpilot.common.pid import PIDController
from openpilot.selfdrive.modeld.constants import ModelConstants
from openpilot.common.params import Params
CONTROL_N_T_IDX = ModelConstants.T_IDXS[:CONTROL_N]
LongCtrlState = car.CarControl.Actuators.LongControlState
def long_control_state_trans(CP, active, long_control_state, v_ego,
should_stop, brake_pressed, cruise_standstill, a_ego, stopping_accel, radarState):
stopping_condition = should_stop
starting_condition = (not should_stop and
not cruise_standstill and
not brake_pressed)
started_condition = v_ego > CP.vEgoStarting
if not active:
long_control_state = LongCtrlState.off
else:
if long_control_state == LongCtrlState.off:
if not starting_condition:
long_control_state = LongCtrlState.stopping
else:
if starting_condition and CP.startingState:
long_control_state = LongCtrlState.starting
else:
long_control_state = LongCtrlState.pid
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
elif long_control_state in [LongCtrlState.starting, LongCtrlState.pid]:
if stopping_condition:
stopping_accel = stopping_accel if stopping_accel < 0.0 else -0.5
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:
long_control_state = LongCtrlState.stopping
if long_control_state == LongCtrlState.starting:
long_control_state = LongCtrlState.stopping
elif started_condition:
long_control_state = LongCtrlState.pid
return long_control_state
class LongControl:
def __init__(self, CP):
self.CP = CP
self.long_control_state = LongCtrlState.off
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
self.params = Params()
self.readParamCount = 0
self.stopping_accel = 0
self.j_lead = 0.0
self.use_accel_pid = False
if CP.brand == "toyota":
self.use_accel_pid = True
def reset(self):
self.pid.reset()
def update(self, active, CS, long_plan, accel_limits, t_since_plan, radarState):
soft_hold_active = CS.softHoldActive > 0
a_target_ff = long_plan.aTarget
v_target_now = long_plan.vTargetNow
j_target_now = long_plan.jTargetNow
should_stop = long_plan.shouldStop
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
"""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,
should_stop, CS.brakePressed,
CS.cruiseState.standstill, CS.aEgo, self.stopping_accel, radarState)
if active and soft_hold_active:
self.long_control_state = LongCtrlState.stopping
if self.long_control_state == LongCtrlState.off:
self.reset()
output_accel = 0.
elif self.long_control_state == LongCtrlState.stopping:
output_accel = self.last_output_accel
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:
output_accel = min(output_accel, 0.0)
output_accel -= self.CP.stoppingDecelRate * DT_CTRL
self.reset()
elif self.long_control_state == LongCtrlState.starting:
output_accel = self.CP.startAccel
self.reset()
else: # LongCtrlState.pid
if self.use_accel_pid:
error = a_target_ff - CS.aEgo
else:
error = v_target_now - CS.vEgo
output_accel = self.pid.update(error, speed=CS.vEgo,
feedforward=a_target_ff)
self.last_output_accel = np.clip(output_accel, accel_limits[0], accel_limits[1])
return self.last_output_accel, a_target_ff, j_target_now