2021-03-12 13:08:51 +08:00
|
|
|
import math
|
|
|
|
|
2020-01-18 04:48:30 +08:00
|
|
|
from cereal import log
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.latcontrol import LatControl
|
2024-09-01 07:49:29 +08:00
|
|
|
from openpilot.common.pid import PIDController
|
2020-01-18 04:48:30 +08:00
|
|
|
|
|
|
|
|
2022-01-27 00:10:41 +08:00
|
|
|
class LatControlPID(LatControl):
|
2021-10-04 16:03:53 +08:00
|
|
|
def __init__(self, CP, CI):
|
2022-01-27 00:10:41 +08:00
|
|
|
super().__init__(CP, CI)
|
2022-04-08 02:34:45 +08:00
|
|
|
self.pid = PIDController((CP.lateralTuning.pid.kpBP, CP.lateralTuning.pid.kpV),
|
2022-04-28 15:42:52 +08:00
|
|
|
(CP.lateralTuning.pid.kiBP, CP.lateralTuning.pid.kiV),
|
|
|
|
k_f=CP.lateralTuning.pid.kf, pos_limit=self.steer_max, neg_limit=-self.steer_max)
|
2021-10-21 17:38:03 +08:00
|
|
|
self.get_steer_feedforward = CI.get_steer_feedforward_function()
|
2020-01-18 04:48:30 +08:00
|
|
|
|
|
|
|
def reset(self):
|
2022-01-27 00:10:41 +08:00
|
|
|
super().reset()
|
2020-01-18 04:48:30 +08:00
|
|
|
self.pid.reset()
|
|
|
|
|
2024-08-14 12:11:16 +08:00
|
|
|
def update(self, active, CS, VM, params, steer_limited, desired_curvature, calibrated_pose):
|
2020-01-18 04:48:30 +08:00
|
|
|
pid_log = log.ControlsState.LateralPIDState.new_message()
|
2021-02-17 13:39:32 +08:00
|
|
|
pid_log.steeringAngleDeg = float(CS.steeringAngleDeg)
|
|
|
|
pid_log.steeringRateDeg = float(CS.steeringRateDeg)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2021-12-17 09:34:12 +08:00
|
|
|
angle_steers_des_no_offset = math.degrees(VM.get_steer_from_curvature(-desired_curvature, CS.vEgo, params.roll))
|
2021-03-12 13:08:51 +08:00
|
|
|
angle_steers_des = angle_steers_des_no_offset + params.angleOffsetDeg
|
2022-04-08 02:34:45 +08:00
|
|
|
error = angle_steers_des - CS.steeringAngleDeg
|
2021-03-12 13:08:51 +08:00
|
|
|
|
2021-12-16 20:08:20 +08:00
|
|
|
pid_log.steeringAngleDesiredDeg = angle_steers_des
|
2022-04-08 02:34:45 +08:00
|
|
|
pid_log.angleError = error
|
2023-01-11 12:51:10 +08:00
|
|
|
if not active:
|
2020-01-18 04:48:30 +08:00
|
|
|
output_steer = 0.0
|
|
|
|
pid_log.active = False
|
|
|
|
self.pid.reset()
|
|
|
|
else:
|
2021-10-04 16:03:53 +08:00
|
|
|
# offset does not contribute to resistive torque
|
|
|
|
steer_feedforward = self.get_steer_feedforward(angle_steers_des_no_offset, CS.vEgo)
|
2021-03-12 13:08:51 +08:00
|
|
|
|
2022-04-08 02:34:45 +08:00
|
|
|
output_steer = self.pid.update(error, override=CS.steeringPressed,
|
|
|
|
feedforward=steer_feedforward, speed=CS.vEgo)
|
2020-01-18 04:48:30 +08:00
|
|
|
pid_log.active = True
|
|
|
|
pid_log.p = self.pid.p
|
|
|
|
pid_log.i = self.pid.i
|
|
|
|
pid_log.f = self.pid.f
|
|
|
|
pid_log.output = output_steer
|
2022-07-25 05:56:55 +08:00
|
|
|
pid_log.saturated = self._check_saturation(self.steer_max - abs(output_steer) < 1e-3, CS, steer_limited)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2021-07-01 05:19:39 +08:00
|
|
|
return output_steer, angle_steers_des, pid_log
|