Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions opendbc/car/tesla/carcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from opendbc.car.tesla.values import CarControllerParams, CANBUS, LEGACY_CARS, CAR
from opendbc.car.vehicle_model import VehicleModel
from opendbc.car.tesla.preap.carcontroller import PreAPLongController, init_preap_can
from opendbc.car.tesla.preap.nap_conf import nap_conf
from opendbc.car.tesla.preap.stock_cc_spoofer import StockCCSpoofer


Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(self, dbc_names, CP):
self.preap_long = PreAPLongController()
self.stock_cc = StockCCSpoofer()
self.tesla_can = init_preap_can(dbc_names, self.packers)
self.radar_vin_idx = 0
else:
self.tesla_can = TeslaCANRaven(self.packers)

Expand Down Expand Up @@ -121,6 +123,17 @@ def _update_preap(self, CC, CS, now_nanos):
if self.stock_cc.pcc_event:
CS.pccEvent = self.stock_cc.pcc_event

# Tinkla 0.6.6 donor contract: stream VIN/position/EPAS on 0x560
# when radar is on. Empty VIN is 17 spaces (this-car passthrough);
# position and EPAS still apply. Panda stays silent until all three
# fragments arrive, so 10 Hz keeps that pause around 300 ms.
if nap_conf.radar_enabled and self.frame % 10 == 0:
can_sends.append(self.tesla_can.create_radar_vin_msg(
self.radar_vin_idx, nap_conf.radar_donor_vin, True,
nap_conf.radar_position, nap_conf.radar_epas_type,
))
self.radar_vin_idx = (self.radar_vin_idx + 1) % 3

# Turn-signal drive: keep the indicator flashing during the lane-change
# arming window and maneuver. controlsd sets CC.leftBlinker/rightBlinker
# whenever laneChangeState != off, and clears them when it returns to off
Expand Down
37 changes: 19 additions & 18 deletions opendbc/car/tesla/preap/carcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,27 @@ def init_preap_can(dbc_names, packers):
ENGAGE_GRACE_PEDAL_RAMP_RATE_UP = 0.9 # DI/update at 50Hz

# The pedal controller updates at 50 Hz. Prompt when a sustained deceleration
# request is not being delivered while the command sits deep in the regen
# range. Available regen varies with SOC and battery temperature, so proximity
# to the DI rail is not a reliable "at the limit" signal: on a full battery the
# rail delivers a fraction of nominal regen, and the integrator only reaches it
# near the end of an under-delivery event. Field capture (2026-07-22, minutes
# after supercharging) showed the command within 0.5 DI of the rail for just
# 0.3 s of a multi-second under-delivery, so a rail gate plus a consecutive
# dwell can never complete. Evidence instead accumulates in a saturating
# up/down counter, which also rides through single-update shortfall noise.
# Trigger and clear thresholds are separated so the visible prompt cannot
# chatter.
REGEN_DECEL_PROMPT_DWELL_UPDATES = 40 # 0.8s of net evidence at 50Hz
# request is not being delivered while the command is in the regen range.
# Available regen varies with SOC and battery temperature, so proximity to
# the DI rail is not a reliable "at the limit" signal: on a full battery the
# rail delivers a fraction of nominal regen, and the integrator only reaches
# it near the end of an under-delivery event. Field capture (2026-07-22,
# minutes after supercharging) showed the command within 0.5 DI of the rail
# for just 0.3 s of a multi-second under-delivery, so a rail gate plus a
# consecutive dwell can never complete. Evidence instead accumulates in a
# saturating up/down counter, which also rides through single-update
# shortfall noise. Trigger and clear thresholds are separated so the visible
# prompt cannot chatter. Thresholds are biased early so the driver has time
# to add friction brake.
REGEN_DECEL_PROMPT_DWELL_UPDATES = 25 # 0.5s of net evidence at 50Hz
REGEN_DECEL_PROMPT_MIN_SPEED = 2.0 # m/s; do not prompt for a stopped/settling car
REGEN_DECEL_PROMPT_CLEAR_SPEED = 1.0 # m/s
REGEN_DECEL_SHORTFALL_TRIGGER = 0.35 # m/s²
REGEN_DECEL_SHORTFALL_CLEAR = 0.15 # m/s²
REGEN_COMMAND_TRIGGER_DI = -2.0 # command deep in the regen range
REGEN_COMMAND_CLEAR_DI = -1.0
REGEN_DECEL_REQUEST_TRIGGER = -0.5 # m/s²; a meaningful deceleration request
REGEN_DECEL_REQUEST_CLEAR = -0.2 # m/s²
REGEN_DECEL_SHORTFALL_TRIGGER = 0.20 # m/s²
REGEN_DECEL_SHORTFALL_CLEAR = 0.08 # m/s²
REGEN_COMMAND_TRIGGER_DI = -1.0 # command in the regen range
REGEN_COMMAND_CLEAR_DI = -0.5
REGEN_DECEL_REQUEST_TRIGGER = -0.35 # m/s²; a meaningful deceleration request
REGEN_DECEL_REQUEST_CLEAR = -0.15 # m/s²


