mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 06:03:41 +08:00
7dbbb43c3b
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>
299 lines
18 KiB
Python
299 lines
18 KiB
Python
"""Qualified SLC decisions supply a planner cap beneath the driver's SET."""
|
|
|
|
from collections.abc import Mapping
|
|
from dataclasses import dataclass, replace
|
|
|
|
from openpilot.starpilot.longitudinal.cruise_ceiling import CruiseCeiling
|
|
from openpilot.starpilot.speed_limits import acceptance as acc
|
|
from openpilot.starpilot.speed_limits import action_arbitration as arb
|
|
from openpilot.starpilot.speed_limits import overrides as ov
|
|
from openpilot.starpilot.speed_limits import selection as sel
|
|
from openpilot.starpilot.speed_limits import speed_domain as sd
|
|
from openpilot.starpilot.speed_limits.runtime_settings import PedalOverride
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class HostState:
|
|
system_long_available: bool
|
|
long_control_active: bool
|
|
selfdrive_enabled: bool
|
|
force_decel: bool
|
|
driver_v_cruise_kph: float
|
|
selected_cluster_kph: float | None
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Frame:
|
|
now_ns: int
|
|
observations: Mapping[sel.Source, acc.Observation]
|
|
selection_policy: sel.SelectionPolicy
|
|
authority: acc.Authority
|
|
acceptance_policy: acc.Policy
|
|
offset_schedule: sd.OffsetSchedule
|
|
selected_kind: sd.DomainStatus
|
|
ego_pair: sd.SpeedPair
|
|
pedal_pressed: bool | None
|
|
host: HostState
|
|
action: acc.DriverAction | None = None
|
|
adopt: acc.AdoptRequest | None = None
|
|
classified_cruise: arb.Result | None = None
|
|
is_metric: bool | None = None
|
|
adoption_observations: Mapping[sel.Source, acc.Observation] | None = None
|
|
confirmation_started_ns: int | None = None
|
|
pedal_override: PedalOverride = PedalOverride.OFF
|
|
preview_reverted: bool = False
|
|
confirmation_grace_ns: int = 0
|
|
wheel_override_enabled: bool = False
|
|
wheel_override_blocked: bool = False
|
|
wheel_override_since_ns: int = 0
|
|
filtered_ego_delta_mps: float | None = None
|
|
# Tesla set-speed rule: SLC writes SET through card, a driver's SET change holds through the override module, and
|
|
# no planner ceiling is supplied. set_speed_ready: card's last SLC command receipt has reached carState.
|
|
set_speed_rule: bool = False
|
|
set_speed_ready: bool = True
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class State:
|
|
session_id: str
|
|
acceptance: acc.State
|
|
last_now_ns: int | None = None
|
|
reset_required: bool = False
|
|
pedal_target_mps: float = 0.
|
|
pedal_blocked_until_release: bool = False
|
|
wheel_override_metric: bool | None = None
|
|
wheel_override_since_ns: int = 0
|
|
override: ov.State | None = None # Tesla set-speed rule only
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Result:
|
|
state: State
|
|
selection: sel.Selection | None = None
|
|
acceptance: acc.Decision | None = None
|
|
domain: sd.DomainResolution | None = None
|
|
coordinate: sd.PlannerCoordinate | None = None
|
|
ceiling: CruiseCeiling | None = None
|
|
status: str = "none"
|
|
errors: tuple[str, ...] = ()
|
|
override: ov.Decision | None = None # Tesla set-speed rule only
|
|
|
|
|
|
def _state_valid(state: object) -> bool:
|
|
if not isinstance(state, State) or not isinstance(state.acceptance, acc.State):
|
|
return False
|
|
return (type(state.session_id) is str and bool(state.session_id) and state.acceptance.session_id == state.session_id and
|
|
acc.number(state.pedal_target_mps) and (state.last_now_ns is None or acc.integer(state.last_now_ns)) and
|
|
type(state.reset_required) is bool and type(state.pedal_blocked_until_release) is bool and
|
|
(state.wheel_override_metric is None or type(state.wheel_override_metric) is bool) and
|
|
acc.integer(state.wheel_override_since_ns) and
|
|
(state.override is None or isinstance(state.override, ov.State) and state.override.session_id == state.session_id))
|
|
|
|
|
|
def _host_valid(host: object) -> bool:
|
|
return (isinstance(host, HostState) and
|
|
all(type(value) is bool for value in (host.system_long_available, host.long_control_active,
|
|
host.selfdrive_enabled, host.force_decel)) and
|
|
acc.number(host.driver_v_cruise_kph) and
|
|
(host.selected_cluster_kph is None or acc.number(host.selected_cluster_kph)))
|
|
|
|
|
|
def new_session(session_id: str, previous_limit: acc.AcceptedLimit | None = None) -> State:
|
|
if previous_limit is not None:
|
|
if not isinstance(previous_limit, acc.AcceptedLimit):
|
|
raise ValueError('invalid previous speed limit')
|
|
acc.new_session(previous_limit.session_id, previous_limit)
|
|
# Remember the value without carrying an old source episode into this session.
|
|
candidate = replace(previous_limit.candidate,
|
|
observation_identity=acc.ObservationIdentity(acc.IdentityKind.SESSION_VALUE, session_id=session_id))
|
|
previous_limit = acc.AcceptedLimit(candidate, session_id, 0)
|
|
return State(session_id, acc.new_session(session_id, previous_limit))
|
|
|
|
|
|
def step(state: State, frame: Frame) -> Result:
|
|
if not _state_valid(state):
|
|
return Result(state, status="invalid_state", errors=("invalid composition state",))
|
|
if state.reset_required:
|
|
return Result(state, status="session_reset_required", errors=("new session required",))
|
|
if (not isinstance(frame, Frame) or not acc.integer(frame.now_ns) or
|
|
(state.last_now_ns is not None and frame.now_ns < state.last_now_ns) or
|
|
not _host_valid(frame.host) or not acc.authority_is_valid(frame.authority) or
|
|
not isinstance(frame.acceptance_policy, acc.Policy) or
|
|
not isinstance(frame.pedal_override, PedalOverride) or
|
|
not acc.integer(frame.wheel_override_since_ns) or
|
|
any(type(value) is not bool for value in (frame.acceptance_policy.confirm_lower,
|
|
frame.acceptance_policy.confirm_higher,
|
|
frame.acceptance_policy.display_only,
|
|
frame.wheel_override_enabled, frame.wheel_override_blocked)) or
|
|
not isinstance(frame.selected_kind, sd.DomainStatus) or not isinstance(frame.ego_pair, sd.SpeedPair) or
|
|
(frame.filtered_ego_delta_mps is not None and not acc.number(frame.filtered_ego_delta_mps)) or
|
|
(frame.is_metric is not None and type(frame.is_metric) is not bool) or
|
|
type(frame.set_speed_rule) is not bool or type(frame.set_speed_ready) is not bool):
|
|
return Result(replace(state, reset_required=True), status="invalid_frame", errors=("invalid frame or clock",))
|
|
host = frame.host
|
|
selected_initialized = host.driver_v_cruise_kph > 0 and host.driver_v_cruise_kph != 255.0
|
|
host_long = host.system_long_available and host.long_control_active and host.selfdrive_enabled
|
|
system_mode = frame.authority.mode in (acc.Mode.LONGITUDINAL_ONLY, acc.Mode.COMBINED) and frame.authority.owner is acc.LongitudinalOwner.SYSTEM
|
|
if (system_mode and frame.authority.longitudinal_active != host_long or
|
|
frame.authority.accelerator_override and not (host.system_long_available and host.selfdrive_enabled and frame.pedal_pressed is True)):
|
|
return Result(replace(state, last_now_ns=frame.now_ns, reset_required=True),
|
|
status="host_authority_disagreement", errors=("host and system-long authority disagree",))
|
|
button_cruise = (frame.authority.owner is acc.LongitudinalOwner.CRUISE_BUTTONS and
|
|
host.selfdrive_enabled and not host.system_long_available and not host.long_control_active)
|
|
engaged_path = (frame.authority.can_control and (host_long or frame.authority.accelerator_override or button_cruise) and
|
|
selected_initialized and not frame.acceptance_policy.display_only)
|
|
active_path = engaged_path and not host.force_decel
|
|
base = replace(state, last_now_ns=frame.now_ns, pedal_target_mps=0., wheel_override_metric=None, wheel_override_since_ns=0)
|
|
|
|
if frame.classified_cruise is not None:
|
|
classified = frame.classified_cruise
|
|
if (not isinstance(classified, arb.Result) or classified.errors or not isinstance(classified.state, arb.Ledger) or
|
|
not isinstance(classified.change, arb.ClassifiedChange) or classified.state.session_id != state.session_id or
|
|
classified.change.session_id != state.session_id or classified.state.reset_required or
|
|
classified.state.last_timestamp_ns != frame.now_ns or
|
|
classified.state.last_effect_id != classified.change.effect_id or
|
|
not isinstance(classified.change.disposition, arb.Disposition) or
|
|
classified.status != classified.change.disposition.value or frame.action is not None or frame.adopt is not None):
|
|
return Result(replace(base, reset_required=True), status="invalid_ledger_result",
|
|
errors=("invalid, stale or ambiguous causal ledger result",))
|
|
selected = sel.select_limit(frame.adoption_observations if frame.adoption_observations is not None else frame.observations,
|
|
frame.selection_policy)
|
|
if selected.errors:
|
|
return Result(replace(base, reset_required=True), selection=selected, status="invalid_selection", errors=selected.errors)
|
|
ego = frame.ego_pair
|
|
candidate = selected.observation.candidate
|
|
matches_set = False
|
|
if (active_path and frame.set_speed_ready and candidate is not None and frame.selected_kind is sd.DomainStatus.VALID and
|
|
ego.status is sd.DomainStatus.VALID and ego.session_id == state.session_id and ego.timestamp_ns == frame.now_ns and
|
|
acc.number(ego.raw_mps) and acc.number(ego.cluster_mps)):
|
|
assert ego.raw_mps is not None and ego.cluster_mps is not None
|
|
target = sd.adjusted_limit(candidate.speed_mps, frame.offset_schedule, minimum_target_mps=ego.delta_mps)
|
|
matches_set = target is not None and sd.set_speed_matches(
|
|
target, host.driver_v_cruise_kph, host.selected_cluster_kph, frame.is_metric)
|
|
acceptance = acc.step(base.acceptance, selected.observation, frame.authority, frame.acceptance_policy,
|
|
now_ns=frame.now_ns, action=frame.action, adopt=frame.adopt, set_speed_matches=matches_set,
|
|
confirmation_started_ns=frame.confirmation_started_ns,
|
|
confirmation_grace_ns=frame.confirmation_grace_ns)
|
|
base = replace(base, acceptance=acceptance.state)
|
|
if acceptance.errors:
|
|
return Result(replace(base, reset_required=True), selected, acceptance,
|
|
status="invalid_acceptance", errors=acceptance.errors)
|
|
|
|
if frame.selected_kind is sd.DomainStatus.VALID and selected_initialized and host.selected_cluster_kph is not None:
|
|
pair = sd.selected_pair_from_kph(host.driver_v_cruise_kph / 3.6, host.selected_cluster_kph,
|
|
session_id=state.session_id, timestamp_ns=frame.now_ns)
|
|
elif selected_initialized:
|
|
pair = sd.SpeedPair(frame.selected_kind)
|
|
else:
|
|
pair = sd.SpeedPair(sd.DomainStatus.ABSENT)
|
|
if frame.set_speed_rule:
|
|
return _set_speed_rule(base, frame, selected, acceptance, pair, active_path)
|
|
domain = sd.resolve(acceptance, frame.offset_schedule, pair, frame.ego_pair)
|
|
if domain.status is sd.DomainStatus.INVALID:
|
|
return Result(replace(base, reset_required=True), selected, acceptance, domain,
|
|
status="invalid_domain", errors=(domain.reason,))
|
|
clear_override = frame.preview_reverted or acceptance.basis in ('driver_accept', 'driver_adopt')
|
|
clear_wheel = clear_override or state.acceptance.accepted != acceptance.state.accepted
|
|
wheel_metric = state.wheel_override_metric if frame.wheel_override_enabled and not clear_wheel else None
|
|
previous = 0. if clear_override else state.pedal_target_mps
|
|
pedal_blocked = (state.pedal_blocked_until_release or clear_override) and (
|
|
frame.pedal_pressed is not False or ego.status is not sd.DomainStatus.VALID)
|
|
base = replace(base, pedal_blocked_until_release=pedal_blocked)
|
|
if not active_path or domain.status is not sd.DomainStatus.VALID:
|
|
# Longitudinal output can resume one frame after the driver releases the pedal.
|
|
if (system_mode and host.selfdrive_enabled and not engaged_path and selected_initialized and
|
|
not (host.force_decel or frame.acceptance_policy.display_only) and
|
|
ego.status is sd.DomainStatus.VALID and pair.status is sd.DomainStatus.VALID):
|
|
base = replace(base, pedal_target_mps=previous if frame.pedal_override is not PedalOverride.OFF else 0.,
|
|
wheel_override_metric=wheel_metric)
|
|
return Result(base, selected, acceptance, domain, status='inactive_or_unavailable')
|
|
context = domain.context
|
|
assert context is not None
|
|
selected_cluster = context.selected_raw_mps + context.selected_delta_mps
|
|
if frame.wheel_override_enabled and frame.is_metric is not None:
|
|
since = 0 if clear_wheel else state.wheel_override_since_ns
|
|
pending = acceptance.state.pending
|
|
ready = (host_long and not frame.pedal_pressed and not frame.wheel_override_blocked and
|
|
(pending is None or pending.automatic))
|
|
adjusted_limit = context.effective_cluster_mps
|
|
change = frame.classified_cruise.change if frame.classified_cruise is not None else None
|
|
# A tap belongs to the eligibility interval at button-down, even if SET changes later.
|
|
if (wheel_metric is None and ready and since > 0 and frame.wheel_override_since_ns == since and change is not None and
|
|
change.disposition is arb.Disposition.DRIVER_INTENT and change.previous_mps is not None and
|
|
not sd.set_above_limit(change.previous_mps, adjusted_limit, frame.is_metric)):
|
|
wheel_metric = frame.is_metric
|
|
# A display-unit edit can round two different speeds to the same number.
|
|
if wheel_metric is not None and not sd.set_above_limit(selected_cluster, adjusted_limit, wheel_metric):
|
|
wheel_metric = None
|
|
base = replace(base, wheel_override_metric=wheel_metric,
|
|
wheel_override_since_ns=(since or frame.now_ns) if ready else 0)
|
|
if wheel_metric is not None:
|
|
coordinate = sd.PlannerCoordinate(None, 'wheel', selected_cluster, context.ego_delta_mps)
|
|
return Result(base, selected, acceptance, domain, coordinate, status=acceptance.basis)
|
|
|
|
pedal_target = 0.
|
|
if frame.pedal_override is not PedalOverride.OFF:
|
|
ego_cluster = context.ego_raw_mps + context.ego_delta_mps
|
|
gas_above_limit = not pedal_blocked and frame.authority.accelerator_override and ego_cluster > context.effective_cluster_mps
|
|
if gas_above_limit or previous > context.effective_cluster_mps:
|
|
pedal_target = (selected_cluster if frame.pedal_override is PedalOverride.SET_SPEED else
|
|
min(selected_cluster, max(previous, ego_cluster if gas_above_limit else 0.)))
|
|
if pedal_target <= context.effective_cluster_mps:
|
|
pedal_target = 0.
|
|
base = replace(base, pedal_target_mps=pedal_target)
|
|
coordinate = sd.to_planner_coordinate(context, override_mps=pedal_target)
|
|
if coordinate.cap_mps is None:
|
|
return Result(replace(base, reset_required=True), selected, acceptance, domain, coordinate,
|
|
status="invalid_coordinate", errors=("planner coordinate unavailable",))
|
|
if frame.filtered_ego_delta_mps is not None and pedal_target == 0.:
|
|
coordinate = sd.to_planner_coordinate(replace(context, ego_delta_mps=frame.filtered_ego_delta_mps),
|
|
override_mps=pedal_target)
|
|
if coordinate.cap_mps is None:
|
|
return Result(replace(base, reset_required=True), selected, acceptance, domain, coordinate,
|
|
status="invalid_coordinate", errors=("planner coordinate unavailable",))
|
|
return Result(base, selected, acceptance, domain, coordinate,
|
|
ceiling=CruiseCeiling(coordinate.cap_mps, frame.authority),
|
|
status=acceptance.basis)
|
|
|
|
|
|
def _override_evidence(kind: sd.DomainStatus) -> ov.EvidenceKind:
|
|
return {sd.DomainStatus.VALID: ov.EvidenceKind.VALID, sd.DomainStatus.ABSENT: ov.EvidenceKind.ABSENT,
|
|
sd.DomainStatus.STALE: ov.EvidenceKind.STALE}.get(kind, ov.EvidenceKind.UNKNOWN)
|
|
|
|
|
|
def _set_speed_rule(base: State, frame: Frame, selected: sel.Selection, acceptance: acc.Decision,
|
|
pair: sd.SpeedPair, active_path: bool) -> Result:
|
|
"""Tesla set-speed rule: the accepted limit plus offset becomes SET through card, and a driver's own SET change
|
|
holds until the limit changes. A cap beneath SET would hold the car under a SET the driver raised, so none is supplied."""
|
|
if acceptance.control_target_mps is not None and acceptance.state.observation.kind is not acc.ObservationKind.VALID:
|
|
# A source gap carries no target: SET stays where it is until a current reading returns.
|
|
acceptance = replace(acceptance, control_target_mps=None)
|
|
domain = sd.resolve(acceptance, frame.offset_schedule, pair, frame.ego_pair)
|
|
ego = frame.ego_pair
|
|
ego_valid = ego.status is sd.DomainStatus.VALID
|
|
change = frame.classified_cruise.change if frame.classified_cruise is not None else None
|
|
override = ov.step(base.override if base.override is not None else ov.new_session(base.session_id),
|
|
acceptance, frame.authority, frame.acceptance_policy,
|
|
ov.SelectedEvidence(_override_evidence(pair.status), pair.raw_mps if pair.status is sd.DomainStatus.VALID else None),
|
|
ov.PedalEvidence(_override_evidence(ego.status), frame.pedal_pressed if ego_valid else None,
|
|
ego.raw_mps if ego_valid else None),
|
|
now_ns=frame.now_ns, change=change, domain=domain)
|
|
base = replace(base, override=override.state)
|
|
if override.errors or domain.status is sd.DomainStatus.INVALID:
|
|
return Result(replace(base, reset_required=True), selected, acceptance, domain, override=override,
|
|
status="invalid_domain_or_override", errors=override.errors or (domain.reason,))
|
|
if not active_path or domain.status is not sd.DomainStatus.VALID:
|
|
return Result(base, selected, acceptance, domain, override=override,
|
|
status=acceptance.basis if acceptance.basis == 'source_switch_pending' else 'inactive_or_unavailable')
|
|
context = domain.context
|
|
if (context is None or override.state.last_timestamp_ns != frame.now_ns or
|
|
override.state.accepted_mps != context.accepted_raw_mps or
|
|
override.state.effective_target_mps != context.effective_cluster_mps):
|
|
return Result(replace(base, reset_required=True), selected, acceptance, domain, override=override,
|
|
status="invalid_join", errors=("override and speed domain mismatch",))
|
|
coordinate = sd.override_planner_coordinate(context, override.contribution_mps)
|
|
if coordinate.cap_mps is None:
|
|
return Result(replace(base, reset_required=True), selected, acceptance, domain, coordinate, override=override,
|
|
status="invalid_coordinate", errors=("planner coordinate unavailable",))
|
|
return Result(base, selected, acceptance, domain, coordinate, override=override, status=acceptance.basis)
|