mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 09:03:43 +08:00
Resume Corolla TSS2 from a PCM hold behind moving traffic
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Vehicle-owned longitudinal behavior at the shared control boundary."""
|
||||
from dataclasses import dataclass
|
||||
import math
|
||||
|
||||
from openpilot.common.realtime import DT_CTRL
|
||||
from openpilot.starpilot.longitudinal.force_stop_braking import eligible as distance_braking_eligible
|
||||
@@ -26,6 +27,7 @@ class LongitudinalContext:
|
||||
profile_max_accel: float | None = None
|
||||
faster_takeoffs: bool = False
|
||||
force_stop_braking: bool = False
|
||||
toyota_resume_fresh: bool = False
|
||||
|
||||
|
||||
class LongitudinalExtension:
|
||||
@@ -40,6 +42,7 @@ class LongitudinalExtension:
|
||||
self.vehicle_stop = self.vehicle_policy.stop_policy() if hasattr(self.vehicle_policy, "stop_policy") else None
|
||||
toyota_production = production_enabled(CP)
|
||||
self.toyota_stopped_lead = toyota_production and str(CP.carFingerprint) == COROLLA
|
||||
self.toyota_pcm_resume = self.toyota_stopped_lead and CP.autoResumeSng
|
||||
self.toyota_output = ToyotaOutputPolicy(CP) if toyota_production or development_enabled(CP) else None
|
||||
self.ioniq6_start = Ioniq6StartPolicy() if ioniq6_start_eligible(CP) else None
|
||||
self.force_stop_distance_braking = distance_braking_eligible(CP)
|
||||
@@ -95,6 +98,14 @@ class LongitudinalExtension:
|
||||
if self.stopping_policy is not None:
|
||||
state = self.stopping_policy.transition(state, previous_state, active, CS, a_target, should_stop,
|
||||
has_lead=context.has_lead)
|
||||
if (self.toyota_pcm_resume and active and
|
||||
CS.standstill and CS.cruiseState.standstill):
|
||||
# The PCM clears its hold after the controller sends a positive request.
|
||||
can_resume = (context.toyota_resume_fresh and CS.canValid and not CS.canTimeout and not CS.accFaulted and
|
||||
not CS.stockAeb and CS.cruiseState.enabled and not CS.brakePressed and not CS.gasPressed and
|
||||
not should_stop and not context.force_stop_braking and math.isfinite(a_target) and a_target >= 0.1 and
|
||||
self.toyota_output.moving_lead_for_resume(context.leads))
|
||||
return LongCtrlState.pid if can_resume else LongCtrlState.stopping
|
||||
if (self.toyota_stopped_lead and active and previous_state == LongCtrlState.stopping and
|
||||
state != LongCtrlState.off and not should_stop and not CS.brakePressed and
|
||||
self.toyota_output.hold_stopped_lead(CS.vEgo, context.leads)):
|
||||
|
||||
@@ -173,7 +173,10 @@ class LongitudinalInputs:
|
||||
if self.ev9_long_enabled:
|
||||
return self._ev9_context(active)
|
||||
if self.toyota_sienna_replay or self.toyota_sienna_production or self.toyota_corolla_stop:
|
||||
return LongitudinalContext(leads=self._toyota_leads() if active else None)
|
||||
leads = self._toyota_leads() if active else None
|
||||
resume_fresh = (leads is not None and self.toyota_corolla_stop and self.sm.all_checks(['longitudinalPlan']) and
|
||||
self.sm['carState'].cruiseState.standstill and self.resume_sources_current())
|
||||
return LongitudinalContext(leads=leads, toyota_resume_fresh=resume_fresh)
|
||||
cc_evidence = (self.gm_cc_evidence if self.gm_cc_enabled else self._gm_cc_evidence()
|
||||
if self.gm_cc_stop_enabled and active else None)
|
||||
volt_observation = self._gm_volt_observation() if self.gm_volt_enabled and active else None
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
from dataclasses import replace
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from opendbc.car import gen_empty_fingerprint, structs
|
||||
from opendbc.car.toyota.interface import CarInterface
|
||||
from opendbc.car.toyota.values import CAR
|
||||
from openpilot.selfdrive.controls.lib.longcontrol import LongControl
|
||||
from openpilot.starpilot.longitudinal.extension import LongitudinalContext
|
||||
from openpilot.starpilot.longitudinal.inputs import LongitudinalInputs, ResumeFreshness
|
||||
from openpilot.starpilot.longitudinal.toyota_output_policy import Lead
|
||||
|
||||
|
||||
LONG_STATE = structs.CarControl.Actuators.LongControlState
|
||||
MOVING_LEAD = Lead(True, 23.0, 0.4, 2.0, 0.0)
|
||||
STOPPED_LEAD = Lead(True, 5.0, 0.2, 0.0, 0.0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('blocker', (None, 'brake', 'gas', 'aeb', 'can', 'plan', 'hold', 'stale', 'no_lead', 'stopped_lead', 'auto_resume_off'))
|
||||
def test_corolla_pcm_hold_requires_current_plan_and_moving_lead(blocker):
|
||||
cp = CarInterface.get_params(CAR.TOYOTA_COROLLA_TSS2, gen_empty_fingerprint(), [], False, False, False)
|
||||
assert cp.autoResumeSng and cp.safetyConfigs[0].safetyParam == 73
|
||||
if blocker == 'auto_resume_off':
|
||||
cp.autoResumeSng = False
|
||||
driver_state = structs.CarState(vEgo=0., standstill=True, canValid=True)
|
||||
driver_state.cruiseState.enabled = driver_state.cruiseState.standstill = True
|
||||
driver_state.brakePressed = blocker == 'brake'
|
||||
driver_state.gasPressed = blocker == 'gas'
|
||||
driver_state.stockAeb = blocker == 'aeb'
|
||||
driver_state.canTimeout = blocker == 'can'
|
||||
leads = () if blocker == 'no_lead' else (MOVING_LEAD, STOPPED_LEAD) if blocker == 'stopped_lead' else (MOVING_LEAD,)
|
||||
context = LongitudinalContext(leads=leads, toyota_resume_fresh=blocker != 'stale', force_stop_braking=blocker == 'hold')
|
||||
owner = LongControl(cp)
|
||||
for _ in range(20):
|
||||
assert owner.update(True, driver_state, -0.3, True, (-3.5, 2.0), context=context) < 0
|
||||
target, should_stop = (0.8, blocker == 'plan')
|
||||
outputs = [owner.update(True, driver_state, target, should_stop, (-3.5, 2.0), context=context) for _ in range(60)]
|
||||
if blocker is None:
|
||||
assert owner.long_control_state == LONG_STATE.pid
|
||||
assert outputs[-1] > 0
|
||||
assert owner.update(True, driver_state, target, False, (-3.5, 2.0),
|
||||
context=replace(context, toyota_resume_fresh=False)) < 0
|
||||
assert owner.long_control_state == LONG_STATE.stopping
|
||||
else:
|
||||
assert owner.long_control_state == LONG_STATE.stopping
|
||||
assert max(outputs) <= 0
|
||||
|
||||
|
||||
def test_corolla_resume_evidence_requires_current_car_and_plan(monkeypatch):
|
||||
names = ('carState', 'longitudinalPlan', 'deviceState')
|
||||
now = [1_050_000_000]
|
||||
car_state = structs.CarState()
|
||||
car_state.cruiseState.standstill = True
|
||||
class Messages(SimpleNamespace):
|
||||
def __getitem__(self, name):
|
||||
return {'carState': car_state, 'deviceState': SimpleNamespace(started=True, startedMonoTime=900_000_000)}[name]
|
||||
sm = Messages(seen={'deviceState': True}, alive={'deviceState': True}, valid={'deviceState': True},
|
||||
all_checks=lambda requested: bool(requested) and requested[0] in names)
|
||||
def publish():
|
||||
sm.logMonoTime = dict.fromkeys(names, now[0] - 5_000_000)
|
||||
sm.recv_time = dict.fromkeys(names, (now[0] - 2_000_000) / 1e9)
|
||||
|
||||
monkeypatch.setattr('openpilot.starpilot.longitudinal.inputs.clock_pair_ns', lambda: (now[0], now[0] + 500_000_000))
|
||||
inputs = LongitudinalInputs.__new__(LongitudinalInputs)
|
||||
inputs.messages = lambda: sm
|
||||
inputs.resume_freshness = ResumeFreshness()
|
||||
inputs.ev9_long_enabled = inputs.toyota_sienna_replay = inputs.toyota_sienna_production = False
|
||||
inputs.toyota_corolla_stop = True
|
||||
inputs._toyota_leads = lambda: (MOVING_LEAD,)
|
||||
publish()
|
||||
assert not inputs._vehicle_context(True).toyota_resume_fresh
|
||||
now[0] += 20_000_000
|
||||
publish()
|
||||
assert inputs._vehicle_context(True).toyota_resume_fresh
|
||||
now[0] += 170_000_000
|
||||
publish()
|
||||
sm.logMonoTime['longitudinalPlan'] = now[0] - 160_000_000
|
||||
assert not inputs._vehicle_context(True).toyota_resume_fresh
|
||||
assert not inputs._vehicle_context(False).toyota_resume_fresh
|
||||
@@ -196,6 +196,13 @@ class ToyotaOutputPolicy:
|
||||
lead.present and 0 < lead.distance_m <= 8.0 and
|
||||
abs(lead.lateral_m) <= 1.75 and abs(lead.speed_mps) <= 0.35 for lead in leads))
|
||||
|
||||
def moving_lead_for_resume(self, leads):
|
||||
if not self._leads_valid(leads):
|
||||
return False
|
||||
nearest = min((lead for lead in leads if lead.present and lead.distance_m > 0 and abs(lead.lateral_m) <= 1.75),
|
||||
key=lambda lead: lead.distance_m, default=None)
|
||||
return nearest is not None and nearest.speed_mps > 0.35
|
||||
|
||||
def target(self, a_target: float, v_ego: float, should_stop: bool, last_output_accel: float,
|
||||
*, leads: tuple[Lead, ...] | None = None) -> float | None:
|
||||
"""Return a shaped target, or None for invalid/missing required evidence."""
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "37357ae5b7e795297ce284ada9f2877cf922a7eb887c982701e23f0e7e9c5fe4", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "582581d52a9d85c1d9b2579daa76ad6d3c0273e408a365cced75c74ecfdf844e", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user