Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toyota long PCM compensation #32753

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a410906
toyota: try a little more long actuator delay
sshane Mar 15, 2024
b07ab9e
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Mar 16, 2024
b1d87bf
little less upper
sshane Mar 16, 2024
dcca795
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Mar 17, 2024
4e86bb7
no pcm compensations
sshane Mar 17, 2024
dfcfa02
pcm neutral force (not enabled yet)
sshane Mar 17, 2024
62b7421
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Mar 25, 2024
6ea3ba2
fixes
sshane Mar 25, 2024
dc70f3d
simpler gains
sshane Mar 25, 2024
382de1a
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Mar 28, 2024
6fa5c65
slower and use neutral force (correlated with pid.i!)
sshane Mar 28, 2024
8209f3f
try creeping
sshane Mar 28, 2024
208c284
2 m/s^2 just to see what this is like
sshane Mar 28, 2024
0697cee
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Mar 29, 2024
c98c9fd
this is all too complex to verify for a single pr!
sshane Mar 29, 2024
901ba7a
1.5
sshane Mar 29, 2024
b486393
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Mar 31, 2024
b3d24f2
try
sshane Mar 31, 2024
91a7ee1
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Apr 13, 2024
bd7c0b9
Revert "this is all too complex to verify for a single pr!"
sshane Apr 13, 2024
943eb5d
slower
sshane Apr 13, 2024
56e863d
only add
sshane Apr 13, 2024
0c9316f
Merge remote-tracking branch 'upstream/master' into toyota-long-delay
sshane Apr 21, 2024
137ecb4
all out
sshane Apr 21, 2024
cc15639
more compensation
sshane Apr 21, 2024
d481185
do as honda
sshane Apr 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions selfdrive/car/toyota/carcontroller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from cereal import car
from openpilot.common.numpy_fast import clip
from openpilot.common.numpy_fast import clip, interp
from openpilot.selfdrive.car import apply_meas_steer_torque_limits, apply_std_steer_angle_limits, common_fault_avoidance, make_can_msg
from openpilot.selfdrive.car.interfaces import CarControllerBase
from openpilot.selfdrive.car.toyota import toyotacan
from openpilot.selfdrive.car.toyota.values import CAR, STATIC_DSU_MSGS, NO_STOP_TIMER_CAR, TSS2_CAR, \
CarControllerParams, ToyotaFlags, \
UNSUPPORTED_DSU_CAR
CarControllerParams, ToyotaFlags, \
UNSUPPORTED_DSU_CAR
from opendbc.can.packer import CANPacker

SteerControlType = car.CarParams.SteerControlType
Expand All @@ -25,6 +25,17 @@
MAX_LTA_DRIVER_TORQUE_ALLOWANCE = 150 # slightly above steering pressed allows some resistance when changing lanes


def compute_gb_toyota(accel, speed):
# from honda nidec
creep_brake = 0.0
creep_speed = 2.3
creep_brake_value = 0.15
if speed < creep_speed:
creep_brake = (creep_speed - speed) / creep_speed * creep_brake_value
gb = accel - creep_brake
return gb


class CarController(CarControllerBase):
def __init__(self, dbc_name, CP, VM):
self.CP = CP
Expand All @@ -36,6 +47,7 @@ def __init__(self, dbc_name, CP, VM):
self.last_standstill = False
self.standstill_req = False
self.steer_rate_counter = 0
self.pcm_accel_comp = 0
self.distance_button = 0

self.packer = CANPacker(dbc_name)
Expand Down Expand Up @@ -100,7 +112,31 @@ def update(self, CC, CS, now_nanos):
lta_active, self.frame // 2, torque_wind_down))

# *** gas and brake ***
pcm_accel_cmd = clip(actuators.accel, self.params.ACCEL_MIN, self.params.ACCEL_MAX)

gas_accel = compute_gb_toyota(actuators.accel, CS.out.vEgo)

# wind brake from air resistance decel at high speed
wind_brake = interp(CS.out.vEgo, [0.0, 2.3, 35.0], [0.001, 0.002, 0.15])

gas_accel += wind_brake

# we will throw out PCM's compensations, but that may be a good thing. for example:
# we lose things like pitch compensation, gas to maintain speed, brake to compensate for creeping, etc.
# but also remove undesirable "snap to standstill" behavior when not requesting enough accel at low speeds,
# lag to start moving, lag to start braking, etc.
# PI should compensate for lack of the desirable behaviors, but might be worse than the PCM doing them
self.pcm_accel_comp = clip(gas_accel - CS.pcm_accel_net, self.pcm_accel_comp - 0.03, self.pcm_accel_comp + 0.03)
pcm_accel_cmd = gas_accel + self.pcm_accel_comp

# add back gas to maintain speed
# accel_offset = CS.pcm_neutral_force / self.CP.mass
# fact = interp(CS.out.vEgo, [3, 6], [0.0, 1.0]) # not at low speed
# pcm_accel_cmd -= min(accel_offset / 2, 0) * fact # only add, reduce effect

