You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
2.2 KiB
54 lines
2.2 KiB
4 years ago
|
from common.numpy_fast import clip, interp
|
||
|
from selfdrive.car.tesla.teslacan import TeslaCAN
|
||
|
from opendbc.can.packer import CANPacker
|
||
|
from selfdrive.car.tesla.values import CANBUS, CarControllerParams
|
||
|
|
||
|
class CarController():
|
||
|
def __init__(self, dbc_name, CP, VM):
|
||
|
self.CP = CP
|
||
|
self.last_angle = 0
|
||
|
self.packer = CANPacker(dbc_name)
|
||
|
self.tesla_can = TeslaCAN(dbc_name, self.packer)
|
||
|
|
||
|
def update(self, enabled, CS, frame, actuators, cruise_cancel):
|
||
|
can_sends = []
|
||
|
|
||
|
# Temp disable steering on a hands_on_fault, and allow for user override
|
||
|
hands_on_fault = (CS.steer_warning == "EAC_ERROR_HANDS_ON" and CS.hands_on_level >= 3)
|
||
|
lkas_enabled = enabled and (not hands_on_fault)
|
||
|
|
||
|
if lkas_enabled:
|
||
|
apply_angle = actuators.steeringAngleDeg
|
||
|
|
||
|
# Angular rate limit based on speed
|
||
|
steer_up = (self.last_angle * apply_angle > 0. and abs(apply_angle) > abs(self.last_angle))
|
||
|
rate_limit = CarControllerParams.RATE_LIMIT_UP if steer_up else CarControllerParams.RATE_LIMIT_DOWN
|
||
|
max_angle_diff = interp(CS.out.vEgo, rate_limit.speed_points, rate_limit.max_angle_diff_points)
|
||
|
apply_angle = clip(apply_angle, (self.last_angle - max_angle_diff), (self.last_angle + max_angle_diff))
|
||
|
|
||
|
# To not fault the EPS
|
||
|
apply_angle = clip(apply_angle, (CS.out.steeringAngleDeg - 20), (CS.out.steeringAngleDeg + 20))
|
||
|
else:
|
||
|
apply_angle = CS.out.steeringAngleDeg
|
||
|
|
||
|
self.last_angle = apply_angle
|
||
|
can_sends.append(self.tesla_can.create_steering_control(apply_angle, lkas_enabled, frame))
|
||
|
|
||
|
# Cancel on user steering override, since there is no steering torque blending
|
||
|
if hands_on_fault:
|
||
|
cruise_cancel = True
|
||
|
|
||
|
# Cancel when openpilot is not enabled anymore
|
||
|
if not enabled and bool(CS.out.cruiseState.enabled):
|
||
|
cruise_cancel = True
|
||
|
|
||
|
if ((frame % 10) == 0 and cruise_cancel):
|
||
|
# Spam every possible counter value, otherwise it might not be accepted
|
||
|
for counter in range(16):
|
||
|
can_sends.append(self.tesla_can.create_action_request(CS.msg_stw_actn_req, cruise_cancel, CANBUS.chassis, counter))
|
||
|
can_sends.append(self.tesla_can.create_action_request(CS.msg_stw_actn_req, cruise_cancel, CANBUS.autopilot, counter))
|
||
|
|
||
|
# TODO: HUD control
|
||
|
|
||
|
return can_sends
|