From 1f5570a96df6e16e43bb219f726e17b33e2032a4 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 18 Feb 2022 00:45:00 -0800 Subject: [PATCH] Longitudinal control: interpolate longitudinal plan (#23787) * interpolate longitudinal actuator delay rename * formatting * interpolate v_target most importantly! * fix interpolation and rename * nicer setup * left in from testing * update refs old-commit-hash: 2c7845fce0be2bfa72a5d2429daa35fb735c154c --- selfdrive/car/interfaces.py | 1 + selfdrive/controls/controlsd.py | 3 ++- selfdrive/controls/lib/longcontrol.py | 15 ++++++++------- selfdrive/test/process_replay/ref_commit | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/selfdrive/car/interfaces.py b/selfdrive/car/interfaces.py index cf40bd6227..98ede7fad1 100644 --- a/selfdrive/car/interfaces.py +++ b/selfdrive/car/interfaces.py @@ -97,6 +97,7 @@ class CarInterfaceBase(ABC): ret.longitudinalTuning.kpV = [1.] ret.longitudinalTuning.kiBP = [0.] ret.longitudinalTuning.kiV = [1.] + # TODO estimate car specific lag, use .15s for now ret.longitudinalActuatorDelayLowerBound = 0.15 ret.longitudinalActuatorDelayUpperBound = 0.15 ret.steerLimitTimer = 1.0 diff --git a/selfdrive/controls/controlsd.py b/selfdrive/controls/controlsd.py index f9260762ed..1a34d31311 100755 --- a/selfdrive/controls/controlsd.py +++ b/selfdrive/controls/controlsd.py @@ -492,7 +492,8 @@ class Controls: if not self.joystick_mode: # accel PID loop pid_accel_limits = self.CI.get_pid_accel_limits(self.CP, CS.vEgo, self.v_cruise_kph * CV.KPH_TO_MS) - actuators.accel = self.LoC.update(self.active, CS, self.CP, long_plan, pid_accel_limits) + t_since_plan = (self.sm.frame - self.sm.rcv_frame['longitudinalPlan']) * DT_CTRL + actuators.accel = self.LoC.update(self.active, CS, self.CP, long_plan, pid_accel_limits, t_since_plan) # Steering PID loop and lateral MPC lat_active = self.active and not CS.steerWarning and not CS.steerError and CS.vEgo > self.CP.minSteerSpeed diff --git a/selfdrive/controls/lib/longcontrol.py b/selfdrive/controls/lib/longcontrol.py index c33b332be8..3ba50fd0cf 100644 --- a/selfdrive/controls/lib/longcontrol.py +++ b/selfdrive/controls/lib/longcontrol.py @@ -53,20 +53,21 @@ class LongControl(): self.pid.reset() self.v_pid = v_pid - def update(self, active, CS, CP, long_plan, accel_limits): + def update(self, active, CS, CP, long_plan, accel_limits, t_since_plan): """Update longitudinal control. This updates the state machine and runs a PID loop""" # Interp control trajectory - # TODO estimate car specific lag, use .15s for now speeds = long_plan.speeds if len(speeds) == CONTROL_N: - v_target_lower = interp(CP.longitudinalActuatorDelayLowerBound, T_IDXS[:CONTROL_N], speeds) - a_target_lower = 2 * (v_target_lower - speeds[0])/CP.longitudinalActuatorDelayLowerBound - long_plan.accels[0] + v_target = interp(t_since_plan, T_IDXS[:CONTROL_N], speeds) + a_target = interp(t_since_plan, T_IDXS[:CONTROL_N], long_plan.accels) - v_target_upper = interp(CP.longitudinalActuatorDelayUpperBound, T_IDXS[:CONTROL_N], speeds) - a_target_upper = 2 * (v_target_upper - speeds[0])/CP.longitudinalActuatorDelayUpperBound - long_plan.accels[0] + v_target_lower = interp(CP.longitudinalActuatorDelayLowerBound + t_since_plan, T_IDXS[:CONTROL_N], speeds) + a_target_lower = 2 * (v_target_lower - v_target) / CP.longitudinalActuatorDelayLowerBound - a_target + + v_target_upper = interp(CP.longitudinalActuatorDelayUpperBound + t_since_plan, T_IDXS[:CONTROL_N], speeds) + a_target_upper = 2 * (v_target_upper - v_target) / CP.longitudinalActuatorDelayUpperBound - a_target a_target = min(a_target_lower, a_target_upper) - v_target = speeds[0] v_target_future = speeds[-1] else: v_target = 0.0 diff --git a/selfdrive/test/process_replay/ref_commit b/selfdrive/test/process_replay/ref_commit index 6c915d8f8f..41c6c2aa59 100644 --- a/selfdrive/test/process_replay/ref_commit +++ b/selfdrive/test/process_replay/ref_commit @@ -1 +1 @@ -302258d8bdfea779c546f76c191ed451b18062f5 \ No newline at end of file +24c4d6b8d49b42416f2ca6a59d563f7b8c984e2b \ No newline at end of file