if not CC.longActive:
pcm_accel_cmd = 0.0

pcm_accel_cmd = clip(pcm_accel_cmd, self.params.ACCEL_MIN, self.params.ACCEL_MAX)

# TODO: probably can delete this. CS.pcm_acc_status uses a different signal
# than CS.cruiseState.enabled. confirm they're not meaningfully different
Expand Down Expand Up @@ -149,7 +185,7 @@ def update(self, CC, CS, now_nanos):
# - there is something to stop displaying
send_ui = False
if ((fcw_alert or steer_alert) and not self.alert_active) or \
(not (fcw_alert or steer_alert) and self.alert_active):
(not (fcw_alert or steer_alert) and self.alert_active):
send_ui = True
self.alert_active = not self.alert_active
elif pcm_cancel_cmd:
Expand Down
5 changes: 5 additions & 0 deletions selfdrive/car/toyota/carstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def __init__(self, CP):
self.low_speed_lockout = False
self.acc_type = 1
self.lkas_hud = {}
self.pcm_accel_net = 0.0
self.pcm_neutral_force = 0.0

def update(self, cp, cp_cam):
ret = car.CarState.new_message()
Expand All @@ -72,6 +74,9 @@ def update(self, cp, cp_cam):
ret.vEgo, ret.aEgo = self.update_speed_kf(ret.vEgoRaw)
ret.vEgoCluster = ret.vEgo * 1.015 # minimum of all the cars

# thought to be the gas/brake as issued by the pcm (0=coasting)
self.pcm_accel_net = cp.vl["PCM_CRUISE"]["ACCEL_NET"]
self.pcm_neutral_force = cp.vl["PCM_CRUISE"]["NEUTRAL_FORCE"]
ret.standstill = abs(ret.vEgoRaw) < 1e-3

ret.steeringAngleDeg = cp.vl["STEER_ANGLE_SENSOR"]["STEER_ANGLE"] + cp.vl["STEER_ANGLE_SENSOR"]["STEER_FRACTION"]
Expand Down
40 changes: 34 additions & 6 deletions selfdrive/car/toyota/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,42 @@ def _get_params(ret, candidate, fingerprint, car_fw, experimental_long, docs):
tune.deadzoneBP = [0., 9.]
tune.deadzoneV = [.0, .15]
if candidate in TSS2_CAR:
tune.kpBP = [0., 5., 20.]
tune.kpV = [1.3, 1.0, 0.7]
tune.kiBP = [0., 5., 12., 20., 27.]
tune.kiV = [.35, .23, .20, .17, .1]
tune.kpBP = [5., 35.]
tune.kpV = [0.5, 0.5]
tune.kiBP = [0.]
tune.kiV = [.25]
if candidate in TSS2_CAR:
ret.vEgoStopping = 0.25
ret.vEgoStarting = 0.25
ret.vEgoStopping = 0.1
ret.vEgoStarting = 0.1
ret.stoppingDecelRate = 0.3 # reach stopping target smoothly

# some notes:
# longcontrol has smart accel calculation that takes into account the difference between:
# - ideal accel given current planned speed and future planned speed from actuator delay
# - the current planned accel
# and outputs an acceleration to move your current planned accel towards the ideal accel from the speeds.
# this likely accounts for slightly incorrect actuator delay, but i'm guessing it can't account for sudden changes in accel and will undershoot

# try playing around with this:
"""
t = 0.15 # + 0.2
ideal_accel = (23.2273 - 23.7665) / t # future - current plan
a_target_now = -1.16447 # current plan
print(f'{ideal_accel=}')
comp_accel = 2 * ideal_accel - a_target_now
print(f'{comp_accel=}') # this generally matched the accel at plan index 6/7 (0.35s/0.48s)

Out: ideal_accel=-1.5337244444444473 # current plan undershooting this, so longcontrol decreases feedforward accel:
Out: comp_accel=-1.9029788888888948
"""

# by getting the delay more dialed in, a_target_now should move towards ideal_accel, causing its effect to lessen and lag to go down

# TODO: is the reason current planned accel doesn't match future planned speeds because the planner is constrained to start near the ego?

# TODO: we add 0.2s delay to lat, why not long?
ret.longitudinalActuatorDelayLowerBound = 0.15 + 0.15
ret.longitudinalActuatorDelayUpperBound = 0.15 + 0.25
else:
tune.kpBP = [0., 5., 35.]
tune.kiBP = [0., 35.]
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/car/toyota/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class CarControllerParams:
ACCEL_MAX = 1.5 # m/s2, lower than allowed 2.0 m/s2 for tuning reasons
ACCEL_MAX = 2.0 # m/s2, lower than allowed 2.0 m/s2 for tuning reasons
ACCEL_MIN = -3.5 # m/s2

STEER_STEP = 1
Expand Down
Loading