Release 260111
This commit is contained in:
74
selfdrive/car/CARS_template.md
Normal file
74
selfdrive/car/CARS_template.md
Normal file
@@ -0,0 +1,74 @@
|
||||
{% set footnote_tag = '[<sup>{}</sup>](#footnotes)' %}
|
||||
{% set star_icon = '[](##)' %}
|
||||
{% set video_icon = '<a href="{}" target="_blank"><img height="18px" src="assets/icon-youtube.svg"></img></a>' %}
|
||||
{# Force hardware column wider by using a blank image with max width. #}
|
||||
{% set width_tag = '<a href="##"><img width=2000></a>%s<br> ' %}
|
||||
{% set hardware_col_name = 'Hardware Needed' %}
|
||||
{% set wide_hardware_col_name = width_tag|format(hardware_col_name) -%}
|
||||
|
||||
<!--- AUTOGENERATED FROM selfdrive/car/CARS_template.md, DO NOT EDIT. --->
|
||||
|
||||
# Supported Cars
|
||||
|
||||
A supported vehicle is one that just works when you install a comma device. All supported cars provide a better experience than any stock system. Supported vehicles reference the US market unless otherwise specified.
|
||||
|
||||
# {{all_car_docs | selectattr('support_type', 'eq', SupportType.UPSTREAM) | list | length}} Supported Cars
|
||||
|
||||
|{{Column | map(attribute='value') | join('|') | replace(hardware_col_name, wide_hardware_col_name)}}|
|
||||
|---|---|---|{% for _ in range((Column | length) - 3) %}{{':---:|'}}{% endfor +%}
|
||||
{% for car_docs in all_car_docs | selectattr('support_type', 'eq', SupportType.UPSTREAM) %}
|
||||
|{% for column in Column %}{{car_docs.get_column(column, star_icon, video_icon, footnote_tag)}}|{% endfor %}
|
||||
|
||||
{% endfor %}
|
||||
|
||||
### Footnotes
|
||||
{% for footnote in footnotes %}
|
||||
<sup>{{loop.index}}</sup>{{footnote | replace('</br>', '')}} <br />
|
||||
{% endfor %}
|
||||
|
||||
## Community Maintained Cars
|
||||
Although they're not upstream, the community has openpilot running on other makes and models. See the 'Community Supported Models' section of each make [on our wiki](https://wiki.comma.ai/).
|
||||
|
||||
# Don't see your car here?
|
||||
|
||||
**openpilot can support many more cars than it currently does.** There are a few reasons your car may not be supported.
|
||||
If your car doesn't fit into any of the incompatibility criteria here, then there's a good chance it can be supported! We're adding support for new cars all the time. **We don't have a roadmap for car support**, and in fact, most car support comes from users like you!
|
||||
|
||||
### Which cars are able to be supported?
|
||||
|
||||
openpilot uses the existing steering, gas, and brake interfaces in your car. If your car lacks any one of these interfaces, openpilot will not be able to control the car. If your car has [ACC](https://en.wikipedia.org/wiki/Adaptive_cruise_control) and any form of [LKAS](https://en.wikipedia.org/wiki/Automated_Lane_Keeping_Systems)/[LCA](https://en.wikipedia.org/wiki/Lane_centering), then it almost certainly has these interfaces. These features generally started shipping on cars around 2016. Note that manufacturers will often make their own [marketing terms](https://en.wikipedia.org/wiki/Adaptive_cruise_control#Vehicle_models_supporting_adaptive_cruise_control) for these features, such as Hyundai's "Smart Cruise Control" branding of Adaptive Cruise Control.
|
||||
|
||||
If your car has the following packages or features, then it's a good candidate for support.
|
||||
|
||||
| Make | Required Package/Features |
|
||||
| ---- | ------------------------- |
|
||||
| Acura | Any car with AcuraWatch Plus will work. AcuraWatch Plus comes standard on many newer models. |
|
||||
| Ford | Any car with Lane Centering will likely work. |
|
||||
| Honda | Any car with Honda Sensing will work. Honda Sensing comes standard on many newer models. |
|
||||
| Subaru | Any car with EyeSight will work. EyeSight comes standard on many newer models. |
|
||||
| Nissan | Any car with ProPILOT will likely work. |
|
||||
| Toyota & Lexus | Any car that has Toyota/Lexus Safety Sense with "Lane Departure Alert with Steering Assist (LDA w/SA)" and/or "Lane Tracing Assist (LTA)" will work. Note that LDA without Steering Assist will not work. These features come standard on most newer models. |
|
||||
| Hyundai, Kia, & Genesis | Any car with Smart Cruise Control (SCC) and Lane Following Assist (LFA) or Lane Keeping Assist (LKAS) will work. LKAS/LFA comes standard on most newer models. Any form of SCC will work, such as NSCC. |
|
||||
| Chrysler, Jeep, & Ram | Any car with LaneSense and Adaptive Cruise Control will likely work. These come standard on many newer models. |
|
||||
|
||||
### FlexRay
|
||||
|
||||
All the cars that openpilot supports use a [CAN bus](https://en.wikipedia.org/wiki/CAN_bus) for communication between all the car's computers, however a CAN bus isn't the only way that the computers in your car can communicate. Most, if not all, vehicles from the following manufacturers use [FlexRay](https://en.wikipedia.org/wiki/FlexRay) instead of a CAN bus: **BMW, Mercedes, Audi, Land Rover, and some Volvo**. These cars may one day be supported, but we have no immediate plans to support FlexRay.
|
||||
|
||||
### Toyota Security
|
||||
|
||||
openpilot does not yet support these Toyota models due to a new message authentication method.
|
||||
[Vote](https://comma.ai/shop#toyota-security) if you'd like to see openpilot support on these models.
|
||||
|
||||
* Toyota RAV4 Prime 2021+
|
||||
* Toyota Sienna 2021+
|
||||
* Toyota Venza 2021+
|
||||
* Toyota Sequoia 2023+
|
||||
* Toyota Tundra 2022+
|
||||
* Toyota Highlander 2024+
|
||||
* Toyota Corolla Cross 2022+ (only US model)
|
||||
* Toyota Camry 2025+
|
||||
* Lexus NX 2022+
|
||||
* Toyota bZ4x 2023+
|
||||
* Subaru Solterra 2023+
|
||||
|
||||
0
selfdrive/car/__init__.py
Normal file
0
selfdrive/car/__init__.py
Normal file
285
selfdrive/car/car_specific.py
Normal file
285
selfdrive/car/car_specific.py
Normal file
@@ -0,0 +1,285 @@
|
||||
from collections import deque
|
||||
from cereal import car, log
|
||||
import cereal.messaging as messaging
|
||||
from opendbc.car import DT_CTRL, structs
|
||||
from opendbc.car.interfaces import MAX_CTRL_SPEED
|
||||
from opendbc.car.volkswagen.values import CarControllerParams as VWCarControllerParams
|
||||
from opendbc.car.hyundai.interface import ENABLE_BUTTONS as HYUNDAI_ENABLE_BUTTONS
|
||||
from opendbc.car.hyundai.carstate import PREV_BUTTON_SAMPLES as HYUNDAI_PREV_BUTTON_SAMPLES
|
||||
|
||||
from openpilot.selfdrive.selfdrived.events import Events, ET
|
||||
|
||||
from openpilot.common.params import Params
|
||||
|
||||
ButtonType = structs.CarState.ButtonEvent.Type
|
||||
GearShifter = structs.CarState.GearShifter
|
||||
EventName = log.OnroadEvent.EventName
|
||||
NetworkLocation = structs.CarParams.NetworkLocation
|
||||
|
||||
|
||||
# TODO: the goal is to abstract this file into the CarState struct and make events generic
|
||||
class MockCarState:
|
||||
def __init__(self):
|
||||
self.sm = messaging.SubMaster(['gpsLocation', 'gpsLocationExternal'])
|
||||
|
||||
def update(self, CS: car.CarState):
|
||||
self.sm.update(0)
|
||||
gps_sock = 'gpsLocationExternal' if self.sm.recv_frame['gpsLocationExternal'] > 1 else 'gpsLocation'
|
||||
|
||||
CS.vEgo = self.sm[gps_sock].speed
|
||||
CS.vEgoRaw = self.sm[gps_sock].speed
|
||||
|
||||
return CS
|
||||
|
||||
|
||||
class CarSpecificEvents:
|
||||
def __init__(self, CP: structs.CarParams):
|
||||
self.CP = CP
|
||||
|
||||
self.steering_unpressed = 0
|
||||
self.low_speed_alert = False
|
||||
self.no_steer_warning = False
|
||||
self.silent_steer_warning = 1
|
||||
|
||||
self.cruise_buttons: deque = deque([], maxlen=HYUNDAI_PREV_BUTTON_SAMPLES)
|
||||
|
||||
self.do_shutdown = False
|
||||
self.params = Params()
|
||||
self.frame = 0
|
||||
self.mute_door = False
|
||||
self.mute_seatbelt = False
|
||||
self.vCruise_prev = 250
|
||||
self.carrotCruise_prev = False
|
||||
|
||||
def update_params(self):
|
||||
if self.frame % 100 == 0:
|
||||
self.mute_seatbelt = self.params.get_bool("MuteSeatbelt")
|
||||
self.mute_door = self.params.get_bool("MuteDoor")
|
||||
|
||||
def update(self, CS: car.CarState, CS_prev: car.CarState, CC: car.CarControl):
|
||||
self.frame += 1
|
||||
self.update_params()
|
||||
if self.CP.brand in ('body', 'mock'):
|
||||
events = Events()
|
||||
|
||||
elif self.CP.brand == 'ford':
|
||||
events = self.create_common_events(CS, CS_prev, extra_gears=[GearShifter.manumatic])
|
||||
|
||||
elif self.CP.brand == 'nissan':
|
||||
events = self.create_common_events(CS, CS_prev, extra_gears=[GearShifter.brake])
|
||||
|
||||
elif self.CP.brand == 'chrysler':
|
||||
events = self.create_common_events(CS, CS_prev, extra_gears=[GearShifter.low])
|
||||
|
||||
# Low speed steer alert hysteresis logic
|
||||
if self.CP.minSteerSpeed > 0. and CS.vEgo < (self.CP.minSteerSpeed + 0.5):
|
||||
self.low_speed_alert = True
|
||||
elif CS.vEgo > (self.CP.minSteerSpeed + 1.):
|
||||
self.low_speed_alert = False
|
||||
if self.low_speed_alert:
|
||||
events.add(EventName.belowSteerSpeed)
|
||||
|
||||
elif self.CP.brand == 'honda':
|
||||
events = self.create_common_events(CS, CS_prev, pcm_enable=False)
|
||||
|
||||
if self.CP.pcmCruise and CS.vEgo < self.CP.minEnableSpeed:
|
||||
events.add(EventName.belowEngageSpeed)
|
||||
|
||||
if self.CP.pcmCruise:
|
||||
# we engage when pcm is active (rising edge)
|
||||
if CS.cruiseState.enabled and not CS_prev.cruiseState.enabled:
|
||||
events.add(EventName.pcmEnable)
|
||||
elif not CS.cruiseState.enabled and (CC.actuators.accel >= 0. or not self.CP.openpilotLongitudinalControl):
|
||||
# it can happen that car cruise disables while comma system is enabled: need to
|
||||
# keep braking if needed or if the speed is very low
|
||||
if CS.vEgo < self.CP.minEnableSpeed + 2.:
|
||||
# non loud alert if cruise disables below 25mph as expected (+ a little margin)
|
||||
events.add(EventName.speedTooLow)
|
||||
else:
|
||||
events.add(EventName.cruiseDisabled)
|
||||
if self.CP.minEnableSpeed > 0 and CS.vEgo < 0.001:
|
||||
events.add(EventName.manualRestart)
|
||||
|
||||
elif self.CP.brand == 'toyota':
|
||||
events = self.create_common_events(CS, CS_prev)
|
||||
|
||||
if self.CP.openpilotLongitudinalControl:
|
||||
if CS.cruiseState.standstill and not CS.brakePressed:
|
||||
events.add(EventName.resumeRequired)
|
||||
if CS.vEgo < self.CP.minEnableSpeed:
|
||||
events.add(EventName.belowEngageSpeed)
|
||||
if CC.actuators.accel > 0.3:
|
||||
# some margin on the actuator to not false trigger cancellation while stopping
|
||||
events.add(EventName.speedTooLow)
|
||||
if CS.vEgo < 0.001:
|
||||
# while in standstill, send a user alert
|
||||
events.add(EventName.manualRestart)
|
||||
|
||||
elif self.CP.brand == 'gm':
|
||||
events = self.create_common_events(CS, CS_prev, extra_gears=[GearShifter.sport, GearShifter.low,
|
||||
GearShifter.eco, GearShifter.manumatic],
|
||||
pcm_enable=self.CP.pcmCruise)
|
||||
|
||||
# Enabling at a standstill with brake is allowed
|
||||
# TODO: verify 17 Volt can enable for the first time at a stop and allow for all GMs
|
||||
if CS.vEgo < self.CP.minEnableSpeed and not (CS.standstill and CS.brake >= 20 and
|
||||
self.CP.networkLocation == NetworkLocation.fwdCamera):
|
||||
events.add(EventName.belowEngageSpeed)
|
||||
if CS.cruiseState.standstill:
|
||||
events.add(EventName.resumeRequired)
|
||||
if CS.vEgo < self.CP.minSteerSpeed:
|
||||
events.add(EventName.belowSteerSpeed)
|
||||
|
||||
elif self.CP.brand == 'volkswagen':
|
||||
events = self.create_common_events(CS, CS_prev, extra_gears=[GearShifter.eco, GearShifter.sport, GearShifter.manumatic],
|
||||
pcm_enable=self.CP.pcmCruise)
|
||||
|
||||
# Low speed steer alert hysteresis logic
|
||||
if (self.CP.minSteerSpeed - 1e-3) > VWCarControllerParams.DEFAULT_MIN_STEER_SPEED and CS.vEgo < (self.CP.minSteerSpeed + 1.):
|
||||
self.low_speed_alert = True
|
||||
elif CS.vEgo > (self.CP.minSteerSpeed + 2.):
|
||||
self.low_speed_alert = False
|
||||
if self.low_speed_alert:
|
||||
events.add(EventName.belowSteerSpeed)
|
||||
|
||||
if self.CP.openpilotLongitudinalControl:
|
||||
if CS.vEgo < self.CP.minEnableSpeed + 0.5:
|
||||
events.add(EventName.belowEngageSpeed)
|
||||
if CC.enabled and CS.vEgo < self.CP.minEnableSpeed:
|
||||
events.add(EventName.speedTooLow)
|
||||
|
||||
# TODO: this needs to be implemented generically in carState struct
|
||||
# if CC.eps_timer_soft_disable_alert: # type: ignore[attr-defined]
|
||||
# events.add(EventName.steerTimeLimit)
|
||||
|
||||
elif self.CP.brand == 'hyundai':
|
||||
# On some newer model years, the CANCEL button acts as a pause/resume button based on the PCM state
|
||||
# To avoid re-engaging when openpilot cancels, check user engagement intention via buttons
|
||||
# Main button also can trigger an engagement on these cars
|
||||
self.cruise_buttons.append(any(ev.type in HYUNDAI_ENABLE_BUTTONS for ev in CS.buttonEvents))
|
||||
events = self.create_common_events(CS, CS_prev, extra_gears=(GearShifter.sport, GearShifter.manumatic),
|
||||
#pcm_enable=self.CP.pcmCruise, allow_enable=any(self.cruise_buttons), allow_button_cancel=False)
|
||||
pcm_enable=self.CP.pcmCruise, allow_enable=True, allow_button_cancel=False)
|
||||
|
||||
# low speed steer alert hysteresis logic (only for cars with steer cut off above 10 m/s)
|
||||
if CS.vEgo < (self.CP.minSteerSpeed + 2.) and self.CP.minSteerSpeed > 10.:
|
||||
self.low_speed_alert = True
|
||||
if CS.vEgo > (self.CP.minSteerSpeed + 4.):
|
||||
self.low_speed_alert = False
|
||||
if self.low_speed_alert:
|
||||
events.add(EventName.belowSteerSpeed)
|
||||
|
||||
else:
|
||||
events = self.create_common_events(CS, CS_prev)
|
||||
|
||||
if CC.enabled:
|
||||
if self.vCruise_prev == 0 and CS.vCruise > 0:
|
||||
events.add(EventName.audioPrompt)
|
||||
|
||||
if self.carrotCruise_prev != CS.carrotCruise:
|
||||
events.add(EventName.audioPrompt)
|
||||
|
||||
self.carrotCruise_prev = CS.carrotCruise
|
||||
self.vCruise_prev = CS.vCruise
|
||||
|
||||
return events
|
||||
|
||||
def create_common_events(self, CS: structs.CarState, CS_prev: car.CarState, extra_gears=None, pcm_enable=True,
|
||||
allow_enable=True, allow_button_cancel=True):
|
||||
events = Events()
|
||||
|
||||
if CS.doorOpen and not self.mute_door:
|
||||
events.add(EventName.doorOpen)
|
||||
if CS.seatbeltUnlatched and not self.mute_seatbelt:
|
||||
events.add(EventName.seatbeltNotLatched)
|
||||
if CS.gearShifter == GearShifter.park:
|
||||
events.add(EventName.wrongGear)
|
||||
if CS.gearShifter == GearShifter.neutral:
|
||||
events.add(EventName.wrongGear)
|
||||
if CS.gearShifter == GearShifter.reverse:
|
||||
events.add(EventName.reverseGear)
|
||||
if not CS.cruiseState.available:
|
||||
events.add(EventName.wrongCarMode)
|
||||
if CS.espDisabled:
|
||||
events.add(EventName.espDisabled)
|
||||
if CS.espActive:
|
||||
events.add(EventName.espActive)
|
||||
if CS.stockFcw:
|
||||
events.add(EventName.stockFcw)
|
||||
if CS.stockAeb:
|
||||
events.add(EventName.stockAeb)
|
||||
if CS.vEgo > MAX_CTRL_SPEED:
|
||||
events.add(EventName.speedTooHigh)
|
||||
if CS.cruiseState.nonAdaptive:
|
||||
events.add(EventName.wrongCruiseMode)
|
||||
if CS.brakeHoldActive and self.CP.openpilotLongitudinalControl:
|
||||
events.add(EventName.brakeHold)
|
||||
if CS.parkingBrake:
|
||||
events.add(EventName.parkBrake)
|
||||
if CS.accFaulted:
|
||||
events.add(EventName.accFaulted)
|
||||
if CS.steeringPressed:
|
||||
events.add(EventName.steerOverride)
|
||||
if CS.brakePressed and CS.standstill:
|
||||
events.add(EventName.preEnableStandstill)
|
||||
if CS.gasPressed:
|
||||
events.add(EventName.gasPressedOverride)
|
||||
if CS.vehicleSensorsInvalid:
|
||||
events.add(EventName.vehicleSensorsInvalid)
|
||||
if CS.invalidLkasSetting:
|
||||
events.add(EventName.invalidLkasSetting)
|
||||
if CS.lowSpeedAlert:
|
||||
events.add(EventName.belowSteerSpeed)
|
||||
if CS.buttonEnable:
|
||||
events.add(EventName.buttonEnable)
|
||||
|
||||
# Handle cancel button presses
|
||||
for b in CS.buttonEvents:
|
||||
# Disable on rising and falling edge of cancel for both stock and OP long
|
||||
# TODO: only check the cancel button with openpilot longitudinal on all brands to match panda safety
|
||||
if b.type == ButtonType.cancel and (allow_button_cancel or not self.CP.pcmCruise):
|
||||
events.add(EventName.buttonCancel)
|
||||
if CS.gearShifter == GearShifter.park and not self.do_shutdown:
|
||||
self.do_shutdown = True
|
||||
self.params.put_bool("DoShutdown", True)
|
||||
|
||||
# Handle permanent and temporary steering faults
|
||||
self.steering_unpressed = 0 if CS.steeringPressed else self.steering_unpressed + 1
|
||||
if CS.steerFaultTemporary:
|
||||
if CS.steeringPressed and (not CS_prev.steerFaultTemporary or self.no_steer_warning):
|
||||
self.no_steer_warning = True
|
||||
else:
|
||||
self.no_steer_warning = False
|
||||
|
||||
# if the user overrode recently, show a less harsh alert
|
||||
if self.silent_steer_warning > 0 or CS.standstill or self.steering_unpressed < int(1.5 / DT_CTRL):
|
||||
self.silent_steer_warning += 1
|
||||
if self.silent_steer_warning > 20:
|
||||
events.add(EventName.steerTempUnavailableSilent)
|
||||
else:
|
||||
events.add(EventName.steerTempUnavailable)
|
||||
else:
|
||||
self.no_steer_warning = False
|
||||
self.silent_steer_warning = 0
|
||||
if CS.steerFaultPermanent:
|
||||
events.add(EventName.steerUnavailable)
|
||||
|
||||
# we engage when pcm is active (rising edge)
|
||||
# enabling can optionally be blocked by the car interface
|
||||
if pcm_enable:
|
||||
if CS.cruiseState.enabled and not CS_prev.cruiseState.enabled and allow_enable:
|
||||
events.add(EventName.pcmEnable)
|
||||
elif not CS.cruiseState.enabled:
|
||||
events.add(EventName.pcmDisable)
|
||||
|
||||
|
||||
if not self.CP.pcmCruise:
|
||||
if CS.activateCruise > 0 and CS_prev.activateCruise <= 0:
|
||||
if not events.contains(ET.NO_ENTRY):
|
||||
events.add(EventName.buttonEnable)
|
||||
elif CS.activateCruise < 0 and CS_prev.activateCruise >= 0:
|
||||
events.add(EventName.buttonCancel)
|
||||
if CS.softHoldActive > 0:
|
||||
events.add(EventName.softHold)
|
||||
|
||||
return events
|
||||
320
selfdrive/car/card.py
Normal file
320
selfdrive/car/card.py
Normal file
@@ -0,0 +1,320 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import time
|
||||
import threading
|
||||
|
||||
import cereal.messaging as messaging
|
||||
|
||||
from cereal import car, log
|
||||
|
||||
from openpilot.common.params import Params
|
||||
from openpilot.common.realtime import config_realtime_process, Priority, Ratekeeper
|
||||
from openpilot.common.swaglog import cloudlog, ForwardingHandler
|
||||
|
||||
from opendbc.car import DT_CTRL, structs
|
||||
from opendbc.car.can_definitions import CanData, CanRecvCallable, CanSendCallable
|
||||
from opendbc.car.carlog import carlog
|
||||
from opendbc.car.fw_versions import ObdCallback
|
||||
from opendbc.car.car_helpers import get_car, interfaces
|
||||
from opendbc.car.interfaces import CarInterfaceBase, RadarInterfaceBase
|
||||
from opendbc.safety import ALTERNATIVE_EXPERIENCE
|
||||
from openpilot.selfdrive.pandad import can_capnp_to_list, can_list_to_can_capnp
|
||||
from openpilot.selfdrive.car.cruise import VCruiseCarrot
|
||||
from openpilot.selfdrive.car.car_specific import MockCarState
|
||||
|
||||
REPLAY = "REPLAY" in os.environ
|
||||
|
||||
EventName = log.OnroadEvent.EventName
|
||||
|
||||
# forward
|
||||
carlog.addHandler(ForwardingHandler(cloudlog))
|
||||
|
||||
|
||||
def obd_callback(params: Params) -> ObdCallback:
|
||||
def set_obd_multiplexing(obd_multiplexing: bool):
|
||||
if params.get_bool("ObdMultiplexingEnabled") != obd_multiplexing:
|
||||
cloudlog.warning(f"Setting OBD multiplexing to {obd_multiplexing}")
|
||||
params.remove("ObdMultiplexingChanged")
|
||||
params.put_bool("ObdMultiplexingEnabled", obd_multiplexing)
|
||||
params.get_bool("ObdMultiplexingChanged", block=True)
|
||||
cloudlog.warning("OBD multiplexing set successfully")
|
||||
return set_obd_multiplexing
|
||||
|
||||
|
||||
def can_comm_callbacks(logcan: messaging.SubSocket, sendcan: messaging.PubSocket) -> tuple[CanRecvCallable, CanSendCallable]:
|
||||
def can_recv(wait_for_one: bool = False) -> list[list[CanData]]:
|
||||
"""
|
||||
wait_for_one: wait the normal logcan socket timeout for a CAN packet, may return empty list if nothing comes
|
||||
|
||||
Returns: CAN packets comprised of CanData objects for easy access
|
||||
"""
|
||||
ret = []
|
||||
for can in messaging.drain_sock(logcan, wait_for_one=wait_for_one):
|
||||
ret.append([CanData(msg.address, msg.dat, msg.src) for msg in can.can])
|
||||
return ret
|
||||
|
||||
def can_send(msgs: list[CanData]) -> None:
|
||||
sendcan.send(can_list_to_can_capnp(msgs, msgtype='sendcan'))
|
||||
|
||||
return can_recv, can_send
|
||||
|
||||
|
||||
class Car:
|
||||
CI: CarInterfaceBase
|
||||
RI: RadarInterfaceBase
|
||||
CP: car.CarParams
|
||||
|
||||
def __init__(self, CI=None, RI=None) -> None:
|
||||
self.can_sock = messaging.sub_sock('can', timeout=20)
|
||||
self.sm = messaging.SubMaster(['pandaStates', 'carControl', 'onroadEvents', 'carrotMan', 'longitudinalPlan', 'radarState', 'modelV2'])
|
||||
self.pm = messaging.PubMaster(['sendcan', 'carState', 'carParams', 'carOutput', 'liveTracks'])
|
||||
|
||||
self.can_rcv_cum_timeout_counter = 0
|
||||
|
||||
self.CC_prev = car.CarControl.new_message()
|
||||
self.CS_prev = car.CarState.new_message()
|
||||
self.initialized_prev = False
|
||||
|
||||
self.last_actuators_output = structs.CarControl.Actuators()
|
||||
|
||||
self.params = Params()
|
||||
|
||||
self.can_callbacks = can_comm_callbacks(self.can_sock, self.pm.sock['sendcan'])
|
||||
|
||||
is_release = True #self.params.get_bool("IsReleaseBranch")
|
||||
|
||||
if CI is None:
|
||||
# wait for one pandaState and one CAN packet
|
||||
print("Waiting for CAN messages...")
|
||||
while True:
|
||||
can = messaging.recv_one_retry(self.can_sock)
|
||||
if len(can.can) > 0:
|
||||
break
|
||||
|
||||
alpha_long_allowed = self.params.get_bool("AlphaLongitudinalEnabled")
|
||||
num_pandas = len(messaging.recv_one_retry(self.sm.sock['pandaStates']).pandaStates)
|
||||
|
||||
cached_params = None
|
||||
cached_params_raw = self.params.get("CarParamsCache")
|
||||
if cached_params_raw is not None:
|
||||
with car.CarParams.from_bytes(cached_params_raw) as _cached_params:
|
||||
cached_params = _cached_params
|
||||
|
||||
self.CI = get_car(*self.can_callbacks, obd_callback(self.params), alpha_long_allowed, is_release, num_pandas, cached_params)
|
||||
self.RI = interfaces[self.CI.CP.carFingerprint].RadarInterface(self.CI.CP)
|
||||
self.CP = self.CI.CP
|
||||
|
||||
# continue onto next fingerprinting step in pandad
|
||||
self.params.put_bool("FirmwareQueryDone", True)
|
||||
else:
|
||||
self.CI, self.CP = CI, CI.CP
|
||||
self.RI = RI
|
||||
|
||||
# set alternative experiences from parameters
|
||||
disengage_on_accelerator = self.params.get_bool("DisengageOnAccelerator")
|
||||
self.CP.alternativeExperience = 0
|
||||
if not disengage_on_accelerator:
|
||||
self.CP.alternativeExperience |= ALTERNATIVE_EXPERIENCE.DISABLE_DISENGAGE_ON_GAS
|
||||
|
||||
openpilot_enabled_toggle = self.params.get_bool("OpenpilotEnabledToggle")
|
||||
|
||||
controller_available = self.CI.CC is not None and openpilot_enabled_toggle and not self.CP.dashcamOnly
|
||||
|
||||
self.CP.passive = not controller_available or self.CP.dashcamOnly
|
||||
if self.CP.passive:
|
||||
safety_config = structs.CarParams.SafetyConfig()
|
||||
safety_config.safetyModel = structs.CarParams.SafetyModel.noOutput
|
||||
self.CP.safetyConfigs = [safety_config]
|
||||
|
||||
if self.CP.secOcRequired and not is_release:
|
||||
# Copy user key if available
|
||||
try:
|
||||
with open("/cache/params/SecOCKey") as f:
|
||||
user_key = f.readline().strip()
|
||||
if len(user_key) == 32:
|
||||
self.params.put("SecOCKey", user_key)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
secoc_key = self.params.get("SecOCKey", encoding='utf8')
|
||||
if secoc_key is not None:
|
||||
saved_secoc_key = bytes.fromhex(secoc_key.strip())
|
||||
if len(saved_secoc_key) == 16:
|
||||
self.CP.secOcKeyAvailable = True
|
||||
self.CI.CS.secoc_key = saved_secoc_key
|
||||
if controller_available:
|
||||
self.CI.CC.secoc_key = saved_secoc_key
|
||||
else:
|
||||
cloudlog.warning("Saved SecOC key is invalid")
|
||||
|
||||
# Write previous route's CarParams
|
||||
prev_cp = self.params.get("CarParamsPersistent")
|
||||
if prev_cp is not None:
|
||||
self.params.put("CarParamsPrevRoute", prev_cp)
|
||||
|
||||
# Write CarParams for controls and radard
|
||||
cp_bytes = self.CP.to_bytes()
|
||||
self.params.put("CarParams", cp_bytes)
|
||||
self.params.put_nonblocking("CarParamsCache", cp_bytes)
|
||||
self.params.put_nonblocking("CarParamsPersistent", cp_bytes)
|
||||
|
||||
self.mock_carstate = MockCarState()
|
||||
self.v_cruise_helper = VCruiseCarrot(self.CP) #VCruiseHelper(self.CP)
|
||||
|
||||
self.is_metric = self.params.get_bool("IsMetric")
|
||||
self.experimental_mode = self.params.get_bool("ExperimentalMode")
|
||||
|
||||
#self.t1 = time.monotonic()
|
||||
#self.t2 = self.t1
|
||||
#self.t3 = self.t2
|
||||
# card is driven by can recv, expected at 100Hz
|
||||
self.rk = Ratekeeper(100, print_delay_threshold=None)
|
||||
|
||||
def state_update(self) -> tuple[car.CarState, structs.RadarDataT | None]:
|
||||
"""carState update loop, driven by can"""
|
||||
|
||||
can_strs = messaging.drain_sock_raw(self.can_sock, wait_for_one=True)
|
||||
can_list = can_capnp_to_list(can_strs)
|
||||
|
||||
rcv_time = time.time()
|
||||
|
||||
# Update carState from CAN
|
||||
CS = self.CI.update(can_list)
|
||||
if self.CP.brand == 'mock':
|
||||
CS = self.mock_carstate.update(CS)
|
||||
|
||||
# Update radar tracks from CAN
|
||||
#RD: structs.RadarDataT | None = self.RI.update_carrot(CS.vEgo, can_list)
|
||||
|
||||
self.sm.update(0)
|
||||
#self.t1 = time.monotonic()
|
||||
|
||||
can_rcv_valid = len(can_strs) > 0
|
||||
|
||||
# Check for CAN timeout
|
||||
if not can_rcv_valid:
|
||||
self.can_rcv_cum_timeout_counter += 1
|
||||
|
||||
if can_rcv_valid and REPLAY:
|
||||
self.can_log_mono_time = messaging.log_from_bytes(can_strs[0]).logMonoTime
|
||||
|
||||
RD: structs.RadarDataT | None = self.RI.update_carrot(CS.vEgo, CS.aEgo, rcv_time, can_list)
|
||||
#self.t2 = time.monotonic()
|
||||
|
||||
#self.v_cruise_helper.update_v_cruise(CS, self.sm['carControl'].enabled, self.is_metric)
|
||||
self.v_cruise_helper.update_v_cruise(CS, self.sm, self.is_metric)
|
||||
#self.t3 = time.monotonic()
|
||||
if self.sm['carControl'].enabled and not self.CC_prev.enabled:
|
||||
# Use CarState w/ buttons from the step selfdrived enables on
|
||||
self.v_cruise_helper.initialize_v_cruise(self.CS_prev, self.experimental_mode)
|
||||
|
||||
# TODO: mirror the carState.cruiseState struct?
|
||||
#self.v_cruise_helper.update_v_cruise(CS, self.sm['carControl'].enabled, self.is_metric)
|
||||
if self.v_cruise_helper._paddle_decel_active:
|
||||
v_cruise_kph = v_cruise_cluster_kph = 0
|
||||
else:
|
||||
v_cruise_kph = self.v_cruise_helper.v_cruise_kph
|
||||
v_cruise_cluster_kph = self.v_cruise_helper.v_cruise_cluster_kph
|
||||
CS.logCarrot = self.v_cruise_helper.log
|
||||
CS.vCruise = float(v_cruise_kph)
|
||||
CS.vCruiseCluster = float(v_cruise_cluster_kph)
|
||||
CS.softHoldActive = self.v_cruise_helper._soft_hold_active
|
||||
CS.activateCruise = self.v_cruise_helper._activate_cruise
|
||||
CS.latEnabled = self.v_cruise_helper._lat_enabled
|
||||
CS.useLaneLineSpeed = self.v_cruise_helper.useLaneLineSpeedApply
|
||||
CS.carrotCruise = 1 if self.v_cruise_helper.carrot_cruise_active else 0
|
||||
|
||||
self.CI.CS.softHoldActive = CS.softHoldActive
|
||||
return CS, RD
|
||||
|
||||
def state_publish(self, CS: car.CarState, RD: structs.RadarDataT | None):
|
||||
"""carState and carParams publish loop"""
|
||||
|
||||
# carParams - logged every 50 seconds (> 1 per segment)
|
||||
if self.sm.frame % int(50. / DT_CTRL) == 0:
|
||||
cp_send = messaging.new_message('carParams')
|
||||
cp_send.valid = True
|
||||
cp_send.carParams = self.CP
|
||||
self.pm.send('carParams', cp_send)
|
||||
|
||||
# publish new carOutput
|
||||
co_send = messaging.new_message('carOutput')
|
||||
co_send.valid = self.sm.all_checks(['carControl'])
|
||||
co_send.carOutput.actuatorsOutput = self.last_actuators_output
|
||||
self.pm.send('carOutput', co_send)
|
||||
|
||||
# kick off controlsd step while we actuate the latest carControl packet
|
||||
cs_send = messaging.new_message('carState')
|
||||
cs_send.valid = CS.canValid
|
||||
cs_send.carState = CS
|
||||
cs_send.carState.canErrorCounter = self.can_rcv_cum_timeout_counter
|
||||
cs_send.carState.cumLagMs = -self.rk.remaining * 1000.
|
||||
self.pm.send('carState', cs_send)
|
||||
|
||||
if RD is not None:
|
||||
tracks_msg = messaging.new_message('liveTracks')
|
||||
tracks_msg.valid = not any(RD.errors.to_dict().values())
|
||||
tracks_msg.liveTracks = RD
|
||||
self.pm.send('liveTracks', tracks_msg)
|
||||
|
||||
def controls_update(self, CS: car.CarState, CC: car.CarControl):
|
||||
"""control update loop, driven by carControl"""
|
||||
|
||||
if not self.initialized_prev:
|
||||
# Initialize CarInterface, once controls are ready
|
||||
# TODO: this can make us miss at least a few cycles when doing an ECU knockout
|
||||
self.CI.init(self.CP, *self.can_callbacks)
|
||||
# signal pandad to switch to car safety mode
|
||||
self.params.put_bool_nonblocking("ControlsReady", True)
|
||||
|
||||
if self.sm.all_alive(['carControl']):
|
||||
# send car controls over can
|
||||
now_nanos = self.can_log_mono_time if REPLAY else int(time.monotonic() * 1e9)
|
||||
self.last_actuators_output, can_sends = self.CI.apply(CC, now_nanos)
|
||||
self.pm.send('sendcan', can_list_to_can_capnp(can_sends, msgtype='sendcan', valid=CS.canValid))
|
||||
|
||||
self.CC_prev = CC
|
||||
|
||||
def step(self):
|
||||
CS, RD = self.state_update()
|
||||
|
||||
self.state_publish(CS, RD)
|
||||
|
||||
initialized = (not any(e.name == EventName.selfdriveInitializing for e in self.sm['onroadEvents']) and
|
||||
self.sm.seen['onroadEvents'])
|
||||
if not self.CP.passive and initialized:
|
||||
self.controls_update(CS, self.sm['carControl'])
|
||||
|
||||
self.initialized_prev = initialized
|
||||
self.CS_prev = CS
|
||||
|
||||
def params_thread(self, evt):
|
||||
while not evt.is_set():
|
||||
self.is_metric = self.params.get_bool("IsMetric")
|
||||
self.experimental_mode = self.params.get_bool("ExperimentalMode") and self.CP.openpilotLongitudinalControl
|
||||
time.sleep(0.1)
|
||||
|
||||
def card_thread(self):
|
||||
e = threading.Event()
|
||||
t = threading.Thread(target=self.params_thread, args=(e, ))
|
||||
try:
|
||||
t.start()
|
||||
while True:
|
||||
#start = time.monotonic()
|
||||
self.step()
|
||||
#if self.sm.frame % 100 == 0:
|
||||
# print(f"elapsed time = {(self.t1 - start)*1000.:.2f}, {(self.t2 - self.t1)*1000.:.2f}, {(self.t3 - self.t1)*1000.:.2f}, {(time.monotonic() - self.t1)*1000.:.2f}")
|
||||
self.rk.monitor_time()
|
||||
finally:
|
||||
e.set()
|
||||
t.join()
|
||||
|
||||
def main():
|
||||
#config_realtime_process(4, Priority.CTRL_HIGH)
|
||||
config_realtime_process(6, Priority.CTRL_HIGH)
|
||||
car = Car()
|
||||
car.card_thread()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
822
selfdrive/car/cruise.py
Normal file
822
selfdrive/car/cruise.py
Normal file
@@ -0,0 +1,822 @@
|
||||
import math
|
||||
import numpy as np
|
||||
|
||||
from cereal import car
|
||||
from openpilot.common.conversions import Conversions as CV
|
||||
|
||||
from opendbc.car import structs
|
||||
GearShifter = structs.CarState.GearShifter
|
||||
|
||||
|
||||
# WARNING: this value was determined based on the model's training distribution,
|
||||
# model predictions above this speed can be unpredictable
|
||||
# V_CRUISE's are in kph
|
||||
V_CRUISE_MIN = 8
|
||||
V_CRUISE_MAX = 145
|
||||
V_CRUISE_UNSET = 255
|
||||
V_CRUISE_INITIAL = 40
|
||||
V_CRUISE_INITIAL_EXPERIMENTAL_MODE = 105
|
||||
IMPERIAL_INCREMENT = round(CV.MPH_TO_KPH, 1) # round here to avoid rounding errors incrementing set speed
|
||||
|
||||
ButtonEvent = car.CarState.ButtonEvent
|
||||
ButtonType = car.CarState.ButtonEvent.Type
|
||||
CRUISE_LONG_PRESS = 50
|
||||
CRUISE_NEAREST_FUNC = {
|
||||
ButtonType.accelCruise: math.ceil,
|
||||
ButtonType.decelCruise: math.floor,
|
||||
}
|
||||
CRUISE_INTERVAL_SIGN = {
|
||||
ButtonType.accelCruise: +1,
|
||||
ButtonType.decelCruise: -1,
|
||||
}
|
||||
|
||||
|
||||
class VCruiseHelper:
|
||||
def __init__(self, CP):
|
||||
self.CP = CP
|
||||
self.v_cruise_kph = V_CRUISE_UNSET
|
||||
self.v_cruise_cluster_kph = V_CRUISE_UNSET
|
||||
self.v_cruise_kph_last = 0
|
||||
self.button_timers = {ButtonType.decelCruise: 0, ButtonType.accelCruise: 0}
|
||||
self.button_change_states = {btn: {"standstill": False, "enabled": False} for btn in self.button_timers}
|
||||
|
||||
@property
|
||||
def v_cruise_initialized(self):
|
||||
return self.v_cruise_kph != V_CRUISE_UNSET
|
||||
|
||||
def update_v_cruise(self, CS, enabled, is_metric):
|
||||
self.v_cruise_kph_last = self.v_cruise_kph
|
||||
|
||||
if CS.cruiseState.available:
|
||||
if not self.CP.pcmCruise:
|
||||
# if stock cruise is completely disabled, then we can use our own set speed logic
|
||||
self._update_v_cruise_non_pcm(CS, enabled, is_metric)
|
||||
self.v_cruise_cluster_kph = self.v_cruise_kph
|
||||
self.update_button_timers(CS, enabled)
|
||||
else:
|
||||
self.v_cruise_kph = CS.cruiseState.speed * CV.MS_TO_KPH
|
||||
self.v_cruise_cluster_kph = CS.cruiseState.speedCluster * CV.MS_TO_KPH
|
||||
if CS.cruiseState.speed == 0:
|
||||
self.v_cruise_kph = V_CRUISE_UNSET
|
||||
self.v_cruise_cluster_kph = V_CRUISE_UNSET
|
||||
elif CS.cruiseState.speed == -1:
|
||||
self.v_cruise_kph = -1
|
||||
self.v_cruise_cluster_kph = -1
|
||||
else:
|
||||
self.v_cruise_kph = V_CRUISE_UNSET
|
||||
self.v_cruise_cluster_kph = V_CRUISE_UNSET
|
||||
|
||||
def _update_v_cruise_non_pcm(self, CS, enabled, is_metric):
|
||||
# handle button presses. TODO: this should be in state_control, but a decelCruise press
|
||||
# would have the effect of both enabling and changing speed is checked after the state transition
|
||||
if not enabled:
|
||||
return
|
||||
|
||||
long_press = False
|
||||
button_type = None
|
||||
|
||||
v_cruise_delta = 1. if is_metric else IMPERIAL_INCREMENT
|
||||
|
||||
for b in CS.buttonEvents:
|
||||
if b.type.raw in self.button_timers and not b.pressed:
|
||||
if self.button_timers[b.type.raw] > CRUISE_LONG_PRESS:
|
||||
return # end long press
|
||||
button_type = b.type.raw
|
||||
break
|
||||
else:
|
||||
for k, timer in self.button_timers.items():
|
||||
if timer and timer % CRUISE_LONG_PRESS == 0:
|
||||
button_type = k
|
||||
long_press = True
|
||||
break
|
||||
|
||||
if button_type is None:
|
||||
return
|
||||
|
||||
# Don't adjust speed when pressing resume to exit standstill
|
||||
cruise_standstill = self.button_change_states[button_type]["standstill"] or CS.cruiseState.standstill
|
||||
if button_type == ButtonType.accelCruise and cruise_standstill:
|
||||
return
|
||||
|
||||
# Don't adjust speed if we've enabled since the button was depressed (some ports enable on rising edge)
|
||||
if not self.button_change_states[button_type]["enabled"]:
|
||||
return
|
||||
|
||||
v_cruise_delta = v_cruise_delta * (5 if long_press else 1)
|
||||
if long_press and self.v_cruise_kph % v_cruise_delta != 0: # partial interval
|
||||
self.v_cruise_kph = CRUISE_NEAREST_FUNC[button_type](self.v_cruise_kph / v_cruise_delta) * v_cruise_delta
|
||||
else:
|
||||
self.v_cruise_kph += v_cruise_delta * CRUISE_INTERVAL_SIGN[button_type]
|
||||
|
||||
# If set is pressed while overriding, clip cruise speed to minimum of vEgo
|
||||
if CS.gasPressed and button_type in (ButtonType.decelCruise, ButtonType.setCruise):
|
||||
self.v_cruise_kph = max(self.v_cruise_kph, CS.vEgo * CV.MS_TO_KPH)
|
||||
|
||||
self.v_cruise_kph = np.clip(round(self.v_cruise_kph, 1), V_CRUISE_MIN, V_CRUISE_MAX)
|
||||
|
||||
def update_button_timers(self, CS, enabled):
|
||||
# increment timer for buttons still pressed
|
||||
for k in self.button_timers:
|
||||
if self.button_timers[k] > 0:
|
||||
self.button_timers[k] += 1
|
||||
|
||||
for b in CS.buttonEvents:
|
||||
if b.type.raw in self.button_timers:
|
||||
# Start/end timer and store current state on change of button pressed
|
||||
self.button_timers[b.type.raw] = 1 if b.pressed else 0
|
||||
self.button_change_states[b.type.raw] = {"standstill": CS.cruiseState.standstill, "enabled": enabled}
|
||||
|
||||
def initialize_v_cruise(self, CS, experimental_mode: bool) -> None:
|
||||
# initializing is handled by the PCM
|
||||
if self.CP.pcmCruise:
|
||||
return
|
||||
|
||||
initial = V_CRUISE_INITIAL_EXPERIMENTAL_MODE if experimental_mode else V_CRUISE_INITIAL
|
||||
|
||||
if any(b.type in (ButtonType.accelCruise, ButtonType.resumeCruise) for b in CS.buttonEvents) and self.v_cruise_initialized:
|
||||
self.v_cruise_kph = self.v_cruise_kph_last
|
||||
else:
|
||||
self.v_cruise_kph = int(round(np.clip(CS.vEgo * CV.MS_TO_KPH, initial, V_CRUISE_MAX)))
|
||||
|
||||
self.v_cruise_cluster_kph = self.v_cruise_kph
|
||||
|
||||
|
||||
|
||||
from openpilot.common.params import Params
|
||||
#from openpilot.selfdrive.selfdrived.events import Events
|
||||
#EventName = log.OnroadEvent.EventName
|
||||
|
||||
class VCruiseCarrot:
|
||||
def __init__(self, CP):
|
||||
self.CP = CP
|
||||
self.frame = 0
|
||||
self.params_memory = Params("/dev/shm/params")
|
||||
self.params = Params()
|
||||
self.v_cruise_kph = 20 #V_CRUISE_UNSET
|
||||
self.v_cruise_cluster_kph = 20 #V_CRUISE_UNSET
|
||||
self.v_cruise_kph_last = 20
|
||||
|
||||
self.enabled_last = False
|
||||
|
||||
self.long_pressed = False
|
||||
self.button_cnt = 0
|
||||
self.button_prev = ButtonType.unknown
|
||||
self.button_long_time = 40
|
||||
|
||||
self.is_metric = True
|
||||
|
||||
self.v_ego_kph_set = 0
|
||||
self._cruise_speed_min, self._cruise_speed_max = 5, 161
|
||||
self._cruise_speed_unit = 10
|
||||
self._cruise_speed_unit_basic = 1
|
||||
self._cruise_button_mode = 2
|
||||
self._cancel_button_mode = 0
|
||||
self._lfa_button_mode = 0
|
||||
|
||||
self._gas_pressed_count = 0
|
||||
self._gas_pressed_count_last = 0
|
||||
self._gas_pressed_value = 0
|
||||
self._gas_tok_timer = int(0.4 / 0.01) # 0.4 sec
|
||||
self._gas_tok = False
|
||||
self._brake_pressed_count = 0
|
||||
self._soft_hold_count = 0
|
||||
self._soft_hold_active = 0
|
||||
self._cruise_ready = False
|
||||
self._cruise_cancel_state = False
|
||||
self._pause_auto_speed_up = False
|
||||
self._activate_cruise = 0
|
||||
self._lat_enabled = self.params.get_int("AutoEngage") > 0
|
||||
self._v_cruise_kph_at_brake = 0
|
||||
self.cruise_state_available_last = False
|
||||
|
||||
self._paddle_decel_active = False
|
||||
self.carrot_cruise_active = False
|
||||
|
||||
#self.events = []
|
||||
self.xState = 0
|
||||
self.trafficState = 0
|
||||
self.aTarget = 0
|
||||
self.nRoadLimitSpeed = 30
|
||||
self.desiredSpeed = 250
|
||||
self.road_limit_kph = 30
|
||||
self.nRoadLimitSpeed_last = 30
|
||||
|
||||
self.carrot_cmd_index_last = 0
|
||||
self.carrot_cmd_index = 0
|
||||
self.carrot_cmd = ""
|
||||
self.carrot_arg = ""
|
||||
|
||||
|
||||
self._cancel_timer = 0
|
||||
self.d_rel = 0
|
||||
self.v_rel = 0
|
||||
self.v_lead_kph = 0
|
||||
|
||||
self._log_timer = 0
|
||||
self._log_timeout = int(3/0.01)
|
||||
self.log = ""
|
||||
|
||||
self.autoCruiseControl = 0
|
||||
self.autoCruiseControl_cancel_timer = 0
|
||||
self.AutoSpeedUptoRoadSpeedLimit = 0.0
|
||||
|
||||
self.useLaneLineSpeed = self.params.get_int("UseLaneLineSpeed")
|
||||
self.useLaneLineSpeedApply = self.useLaneLineSpeed
|
||||
|
||||
|
||||
@property
|
||||
def v_cruise_initialized(self):
|
||||
return self.v_cruise_kph != V_CRUISE_UNSET
|
||||
|
||||
def _add_log(self, log):
|
||||
if len(log) == 0:
|
||||
self._log_timer = max(0, self._log_timer - 1)
|
||||
if self._log_timer <= 0:
|
||||
self.log = ""
|
||||
#self.event = -1
|
||||
else:
|
||||
self.log = log
|
||||
#self.event = event
|
||||
self._log_timer = self._log_timeout
|
||||
|
||||
def update_params(self, is_metric):
|
||||
unit_factor = 1.0 if is_metric else CV.MPH_TO_KPH
|
||||
if self.frame % 10 == 0:
|
||||
self.autoCruiseControl = self.params.get_int("AutoCruiseControl") * unit_factor
|
||||
self.autoGasTokSpeed = self.params.get_int("AutoGasTokSpeed") * unit_factor
|
||||
self.autoGasSyncSpeed = self.params.get_bool("AutoGasSyncSpeed") * unit_factor
|
||||
self.autoSpeedUptoRoadSpeedLimit = self.params.get_float("AutoSpeedUptoRoadSpeedLimit") * 0.01
|
||||
self.autoRoadSpeedAdjust = self.params.get_float("AutoRoadSpeedAdjust") * 0.01
|
||||
self.smartSpeedControl = self.params.get_int("CarrotSmartSpeedControl")
|
||||
|
||||
useLaneLineSpeed = self.params.get_int("UseLaneLineSpeed") * unit_factor
|
||||
if self.useLaneLineSpeed != useLaneLineSpeed:
|
||||
self.useLaneLineSpeedApply = useLaneLineSpeed
|
||||
self.useLaneLineSpeed = useLaneLineSpeed
|
||||
|
||||
self.speed_from_pcm = self.params.get_int("SpeedFromPCM")
|
||||
self._cruise_speed_unit = self.params.get_int("CruiseSpeedUnit")
|
||||
self._cruise_speed_unit_basic = self.params.get_int("CruiseSpeedUnitBasic")
|
||||
self._paddle_mode = self.params.get_int("PaddleMode")
|
||||
self._cruise_button_mode = self.params.get_int("CruiseButtonMode")
|
||||
self._cancel_button_mode = self.params.get_int("CancelButtonMode")
|
||||
self._lfa_button_mode = self.params.get_int("LfaButtonMode")
|
||||
self.autoRoadSpeedLimitOffset = self.params.get_int("AutoRoadSpeedLimitOffset")
|
||||
self.autoNaviSpeedSafetyFactor = self.params.get_float("AutoNaviSpeedSafetyFactor") * 0.01
|
||||
self.cruiseOnDist = self.params.get_float("CruiseOnDist") * 0.01
|
||||
cruiseSpeed1 = self.params.get_float("CruiseSpeed1") * unit_factor
|
||||
cruiseSpeed2 = self.params.get_float("CruiseSpeed2") * unit_factor
|
||||
cruiseSpeed3 = self.params.get_float("CruiseSpeed3") * unit_factor
|
||||
cruiseSpeed4 = self.params.get_float("CruiseSpeed4") * unit_factor
|
||||
cruiseSpeed5 = self.params.get_float("CruiseSpeed5") * unit_factor
|
||||
if cruiseSpeed1 <= 0:
|
||||
if self.autoRoadSpeedLimitOffset < 0:
|
||||
cruiseSpeed1 = self.nRoadLimitSpeed * self.autoNaviSpeedSafetyFactor
|
||||
else:
|
||||
cruiseSpeed1 = self.nRoadLimitSpeed + self.autoRoadSpeedLimitOffset
|
||||
self._cruise_speed_table = [cruiseSpeed1, cruiseSpeed2, cruiseSpeed3, cruiseSpeed4, cruiseSpeed5]
|
||||
|
||||
def update_v_cruise(self, CS, sm, is_metric):
|
||||
self._add_log("")
|
||||
self.update_params(is_metric)
|
||||
self.frame += 1
|
||||
if CS.gearShifter != GearShifter.drive:
|
||||
self.autoCruiseControl_cancel_timer = 20 * 100 # 20 sec
|
||||
else:
|
||||
self.autoCruiseControl_cancel_timer = max(0, self.autoCruiseControl_cancel_timer - 1)
|
||||
|
||||
CC = sm['carControl']
|
||||
if sm.alive['carrotMan']:
|
||||
carrot_man = sm['carrotMan']
|
||||
self.nRoadLimitSpeed = carrot_man.nRoadLimitSpeed
|
||||
self.desiredSpeed = carrot_man.desiredSpeed
|
||||
self.carrot_cmd_index = carrot_man.carrotCmdIndex
|
||||
self.carrot_cmd = carrot_man.carrotCmd
|
||||
self.carrot_arg = carrot_man.carrotArg
|
||||
if sm.alive['longitudinalPlan']:
|
||||
lp = sm['longitudinalPlan']
|
||||
self.xState = lp.xState
|
||||
self.trafficState = lp.trafficState
|
||||
self.aTarget = lp.aTarget
|
||||
if sm.alive['radarState']:
|
||||
lead = sm['radarState'].leadOne
|
||||
self.d_rel = lead.dRel if lead.status else 0
|
||||
self.v_rel = lead.vRel if lead.status else 0
|
||||
self.v_lead_kph = lead.vLeadK * CV.MS_TO_KPH if lead.status else 0
|
||||
if sm.alive['modelV2']:
|
||||
self.model_v_kph = sm['modelV2'].velocity.x[0] * CV.MS_TO_KPH
|
||||
else:
|
||||
self.model_v_kph = 0
|
||||
|
||||
self.v_cruise_kph_last = self.v_cruise_kph
|
||||
self.is_metric = is_metric
|
||||
|
||||
self._cancel_timer = max(0, self._cancel_timer - 1)
|
||||
|
||||
#self.events = []
|
||||
self.v_ego_kph_set = int(CS.vEgoCluster * CV.MS_TO_KPH + 0.5)
|
||||
self._activate_cruise = 0
|
||||
self._prepare_brake_gas(CS, CC)
|
||||
if CC.enabled:
|
||||
self._cruise_ready = False
|
||||
v_cruise_kph = self._update_cruise_buttons(CS, CC, self.v_cruise_kph)
|
||||
|
||||
if self._activate_cruise > 0:
|
||||
#self.events.append(EventName.buttonEnable)
|
||||
self._cruise_ready = False
|
||||
elif self._activate_cruise < 0:
|
||||
#self.events.append(EventName.buttonCancel)
|
||||
self._cruise_ready = True if self._activate_cruise == -2 else False
|
||||
|
||||
if CS.cruiseState.available:
|
||||
if not self.cruise_state_available_last:
|
||||
self._lat_enabled = True
|
||||
if not self.CP.pcmCruise:
|
||||
# if stock cruise is completely disabled, then we can use our own set speed logic
|
||||
self.v_cruise_kph = np.clip(v_cruise_kph, self._cruise_speed_min, self._cruise_speed_max)
|
||||
self.v_cruise_cluster_kph = self.v_cruise_kph
|
||||
else:
|
||||
if self.speed_from_pcm == 1:
|
||||
self.v_cruise_kph = CS.cruiseState.speed * CV.MS_TO_KPH
|
||||
self.v_cruise_cluster_kph = CS.cruiseState.speedCluster * CV.MS_TO_KPH
|
||||
else:
|
||||
self.v_cruise_kph = np.clip(v_cruise_kph, 30, self._cruise_speed_max)
|
||||
self.v_cruise_cluster_kph = self.v_cruise_kph
|
||||
else:
|
||||
self.v_cruise_kph = np.clip(v_cruise_kph, self._cruise_speed_min, self._cruise_speed_max) #max(20, self.v_ego_kph_set) #V_CRUISE_UNSET
|
||||
self.v_cruise_cluster_kph = self.v_cruise_kph #V_CRUISE_UNSET
|
||||
#if self.cruise_state_available_last: # 최초 한번이라도 cruiseState.available이 True였다면
|
||||
# self._lat_enabled = False
|
||||
|
||||
self.cruise_state_available_last = CS.cruiseState.available
|
||||
self.enabled_last = CC.enabled
|
||||
|
||||
def initialize_v_cruise(self, CS, experimental_mode: bool) -> None:
|
||||
return
|
||||
# initializing is handled by the PCM
|
||||
if self.CP.pcmCruise and self.speed_from_pcm == 1:
|
||||
return
|
||||
|
||||
initial = V_CRUISE_INITIAL_EXPERIMENTAL_MODE if experimental_mode else CS.vEgoCluster * CV.MS_TO_KPH
|
||||
|
||||
v_ego_kph = int(round(np.clip(CS.vEgoCluster * CV.MS_TO_KPH, initial, V_CRUISE_MAX)))
|
||||
print(CS.buttonEvents)
|
||||
if any(b.type in (ButtonType.accelCruise, ButtonType.resumeCruise) for b in CS.buttonEvents): # and self.v_cruise_initialized:
|
||||
self.v_cruise_kph = max(self._v_cruise_kph_at_brake, v_ego_kph) if self._v_cruise_kph_at_brake > 0 else self.v_cruise_kph_last
|
||||
self._add_log(f"{self.v_cruise_kph},{self._v_cruise_kph_at_brake} Cruise resume")
|
||||
else:
|
||||
self.v_cruise_kph = v_ego_kph
|
||||
self._add_log(f"{self.v_cruise_kph} Cruise Set")
|
||||
|
||||
self.v_cruise_kph = np.clip(self.v_cruise_kph, self._cruise_speed_min, self._cruise_speed_max)
|
||||
self.v_cruise_cluster_kph = self.v_cruise_kph
|
||||
|
||||
def _prepare_buttons(self, CS, v_cruise_kph):
|
||||
button_kph = v_cruise_kph
|
||||
button_type = 0
|
||||
buttonEvents = CS.buttonEvents
|
||||
|
||||
SPEED_UP_UNIT = self._cruise_speed_unit_basic
|
||||
SPEED_DOWN_UNIT = self._cruise_speed_unit if self._cruise_button_mode in [1, 2, 3] else self._cruise_speed_unit_basic
|
||||
V_CRUISE_DELTA = 10
|
||||
is_metric = self.is_metric
|
||||
|
||||
# long press tracking
|
||||
if self.button_cnt > 0:
|
||||
self.button_cnt += 1
|
||||
|
||||
for b in buttonEvents:
|
||||
bt = b.type
|
||||
|
||||
if bt in [ButtonType.paddleLeft, ButtonType.paddleRight] and b.pressed:
|
||||
# Paddle: 즉시 이벤트 발생
|
||||
button_type = bt
|
||||
self.long_pressed = False
|
||||
self.button_cnt = 0
|
||||
continue
|
||||
|
||||
if b.pressed and self.button_cnt == 0 and bt in [
|
||||
ButtonType.accelCruise, ButtonType.decelCruise,
|
||||
ButtonType.gapAdjustCruise, ButtonType.cancel,
|
||||
ButtonType.lfaButton
|
||||
]:
|
||||
self.button_cnt = 1
|
||||
self.button_prev = bt
|
||||
self.button_long_time = 40 if bt in [ButtonType.accelCruise, ButtonType.decelCruise] else 70
|
||||
|
||||
elif not b.pressed and self.button_cnt > 0 and bt == self.button_prev:
|
||||
if bt == ButtonType.cancel:
|
||||
button_type = bt
|
||||
elif not self.long_pressed:
|
||||
if bt == ButtonType.accelCruise:
|
||||
unit = SPEED_UP_UNIT if is_metric else SPEED_UP_UNIT * CV.MPH_TO_KPH
|
||||
button_kph = math.ceil((button_kph + 0.01) / unit) * unit
|
||||
elif bt == ButtonType.decelCruise:
|
||||
unit = SPEED_DOWN_UNIT if is_metric else SPEED_DOWN_UNIT * CV.MPH_TO_KPH
|
||||
button_kph = math.floor((button_kph - 0.01) / unit) * unit
|
||||
button_type = bt
|
||||
self.long_pressed = False
|
||||
self.button_cnt = 0
|
||||
|
||||
# Long press 처리
|
||||
if self.button_cnt > self.button_long_time:
|
||||
self.long_pressed = True
|
||||
bt = self.button_prev
|
||||
|
||||
#if bt == ButtonType.cancel:
|
||||
# button_type = bt
|
||||
# self.button_cnt = 0
|
||||
if bt in [ButtonType.accelCruise, ButtonType.decelCruise]:
|
||||
mod = button_kph % V_CRUISE_DELTA
|
||||
if bt == ButtonType.accelCruise:
|
||||
button_kph += V_CRUISE_DELTA - mod
|
||||
else:
|
||||
button_kph -= V_CRUISE_DELTA - (-mod % V_CRUISE_DELTA)
|
||||
button_type = bt
|
||||
self.button_cnt %= self.button_long_time
|
||||
else: #if bt in [ButtonType.gapAdjustCruise, ButtonType.lfaButton]:
|
||||
if self.button_cnt < self.button_long_time + 2:
|
||||
button_type = bt
|
||||
#self.button_cnt %= self.button_long_time
|
||||
|
||||
return button_kph, button_type, self.long_pressed
|
||||
|
||||
def _carrot_command(self, v_cruise_kph, button_type, long_pressed):
|
||||
carrot_speed = self.params_memory.get_int("CarrotSpeed")
|
||||
if carrot_speed != 0:
|
||||
if carrot_speed > 0:
|
||||
if self.smartSpeedControl in [1,3]:
|
||||
v_cruise_kph = max(carrot_speed, v_cruise_kph)
|
||||
else:
|
||||
if self.smartSpeedControl == 3:
|
||||
v_cruise_kph = -carrot_speed
|
||||
#elif self.smartSpeedControl == 1:
|
||||
# v_cruise_kph = max(-carrot_speed, v_cruise_kph)
|
||||
elif self.smartSpeedControl == 2:
|
||||
v_cruise_kph = min(-carrot_speed, v_cruise_kph)
|
||||
self.params_memory.put_int_nonblocking("CarrotSpeed", 0)
|
||||
self._add_log(f"Carrot speed set to {v_cruise_kph}")
|
||||
if self.carrot_cmd_index_last != self.carrot_cmd_index:
|
||||
self.carrot_cmd_index_last = self.carrot_cmd_index
|
||||
print(f"Carrot command(cruise.py): {self.carrot_cmd} {self.carrot_arg}")
|
||||
if self.carrot_cmd == "CRUISE":
|
||||
if self.carrot_arg == "OFF":
|
||||
self._cruise_control(-2, -1, "Cruise off (carrot command)")
|
||||
elif self.carrot_arg == "ON":
|
||||
self._cruise_control(1, -1, "Cruise on (carrot command)")
|
||||
elif self.carrot_arg == "GO":
|
||||
if button_type == 0:
|
||||
button_type = ButtonType.accelCruise
|
||||
long_pressed = False
|
||||
self._add_log("Cruise accelCruise (carrot command)")
|
||||
elif self.carrot_arg == "STOP":
|
||||
v_cruise_kph = 5
|
||||
self._add_log("Cruise stop (carrot command)")
|
||||
|
||||
elif self.carrot_cmd == "SPEED":
|
||||
if self.carrot_arg == "UP":
|
||||
v_cruise_kph = self._auto_speed_up(v_cruise_kph)
|
||||
self._add_log("Cruise speed up (carrot command)")
|
||||
elif self.carrot_arg == "DOWN":
|
||||
if v_cruise_kph > 20:
|
||||
v_cruise_kph -= 10
|
||||
self._add_log("Cruise speed downup (carrot command)")
|
||||
else:
|
||||
speed_kph = int(self.carrot_arg)
|
||||
if 0 < speed_kph < 200:
|
||||
v_cruise_kph = speed_kph
|
||||
self._add_log(f"Cruise speed set to {v_cruise_kph} (carrot command)")
|
||||
|
||||
return v_cruise_kph, button_type, long_pressed
|
||||
|
||||
def _update_cruise_buttons(self, CS, CC, v_cruise_kph):
|
||||
button_kph, button_type, long_pressed = self._prepare_buttons(CS, v_cruise_kph)
|
||||
|
||||
v_cruise_kph, button_type, long_pressed = self._carrot_command(v_cruise_kph, button_type, long_pressed)
|
||||
|
||||
if button_type in [ButtonType.accelCruise, ButtonType.decelCruise]:
|
||||
self._paddle_decel_active = False
|
||||
if self.autoCruiseControl_cancel_timer > 0:
|
||||
self._add_log(f"AutoCruiseControl cancel timer RESET {button_type}")
|
||||
self.autoCruiseControl_cancel_timer = 0
|
||||
if self._cruise_cancel_state:
|
||||
self._add_log(f"Cruise Cancel state RESET {button_type}")
|
||||
self._cruise_cancel_state = False
|
||||
|
||||
if not long_pressed:
|
||||
if button_type == ButtonType.accelCruise:
|
||||
self._lat_enabled = True
|
||||
self._pause_auto_speed_up = False
|
||||
if self._soft_hold_active > 0:
|
||||
self._soft_hold_active = 0
|
||||
elif self._cruise_ready or not CC.enabled or CS.cruiseState.standstill or self.carrot_cruise_active:
|
||||
if False: #self._cruise_button_mode in [2, 3]:
|
||||
road_limit_kph = self.nRoadLimitSpeed * self.autoSpeedUptoRoadSpeedLimit
|
||||
if road_limit_kph > 1.0:
|
||||
v_cruise_kph = max(v_cruise_kph, road_limit_kph)
|
||||
elif self._v_cruise_kph_at_brake > 0 and v_cruise_kph < self._v_cruise_kph_at_brake:
|
||||
v_cruise_kph = self._v_cruise_kph_at_brake
|
||||
self._v_cruise_kph_at_brake = 0
|
||||
elif self._cruise_button_mode == 0:
|
||||
v_cruise_kph = button_kph
|
||||
else:
|
||||
v_cruise_kph = self._v_cruise_desired(CS, v_cruise_kph)
|
||||
self.carrot_cruise_active = False
|
||||
|
||||
elif button_type == ButtonType.decelCruise:
|
||||
self._lat_enabled = True
|
||||
self._pause_auto_speed_up = True
|
||||
#self.carrot_cruise_active = False
|
||||
|
||||
if self._soft_hold_active > 0:
|
||||
self._cruise_control(-1, -1, "Cruise off,softhold mode (decelCruise)")
|
||||
elif self._cruise_ready:
|
||||
self._paddle_decel_active = True
|
||||
pass
|
||||
elif not CC.enabled:
|
||||
v_cruise_kph = max(self.v_ego_kph_set, self._cruise_speed_min)
|
||||
elif self.v_ego_kph_set > v_cruise_kph + 2 and self._cruise_button_mode in [2, 3]:
|
||||
v_cruise_kph = max(self.v_ego_kph_set, self._cruise_speed_min)
|
||||
elif self._cruise_button_mode in [0, 1]:
|
||||
v_cruise_kph = button_kph
|
||||
elif self.v_ego_kph_set < 1.0:
|
||||
self.carrot_cruise_active = True
|
||||
elif self.v_ego_kph_set > self._cruise_speed_min and v_cruise_kph > self.v_ego_kph_set:
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
else:
|
||||
#self._cruise_control(-2, -1, "Cruise off (decelCruise)")
|
||||
self.carrot_cruise_active = True
|
||||
#self.events.append(EventName.audioPrompt)
|
||||
self._v_cruise_kph_at_brake = 0
|
||||
|
||||
elif button_type == ButtonType.gapAdjustCruise:
|
||||
longitudinalPersonalityMax = self.params.get_int("LongitudinalPersonalityMax")
|
||||
if CS.pcmCruiseGap == 0:
|
||||
personality = (self.params.get_int('LongitudinalPersonality') - 1) % longitudinalPersonalityMax
|
||||
else:
|
||||
personality = np.clip(CS.pcmCruiseGap - 1, 0, longitudinalPersonalityMax)
|
||||
self.params.put_int_nonblocking('LongitudinalPersonality', personality)
|
||||
#self.events.append(EventName.personalityChanged)
|
||||
elif button_type == ButtonType.lfaButton:
|
||||
if self._lfa_button_mode == 0:
|
||||
self._lat_enabled = not self._lat_enabled
|
||||
self._add_log("Lateral " + "enabled" if self._lat_enabled else "disabled")
|
||||
elif self._lfa_button_mode == 2:
|
||||
self.carrot_cruise_active = True
|
||||
else:
|
||||
if False: #CC.enabled and self._paddle_decel_active: # 수정필요...
|
||||
self._paddle_decel_active = False
|
||||
else:
|
||||
self._paddle_decel_active = True
|
||||
print("lfaButton")
|
||||
elif button_type == ButtonType.cancel:
|
||||
self._paddle_decel_active = False
|
||||
if self._cancel_button_mode in [1]:
|
||||
self._lat_enabled = False
|
||||
self._add_log("Lateral " + "enabled" if self._lat_enabled else "disabled")
|
||||
self._cruise_cancel_state = True
|
||||
#self._v_cruise_kph_at_brake = 0
|
||||
else:
|
||||
if button_type == ButtonType.accelCruise:
|
||||
v_cruise_kph = button_kph
|
||||
self._v_cruise_kph_at_brake = 0
|
||||
elif button_type == ButtonType.decelCruise:
|
||||
self._pause_auto_speed_up = True
|
||||
v_cruise_kph = button_kph
|
||||
self._v_cruise_kph_at_brake = 0
|
||||
elif button_type == ButtonType.gapAdjustCruise:
|
||||
self.params.put_int_nonblocking("MyDrivingMode", self.params.get_int("MyDrivingMode") % 4 + 1) # 1,2,3,4 (1:eco, 2:safe, 3:normal, 4:high speed)
|
||||
elif button_type == ButtonType.lfaButton:
|
||||
useLaneLineSpeed = max(1, self.useLaneLineSpeed)
|
||||
self.useLaneLineSpeedApply = useLaneLineSpeed if self.useLaneLineSpeedApply == 0 else 0
|
||||
|
||||
elif button_type == ButtonType.cancel:
|
||||
self._cruise_cancel_state = True
|
||||
self._lat_enabled = False
|
||||
self._paddle_decel_active = False
|
||||
#self.params.put_bool_nonblocking("ExperimentalMode", not self.params.get_bool("ExperimentalMode"))
|
||||
self._add_log("Lateral " + "enabled" if self._lat_enabled else "disabled")
|
||||
|
||||
if self._paddle_mode > 0 and button_type in [ButtonType.paddleLeft, ButtonType.paddleRight]: # paddle button
|
||||
self._cruise_control(-2, -1, "Cruise off & Ready (paddle)")
|
||||
if self._paddle_mode == 2:
|
||||
self._paddle_decel_active = True
|
||||
elif self._paddle_decel_active:
|
||||
if not CC.enabled:
|
||||
self._cruise_control(1, -1, "Cruise on (paddle decel)")
|
||||
|
||||
v_cruise_kph = self._update_cruise_state(CS, CC, v_cruise_kph)
|
||||
return v_cruise_kph
|
||||
|
||||
## desiredSpeed :
|
||||
# leadCar_distance, leadCar_speed, leadCar_accel,
|
||||
# v_ego, tbt_distance, tbt_speed,
|
||||
# nRoadLimitSpeed, vTurnSpeed
|
||||
# gasPressed, brakePressed, standstill
|
||||
def _v_cruise_desired(self, CS, v_cruise_kph):
|
||||
if self._cruise_button_mode == 3:
|
||||
for speed in self._cruise_speed_table:
|
||||
if v_cruise_kph < speed:
|
||||
v_cruise_kph = speed
|
||||
break
|
||||
else:
|
||||
v_cruise_kph = ((v_cruise_kph // self._cruise_speed_unit) + 1) * self._cruise_speed_unit
|
||||
|
||||
elif v_cruise_kph < 30: #self.nRoadLimitSpeed:
|
||||
v_cruise_kph = 30 #self.nRoadLimitSpeed
|
||||
else:
|
||||
for speed in range (40, 160, self._cruise_speed_unit):
|
||||
if v_cruise_kph < speed:
|
||||
v_cruise_kph = speed
|
||||
break
|
||||
return v_cruise_kph
|
||||
|
||||
def _auto_speed_up(self, v_cruise_kph):
|
||||
#if self._pause_auto_speed_up:
|
||||
# return v_cruise_kph
|
||||
|
||||
road_limit_kph = self.nRoadLimitSpeed * self.autoSpeedUptoRoadSpeedLimit
|
||||
if road_limit_kph < 1.0:
|
||||
return v_cruise_kph
|
||||
|
||||
if self.autoRoadSpeedLimitOffset > 0:
|
||||
self._v_cruise_kph_at_brake = self.nRoadLimitSpeed + self.autoRoadSpeedLimitOffset
|
||||
|
||||
if not self._pause_auto_speed_up and self.v_lead_kph + 5 > v_cruise_kph and v_cruise_kph < road_limit_kph and self.d_rel < 60:
|
||||
v_cruise_kph = min(v_cruise_kph + 5, road_limit_kph)
|
||||
elif self.autoRoadSpeedAdjust < 0 and self.nRoadLimitSpeed != self.nRoadLimitSpeed_last: # 도로제한속도가 바뀌면, 바뀐속도로 속도를 바꿈.
|
||||
if self.autoRoadSpeedLimitOffset < 0:
|
||||
v_cruise_kph = self.nRoadLimitSpeed * self.autoNaviSpeedSafetyFactor
|
||||
else:
|
||||
v_cruise_kph = self.nRoadLimitSpeed + self.autoRoadSpeedLimitOffset
|
||||
elif self.nRoadLimitSpeed < self.nRoadLimitSpeed_last and self.autoRoadSpeedAdjust > 0:
|
||||
new_road_limit_kph = self.nRoadLimitSpeed * self.autoRoadSpeedAdjust + v_cruise_kph * (1 - self.autoRoadSpeedAdjust)
|
||||
self._add_log(f"AutoSpeed change {v_cruise_kph} -> {new_road_limit_kph:.1f}")
|
||||
v_cruise_kph = min(v_cruise_kph, new_road_limit_kph)
|
||||
self.road_limit_kph = road_limit_kph
|
||||
self.nRoadLimitSpeed_last = self.nRoadLimitSpeed
|
||||
return v_cruise_kph
|
||||
|
||||
def _cruise_control(self, enable, cancel_timer, reason):
|
||||
if self._cruise_cancel_state: # and self._soft_hold_active != 2:
|
||||
self._add_log(reason + " > Cancel state")
|
||||
elif enable > 0 and self._cancel_timer > 0 and cancel_timer >= 0:
|
||||
enable = 0
|
||||
self._add_log(reason + " > Canceled")
|
||||
else:
|
||||
if self.autoCruiseControl == 0 and enable != 0:
|
||||
enable = 0
|
||||
self._soft_hold_active = 0
|
||||
return
|
||||
if self.autoCruiseControl_cancel_timer > 0 and enable != 0:
|
||||
self._add_log(reason + " > timer Canceled")
|
||||
enable = 0
|
||||
self._soft_hold_active = 0
|
||||
return
|
||||
self._activate_cruise = enable
|
||||
self._cancel_timer = int(cancel_timer / 0.01) # DT_CTRL: 0.01
|
||||
self._add_log(reason)
|
||||
|
||||
def _check_safe_stop(self, CS, safe_distance = 3):
|
||||
v_ego = CS.vEgo
|
||||
decel_rate = 1.5
|
||||
d_stop_ego = (v_ego ** 2) / (2 * decel_rate)
|
||||
d_stop_rel = (self.v_rel ** 2) / (2 * decel_rate)
|
||||
|
||||
d_final = self.d_rel - d_stop_ego - d_stop_rel
|
||||
|
||||
if d_final >= safe_distance:
|
||||
return True, d_final
|
||||
else:
|
||||
return False, d_final
|
||||
|
||||
def _update_cruise_state(self, CS, CC, v_cruise_kph):
|
||||
if not CC.enabled:
|
||||
self._pause_auto_speed_up = False
|
||||
if self._brake_pressed_count == -1 and self._soft_hold_active > 0:
|
||||
self._soft_hold_active = 2
|
||||
#self.autoCruiseControl_cancel_timer = 0
|
||||
self._cruise_control(1, -1, "Cruise on (soft hold)")
|
||||
# GM: autoResume
|
||||
elif self.params.get_bool("ActivateCruiseAfterBrake"):
|
||||
self.params.put_bool_nonblocking("ActivateCruiseAfterBrake", False)
|
||||
self._cruise_control(1, -1, "Cruise on (brake)")
|
||||
elif self.v_cruise_kph < self.v_ego_kph_set:
|
||||
self.v_cruise_kph = self.v_ego_kph_set
|
||||
|
||||
if self._soft_hold_active > 0:
|
||||
#self.events.append(EventName.softHold)
|
||||
#self._cruise_cancel_state = False
|
||||
pass
|
||||
|
||||
if self._gas_tok and self.v_ego_kph_set >= self.autoGasTokSpeed:
|
||||
if not CC.enabled:
|
||||
#self._cruise_cancel_state = False
|
||||
self._cruise_control(1, -1, "Cruise on (gas tok)")
|
||||
if self.v_ego_kph_set > v_cruise_kph:
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
else:
|
||||
v_cruise_kph = self._v_cruise_desired(CS, v_cruise_kph)
|
||||
elif self._gas_pressed_count == -1:
|
||||
if 0 < self.d_rel < CS.vEgo * 0.8:
|
||||
if CS.vEgo < 1.0:
|
||||
self._cruise_control(1, -1 if self.aTarget > 0.0 else 0, "Cruise on (safe speed)")
|
||||
else:
|
||||
self._cruise_control(-1, 0, "Cruise off (lead car too close)")
|
||||
elif self.v_ego_kph_set < 30:
|
||||
self._cruise_control(-1, 0, "Cruise off (gas speed)")
|
||||
elif self.xState == 3:
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
self._cruise_control(-1, 3, "Cruise off (traffic sign)")
|
||||
elif self.v_ego_kph_set >= self.autoGasTokSpeed and not CC.enabled:
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
self._cruise_control(1, -1 if self.aTarget > 0.0 else 0, "Cruise on (gas pressed)")
|
||||
elif self._brake_pressed_count == -1 and self._soft_hold_active == 0:
|
||||
if self.v_ego_kph_set > self.autoGasTokSpeed:
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
self._cruise_control(1, -1 if self.aTarget > 0.0 else 0, "Cruise on (speed)")
|
||||
elif abs(CS.steeringAngleDeg) < 20:
|
||||
if self.xState in [3, 5]:
|
||||
if self.xState == 3: # 감속중
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
self._cruise_control(1, 0, "Cruise on (traffic sign)")
|
||||
elif 0 < self.d_rel < 20:
|
||||
# v_cruise_kph = self.v_ego_kph_set # 전방에 차가 가까이 있을때, 기존속도 유지
|
||||
self._cruise_control(1, -1 if self.v_ego_kph_set < 1 else 0, "Cruise on (lead car)")
|
||||
|
||||
elif self._brake_pressed_count < 0 and self._gas_pressed_count < 0:
|
||||
if not CC.enabled:
|
||||
if self.d_rel > 0 and CS.vEgo > 0.02:
|
||||
safe_state, safe_dist = self._check_safe_stop(CS, 4)
|
||||
if abs(CS.steeringAngleDeg) > 70:
|
||||
pass
|
||||
elif not safe_state:
|
||||
self._cruise_control(1, -1, "Cruise on (fcw)")
|
||||
elif self.d_rel < self.cruiseOnDist:
|
||||
self._cruise_control(1, 0, "Cruise on (fcw dist)")
|
||||
else:
|
||||
self._add_log(f"leadCar d={self.d_rel:.1f},v={self.v_rel:.1f},{CS.vEgo:.1f}, {safe_dist:.1f}")
|
||||
#self.events.append(EventName.stopStop)
|
||||
if self.desiredSpeed < self.v_ego_kph_set:
|
||||
self._cruise_control(1, -1, "Cruise on (desired speed)")
|
||||
if self._cruise_ready:
|
||||
if self.xState in [3]:
|
||||
self._cruise_control(1, 0, "Cruise on (traffic sign)")
|
||||
elif self.d_rel > 0:
|
||||
self._cruise_control(1, 0, "Cruise on (lead car)")
|
||||
elif self._paddle_decel_active:
|
||||
if self.xState in [3]:
|
||||
self._paddle_decel_active = False
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
elif self.d_rel > 0:
|
||||
self._paddle_decel_active = False
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
|
||||
|
||||
if self._gas_pressed_count > self._gas_tok_timer:
|
||||
if CS.aEgo < -0.5:
|
||||
self._cruise_control(-1, 5.0, "Cruise off (gas pressed while braking)")
|
||||
if self.v_ego_kph_set > v_cruise_kph and self.autoGasSyncSpeed:
|
||||
v_cruise_kph = self.v_ego_kph_set
|
||||
|
||||
if self._gas_pressed_count == 1 or CS.vEgo < 0.1:
|
||||
self._pause_auto_speed_up = False
|
||||
if self._gas_pressed_count == 1 and CS.vEgo < 0.1:
|
||||
self._cruise_control(-1, -1, "Cruise off (gasPressed)")
|
||||
elif self._brake_pressed_count == 1:
|
||||
self._pause_auto_speed_up = True
|
||||
|
||||
return self._auto_speed_up(v_cruise_kph)
|
||||
|
||||
def _prepare_brake_gas(self, CS, CC):
|
||||
if CS.gasPressed:
|
||||
self._paddle_decel_active = False
|
||||
self._gas_pressed_count = max(1, self._gas_pressed_count + 1)
|
||||
self._gas_pressed_count_last = self._gas_pressed_count
|
||||
self._gas_pressed_value = max(CS.gas, self._gas_pressed_value) if self._gas_pressed_count > 1 else CS.gas
|
||||
self._gas_tok = False
|
||||
#if self._cruise_cancel_state and self._soft_hold_active == 2:
|
||||
# self._cruise_control(-1, -1, "Cruise off,softhold mode (gasPressed)")
|
||||
self._soft_hold_active = 0
|
||||
else:
|
||||
self._gas_tok = True if 0 < self._gas_pressed_count < self._gas_tok_timer else False
|
||||
self._gas_pressed_count = min(-1, self._gas_pressed_count - 1)
|
||||
if self._gas_pressed_count < -1:
|
||||
self._gas_pressed_count_last = 0
|
||||
self._gas_tok = False
|
||||
|
||||
if CS.brakePressed:
|
||||
self._cruise_ready = False
|
||||
self._paddle_decel_active = False
|
||||
self._brake_pressed_count = max(1, self._brake_pressed_count + 1)
|
||||
if self._brake_pressed_count == 1 and self.enabled_last:
|
||||
self._v_cruise_kph_at_brake = self.v_cruise_kph
|
||||
self._add_log(f"{self.v_cruise_kph} Cruise speed at brake")
|
||||
self._soft_hold_count = self._soft_hold_count + 1 if CS.vEgo < 0.1 and CS.gearShifter == GearShifter.drive else 0
|
||||
if self.autoCruiseControl == 0 or self.CP.pcmCruise:
|
||||
self._soft_hold_active = 0
|
||||
else:
|
||||
self._soft_hold_active = 1 if self._soft_hold_count > 60 else 0
|
||||
else:
|
||||
self._soft_hold_count = 0
|
||||
self._brake_pressed_count = min(-1, self._brake_pressed_count - 1)
|
||||
21
selfdrive/car/docs.py
Executable file
21
selfdrive/car/docs.py
Executable file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import os
|
||||
|
||||
from openpilot.common.basedir import BASEDIR
|
||||
from opendbc.car.docs import get_all_car_docs, generate_cars_md
|
||||
|
||||
CARS_MD_OUT = os.path.join(BASEDIR, "docs", "CARS.md")
|
||||
CARS_MD_TEMPLATE = os.path.join(BASEDIR, "selfdrive", "car", "CARS_template.md")
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Auto generates supported cars documentation",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
|
||||
parser.add_argument("--template", default=CARS_MD_TEMPLATE, help="Override default template filename")
|
||||
parser.add_argument("--out", default=CARS_MD_OUT, help="Override default generated filename")
|
||||
args = parser.parse_args()
|
||||
|
||||
with open(args.out, 'w') as f:
|
||||
f.write(generate_cars_md(get_all_car_docs(), args.template))
|
||||
print(f"Generated and written to {args.out}")
|
||||
0
selfdrive/car/tests/__init__.py
Normal file
0
selfdrive/car/tests/__init__.py
Normal file
12
selfdrive/car/tests/big_cars_test.sh
Executable file
12
selfdrive/car/tests/big_cars_test.sh
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
SCRIPT_DIR=$(dirname "$0")
|
||||
BASEDIR=$(realpath "$SCRIPT_DIR/../../../")
|
||||
cd $BASEDIR
|
||||
|
||||
export MAX_EXAMPLES=300
|
||||
export INTERNAL_SEG_CNT=300
|
||||
export FILEREADER_CACHE=1
|
||||
export INTERNAL_SEG_LIST=selfdrive/car/tests/test_models_segs.txt
|
||||
|
||||
cd selfdrive/car/tests && pytest test_models.py test_car_interfaces.py
|
||||
100
selfdrive/car/tests/test_car_interfaces.py
Normal file
100
selfdrive/car/tests/test_car_interfaces.py
Normal file
@@ -0,0 +1,100 @@
|
||||
import os
|
||||
import math
|
||||
import hypothesis.strategies as st
|
||||
from hypothesis import Phase, given, settings
|
||||
from parameterized import parameterized
|
||||
|
||||
from cereal import car
|
||||
from opendbc.car import DT_CTRL
|
||||
from opendbc.car.car_helpers import interfaces
|
||||
from opendbc.car.structs import CarParams
|
||||
from opendbc.car.tests.test_car_interfaces import get_fuzzy_car_interface_args
|
||||
from opendbc.car.fw_versions import FW_VERSIONS, FW_QUERY_CONFIGS
|
||||
from opendbc.car.mock.values import CAR as MOCK
|
||||
from opendbc.car.values import PLATFORMS
|
||||
from openpilot.selfdrive.controls.lib.latcontrol_angle import LatControlAngle
|
||||
from openpilot.selfdrive.controls.lib.latcontrol_pid import LatControlPID
|
||||
from openpilot.selfdrive.controls.lib.latcontrol_torque import LatControlTorque
|
||||
from openpilot.selfdrive.controls.lib.longcontrol import LongControl
|
||||
from openpilot.selfdrive.test.fuzzy_generation import FuzzyGenerator
|
||||
|
||||
ALL_ECUS = {ecu for ecus in FW_VERSIONS.values() for ecu in ecus.keys()}
|
||||
ALL_ECUS |= {ecu for config in FW_QUERY_CONFIGS.values() for ecu in config.extra_ecus}
|
||||
|
||||
ALL_REQUESTS = {tuple(r.request) for config in FW_QUERY_CONFIGS.values() for r in config.requests}
|
||||
|
||||
MAX_EXAMPLES = int(os.environ.get('MAX_EXAMPLES', '60'))
|
||||
|
||||
|
||||
class TestCarInterfaces:
|
||||
# FIXME: Due to the lists used in carParams, Phase.target is very slow and will cause
|
||||
# many generated examples to overrun when max_examples > ~20, don't use it
|
||||
@parameterized.expand([(car,) for car in sorted(PLATFORMS)] + [MOCK.MOCK])
|
||||
@settings(max_examples=MAX_EXAMPLES, deadline=None,
|
||||
phases=(Phase.reuse, Phase.generate, Phase.shrink))
|
||||
@given(data=st.data())
|
||||
def test_car_interfaces(self, car_name, data):
|
||||
CarInterface = interfaces[car_name]
|
||||
|
||||
args = get_fuzzy_car_interface_args(data.draw)
|
||||
|
||||
car_params = CarInterface.get_params(car_name, args['fingerprints'], args['car_fw'],
|
||||
alpha_long=args['alpha_long'], is_release=False, docs=False)
|
||||
car_params = car_params.as_reader()
|
||||
car_interface = CarInterface(car_params)
|
||||
assert car_params
|
||||
assert car_interface
|
||||
|
||||
assert car_params.mass > 1
|
||||
assert car_params.wheelbase > 0
|
||||
# centerToFront is center of gravity to front wheels, assert a reasonable range
|
||||
assert car_params.wheelbase * 0.3 < car_params.centerToFront < car_params.wheelbase * 0.7
|
||||
assert car_params.maxLateralAccel > 0
|
||||
|
||||
# Longitudinal sanity checks
|
||||
assert len(car_params.longitudinalTuning.kpV) == len(car_params.longitudinalTuning.kpBP)
|
||||
assert len(car_params.longitudinalTuning.kiV) == len(car_params.longitudinalTuning.kiBP)
|
||||
|
||||
# Lateral sanity checks
|
||||
if car_params.steerControlType != CarParams.SteerControlType.angle:
|
||||
tune = car_params.lateralTuning
|
||||
if tune.which() == 'pid':
|
||||
if car_name != MOCK.MOCK:
|
||||
assert not math.isnan(tune.pid.kf) and tune.pid.kf > 0
|
||||
assert len(tune.pid.kpV) > 0 and len(tune.pid.kpV) == len(tune.pid.kpBP)
|
||||
assert len(tune.pid.kiV) > 0 and len(tune.pid.kiV) == len(tune.pid.kiBP)
|
||||
|
||||
elif tune.which() == 'torque':
|
||||
assert not math.isnan(tune.torque.kf) and tune.torque.kf > 0
|
||||
assert not math.isnan(tune.torque.friction) and tune.torque.friction > 0
|
||||
|
||||
cc_msg = FuzzyGenerator.get_random_msg(data.draw, car.CarControl, real_floats=True)
|
||||
# Run car interface
|
||||
now_nanos = 0
|
||||
CC = car.CarControl.new_message(**cc_msg)
|
||||
CC = CC.as_reader()
|
||||
for _ in range(10):
|
||||
car_interface.update([])
|
||||
car_interface.apply(CC, now_nanos)
|
||||
now_nanos += DT_CTRL * 1e9 # 10 ms
|
||||
|
||||
CC = car.CarControl.new_message(**cc_msg)
|
||||
CC.enabled = True
|
||||
CC.latActive = True
|
||||
CC.longActive = True
|
||||
CC = CC.as_reader()
|
||||
for _ in range(10):
|
||||
car_interface.update([])
|
||||
car_interface.apply(CC, now_nanos)
|
||||
now_nanos += DT_CTRL * 1e9 # 10ms
|
||||
|
||||
# Test controller initialization
|
||||
# TODO: wait until card refactor is merged to run controller a few times,
|
||||
# hypothesis also slows down significantly with just one more message draw
|
||||
LongControl(car_params)
|
||||
if car_params.steerControlType == CarParams.SteerControlType.angle:
|
||||
LatControlAngle(car_params, car_interface)
|
||||
elif car_params.lateralTuning.which() == 'pid':
|
||||
LatControlPID(car_params, car_interface)
|
||||
elif car_params.lateralTuning.which() == 'torque':
|
||||
LatControlTorque(car_params, car_interface)
|
||||
151
selfdrive/car/tests/test_cruise_speed.py
Normal file
151
selfdrive/car/tests/test_cruise_speed.py
Normal file
@@ -0,0 +1,151 @@
|
||||
import pytest
|
||||
import itertools
|
||||
import numpy as np
|
||||
|
||||
from parameterized import parameterized_class
|
||||
from cereal import log
|
||||
from openpilot.selfdrive.car.cruise import VCruiseHelper, V_CRUISE_MIN, V_CRUISE_MAX, V_CRUISE_INITIAL, IMPERIAL_INCREMENT
|
||||
from cereal import car
|
||||
from openpilot.common.conversions import Conversions as CV
|
||||
from openpilot.selfdrive.test.longitudinal_maneuvers.maneuver import Maneuver
|
||||
|
||||
ButtonEvent = car.CarState.ButtonEvent
|
||||
ButtonType = car.CarState.ButtonEvent.Type
|
||||
|
||||
|
||||
def run_cruise_simulation(cruise, e2e, personality, t_end=20.):
|
||||
man = Maneuver(
|
||||
'',
|
||||
duration=t_end,
|
||||
initial_speed=max(cruise - 1., 0.0),
|
||||
lead_relevancy=True,
|
||||
initial_distance_lead=100,
|
||||
cruise_values=[cruise],
|
||||
prob_lead_values=[0.0],
|
||||
breakpoints=[0.],
|
||||
e2e=e2e,
|
||||
personality=personality,
|
||||
)
|
||||
valid, output = man.evaluate()
|
||||
assert valid
|
||||
return output[-1, 3]
|
||||
|
||||
|
||||
@parameterized_class(("e2e", "personality", "speed"), itertools.product(
|
||||
[True, False], # e2e
|
||||
log.LongitudinalPersonality.schema.enumerants, # personality
|
||||
[5,35])) # speed
|
||||
class TestCruiseSpeed:
|
||||
def test_cruise_speed(self):
|
||||
print(f'Testing {self.speed} m/s')
|
||||
cruise_speed = float(self.speed)
|
||||
|
||||
simulation_steady_state = run_cruise_simulation(cruise_speed, self.e2e, self.personality)
|
||||
assert simulation_steady_state == pytest.approx(cruise_speed, abs=.01), f'Did not reach {self.speed} m/s'
|
||||
|
||||
|
||||
# TODO: test pcmCruise
|
||||
@parameterized_class(('pcm_cruise',), [(False,)])
|
||||
class TestVCruiseHelper:
|
||||
def setup_method(self):
|
||||
self.CP = car.CarParams(pcmCruise=self.pcm_cruise)
|
||||
self.v_cruise_helper = VCruiseHelper(self.CP)
|
||||
self.reset_cruise_speed_state()
|
||||
|
||||
def reset_cruise_speed_state(self):
|
||||
# Two resets previous cruise speed
|
||||
for _ in range(2):
|
||||
self.v_cruise_helper.update_v_cruise(car.CarState(cruiseState={"available": False}), enabled=False, is_metric=False)
|
||||
|
||||
def enable(self, v_ego, experimental_mode):
|
||||
# Simulates user pressing set with a current speed
|
||||
self.v_cruise_helper.initialize_v_cruise(car.CarState(vEgo=v_ego), experimental_mode)
|
||||
|
||||
def test_adjust_speed(self):
|
||||
"""
|
||||
Asserts speed changes on falling edges of buttons.
|
||||
"""
|
||||
|
||||
self.enable(V_CRUISE_INITIAL * CV.KPH_TO_MS, False)
|
||||
|
||||
for btn in (ButtonType.accelCruise, ButtonType.decelCruise):
|
||||
for pressed in (True, False):
|
||||
CS = car.CarState(cruiseState={"available": True})
|
||||
CS.buttonEvents = [ButtonEvent(type=btn, pressed=pressed)]
|
||||
|
||||
self.v_cruise_helper.update_v_cruise(CS, enabled=True, is_metric=False)
|
||||
assert pressed == (self.v_cruise_helper.v_cruise_kph == self.v_cruise_helper.v_cruise_kph_last)
|
||||
|
||||
def test_rising_edge_enable(self):
|
||||
"""
|
||||
Some car interfaces may enable on rising edge of a button,
|
||||
ensure we don't adjust speed if enabled changes mid-press.
|
||||
"""
|
||||
|
||||
# NOTE: enabled is always one frame behind the result from button press in controlsd
|
||||
for enabled, pressed in ((False, False),
|
||||
(False, True),
|
||||
(True, False)):
|
||||
CS = car.CarState(cruiseState={"available": True})
|
||||
CS.buttonEvents = [ButtonEvent(type=ButtonType.decelCruise, pressed=pressed)]
|
||||
self.v_cruise_helper.update_v_cruise(CS, enabled=enabled, is_metric=False)
|
||||
if pressed:
|
||||
self.enable(V_CRUISE_INITIAL * CV.KPH_TO_MS, False)
|
||||
|
||||
# Expected diff on enabling. Speed should not change on falling edge of pressed
|
||||
assert not pressed == self.v_cruise_helper.v_cruise_kph == self.v_cruise_helper.v_cruise_kph_last
|
||||
|
||||
def test_resume_in_standstill(self):
|
||||
"""
|
||||
Asserts we don't increment set speed if user presses resume/accel to exit cruise standstill.
|
||||
"""
|
||||
|
||||
self.enable(0, False)
|
||||
|
||||
for standstill in (True, False):
|
||||
for pressed in (True, False):
|
||||
CS = car.CarState(cruiseState={"available": True, "standstill": standstill})
|
||||
CS.buttonEvents = [ButtonEvent(type=ButtonType.accelCruise, pressed=pressed)]
|
||||
self.v_cruise_helper.update_v_cruise(CS, enabled=True, is_metric=False)
|
||||
|
||||
# speed should only update if not at standstill and button falling edge
|
||||
should_equal = standstill or pressed
|
||||
assert should_equal == (self.v_cruise_helper.v_cruise_kph == self.v_cruise_helper.v_cruise_kph_last)
|
||||
|
||||
def test_set_gas_pressed(self):
|
||||
"""
|
||||
Asserts pressing set while enabled with gas pressed sets
|
||||
the speed to the maximum of vEgo and current cruise speed.
|
||||
"""
|
||||
|
||||
for v_ego in np.linspace(0, 100, 101):
|
||||
self.reset_cruise_speed_state()
|
||||
self.enable(V_CRUISE_INITIAL * CV.KPH_TO_MS, False)
|
||||
|
||||
# first decrement speed, then perform gas pressed logic
|
||||
expected_v_cruise_kph = self.v_cruise_helper.v_cruise_kph - IMPERIAL_INCREMENT
|
||||
expected_v_cruise_kph = max(expected_v_cruise_kph, v_ego * CV.MS_TO_KPH) # clip to min of vEgo
|
||||
expected_v_cruise_kph = float(np.clip(round(expected_v_cruise_kph, 1), V_CRUISE_MIN, V_CRUISE_MAX))
|
||||
|
||||
CS = car.CarState(vEgo=float(v_ego), gasPressed=True, cruiseState={"available": True})
|
||||
CS.buttonEvents = [ButtonEvent(type=ButtonType.decelCruise, pressed=False)]
|
||||
self.v_cruise_helper.update_v_cruise(CS, enabled=True, is_metric=False)
|
||||
|
||||
# TODO: fix skipping first run due to enabled on rising edge exception
|
||||
if v_ego == 0.0:
|
||||
continue
|
||||
assert expected_v_cruise_kph == self.v_cruise_helper.v_cruise_kph
|
||||
|
||||
def test_initialize_v_cruise(self):
|
||||
"""
|
||||
Asserts allowed cruise speeds on enabling with SET.
|
||||
"""
|
||||
|
||||
for experimental_mode in (True, False):
|
||||
for v_ego in np.linspace(0, 100, 101):
|
||||
self.reset_cruise_speed_state()
|
||||
assert not self.v_cruise_helper.v_cruise_initialized
|
||||
|
||||
self.enable(float(v_ego), experimental_mode)
|
||||
assert V_CRUISE_INITIAL <= self.v_cruise_helper.v_cruise_kph <= V_CRUISE_MAX
|
||||
assert self.v_cruise_helper.v_cruise_initialized
|
||||
22
selfdrive/car/tests/test_docs.py
Normal file
22
selfdrive/car/tests/test_docs.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import os
|
||||
|
||||
from openpilot.common.basedir import BASEDIR
|
||||
from opendbc.car.docs import generate_cars_md, get_all_car_docs
|
||||
from openpilot.selfdrive.debug.dump_car_docs import dump_car_docs
|
||||
from openpilot.selfdrive.debug.print_docs_diff import print_car_docs_diff
|
||||
from openpilot.selfdrive.car.docs import CARS_MD_TEMPLATE
|
||||
|
||||
|
||||
class TestCarDocs:
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
cls.all_cars = get_all_car_docs()
|
||||
|
||||
def test_generator(self):
|
||||
generate_cars_md(self.all_cars, CARS_MD_TEMPLATE)
|
||||
|
||||
def test_docs_diff(self):
|
||||
dump_path = os.path.join(BASEDIR, "selfdrive", "car", "tests", "cars_dump")
|
||||
dump_car_docs(dump_path)
|
||||
print_car_docs_diff(dump_path)
|
||||
os.remove(dump_path)
|
||||
475
selfdrive/car/tests/test_models.py
Normal file
475
selfdrive/car/tests/test_models.py
Normal file
@@ -0,0 +1,475 @@
|
||||
import time
|
||||
import os
|
||||
import pytest
|
||||
import random
|
||||
import unittest # noqa: TID251
|
||||
from collections import defaultdict, Counter
|
||||
import hypothesis.strategies as st
|
||||
from hypothesis import Phase, given, settings
|
||||
from parameterized import parameterized_class
|
||||
|
||||
from opendbc.car import DT_CTRL, gen_empty_fingerprint, structs
|
||||
from opendbc.car.can_definitions import CanData
|
||||
from opendbc.car.car_helpers import FRAME_FINGERPRINT, interfaces
|
||||
from opendbc.car.fingerprints import MIGRATION
|
||||
from opendbc.car.honda.values import CAR as HONDA, HondaFlags
|
||||
from opendbc.car.structs import car
|
||||
from opendbc.car.tests.routes import non_tested_cars, routes, CarTestRoute
|
||||
from opendbc.car.values import Platform, PLATFORMS
|
||||
from opendbc.safety.tests.libsafety import libsafety_py
|
||||
from openpilot.common.basedir import BASEDIR
|
||||
from openpilot.selfdrive.pandad import can_capnp_to_list
|
||||
from openpilot.selfdrive.test.helpers import read_segment_list
|
||||
from openpilot.system.hardware.hw import DEFAULT_DOWNLOAD_CACHE_ROOT
|
||||
from openpilot.tools.lib.logreader import LogReader, LogsUnavailable, openpilotci_source, internal_source, comma_api_source
|
||||
from openpilot.tools.lib.route import SegmentName
|
||||
|
||||
SafetyModel = car.CarParams.SafetyModel
|
||||
SteerControlType = structs.CarParams.SteerControlType
|
||||
|
||||
NUM_JOBS = int(os.environ.get("NUM_JOBS", "1"))
|
||||
JOB_ID = int(os.environ.get("JOB_ID", "0"))
|
||||
INTERNAL_SEG_LIST = os.environ.get("INTERNAL_SEG_LIST", "")
|
||||
INTERNAL_SEG_CNT = int(os.environ.get("INTERNAL_SEG_CNT", "0"))
|
||||
MAX_EXAMPLES = int(os.environ.get("MAX_EXAMPLES", "300"))
|
||||
CI = os.environ.get("CI", None) is not None
|
||||
|
||||
|
||||
def get_test_cases() -> list[tuple[str, CarTestRoute | None]]:
|
||||
# build list of test cases
|
||||
test_cases = []
|
||||
if not len(INTERNAL_SEG_LIST):
|
||||
routes_by_car = defaultdict(set)
|
||||
for r in routes:
|
||||
routes_by_car[str(r.car_model)].add(r)
|
||||
|
||||
for i, c in enumerate(sorted(PLATFORMS)):
|
||||
if i % NUM_JOBS == JOB_ID:
|
||||
test_cases.extend(sorted((c, r) for r in routes_by_car.get(c, (None,))))
|
||||
|
||||
else:
|
||||
segment_list = read_segment_list(os.path.join(BASEDIR, INTERNAL_SEG_LIST))
|
||||
segment_list = random.sample(segment_list, INTERNAL_SEG_CNT or len(segment_list))
|
||||
for platform, segment in segment_list:
|
||||
platform = MIGRATION.get(platform, platform)
|
||||
segment_name = SegmentName(segment)
|
||||
test_cases.append((platform, CarTestRoute(segment_name.route_name.canonical_name, platform,
|
||||
segment=segment_name.segment_num)))
|
||||
return test_cases
|
||||
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.shared_download_cache
|
||||
class TestCarModelBase(unittest.TestCase):
|
||||
platform: Platform | None = None
|
||||
test_route: CarTestRoute | None = None
|
||||
|
||||
can_msgs: list[tuple[int, list[CanData]]]
|
||||
fingerprint: dict[int, dict[int, int]]
|
||||
elm_frame: int | None
|
||||
car_safety_mode_frame: int | None
|
||||
|
||||
@classmethod
|
||||
def get_testing_data_from_logreader(cls, lr):
|
||||
car_fw = []
|
||||
can_msgs = []
|
||||
cls.elm_frame = None
|
||||
cls.car_safety_mode_frame = None
|
||||
cls.fingerprint = gen_empty_fingerprint()
|
||||
alpha_long = False
|
||||
for msg in lr:
|
||||
if msg.which() == "can":
|
||||
can = can_capnp_to_list((msg.as_builder().to_bytes(),))[0]
|
||||
can_msgs.append((can[0], [CanData(*can) for can in can[1]]))
|
||||
if len(can_msgs) <= FRAME_FINGERPRINT:
|
||||
for m in msg.can:
|
||||
if m.src < 64:
|
||||
cls.fingerprint[m.src][m.address] = len(m.dat)
|
||||
|
||||
elif msg.which() == "carParams":
|
||||
car_fw = msg.carParams.carFw
|
||||
if msg.carParams.openpilotLongitudinalControl:
|
||||
alpha_long = True
|
||||
if cls.platform is None:
|
||||
live_fingerprint = msg.carParams.carFingerprint
|
||||
cls.platform = MIGRATION.get(live_fingerprint, live_fingerprint)
|
||||
|
||||
# Log which can frame the panda safety mode left ELM327, for CAN validity checks
|
||||
elif msg.which() == 'pandaStates':
|
||||
for ps in msg.pandaStates:
|
||||
if cls.elm_frame is None and ps.safetyModel != SafetyModel.elm327:
|
||||
cls.elm_frame = len(can_msgs)
|
||||
if cls.car_safety_mode_frame is None and ps.safetyModel not in \
|
||||
(SafetyModel.elm327, SafetyModel.noOutput):
|
||||
cls.car_safety_mode_frame = len(can_msgs)
|
||||
|
||||
elif msg.which() == 'pandaStateDEPRECATED':
|
||||
if cls.elm_frame is None and msg.pandaStateDEPRECATED.safetyModel != SafetyModel.elm327:
|
||||
cls.elm_frame = len(can_msgs)
|
||||
if cls.car_safety_mode_frame is None and msg.pandaStateDEPRECATED.safetyModel not in \
|
||||
(SafetyModel.elm327, SafetyModel.noOutput):
|
||||
cls.car_safety_mode_frame = len(can_msgs)
|
||||
|
||||
assert len(can_msgs) > int(50 / DT_CTRL), "no can data found"
|
||||
return car_fw, can_msgs, alpha_long
|
||||
|
||||
@classmethod
|
||||
def get_testing_data(cls):
|
||||
test_segs = (2, 1, 0)
|
||||
if cls.test_route.segment is not None:
|
||||
test_segs = (cls.test_route.segment,)
|
||||
|
||||
for seg in test_segs:
|
||||
segment_range = f"{cls.test_route.route}/{seg}"
|
||||
|
||||
try:
|
||||
sources = [internal_source] if len(INTERNAL_SEG_LIST) else [openpilotci_source, comma_api_source]
|
||||
lr = LogReader(segment_range, sources=sources, sort_by_time=True)
|
||||
return cls.get_testing_data_from_logreader(lr)
|
||||
except (LogsUnavailable, AssertionError):
|
||||
pass
|
||||
|
||||
raise Exception(f"Route: {repr(cls.test_route.route)} with segments: {test_segs} not found or no CAN msgs found. Is it uploaded and public?")
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if cls.__name__ == 'TestCarModel' or cls.__name__.endswith('Base'):
|
||||
raise unittest.SkipTest
|
||||
|
||||
if cls.test_route is None:
|
||||
if cls.platform in non_tested_cars:
|
||||
print(f"Skipping tests for {cls.platform}: missing route")
|
||||
raise unittest.SkipTest
|
||||
raise Exception(f"missing test route for {cls.platform}")
|
||||
|
||||
car_fw, cls.can_msgs, alpha_long = cls.get_testing_data()
|
||||
|
||||
# if relay is expected to be open in the route
|
||||
cls.openpilot_enabled = cls.car_safety_mode_frame is not None
|
||||
|
||||
cls.CarInterface = interfaces[cls.platform]
|
||||
cls.CP = cls.CarInterface.get_params(cls.platform, cls.fingerprint, car_fw, alpha_long, False, docs=False)
|
||||
assert cls.CP
|
||||
assert cls.CP.carFingerprint == cls.platform
|
||||
|
||||
os.environ["COMMA_CACHE"] = DEFAULT_DOWNLOAD_CACHE_ROOT
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del cls.can_msgs
|
||||
|
||||
def setUp(self):
|
||||
self.CI = self.CarInterface(self.CP.copy())
|
||||
assert self.CI
|
||||
|
||||
# TODO: check safetyModel is in release panda build
|
||||
self.safety = libsafety_py.libsafety
|
||||
|
||||
cfg = self.CP.safetyConfigs[-1]
|
||||
set_status = self.safety.set_safety_hooks(cfg.safetyModel.raw, cfg.safetyParam)
|
||||
self.assertEqual(0, set_status, f"failed to set safetyModel {cfg}")
|
||||
self.safety.init_tests()
|
||||
|
||||
def test_car_params(self):
|
||||
if self.CP.dashcamOnly:
|
||||
self.skipTest("no need to check carParams for dashcamOnly")
|
||||
|
||||
# make sure car params are within a valid range
|
||||
self.assertGreater(self.CP.mass, 1)
|
||||
|
||||
if self.CP.steerControlType != SteerControlType.angle:
|
||||
tuning = self.CP.lateralTuning.which()
|
||||
if tuning == 'pid':
|
||||
self.assertTrue(len(self.CP.lateralTuning.pid.kpV))
|
||||
elif tuning == 'torque':
|
||||
self.assertTrue(self.CP.lateralTuning.torque.kf > 0)
|
||||
else:
|
||||
raise Exception("unknown tuning")
|
||||
|
||||
def test_car_interface(self):
|
||||
# TODO: also check for checksum violations from can parser
|
||||
can_invalid_cnt = 0
|
||||
CC = structs.CarControl().as_reader()
|
||||
|
||||
for i, msg in enumerate(self.can_msgs):
|
||||
CS = self.CI.update(msg)
|
||||
self.CI.apply(CC, msg[0])
|
||||
|
||||
# wait max of 2s for low frequency msgs to be seen
|
||||
if i > 250:
|
||||
can_invalid_cnt += not CS.canValid
|
||||
|
||||
self.assertEqual(can_invalid_cnt, 0)
|
||||
|
||||
def test_radar_interface(self):
|
||||
RI = self.CarInterface.RadarInterface(self.CP)
|
||||
assert RI
|
||||
|
||||
# Since OBD port is multiplexed to bus 1 (commonly radar bus) while fingerprinting,
|
||||
# start parsing CAN messages after we've left ELM mode and can expect CAN traffic
|
||||
error_cnt = 0
|
||||
for i, msg in enumerate(self.can_msgs[self.elm_frame:]):
|
||||
rr: structs.RadarData | None = RI.update(msg)
|
||||
if rr is not None and i > 50:
|
||||
error_cnt += rr.errors.canError
|
||||
self.assertEqual(error_cnt, 0)
|
||||
|
||||
def test_panda_safety_rx_checks(self):
|
||||
if self.CP.dashcamOnly:
|
||||
self.skipTest("no need to check panda safety for dashcamOnly")
|
||||
|
||||
start_ts = self.can_msgs[0][0]
|
||||
|
||||
failed_addrs = Counter()
|
||||
for can in self.can_msgs:
|
||||
# update panda timer
|
||||
t = (can[0] - start_ts) / 1e3
|
||||
self.safety.set_timer(int(t))
|
||||
|
||||
# run all msgs through the safety RX hook
|
||||
for msg in can[1]:
|
||||
if msg.src >= 64:
|
||||
continue
|
||||
|
||||
to_send = libsafety_py.make_CANPacket(msg.address, msg.src % 4, msg.dat)
|
||||
if self.safety.safety_rx_hook(to_send) != 1:
|
||||
failed_addrs[hex(msg.address)] += 1
|
||||
|
||||
# ensure all msgs defined in the addr checks are valid
|
||||
self.safety.safety_tick_current_safety_config()
|
||||
if t > 1e6:
|
||||
self.assertTrue(self.safety.safety_config_valid())
|
||||
|
||||
# Don't check relay malfunction on disabled routes (relay closed),
|
||||
# or before fingerprinting is done (elm327 and noOutput)
|
||||
if self.openpilot_enabled and t / 1e4 > self.car_safety_mode_frame:
|
||||
self.assertFalse(self.safety.get_relay_malfunction())
|
||||
else:
|
||||
self.safety.set_relay_malfunction(False)
|
||||
|
||||
self.assertFalse(len(failed_addrs), f"panda safety RX check failed: {failed_addrs}")
|
||||
|
||||
# ensure RX checks go invalid after small time with no traffic
|
||||
self.safety.set_timer(int(t + (2*1e6)))
|
||||
self.safety.safety_tick_current_safety_config()
|
||||
self.assertFalse(self.safety.safety_config_valid())
|
||||
|
||||
def test_panda_safety_tx_cases(self, data=None):
|
||||
"""Asserts we can tx common messages"""
|
||||
if self.CP.dashcamOnly:
|
||||
self.skipTest("no need to check panda safety for dashcamOnly")
|
||||
|
||||
if self.CP.notCar:
|
||||
self.skipTest("Skipping test for notCar")
|
||||
|
||||
def test_car_controller(car_control):
|
||||
now_nanos = 0
|
||||
msgs_sent = 0
|
||||
CI = self.CarInterface(self.CP)
|
||||
for _ in range(round(10.0 / DT_CTRL)): # make sure we hit the slowest messages
|
||||
CI.update([])
|
||||
_, sendcan = CI.apply(car_control, now_nanos)
|
||||
|
||||
now_nanos += DT_CTRL * 1e9
|
||||
msgs_sent += len(sendcan)
|
||||
for addr, dat, bus in sendcan:
|
||||
to_send = libsafety_py.make_CANPacket(addr, bus % 4, dat)
|
||||
self.assertTrue(self.safety.safety_tx_hook(to_send), (addr, dat, bus))
|
||||
|
||||
# Make sure we attempted to send messages
|
||||
self.assertGreater(msgs_sent, 50)
|
||||
|
||||
# Make sure we can send all messages while inactive
|
||||
CC = structs.CarControl()
|
||||
test_car_controller(CC.as_reader())
|
||||
|
||||
# Test cancel + general messages (controls_allowed=False & cruise_engaged=True)
|
||||
self.safety.set_cruise_engaged_prev(True)
|
||||
CC = structs.CarControl(cruiseControl=structs.CarControl.CruiseControl(cancel=True))
|
||||
test_car_controller(CC.as_reader())
|
||||
|
||||
# Test resume + general messages (controls_allowed=True & cruise_engaged=True)
|
||||
self.safety.set_controls_allowed(True)
|
||||
CC = structs.CarControl(cruiseControl=structs.CarControl.CruiseControl(resume=True))
|
||||
test_car_controller(CC.as_reader())
|
||||
|
||||
# Skip stdout/stderr capture with pytest, causes elevated memory usage
|
||||
@pytest.mark.nocapture
|
||||
@settings(max_examples=MAX_EXAMPLES, deadline=None,
|
||||
phases=(Phase.reuse, Phase.generate, Phase.shrink))
|
||||
@given(data=st.data())
|
||||
def test_panda_safety_carstate_fuzzy(self, data):
|
||||
"""
|
||||
For each example, pick a random CAN message on the bus and fuzz its data,
|
||||
checking for panda state mismatches.
|
||||
"""
|
||||
|
||||
if self.CP.dashcamOnly:
|
||||
self.skipTest("no need to check panda safety for dashcamOnly")
|
||||
|
||||
valid_addrs = [(addr, bus, size) for bus, addrs in self.fingerprint.items() for addr, size in addrs.items()]
|
||||
address, bus, size = data.draw(st.sampled_from(valid_addrs))
|
||||
|
||||
msg_strategy = st.binary(min_size=size, max_size=size)
|
||||
msgs = data.draw(st.lists(msg_strategy, min_size=20))
|
||||
|
||||
vehicle_speed_seen = self.CP.steerControlType == SteerControlType.angle and not self.CP.notCar
|
||||
|
||||
for n, dat in enumerate(msgs):
|
||||
# due to panda updating state selectively, only edges are expected to match
|
||||
# TODO: warm up CarState with real CAN messages to check edge of both sources
|
||||
# (eg. toyota's gasPressed is the inverse of a signal being set)
|
||||
prev_panda_gas = self.safety.get_gas_pressed_prev()
|
||||
prev_panda_brake = self.safety.get_brake_pressed_prev()
|
||||
prev_panda_regen_braking = self.safety.get_regen_braking_prev()
|
||||
prev_panda_steering_disengage = self.safety.get_steering_disengage_prev()
|
||||
prev_panda_vehicle_moving = self.safety.get_vehicle_moving()
|
||||
prev_panda_vehicle_speed_min = self.safety.get_vehicle_speed_min()
|
||||
prev_panda_vehicle_speed_max = self.safety.get_vehicle_speed_max()
|
||||
prev_panda_cruise_engaged = self.safety.get_cruise_engaged_prev()
|
||||
prev_panda_acc_main_on = self.safety.get_acc_main_on()
|
||||
|
||||
to_send = libsafety_py.make_CANPacket(address, bus, dat)
|
||||
self.safety.safety_rx_hook(to_send)
|
||||
|
||||
can = [(int(time.monotonic() * 1e9), [CanData(address=address, dat=dat, src=bus)])]
|
||||
CS = self.CI.update(can)
|
||||
if n < 5: # CANParser warmup time
|
||||
continue
|
||||
|
||||
if self.safety.get_gas_pressed_prev() != prev_panda_gas:
|
||||
self.assertEqual(CS.gasPressed, self.safety.get_gas_pressed_prev())
|
||||
|
||||
if self.safety.get_brake_pressed_prev() != prev_panda_brake:
|
||||
# TODO: remove this exception once this mismatch is resolved
|
||||
brake_pressed = CS.brakePressed
|
||||
if CS.brakePressed and not self.safety.get_brake_pressed_prev():
|
||||
if self.CP.carFingerprint in (HONDA.HONDA_PILOT, HONDA.HONDA_RIDGELINE) and CS.brake > 0.05:
|
||||
brake_pressed = False
|
||||
|
||||
self.assertEqual(brake_pressed, self.safety.get_brake_pressed_prev())
|
||||
|
||||
if self.safety.get_regen_braking_prev() != prev_panda_regen_braking:
|
||||
self.assertEqual(CS.regenBraking, self.safety.get_regen_braking_prev())
|
||||
|
||||
if self.safety.get_steering_disengage_prev() != prev_panda_steering_disengage:
|
||||
self.assertEqual(CS.steeringDisengage, self.safety.get_steering_disengage_prev())
|
||||
|
||||
if self.safety.get_vehicle_moving() != prev_panda_vehicle_moving and not self.CP.notCar:
|
||||
self.assertEqual(not CS.standstill, self.safety.get_vehicle_moving())
|
||||
|
||||
# check vehicle speed if angle control car or available
|
||||
if self.safety.get_vehicle_speed_min() > 0 or self.safety.get_vehicle_speed_max() > 0:
|
||||
vehicle_speed_seen = True
|
||||
|
||||
if vehicle_speed_seen and (self.safety.get_vehicle_speed_min() != prev_panda_vehicle_speed_min or
|
||||
self.safety.get_vehicle_speed_max() != prev_panda_vehicle_speed_max):
|
||||
v_ego_raw = CS.vEgoRaw / self.CP.wheelSpeedFactor
|
||||
self.assertFalse(v_ego_raw > (self.safety.get_vehicle_speed_max() + 1e-3) or
|
||||
v_ego_raw < (self.safety.get_vehicle_speed_min() - 1e-3))
|
||||
|
||||
if not (self.CP.brand == "honda" and not (self.CP.flags & HondaFlags.BOSCH)):
|
||||
if self.safety.get_cruise_engaged_prev() != prev_panda_cruise_engaged:
|
||||
self.assertEqual(CS.cruiseState.enabled, self.safety.get_cruise_engaged_prev())
|
||||
|
||||
if self.CP.brand == "honda":
|
||||
if self.safety.get_acc_main_on() != prev_panda_acc_main_on:
|
||||
self.assertEqual(CS.cruiseState.available, self.safety.get_acc_main_on())
|
||||
|
||||
def test_panda_safety_carstate(self):
|
||||
"""
|
||||
Assert that panda safety matches openpilot's carState
|
||||
"""
|
||||
if self.CP.dashcamOnly:
|
||||
self.skipTest("no need to check panda safety for dashcamOnly")
|
||||
|
||||
# warm up pass, as initial states may be different
|
||||
for can in self.can_msgs[:300]:
|
||||
self.CI.update(can)
|
||||
for msg in filter(lambda m: m.src < 64, can[1]):
|
||||
to_send = libsafety_py.make_CANPacket(msg.address, msg.src % 4, msg.dat)
|
||||
self.safety.safety_rx_hook(to_send)
|
||||
|
||||
controls_allowed_prev = False
|
||||
CS_prev = car.CarState.new_message()
|
||||
checks = defaultdict(int)
|
||||
vehicle_speed_seen = self.CP.steerControlType == SteerControlType.angle and not self.CP.notCar
|
||||
for idx, can in enumerate(self.can_msgs):
|
||||
CS = self.CI.update(can).as_reader()
|
||||
for msg in filter(lambda m: m.src < 64, can[1]):
|
||||
to_send = libsafety_py.make_CANPacket(msg.address, msg.src % 4, msg.dat)
|
||||
ret = self.safety.safety_rx_hook(to_send)
|
||||
self.assertEqual(1, ret, f"safety rx failed ({ret=}): {(msg.address, msg.src % 4)}")
|
||||
|
||||
# Skip first frame so CS_prev is properly initialized
|
||||
if idx == 0:
|
||||
CS_prev = CS
|
||||
# Button may be left pressed in warm up period
|
||||
if not self.CP.pcmCruise:
|
||||
self.safety.set_controls_allowed(0)
|
||||
continue
|
||||
|
||||
# TODO: check rest of panda's carstate (steering, ACC main on, etc.)
|
||||
|
||||
checks['gasPressed'] += CS.gasPressed != self.safety.get_gas_pressed_prev()
|
||||
checks['standstill'] += (CS.standstill == self.safety.get_vehicle_moving()) and not self.CP.notCar
|
||||
|
||||
# check vehicle speed if angle control car or available
|
||||
if self.safety.get_vehicle_speed_min() > 0 or self.safety.get_vehicle_speed_max() > 0:
|
||||
vehicle_speed_seen = True
|
||||
|
||||
if vehicle_speed_seen:
|
||||
v_ego_raw = CS.vEgoRaw / self.CP.wheelSpeedFactor
|
||||
checks['vEgoRaw'] += (v_ego_raw > (self.safety.get_vehicle_speed_max() + 1e-3) or
|
||||
v_ego_raw < (self.safety.get_vehicle_speed_min() - 1e-3))
|
||||
|
||||
# TODO: remove this exception once this mismatch is resolved
|
||||
brake_pressed = CS.brakePressed
|
||||
if CS.brakePressed and not self.safety.get_brake_pressed_prev():
|
||||
if self.CP.carFingerprint in (HONDA.HONDA_PILOT, HONDA.HONDA_RIDGELINE) and CS.brake > 0.05:
|
||||
brake_pressed = False
|
||||
checks['brakePressed'] += brake_pressed != self.safety.get_brake_pressed_prev()
|
||||
checks['regenBraking'] += CS.regenBraking != self.safety.get_regen_braking_prev()
|
||||
checks['steeringDisengage'] += CS.steeringDisengage != self.safety.get_steering_disengage_prev()
|
||||
|
||||
if self.CP.pcmCruise:
|
||||
# On most pcmCruise cars, openpilot's state is always tied to the PCM's cruise state.
|
||||
# On Honda Nidec, we always engage on the rising edge of the PCM cruise state, but
|
||||
# openpilot brakes to zero even if the min ACC speed is non-zero (i.e. the PCM disengages).
|
||||
if self.CP.brand == "honda" and not (self.CP.flags & HondaFlags.BOSCH):
|
||||
# only the rising edges are expected to match
|
||||
if CS.cruiseState.enabled and not CS_prev.cruiseState.enabled:
|
||||
checks['controlsAllowed'] += not self.safety.get_controls_allowed()
|
||||
else:
|
||||
checks['controlsAllowed'] += not CS.cruiseState.enabled and self.safety.get_controls_allowed()
|
||||
|
||||
# TODO: fix notCar mismatch
|
||||
if not self.CP.notCar:
|
||||
checks['cruiseState'] += CS.cruiseState.enabled != self.safety.get_cruise_engaged_prev()
|
||||
else:
|
||||
# Check for user button enable on rising edge of controls allowed
|
||||
button_enable = CS.buttonEnable and (not CS.brakePressed or CS.standstill)
|
||||
mismatch = button_enable != (self.safety.get_controls_allowed() and not controls_allowed_prev)
|
||||
checks['controlsAllowed'] += mismatch
|
||||
controls_allowed_prev = self.safety.get_controls_allowed()
|
||||
if button_enable and not mismatch:
|
||||
self.safety.set_controls_allowed(False)
|
||||
|
||||
if self.CP.brand == "honda":
|
||||
checks['mainOn'] += CS.cruiseState.available != self.safety.get_acc_main_on()
|
||||
|
||||
CS_prev = CS
|
||||
|
||||
failed_checks = {k: v for k, v in checks.items() if v > 0}
|
||||
self.assertFalse(len(failed_checks), f"panda safety doesn't agree with openpilot: {failed_checks}")
|
||||
|
||||
|
||||
@parameterized_class(('platform', 'test_route'), get_test_cases())
|
||||
@pytest.mark.xdist_group_class_property('test_route')
|
||||
class TestCarModel(TestCarModelBase):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
3884
selfdrive/car/tests/test_models_segs.txt
Normal file
3884
selfdrive/car/tests/test_models_segs.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user