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

302 lines
13 KiB
Python

"""Optional, receive-only Model 3/Y inputs. Never participates in CAN validity."""
from dataclasses import dataclass
from typing import Any
from openpilot.starpilot.common.car_control_config import STALK_HOLD, STALK_TAP, STALK_TAP_FULL, WHEEL_HOLDS
# Raw scroll count in one ~100 ms packet; retain the peak until neutral.
FAST_SCROLL_TICKS = 2
# Wheel tilts and presses, which can end with one stray scroll click as the wheel springs back.
NUDGES = frozenset(f'{side}_wheel_{action}' for side in ('left', 'right') for action in ('left', 'right', 'press'))
# Holding the right stalk down sets the Tesla's own set speed to its detected limit after about 1 s (measured 1.02 to
# 1.07 s into a hold); taps to engage last under 0.4 s. The hold fires earlier, so the comma's own change lands first
# and the Tesla's reply is not adopted as a driver change. A down press shorter than this, started once cruise had been
# on for ENGAGED_BEFORE_PRESS_SECONDS, is a tap while engaged.
HOLD_SECONDS = .6
# A down press counts as "while engaged" only if cruise had already been on for this long, in frame time, when the press
# started. The car engages on the same press that engages cruise, so cruise is already on by that press's first frames.
ENGAGED_BEFORE_PRESS_SECONDS = .5
# The right scroll wheel nudged left or right. Its status arrives every 0.1 s: a quick nudge reads as one or two frames,
# a 1.3 s hold as thirteen. A hold fires once while the wheel is still held, and its release then gives nothing.
WHEEL_HOLD_SECONDS = .6
WHEEL_FRAME_GAP_SECONDS = .35 # three missed frames: start again
WHEEL_TIME_SLACK = .005 # frame times jitter by a few ms: six 0.1 s intervals count as 0.6 s
class WheelHold:
"""Hold of one right scroll wheel nudge, from its 10 Hz pressed bit."""
def __init__(self):
self.clear()
def clear(self):
self.armed = False # a released frame has been seen since the last clear
self.start = None # when the current nudge was first seen
self.fired = False # the current nudge already gave its hold
self.last = None
def update(self, pressed, timestamp):
"""True once, when the nudge has been held for WHEEL_HOLD_SECONDS."""
if self.last is not None and not 0 < timestamp - self.last <= WHEEL_FRAME_GAP_SECONDS:
self.clear()
self.last = timestamp
if not pressed:
self.armed, self.start, self.fired = True, None, False
return False
if not self.armed:
return False
if self.start is None:
self.start = timestamp
return False
if not self.fired and timestamp - self.start >= WHEEL_HOLD_SECONDS - WHEEL_TIME_SLACK:
self.fired = True
return True
return False
@dataclass
class Gesture:
armed: bool = False
key: str | None = None
family: str | None = None
rank: int = 0
def clear(self):
self.armed, self.key, self.family, self.rank = False, None, None, 0
def update(self, values, details=False):
if values is None or len(values) > 1:
self.clear()
return []
if not values:
result = [(self.key, self.rank) if details else self.key] if self.armed and self.key else []
self.clear()
self.armed = True
return result
if not self.armed:
return []
key, family, rank = values[0]
if self.family is not None and family != self.family:
if (self.key in NUDGES and family in (f"{self.key.split('_')[0]}_wheel_up", f"{self.key.split('_')[0]}_wheel_down")
and rank == 1):
# A wheel tilted or pressed often clicks once as it springs back: that click ends the nudge (or press) and is
# not a scroll. Emit the nudge; the next neutral frame arms the next gesture.
result = [(self.key, self.rank) if details else self.key]
self.clear()
return result
self.clear()
elif rank >= self.rank:
self.key, self.family, self.rank = key, family, rank
return []
class ScreenGesture:
"""Asynchronous count-only touch input; emit once on complete release."""
MAX_DURATION = 1.2
def __init__(self):
self.clear()
def clear(self):
self.armed = False
self.started = None
self.peak = 0
self.last = None
def feed(self, data, timestamp, details):
if len(data) != 8 or (self.last is not None and timestamp < self.last):
self.clear()
return []
self.last = timestamp
count = data[3]
if count == 0:
emit = self.armed and self.peak == 3 and self.started is not None and 0 < timestamp - self.started <= self.MAX_DURATION
self.clear()
self.armed, self.last = True, timestamp
return [('screen_three_finger_tap', False) if details else 'screen_three_finger_tap'] if emit else []
if not self.armed:
return []
if self.started is None:
self.started = timestamp
self.peak = max(self.peak, count)
if self.peak > 3 or timestamp - self.started > self.MAX_DURATION:
self.armed = False
return []
class TeslaInputDecoder:
# >3x measured maximum intervals: wheel (mux 1) .2031s, right stalk .2032s.
TIMEOUTS = {0x3c2: .75, 0x229: .75, 0x3df: None}
def __init__(self, bus, *, stalk_hold=False, stalk_tap=False, stalk_tap_full=False, wheel_holds=()):
self.bus = bus
# Only the assigned ones: each right scroll wheel nudge's hold control.
self.wheel_holds = {base: name for base, name in WHEEL_HOLDS.items() if name in wheel_holds}
# Only when assigned: a hold, or a half / full press while engaged, then replaces the down press it would end as.
self.stalk_hold = stalk_hold
self.stalk_tap = stalk_tap
self.stalk_tap_full = stalk_tap_full
# The worker sets this from carState before feeding each batch of frames.
self.cruise_engaged = False
self.reset()
def reset(self):
previous_screen = getattr(self, 'groups', {}).get(0x3df)
released = bool(previous_screen and previous_screen.armed and previous_screen.started is None)
# One Gesture per wheel or stalk message, and the ScreenGesture for the touch count.
self.groups: dict[int, Any] = {address: Gesture() for address in self.TIMEOUTS}
self.groups[0x3df] = ScreenGesture()
# Keep a positively observed released baseline, never an in-progress touch.
self.groups[0x3df].armed = released
self.last = {}
self.counters = {}
self.states = {}
self.lengths = {}
self.down_start = None
self.down_engaged = False
self.wheel_presses = {base: WheelHold() for base in WHEEL_HOLDS}
# Frame time of the first frame seen with cruise on; None while cruise is off.
self.engaged_since = None
@staticmethod
def _wheel(bits):
values = []
for name, shift in [('left_wheel_left',14),('left_wheel_right',3),('left_wheel_press',5),
('right_wheel_left',8),('right_wheel_right',10),('right_wheel_press',12)]:
value = (bits >> shift) & 3
if value not in (1, 2):
return None
if value == 2:
values.append((name, name, 1))
for side, shift in [('left',16),('right',24)]:
value = (bits >> shift) & 63
value = value - 64 if value >= 32 else value
if value:
key = f'{side}_wheel_{"up" if value > 0 else "down"}'
values.append((key, key, abs(value)))
return values
@staticmethod
def _stalk(bits):
"""The right stalk's position and its Park button."""
values = []
def add(value, mapping):
if value == 0:
return True
entry = mapping.get(value)
if entry is None:
return False
values.append(entry)
return True
good = add((bits >> 12) & 7, {
1: ('right_stalk_up_half','right_stalk_up',1), 2: ('right_stalk_up_full','right_stalk_up',2),
3: ('right_stalk_down_half','right_stalk_down',1), 4: ('right_stalk_down_full','right_stalk_down',2),
}) and add((bits >> 16) & 3, {1: ('park_press','park_press',1)})
return values if good else None
def _stalk_down(self, group, values, timestamp, released):
"""The right stalk down from neutral: a hold (fires once while still held; the release then emits nothing) or a tap
while engaged (a release before HOLD_SECONDS of a press that started with cruise already on for
ENGAGED_BEFORE_PRESS_SECONDS). None keeps the plain press."""
if group.armed and values and len(values) == 1 and values[0][1] == 'right_stalk_down':
if self.down_start is None:
self.down_start = timestamp
self.down_engaged = (self.engaged_since is not None and
timestamp - self.engaged_since >= ENGAGED_BEFORE_PRESS_SECONDS)
elif self.stalk_hold and timestamp - self.down_start >= HOLD_SECONDS:
group.clear()
self.down_start = None
return STALK_HOLD
return None
start, engaged = self.down_start, self.down_engaged
self.down_start, self.down_engaged = None, False
if (engaged and start is not None and len(released) == 1 and timestamp - start < HOLD_SECONDS and
released[0] in ('right_stalk_down_half', 'right_stalk_down_full')):
if released[0] == 'right_stalk_down_half' and self.stalk_tap:
return STALK_TAP
if released[0] == 'right_stalk_down_full' and self.stalk_tap_full:
return STALK_TAP_FULL
return None
def _wheel_hold(self, values, timestamp):
"""The assigned hold that this frame completes, or None. Each nudge counts on its own: tilting the other way,
pressing or scrolling the right wheel at the same time is not its hold (one stray scroll click as the wheel moves is
allowed), and the left wheel is its own business."""
if values is None:
for press in self.wheel_presses.values():
press.clear()
return None
right = [(key, rank) for key, _, rank in values if key.startswith('right_wheel_')]
fired = None
for base, press in self.wheel_presses.items():
if any(key != base and not (key.endswith(('_up', '_down')) and rank == 1) for key, rank in right):
press.clear()
continue
if press.update(any(key == base for key, _ in right), timestamp) and fired is None:
fired = self.wheel_holds.get(base)
return fired
def feed(self, address, data, bus, timestamp, *, details=False):
if bus != self.bus or address not in self.groups:
return []
if not self.cruise_engaged:
self.engaged_since = None
elif self.engaged_since is None:
self.engaged_since = timestamp
group = self.groups[address]
if address == 0x3df:
return group.feed(data, timestamp, details)
if len(data) != (8 if address == 0x3c2 else 3):
group.clear()
return []
bits = int.from_bytes(data, 'little')
if address == 0x3c2 and bits & 3 != 1:
if bits & 3 in (2,3):
group.clear()
return []
previous = self.last.get(address)
if previous is not None and timestamp < previous:
group.clear()
return []
if previous is None or timestamp - previous > self.TIMEOUTS[address]:
group.clear()
self.counters.pop(address, None)
self.last[address] = timestamp
if self.lengths.get(address, len(data)) != len(data):
group.clear()
self.counters.pop(address, None)
self.lengths[address] = len(data)
if address != 0x3c2:
counter = (bits >> 8) & 15
previous_counter = self.counters.get(address)
previous_state = self.states.get(address)
self.states[address] = bits >> 12
self.counters[address] = counter
if previous_counter is not None:
delta = (counter - previous_counter) % 16
if delta == 0:
# Captured Park event followed by its periodic release reuses the
# counter. Permit that exact state-bit transition, never a stalk move.
park_only_change = address == 0x229 and previous_state is not None and (previous_state ^ (bits >> 12)) == 16
if not park_only_change:
if previous_state != bits >> 12:
group.clear()
return []
if delta not in (0, 1):
group.clear()
values = self._wheel(bits) if address == 0x3c2 else self._stalk(bits)
hold = self._wheel_hold(values, timestamp) if address == 0x3c2 and self.wheel_holds else None
if hold:
group.clear() # the release that follows is part of this hold
return [(hold, False)] if details else [hold]
result = group.update(values, details=details)
if address == 0x229 and (self.stalk_hold or self.stalk_tap or self.stalk_tap_full):
released = [item[0] if details else item for item in result]
special = self._stalk_down(group, values, timestamp, released)
if special:
return [(special, False)] if details else [special]
if details:
return [(key, address == 0x3c2 and key.endswith(('_wheel_up', '_wheel_down')) and rank >= FAST_SCROLL_TICKS)
for key, rank in result]
return result