class RegenDecelMonitor:
Expand Down
7 changes: 1 addition & 6 deletions opendbc/car/tesla/preap/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# Safety param flags matching tesla_preap.h (renumbered — LONG_CONTROL removed as dead code)
PREAP_FLAG_ENABLE_PEDAL = 1
PREAP_FLAG_RADAR_EMULATION = 2
PREAP_FLAG_RADAR_BEHIND_NOSECONE = 4
from opendbc.car.tesla.preap.constants import (
ACCEL_PREAP_BP, ACCEL_PREAP_PROFILES,
PEDAL_LONG_K_BP, PEDAL_LONG_KP_V, PEDAL_LONG_KI_V,
Expand Down Expand Up @@ -37,16 +36,12 @@ def get_preap_params(ret, fingerprint):
flags = 0
use_pedal = nap_conf.use_pedal
radar_enabled = nap_conf.radar_enabled
radar_behind_nosecone = nap_conf.radar_behind_nosecone
carlog.info("Pre-AP fingerprint: use_pedal=%s radar_enabled=%s behind_nosecone=%s",
use_pedal, radar_enabled, radar_behind_nosecone)
carlog.info("Pre-AP fingerprint: use_pedal=%s radar_enabled=%s", use_pedal, radar_enabled)

if use_pedal:
flags |= PREAP_FLAG_ENABLE_PEDAL
if radar_enabled:
flags |= PREAP_FLAG_RADAR_EMULATION
if radar_behind_nosecone:
flags |= PREAP_FLAG_RADAR_BEHIND_NOSECONE

ret.safetyConfigs = [
get_safety_config(structs.CarParams.SafetyModel.teslaPreap, int(flags)),
Expand Down
77 changes: 77 additions & 0 deletions opendbc/car/tesla/preap/nap_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,23 @@
'pedal_calib_zero': 0.0,
'pedal_calib_factor': 1.0,
'radar_enabled': False,
'radar_ignore_hw_fail': False,
'radar_behind_nosecone': False,
'radar_offset': 0.0,
'radar_donor_vin': '',
'radar_epas_type': 0,
'radar_position': 0,
}


def normalize_radar_donor_vin(value):
"""Return a 17-char donor VIN, or empty if the value is not one."""
text = "" if value is None else str(value)
if isinstance(value, bytes):
text = value.decode("ascii", errors="ignore")
vin = "".join(ch for ch in text.upper() if ch.isalnum())
return vin if len(vin) == 17 else ""

# Pedal DI (Driver Intent) constants — internal representation before calibration
PEDAL_DI_MIN = -5 # Max regen (coasting hard)
PEDAL_DI_ZERO = 0 # Neutral
Expand Down Expand Up @@ -149,6 +162,36 @@ def _put_param_float(self, param_key, json_key, value):
_params.put(param_key, float(value))
self._put(json_key, float(value))

def _get_param_int(self, param_key, json_key, default):
if _PARAMS_AVAILABLE:
val = _params.get(param_key, return_default=True)
try:
return int(val)
except (TypeError, ValueError):
return default
return int(self._get(json_key, default))

def _put_param_int(self, param_key, json_key, value):
if _PARAMS_AVAILABLE:
_params.put(param_key, int(value))
self._put(json_key, int(value))

def _get_param_str(self, param_key, json_key, default=""):
if _PARAMS_AVAILABLE:
val = _params.get(param_key, return_default=True)
if val is None:
return default
if isinstance(val, bytes):
return val.decode("ascii", errors="ignore")
return str(val)
return str(self._get(json_key, default))

def _put_param_str(self, param_key, json_key, value):
text = str(value)
if _PARAMS_AVAILABLE:
_params.put(param_key, text)
self._put(json_key, text)

# Bool properties

@property
Expand All @@ -167,6 +210,14 @@ def radar_enabled(self):
def radar_enabled(self, value):
self._put_param_bool(NAPParamKeys.RADAR_ENABLED, 'radar_enabled', value)

@property
def radar_ignore_hw_fail(self):
return self._get_param_bool(NAPParamKeys.RADAR_IGNORE_HW_FAIL, 'radar_ignore_hw_fail')

@radar_ignore_hw_fail.setter
def radar_ignore_hw_fail(self, value):
self._put_param_bool(NAPParamKeys.RADAR_IGNORE_HW_FAIL, 'radar_ignore_hw_fail', value)

@property
def radar_behind_nosecone(self):
return self._get_param_bool(NAPParamKeys.RADAR_BEHIND_NOSECONE, 'radar_behind_nosecone')
Expand All @@ -175,6 +226,30 @@ def radar_behind_nosecone(self):
def radar_behind_nosecone(self, value):
self._put_param_bool(NAPParamKeys.RADAR_BEHIND_NOSECONE, 'radar_behind_nosecone', value)

@property
def radar_donor_vin(self):
return normalize_radar_donor_vin(self._get_param_str(NAPParamKeys.RADAR_DONOR_VIN, 'radar_donor_vin', ''))

@radar_donor_vin.setter
def radar_donor_vin(self, value):
self._put_param_str(NAPParamKeys.RADAR_DONOR_VIN, 'radar_donor_vin', normalize_radar_donor_vin(value))

@property
def radar_epas_type(self):
return max(0, min(4, self._get_param_int(NAPParamKeys.RADAR_EPAS_TYPE, 'radar_epas_type', 0)))

@radar_epas_type.setter
def radar_epas_type(self, value):
self._put_param_int(NAPParamKeys.RADAR_EPAS_TYPE, 'radar_epas_type', max(0, min(4, int(value))))

@property
def radar_position(self):
return max(0, min(2, self._get_param_int(NAPParamKeys.RADAR_POSITION, 'radar_position', 0)))

@radar_position.setter
def radar_position(self, value):
self._put_param_int(NAPParamKeys.RADAR_POSITION, 'radar_position', max(0, min(2, int(value))))

@property
def pedal_calibrated(self):
if not self._get_param_bool(NAPParamKeys.PEDAL_CALIB_DONE, 'pedal_calibrated'):
Expand Down Expand Up @@ -349,6 +424,7 @@ def print_config(self):
print("")
print(" [RADAR]")
print(f" Radar Enabled: {'ON' if self.radar_enabled else 'OFF'}")
print(f" Ignore HWFail: {'ON' if self.radar_ignore_hw_fail else 'OFF'}")
print(f" Behind Nosecone: {'YES' if self.radar_behind_nosecone else 'NO'}")
print(f" Radar Offset: {self.radar_offset}m")
print("")
Expand All @@ -369,6 +445,7 @@ def get_all_params(self):
'pedal_can_bus': self.pedal_can_bus,
'pedal_can_zero': self.pedal_can_zero,
'radar_enabled': self.radar_enabled,
'radar_ignore_hw_fail': self.radar_ignore_hw_fail,
'radar_behind_nosecone': self.radar_behind_nosecone,
'radar_offset': self.radar_offset,
}
Expand Down
12 changes: 12 additions & 0 deletions opendbc/car/tesla/preap/nap_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ class NAPParamKeys:

