Files
StarPilot/openpilot/starpilot/controllers/tesla_input_protocol.py
T
Claude 7dbbb43c3b Add Tesla Model 3/Y wheel controls and comma-set dashboard speed
Everything here is limited to Tesla Model 3 and Model Y; other cars are unchanged. Most of it needs the Tesla CAN
add-on cable (Vehicle CAN).

Wheel Controls
- Right scroll wheel nudge left / right and holding either, right stalk half and full press while engaged, and a
  three-finger tap on the Tesla screen as assignable actions. Scroll up / down and stalk Down / Up are shown grayed,
  the scroll rows with their step (1, or to the next 5 for a fast scroll).
  Nudge right starts on Cycle Driving Personality.
- The three-finger tap's Always On Lateral on/off is chosen on its own row here: the Three-Finger AOL Tap row leaves
  Vehicle Settings, and TeslaAOLScreenTap is saved on by default.
- The Short press and Hold step rows are left out on a Model 3/Y, where StarPilot never steps the set speed from
  cruise buttons.
- A receive-only worker reads the wheel, stalk and screen frames. It only runs on a Model 3/Y with the cable.

Vehicle Settings
- Tesla CAN add-on cable row: detected or not, with buy and install links.
- Comma can set speed on Tesla dashboard (TeslaSpeedSync): off by default, experimental, needs the cable and Alpha
  Longitudinal, not offered on release builds. Takes effect within a second, without a restart.

With the switch on
- StarPilot keeps its own set speed; the right scroll wheel changes it by 1, or to the next multiple of 5.
- The Tesla's set speed is scrolled to match with right-scroll-wheel frames on Vehicle CAN, and scrolled back if the
  Tesla changes it.
- Speed Limit Controller writes the limit plus offset into the set speed instead of capping beneath it. Adopt limits
  early writes it early, and back again if the Maps preview is withdrawn. After an accelerator override: with Hold the
  speed you reached, lifting off above the limit makes the speed reached the set speed until the limit changes.

With the switch off, Speed Limit Controller and the set speed behave as before.

Also adds the Tesla dashboard speed limit (DAS_fusedSpeedLimit) as a Speed Limit Controller Dashboard source.

Panda: adds opendbc/safety/modes/tesla_speed_sync.h and safety-param bit 4096 (debug builds only), with tests. The bit
is set at startup on every eligible Model 3/Y so the switch works without a restart, so the images need rebuilding.
Always On Lateral's two exact-word checks accept the new bit. test_screen_recovery_callers.py and
test_tesla_screen_integration.py expect the bit on a car with the cable and StarPilot longitudinal.

Schema: SlcAction.ControllerCruiseAction gains stepUnits @13 and sourceTeslaGesture @14 (new TeslaGesture struct),
SlcAction.Kind gains cruiseStep @13. Settings keys: TeslaSpeedSync, CarControlAssignments.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-23 18:32:41 +00:00

70 lines
2.8 KiB
Python

"""Tesla input worker: private receiver protocol; no authority is granted by the worker."""
import hashlib
import json
import math
from openpilot.starpilot.conditional_mode.ui_action import fresh_service
MAX_PACKET = 2048
MAX_EVENTS = 4
MAX_AGE = .25
# The worker's first message: it is up and reading CAN.
READY = b'TESLA_INPUT_READY'
def drive_id(sm, now):
try:
now_ns = int(now * 1e9)
drive = int(sm['deviceState'].startedMonoTime)
if (sm['deviceState'].started and 0 < drive < now_ns
and fresh_service(sm, 'deviceState', drive, now_ns, 1_000_000_000)
and fresh_service(sm, 'carState', drive, now_ns)
and sm['carState'].canValid and not sm['carState'].canTimeout):
return drive
except (AttributeError, KeyError, TypeError, ValueError, OverflowError):
pass
return None
def document_hash(raw):
return hashlib.sha256(raw).hexdigest()
def encode_event(sequence, name, fast, stamp, drive, document, vehicle, *, provenance=None):
value = {'version': 1, 'sequence': sequence, 'name': name, 'fast': bool(fast),
'stamp': stamp, 'drive': drive, 'document': document, 'vehicle': vehicle}
if provenance is not None:
# Which physical scroll this was (speed sync matches it to the frame it saw on the car's bus).
from openpilot.starpilot.controllers.tesla_gesture_context import source_from_event
value.update(version=2, provenance=provenance)
if source_from_event(value) is None:
raise ValueError("Invalid Tesla gesture provenance")
return json.dumps(value, separators=(',', ':')).encode()
def decode_event(raw, now):
try:
if not raw or len(raw) > MAX_PACKET:
return None
value = json.loads(raw)
keys = {'version', 'sequence', 'name', 'fast', 'stamp', 'drive', 'document', 'vehicle'}
if isinstance(value, dict) and value.get('version') == 2:
keys.add('provenance')
if (not isinstance(value, dict) or set(value) != keys
or type(value['version']) is not int or value['version'] not in (1, 2)
or type(value['sequence']) is not int or value['sequence'] <= 0
or type(value['name']) is not str or type(value['fast']) is not bool
or type(value['stamp']) not in (int, float) or not math.isfinite(value['stamp'])
or not 0 <= now - value['stamp'] <= MAX_AGE
or type(value['drive']) is not int or not 0 < value['drive'] < value['stamp'] * 1e9
or any(type(value[k]) is not str or len(value[k]) != 64
or any(c not in '0123456789abcdef' for c in value[k]) for k in ('document', 'vehicle'))):
return None
if value['version'] == 2:
from openpilot.starpilot.controllers.tesla_gesture_context import source_from_event
if source_from_event(value) is None:
return None
return value
except (ValueError, TypeError, UnicodeError, OverflowError):
return None