Files
StarPilot/openpilot/starpilot/controllers/tesla_gesture_context.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

80 lines
2.9 KiB
Python

"""Speed sync: optional gesture identity. Carries context, never dispatches actions."""
from contextlib import contextmanager
from contextvars import ContextVar
from dataclasses import dataclass
import re
HEX32 = re.compile(r"[0-9a-f]{32}\Z")
HEX64 = re.compile(r"[0-9a-f]{64}\Z")
_FIELDS = {"version", "workerSession", "sequence", "sourceCanBootTime", "sourceCanFrameIndex",
"name", "fast", "assignmentsHash", "vehicleHash", "driveStartMonoTime"}
_current = ContextVar("tesla_gesture", default=None)
@dataclass(frozen=True)
class GestureSource:
worker_session: str
sequence: int
boot_ns: int
frame_index: int
name: str
fast: bool
document: str
vehicle: str
drive: int
def to_wire(self):
return {"version": 1, "workerSession": self.worker_session, "sequence": self.sequence,
"sourceCanBootTime": self.boot_ns, "sourceCanFrameIndex": self.frame_index,
"name": self.name, "fast": self.fast, "assignmentsHash": self.document,
"vehicleHash": self.vehicle, "driveStartMonoTime": self.drive}
@property
def step(self):
"""The comma speed step this scroll produces: one unit, or five (to the next multiple of five) for a fast scroll."""
return (1 if self.name == "right_wheel_up" else -1) * (5 if self.fast else 1)
def parse_source(value):
try:
if not isinstance(value, dict) or set(value) != _FIELDS:
return None
if (type(value["version"]) is not int or value["version"] != 1 or
any(type(value[k]) is not int or not 0 < value[k] < 2**64
for k in ("sequence", "sourceCanBootTime", "driveStartMonoTime")) or
type(value["sourceCanFrameIndex"]) is not int or not 0 <= value["sourceCanFrameIndex"] < 4096 or
type(value["fast"]) is not bool or value["name"] not in ("right_wheel_up", "right_wheel_down") or
type(value["workerSession"]) is not str or not HEX32.fullmatch(value["workerSession"]) or
any(type(value[k]) is not str or not HEX64.fullmatch(value[k])
for k in ("assignmentsHash", "vehicleHash"))):
return None
return GestureSource(value["workerSession"], value["sequence"], value["sourceCanBootTime"],
value["sourceCanFrameIndex"], value["name"], value["fast"],
value["assignmentsHash"], value["vehicleHash"], value["driveStartMonoTime"])
except (KeyError, TypeError, ValueError):
return None
def source_from_event(event):
source = parse_source(event.get("provenance"))
if source is None:
return None
if (source.sequence != event["sequence"] or source.name != event["name"] or
source.fast != event["fast"] or source.document != event["document"] or
source.vehicle != event["vehicle"] or source.drive != event["drive"]):
return None
return source
@contextmanager
def source_scope(event):
token = _current.set(source_from_event(event))
try:
yield
finally:
_current.reset(token)
def current_source():
return _current.get()