# Radar
RADAR_ENABLED = "NAPRadarEnabled"
RADAR_HUD = "NAPRadarHud"
RADAR_IGNORE_HW_FAIL = "NAPRadarIgnoreHwFail"
RADAR_BEHIND_NOSECONE = "NAPRadarBehindNosecone"
RADAR_OFFSET = "NAPRadarOffset"
RADAR_DONOR_VIN = "NAPRadarDonorVin"
RADAR_EPAS_TYPE = "NAPRadarEpasType"
RADAR_POSITION = "NAPRadarPosition"
RADAR_READ_VIN = "NAPRadarReadVin"
RADAR_VIN_READ_STATUS = "NAPRadarVinReadStatus"

# iBooster / Braking
IBOOSTER_ENABLED = "NAPiBoosterEnabled"
Expand All @@ -48,8 +55,13 @@ class NAPParamKeys:
NAPParamKeys.PEDAL_CALIB_FACTOR: 1.0,
NAPParamKeys.PEDAL_CALIB_ZERO: 0.0,
NAPParamKeys.RADAR_ENABLED: False,
NAPParamKeys.RADAR_HUD: False,
NAPParamKeys.RADAR_IGNORE_HW_FAIL: False,
NAPParamKeys.RADAR_BEHIND_NOSECONE: False,
NAPParamKeys.RADAR_OFFSET: 0.0,
NAPParamKeys.RADAR_DONOR_VIN: "",
NAPParamKeys.RADAR_EPAS_TYPE: 0,
NAPParamKeys.RADAR_POSITION: 0,
NAPParamKeys.IBOOSTER_ENABLED: False,
NAPParamKeys.BRAKE_FACTOR: 1.0,
NAPParamKeys.FORCE_PRE_AP: False,
Expand Down
Loading
Loading