2020-01-18 04:48:30 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
2021-03-12 13:08:51 +08:00
|
|
|
import math
|
2022-05-12 20:08:11 +08:00
|
|
|
from typing import SupportsFloat
|
2021-09-07 09:45:59 +08:00
|
|
|
|
2020-01-18 04:48:30 +08:00
|
|
|
from cereal import car, log
|
|
|
|
from common.numpy_fast import clip
|
2020-10-03 08:18:18 +08:00
|
|
|
from common.realtime import sec_since_boot, config_realtime_process, Priority, Ratekeeper, DT_CTRL
|
2020-01-18 04:48:30 +08:00
|
|
|
from common.profiler import Profiler
|
2020-04-01 03:09:38 +08:00
|
|
|
from common.params import Params, put_nonblocking
|
2020-01-18 04:48:30 +08:00
|
|
|
import cereal.messaging as messaging
|
2022-03-14 12:42:39 +08:00
|
|
|
from common.conversions import Conversions as CV
|
2022-04-05 12:59:52 +08:00
|
|
|
from panda import ALTERNATIVE_EXPERIENCE
|
2022-06-12 14:19:27 +08:00
|
|
|
from system.swaglog import cloudlog
|
2022-11-22 08:18:51 +08:00
|
|
|
from system.version import is_release_branch, get_short_branch
|
2020-01-18 04:48:30 +08:00
|
|
|
from selfdrive.boardd.boardd import can_list_to_can_capnp
|
2020-07-29 14:52:18 +08:00
|
|
|
from selfdrive.car.car_helpers import get_car, get_startup_event, get_one_can
|
2022-09-05 04:07:51 +08:00
|
|
|
from selfdrive.controls.lib.lateral_planner import CAMERA_OFFSET
|
2022-11-12 10:53:48 +08:00
|
|
|
from selfdrive.controls.lib.drive_helpers import VCruiseHelper, get_lag_adjusted_curvature
|
2022-05-12 20:08:11 +08:00
|
|
|
from selfdrive.controls.lib.latcontrol import LatControl
|
2021-09-12 09:01:54 +08:00
|
|
|
from selfdrive.controls.lib.longcontrol import LongControl
|
2020-01-18 04:48:30 +08:00
|
|
|
from selfdrive.controls.lib.latcontrol_pid import LatControlPID
|
|
|
|
from selfdrive.controls.lib.latcontrol_indi import LatControlINDI
|
2021-03-12 13:08:51 +08:00
|
|
|
from selfdrive.controls.lib.latcontrol_angle import LatControlAngle
|
2022-04-20 10:34:31 +08:00
|
|
|
from selfdrive.controls.lib.latcontrol_torque import LatControlTorque
|
2020-05-15 06:21:21 +08:00
|
|
|
from selfdrive.controls.lib.events import Events, ET
|
2021-11-18 12:29:00 +08:00
|
|
|
from selfdrive.controls.lib.alertmanager import AlertManager, set_offroad_alert
|
2020-01-18 04:48:30 +08:00
|
|
|
from selfdrive.controls.lib.vehicle_model import VehicleModel
|
2020-09-11 03:16:29 +08:00
|
|
|
from selfdrive.locationd.calibrationd import Calibration
|
2022-06-12 07:38:24 +08:00
|
|
|
from system.hardware import HARDWARE
|
2021-08-07 06:48:16 +08:00
|
|
|
from selfdrive.manager.process_config import managed_processes
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2021-12-12 09:07:37 +08:00
|
|
|
SOFT_DISABLE_TIME = 3 # seconds
|
2020-05-13 06:06:48 +08:00
|
|
|
LDW_MIN_SPEED = 31 * CV.MPH_TO_MS
|
2020-01-18 04:48:30 +08:00
|
|
|
LANE_DEPARTURE_THRESHOLD = 0.1
|
2020-09-10 18:19:14 +08:00
|
|
|
|
2021-11-13 10:22:39 +08:00
|
|
|
REPLAY = "REPLAY" in os.environ
|
2020-09-10 18:14:49 +08:00
|
|
|
SIMULATION = "SIMULATION" in os.environ
|
2020-09-10 18:19:14 +08:00
|
|
|
NOSENSOR = "NOSENSOR" in os.environ
|
2022-04-19 08:55:23 +08:00
|
|
|
IGNORE_PROCESSES = {"uploader", "deleter", "loggerd", "logmessaged", "tombstoned", "statsd",
|
2022-07-01 23:26:41 +08:00
|
|
|
"logcatd", "proclogd", "clocksd", "updated", "timezoned", "manage_athenad", "laikad"} | \
|
2022-04-05 08:13:29 +08:00
|
|
|
{k for k, v in managed_processes.items() if not v.enabled}
|
2021-08-20 01:00:03 +08:00
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
ThermalStatus = log.DeviceState.ThermalStatus
|
2020-01-18 04:48:30 +08:00
|
|
|
State = log.ControlsState.OpenpilotState
|
2021-02-17 13:39:32 +08:00
|
|
|
PandaType = log.PandaState.PandaType
|
2021-02-04 11:57:30 +08:00
|
|
|
Desire = log.LateralPlan.Desire
|
|
|
|
LaneChangeState = log.LateralPlan.LaneChangeState
|
|
|
|
LaneChangeDirection = log.LateralPlan.LaneChangeDirection
|
2020-05-15 06:21:21 +08:00
|
|
|
EventName = car.CarEvent.EventName
|
2022-08-18 09:03:33 +08:00
|
|
|
ButtonType = car.CarState.ButtonEvent.Type
|
2021-10-08 23:54:34 +08:00
|
|
|
SafetyModel = car.CarParams.SafetyModel
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-04-05 08:13:29 +08:00
|
|
|
IGNORED_SAFETY_MODES = (SafetyModel.silent, SafetyModel.noOutput)
|
2022-04-15 04:17:32 +08:00
|
|
|
CSID_MAP = {"1": EventName.roadCameraError, "2": EventName.wideRoadCameraError, "0": EventName.driverCameraError}
|
2022-04-05 08:13:29 +08:00
|
|
|
ACTUATOR_FIELDS = tuple(car.CarControl.Actuators.schema.fields.keys())
|
2022-04-05 16:29:23 +08:00
|
|
|
ACTIVE_STATES = (State.enabled, State.softDisabling, State.overriding)
|
2022-04-05 08:13:29 +08:00
|
|
|
ENABLED_STATES = (State.preEnabled, *ACTIVE_STATES)
|
|
|
|
|
2020-06-20 07:54:42 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
class Controls:
|
2022-04-05 08:13:29 +08:00
|
|
|
def __init__(self, sm=None, pm=None, can_sock=None, CI=None):
|
2022-04-30 04:36:25 +08:00
|
|
|
config_realtime_process(4, Priority.CTRL_HIGH)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-07-04 23:13:30 +08:00
|
|
|
# Ensure the current branch is cached, otherwise the first iteration of controlsd lags
|
|
|
|
self.branch = get_short_branch("")
|
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Setup sockets
|
|
|
|
self.pm = pm
|
|
|
|
if self.pm is None:
|
2020-06-01 08:41:18 +08:00
|
|
|
self.pm = messaging.PubMaster(['sendcan', 'controlsState', 'carState',
|
2020-05-13 06:06:48 +08:00
|
|
|
'carControl', 'carEvents', 'carParams'])
|
|
|
|
|
2022-06-02 21:20:51 +08:00
|
|
|
self.camera_packets = ["roadCameraState", "driverCameraState", "wideRoadCameraState"]
|
2021-05-20 12:28:16 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
self.can_sock = can_sock
|
|
|
|
if can_sock is None:
|
2022-04-30 04:36:25 +08:00
|
|
|
can_timeout = None if os.environ.get('NO_CAN_TIMEOUT', False) else 20
|
2020-05-13 06:06:48 +08:00
|
|
|
self.can_sock = messaging.sub_sock('can', timeout=can_timeout)
|
|
|
|
|
2022-06-02 21:20:51 +08:00
|
|
|
self.log_sock = messaging.sub_sock('androidLog')
|
2021-05-20 12:28:16 +08:00
|
|
|
|
2022-10-26 06:51:39 +08:00
|
|
|
self.params = Params()
|
2022-04-07 02:31:31 +08:00
|
|
|
self.sm = sm
|
|
|
|
if self.sm is None:
|
2022-10-18 14:04:06 +08:00
|
|
|
ignore = ['testJoystick']
|
2022-07-15 10:55:18 +08:00
|
|
|
if SIMULATION:
|
|
|
|
ignore += ['driverCameraState', 'managerState']
|
2022-10-26 06:51:39 +08:00
|
|
|
if self.params.get_bool('WideCameraOnly'):
|
2022-07-15 10:55:18 +08:00
|
|
|
ignore += ['roadCameraState']
|
2022-04-07 02:31:31 +08:00
|
|
|
self.sm = messaging.SubMaster(['deviceState', 'pandaStates', 'peripheralState', 'modelV2', 'liveCalibration',
|
|
|
|
'driverMonitoringState', 'longitudinalPlan', 'lateralPlan', 'liveLocationKalman',
|
2022-10-18 14:04:06 +08:00
|
|
|
'managerState', 'liveParameters', 'radarState', 'liveTorqueParameters', 'testJoystick'] + self.camera_packets,
|
|
|
|
ignore_alive=ignore, ignore_avg_freq=['radarState', 'longitudinalPlan', 'testJoystick'])
|
|
|
|
|
|
|
|
if CI is None:
|
|
|
|
# wait for one pandaState and one CAN packet
|
|
|
|
print("Waiting for CAN messages...")
|
|
|
|
get_one_can(self.can_sock)
|
|
|
|
|
|
|
|
num_pandas = len(messaging.recv_one_retry(self.sm.sock['pandaStates']).pandaStates)
|
|
|
|
self.CI, self.CP = get_car(self.can_sock, self.pm.sock['sendcan'], num_pandas)
|
|
|
|
else:
|
|
|
|
self.CI, self.CP = CI, CI.CP
|
|
|
|
|
2022-10-26 06:51:39 +08:00
|
|
|
self.joystick_mode = self.params.get_bool("JoystickDebugMode") or (self.CP.notCar and sm is None)
|
2022-04-07 02:31:31 +08:00
|
|
|
|
2022-04-05 12:59:52 +08:00
|
|
|
# set alternative experiences from parameters
|
2022-10-26 06:51:39 +08:00
|
|
|
self.disengage_on_accelerator = self.params.get_bool("DisengageOnAccelerator")
|
2022-04-05 12:59:52 +08:00
|
|
|
self.CP.alternativeExperience = 0
|
|
|
|
if not self.disengage_on_accelerator:
|
|
|
|
self.CP.alternativeExperience |= ALTERNATIVE_EXPERIENCE.DISABLE_DISENGAGE_ON_GAS
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-10-26 06:51:39 +08:00
|
|
|
if self.CP.dashcamOnly and self.params.get_bool("DashcamOverride"):
|
2022-06-17 17:56:04 +08:00
|
|
|
self.CP.dashcamOnly = False
|
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# read params
|
2022-10-26 06:51:39 +08:00
|
|
|
self.is_metric = self.params.get_bool("IsMetric")
|
|
|
|
self.is_ldw_enabled = self.params.get_bool("IsLdwEnabled")
|
|
|
|
openpilot_enabled_toggle = self.params.get_bool("OpenpilotEnabledToggle")
|
|
|
|
passive = self.params.get_bool("Passive") or not openpilot_enabled_toggle
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
# detect sound card presence and ensure successful init
|
2020-08-26 20:57:17 +08:00
|
|
|
sounds_available = HARDWARE.get_sound_card_online()
|
2020-05-15 06:21:21 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
car_recognized = self.CP.carName != 'mock'
|
2021-04-20 18:00:36 +08:00
|
|
|
|
2021-07-08 05:59:18 +08:00
|
|
|
controller_available = self.CI.CC is not None and not passive and not self.CP.dashcamOnly
|
2022-01-25 06:49:47 +08:00
|
|
|
self.read_only = not car_recognized or not controller_available or self.CP.dashcamOnly
|
2020-05-13 06:06:48 +08:00
|
|
|
if self.read_only:
|
2021-10-08 23:54:34 +08:00
|
|
|
safety_config = car.CarParams.SafetyConfig.new_message()
|
|
|
|
safety_config.safetyModel = car.CarParams.SafetyModel.noOutput
|
|
|
|
self.CP.safetyConfigs = [safety_config]
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-11-22 08:18:51 +08:00
|
|
|
if is_release_branch():
|
2022-09-08 08:12:49 +08:00
|
|
|
self.CP.experimentalLongitudinalAvailable = False
|
|
|
|
|
2021-05-01 04:55:17 +08:00
|
|
|
# Write CarParams for radard
|
2020-05-13 06:06:48 +08:00
|
|
|
cp_bytes = self.CP.to_bytes()
|
2022-10-26 06:51:39 +08:00
|
|
|
self.params.put("CarParams", cp_bytes)
|
2020-05-13 06:06:48 +08:00
|
|
|
put_nonblocking("CarParamsCache", cp_bytes)
|
2022-09-08 08:12:49 +08:00
|
|
|
put_nonblocking("CarParamsPersistent", cp_bytes)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-10-05 08:23:41 +08:00
|
|
|
# cleanup old params
|
|
|
|
if not self.CP.experimentalLongitudinalAvailable:
|
2022-10-26 06:51:39 +08:00
|
|
|
self.params.remove("ExperimentalLongitudinalEnabled")
|
2022-10-06 04:00:22 +08:00
|
|
|
if not self.CP.openpilotLongitudinalControl:
|
2022-11-09 06:45:01 +08:00
|
|
|
self.params.remove("ExperimentalMode")
|
2022-10-05 08:23:41 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
self.CC = car.CarControl.new_message()
|
2022-03-16 11:51:07 +08:00
|
|
|
self.CS_prev = car.CarState.new_message()
|
2020-05-13 06:06:48 +08:00
|
|
|
self.AM = AlertManager()
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events = Events()
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-08-31 00:32:52 +08:00
|
|
|
self.LoC = LongControl(self.CP)
|
2020-05-13 06:06:48 +08:00
|
|
|
self.VM = VehicleModel(self.CP)
|
|
|
|
|
2022-05-12 20:08:11 +08:00
|
|
|
self.LaC: LatControl
|
2021-03-12 13:08:51 +08:00
|
|
|
if self.CP.steerControlType == car.CarParams.SteerControlType.angle:
|
2022-01-27 00:10:41 +08:00
|
|
|
self.LaC = LatControlAngle(self.CP, self.CI)
|
2021-03-12 13:08:51 +08:00
|
|
|
elif self.CP.lateralTuning.which() == 'pid':
|
2021-10-04 16:03:53 +08:00
|
|
|
self.LaC = LatControlPID(self.CP, self.CI)
|
2020-05-13 06:06:48 +08:00
|
|
|
elif self.CP.lateralTuning.which() == 'indi':
|
2022-01-27 00:10:41 +08:00
|
|
|
self.LaC = LatControlINDI(self.CP, self.CI)
|
2022-04-20 10:34:31 +08:00
|
|
|
elif self.CP.lateralTuning.which() == 'torque':
|
|
|
|
self.LaC = LatControlTorque(self.CP, self.CI)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-05-01 04:55:17 +08:00
|
|
|
self.initialized = False
|
2020-05-13 06:06:48 +08:00
|
|
|
self.state = State.disabled
|
|
|
|
self.enabled = False
|
|
|
|
self.active = False
|
2022-08-26 11:30:43 +08:00
|
|
|
self.can_rcv_timeout = False
|
2020-05-13 06:06:48 +08:00
|
|
|
self.soft_disable_timer = 0
|
|
|
|
self.mismatch_counter = 0
|
2021-11-11 05:01:42 +08:00
|
|
|
self.cruise_mismatch_counter = 0
|
2022-08-26 11:30:43 +08:00
|
|
|
self.can_rcv_timeout_counter = 0
|
2020-05-13 06:06:48 +08:00
|
|
|
self.last_blinker_frame = 0
|
2020-06-20 05:11:23 +08:00
|
|
|
self.distance_traveled = 0
|
2020-10-07 18:15:09 +08:00
|
|
|
self.last_functional_fan_frame = 0
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events_prev = []
|
2020-06-20 07:16:48 +08:00
|
|
|
self.current_alert_types = [ET.PERMANENT]
|
2022-04-30 04:36:25 +08:00
|
|
|
self.logged_comm_issue = None
|
2021-12-16 20:08:20 +08:00
|
|
|
self.last_actuators = car.CarControl.Actuators.new_message()
|
2022-07-25 05:56:55 +08:00
|
|
|
self.steer_limited = False
|
2022-05-13 07:59:52 +08:00
|
|
|
self.desired_curvature = 0.0
|
|
|
|
self.desired_curvature_rate = 0.0
|
2022-11-12 10:53:48 +08:00
|
|
|
self.v_cruise_helper = VCruiseHelper(self.CP)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-05-01 04:55:17 +08:00
|
|
|
# TODO: no longer necessary, aside from process replay
|
2021-03-15 18:33:45 +08:00
|
|
|
self.sm['liveParameters'].valid = True
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-09-24 08:15:33 +08:00
|
|
|
self.startup_event = get_startup_event(car_recognized, controller_available, len(self.CP.carFw) > 0)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
|
|
|
if not sounds_available:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.soundsUnavailable, static=True)
|
2020-09-18 13:06:31 +08:00
|
|
|
if not car_recognized:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.carUnrecognized, static=True)
|
2021-11-18 12:29:00 +08:00
|
|
|
if len(self.CP.carFw) > 0:
|
|
|
|
set_offroad_alert("Offroad_CarUnrecognized", True)
|
|
|
|
else:
|
|
|
|
set_offroad_alert("Offroad_NoFirmware", True)
|
2021-03-26 12:37:38 +08:00
|
|
|
elif self.read_only:
|
|
|
|
self.events.add(EventName.dashcamMode, static=True)
|
2021-06-12 05:33:17 +08:00
|
|
|
elif self.joystick_mode:
|
|
|
|
self.events.add(EventName.joystickDebug, static=True)
|
|
|
|
self.startup_event = None
|
2020-05-15 06:21:21 +08:00
|
|
|
|
|
|
|
# controlsd is driven by can recv, expected at 100Hz
|
|
|
|
self.rk = Ratekeeper(100, print_delay_threshold=None)
|
|
|
|
self.prof = Profiler(False) # off by default
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-08-17 13:02:03 +08:00
|
|
|
def set_initial_state(self):
|
|
|
|
if REPLAY:
|
|
|
|
controls_state = Params().get("ReplayControlsState")
|
|
|
|
if controls_state is not None:
|
|
|
|
controls_state = log.ControlsState.from_bytes(controls_state)
|
2022-11-12 10:53:48 +08:00
|
|
|
self.v_cruise_helper.v_cruise_kph = controls_state.vCruise
|
2022-08-17 13:02:03 +08:00
|
|
|
|
2022-09-14 11:35:16 +08:00
|
|
|
if any(ps.controlsAllowed for ps in self.sm['pandaStates']):
|
2022-08-17 13:02:03 +08:00
|
|
|
self.state = State.enabled
|
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
def update_events(self, CS):
|
2020-05-13 06:06:48 +08:00
|
|
|
"""Compute carEvents from carState"""
|
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.clear()
|
|
|
|
|
2022-01-31 12:38:05 +08:00
|
|
|
# Add startup event
|
2020-05-15 06:21:21 +08:00
|
|
|
if self.startup_event is not None:
|
|
|
|
self.events.add(self.startup_event)
|
|
|
|
self.startup_event = None
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-05-01 04:55:17 +08:00
|
|
|
# Don't add any more events if not initialized
|
|
|
|
if not self.initialized:
|
|
|
|
self.events.add(EventName.controlsInitializing)
|
|
|
|
return
|
|
|
|
|
2022-09-23 04:43:27 +08:00
|
|
|
# no more events while in dashcam mode
|
|
|
|
if self.read_only:
|
|
|
|
return
|
|
|
|
|
2022-08-18 09:03:33 +08:00
|
|
|
# Block resume if cruise never previously enabled
|
|
|
|
resume_pressed = any(be.type in (ButtonType.accelCruise, ButtonType.resumeCruise) for be in CS.buttonEvents)
|
2022-11-12 10:53:48 +08:00
|
|
|
if not self.CP.pcmCruise and not self.v_cruise_helper.v_cruise_initialized and resume_pressed:
|
2022-08-18 09:03:33 +08:00
|
|
|
self.events.add(EventName.resumeBlocked)
|
|
|
|
|
2022-04-05 12:59:52 +08:00
|
|
|
# Disable on rising edge of accelerator or brake. Also disable on brake when speed > 0
|
|
|
|
if (CS.gasPressed and not self.CS_prev.gasPressed and self.disengage_on_accelerator) or \
|
2022-11-11 11:06:31 +08:00
|
|
|
(CS.brakePressed and (not self.CS_prev.brakePressed or not CS.standstill)) or \
|
|
|
|
(CS.regenBraking and (not self.CS_prev.regenBraking or not CS.standstill)):
|
2022-03-16 11:51:07 +08:00
|
|
|
self.events.add(EventName.pedalPressed)
|
|
|
|
|
2022-11-30 09:34:10 +08:00
|
|
|
if CS.brakePressed and CS.standstill:
|
|
|
|
self.events.add(EventName.preEnableStandstill)
|
|
|
|
|
2022-04-05 12:59:52 +08:00
|
|
|
if CS.gasPressed:
|
2022-10-28 11:53:15 +08:00
|
|
|
self.events.add(EventName.gasPressedOverride)
|
2022-04-05 12:59:52 +08:00
|
|
|
|
2022-04-05 10:37:21 +08:00
|
|
|
if not self.CP.notCar:
|
|
|
|
self.events.add_from_msg(self.sm['driverMonitoringState'].events)
|
2022-01-31 12:38:05 +08:00
|
|
|
|
2022-07-15 10:55:18 +08:00
|
|
|
# Add car events, ignore if CAN isn't valid
|
|
|
|
if CS.canValid:
|
2022-04-30 04:36:25 +08:00
|
|
|
self.events.add_from_msg(CS.events)
|
|
|
|
|
2022-04-19 08:55:23 +08:00
|
|
|
# Create events for temperature, disk space, and memory
|
2021-02-17 13:39:32 +08:00
|
|
|
if self.sm['deviceState'].thermalStatus >= ThermalStatus.red:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.overheat)
|
2021-06-16 17:59:15 +08:00
|
|
|
if self.sm['deviceState'].freeSpacePercent < 7 and not SIMULATION:
|
2020-05-13 06:06:48 +08:00
|
|
|
# under 7% of space free no enable allowed
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.outOfSpace)
|
2021-05-27 10:47:10 +08:00
|
|
|
# TODO: make tici threshold the same
|
2022-04-30 04:36:25 +08:00
|
|
|
if self.sm['deviceState'].memoryUsagePercent > 90 and not SIMULATION:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.lowMemory)
|
2021-10-26 06:03:28 +08:00
|
|
|
|
|
|
|
# TODO: enable this once loggerd CPU usage is more reasonable
|
2022-04-19 08:55:23 +08:00
|
|
|
#cpus = list(self.sm['deviceState'].cpuUsagePercent)
|
2021-10-26 06:03:28 +08:00
|
|
|
#if max(cpus, default=0) > 95 and not SIMULATION:
|
|
|
|
# self.events.add(EventName.highCpuUsage)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2020-10-07 18:15:09 +08:00
|
|
|
# Alert if fan isn't spinning for 5 seconds
|
2022-11-23 03:46:28 +08:00
|
|
|
if self.sm['peripheralState'].pandaType != log.PandaState.PandaType.unknown:
|
|
|
|
if self.sm['peripheralState'].fanSpeedRpm == 0 and self.sm['deviceState'].fanSpeedPercentDesired > 50:
|
|
|
|
if (self.sm.frame - self.last_functional_fan_frame) * DT_CTRL > 5.0:
|
|
|
|
self.events.add(EventName.fanMalfunction)
|
|
|
|
else:
|
|
|
|
self.last_functional_fan_frame = self.sm.frame
|
2020-10-07 18:15:09 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Handle calibration status
|
|
|
|
cal_status = self.sm['liveCalibration'].calStatus
|
|
|
|
if cal_status != Calibration.CALIBRATED:
|
|
|
|
if cal_status == Calibration.UNCALIBRATED:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.calibrationIncomplete)
|
2020-05-13 06:06:48 +08:00
|
|
|
else:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.calibrationInvalid)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Handle lane change
|
2021-02-04 11:57:30 +08:00
|
|
|
if self.sm['lateralPlan'].laneChangeState == LaneChangeState.preLaneChange:
|
|
|
|
direction = self.sm['lateralPlan'].laneChangeDirection
|
2020-06-25 08:31:09 +08:00
|
|
|
if (CS.leftBlindspot and direction == LaneChangeDirection.left) or \
|
|
|
|
(CS.rightBlindspot and direction == LaneChangeDirection.right):
|
|
|
|
self.events.add(EventName.laneChangeBlocked)
|
2020-05-13 06:06:48 +08:00
|
|
|
else:
|
2020-06-25 08:31:09 +08:00
|
|
|
if direction == LaneChangeDirection.left:
|
|
|
|
self.events.add(EventName.preLaneChangeLeft)
|
|
|
|
else:
|
|
|
|
self.events.add(EventName.preLaneChangeRight)
|
2022-01-10 18:26:58 +08:00
|
|
|
elif self.sm['lateralPlan'].laneChangeState in (LaneChangeState.laneChangeStarting,
|
|
|
|
LaneChangeState.laneChangeFinishing):
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.laneChange)
|
2020-09-10 18:19:14 +08:00
|
|
|
|
2021-10-08 23:54:34 +08:00
|
|
|
for i, pandaState in enumerate(self.sm['pandaStates']):
|
2021-10-29 19:04:26 +08:00
|
|
|
# All pandas must match the list of safetyConfigs, and if outside this list, must be silent or noOutput
|
2021-10-08 23:54:34 +08:00
|
|
|
if i < len(self.CP.safetyConfigs):
|
2022-01-25 19:09:13 +08:00
|
|
|
safety_mismatch = pandaState.safetyModel != self.CP.safetyConfigs[i].safetyModel or \
|
|
|
|
pandaState.safetyParam != self.CP.safetyConfigs[i].safetyParam or \
|
2022-03-25 05:59:16 +08:00
|
|
|
pandaState.alternativeExperience != self.CP.alternativeExperience
|
2021-10-08 23:54:34 +08:00
|
|
|
else:
|
2021-10-29 19:04:26 +08:00
|
|
|
safety_mismatch = pandaState.safetyModel not in IGNORED_SAFETY_MODES
|
2022-01-21 20:13:34 +08:00
|
|
|
|
2022-10-04 07:06:29 +08:00
|
|
|
if safety_mismatch or pandaState.safetyRxChecksInvalid or self.mismatch_counter >= 200:
|
2021-10-08 23:54:34 +08:00
|
|
|
self.events.add(EventName.controlsMismatch)
|
2021-02-02 05:41:04 +08:00
|
|
|
|
2021-10-29 19:04:26 +08:00
|
|
|
if log.PandaState.FaultType.relayMalfunction in pandaState.faults:
|
|
|
|
self.events.add(EventName.relayMalfunction)
|
|
|
|
|
2022-04-30 04:36:25 +08:00
|
|
|
# Handle HW and system malfunctions
|
|
|
|
# Order is very intentional here. Be careful when modifying this.
|
2022-05-24 08:46:46 +08:00
|
|
|
# All events here should at least have NO_ENTRY and SOFT_DISABLE.
|
2022-04-30 04:36:25 +08:00
|
|
|
num_events = len(self.events)
|
|
|
|
|
|
|
|
not_running = {p.name for p in self.sm['managerState'].processes if not p.running and p.shouldBeRunning}
|
|
|
|
if self.sm.rcv_frame['managerState'] and (not_running - IGNORE_PROCESSES):
|
|
|
|
self.events.add(EventName.processNotRunning)
|
|
|
|
else:
|
|
|
|
if not SIMULATION and not self.rk.lagging:
|
|
|
|
if not self.sm.all_alive(self.camera_packets):
|
|
|
|
self.events.add(EventName.cameraMalfunction)
|
|
|
|
elif not self.sm.all_freq_ok(self.camera_packets):
|
|
|
|
self.events.add(EventName.cameraFrameRate)
|
2022-04-29 10:51:42 +08:00
|
|
|
if self.rk.lagging:
|
|
|
|
self.events.add(EventName.controlsdLagging)
|
2022-07-15 10:55:18 +08:00
|
|
|
if len(self.sm['radarState'].radarErrors) or not self.sm.all_checks(['radarState']):
|
2021-02-04 11:57:30 +08:00
|
|
|
self.events.add(EventName.radarFault)
|
2022-04-30 04:36:25 +08:00
|
|
|
if not self.sm.valid['pandaStates']:
|
2021-05-15 12:26:02 +08:00
|
|
|
self.events.add(EventName.usbError)
|
2022-07-15 10:55:18 +08:00
|
|
|
if CS.canTimeout:
|
|
|
|
self.events.add(EventName.canBusMissing)
|
|
|
|
elif not CS.canValid:
|
|
|
|
self.events.add(EventName.canError)
|
2022-04-07 07:43:32 +08:00
|
|
|
|
2022-04-30 04:36:25 +08:00
|
|
|
# generic catch-all. ideally, a more specific event should be added above instead
|
2022-07-15 10:55:18 +08:00
|
|
|
has_disable_events = self.events.any(ET.NO_ENTRY) and (self.events.any(ET.SOFT_DISABLE) or self.events.any(ET.IMMEDIATE_DISABLE))
|
|
|
|
no_system_errors = (not has_disable_events) or (len(self.events) == num_events)
|
2022-08-26 11:30:43 +08:00
|
|
|
if (not self.sm.all_checks() or self.can_rcv_timeout) and no_system_errors:
|
2022-04-07 07:43:32 +08:00
|
|
|
if not self.sm.all_alive():
|
|
|
|
self.events.add(EventName.commIssue)
|
|
|
|
elif not self.sm.all_freq_ok():
|
|
|
|
self.events.add(EventName.commIssueAvgFreq)
|
2022-08-26 11:30:43 +08:00
|
|
|
else: # invalid or can_rcv_timeout.
|
2022-04-07 07:43:32 +08:00
|
|
|
self.events.add(EventName.commIssue)
|
|
|
|
|
2022-04-30 04:36:25 +08:00
|
|
|
logs = {
|
|
|
|
'invalid': [s for s, valid in self.sm.valid.items() if not valid],
|
|
|
|
'not_alive': [s for s, alive in self.sm.alive.items() if not alive],
|
|
|
|
'not_freq_ok': [s for s, freq_ok in self.sm.freq_ok.items() if not freq_ok],
|
2022-08-26 11:30:43 +08:00
|
|
|
'can_rcv_timeout': self.can_rcv_timeout,
|
2022-04-30 04:36:25 +08:00
|
|
|
}
|
|
|
|
if logs != self.logged_comm_issue:
|
|
|
|
cloudlog.event("commIssue", error=True, **logs)
|
|
|
|
self.logged_comm_issue = logs
|
2021-02-02 05:41:04 +08:00
|
|
|
else:
|
2022-04-30 04:36:25 +08:00
|
|
|
self.logged_comm_issue = None
|
2021-02-02 05:41:04 +08:00
|
|
|
|
2021-11-11 05:01:42 +08:00
|
|
|
if not self.sm['liveParameters'].valid:
|
|
|
|
self.events.add(EventName.vehicleModelInvalid)
|
2021-02-04 11:57:30 +08:00
|
|
|
if not self.sm['lateralPlan'].mpcSolutionValid:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.plannerError)
|
2022-07-29 04:10:01 +08:00
|
|
|
if not (self.sm['liveParameters'].sensorValid or self.sm['liveLocationKalman'].sensorsOK) and not NOSENSOR:
|
2020-05-21 02:25:27 +08:00
|
|
|
if self.sm.frame > 5 / DT_CTRL: # Give locationd some time to receive all the inputs
|
|
|
|
self.events.add(EventName.sensorDataInvalid)
|
2020-05-20 07:45:20 +08:00
|
|
|
if not self.sm['liveLocationKalman'].posenetOK:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.posenetInvalid)
|
2020-08-04 06:51:56 +08:00
|
|
|
if not self.sm['liveLocationKalman'].deviceStable:
|
|
|
|
self.events.add(EventName.deviceFalling)
|
2021-10-20 22:16:40 +08:00
|
|
|
|
2021-11-13 10:22:39 +08:00
|
|
|
if not REPLAY:
|
|
|
|
# Check for mismatch between openpilot and car's PCM
|
|
|
|
cruise_mismatch = CS.cruiseState.enabled and (not self.enabled or not self.CP.pcmCruise)
|
|
|
|
self.cruise_mismatch_counter = self.cruise_mismatch_counter + 1 if cruise_mismatch else 0
|
2022-05-05 06:58:08 +08:00
|
|
|
if self.cruise_mismatch_counter > int(6. / DT_CTRL):
|
2021-11-13 10:22:39 +08:00
|
|
|
self.events.add(EventName.cruiseMismatch)
|
2021-11-11 05:01:42 +08:00
|
|
|
|
|
|
|
# Check for FCW
|
2022-03-07 17:42:07 +08:00
|
|
|
stock_long_is_braking = self.enabled and not self.CP.openpilotLongitudinalControl and CS.aEgo < -1.25
|
2021-10-20 22:16:40 +08:00
|
|
|
model_fcw = self.sm['modelV2'].meta.hardBrakePredicted and not CS.brakePressed and not stock_long_is_braking
|
2021-09-30 02:55:54 +08:00
|
|
|
planner_fcw = self.sm['longitudinalPlan'].fcw and self.enabled
|
|
|
|
if planner_fcw or model_fcw:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.fcw)
|
2020-11-24 05:42:18 +08:00
|
|
|
|
2022-06-02 21:20:51 +08:00
|
|
|
for m in messaging.drain_sock(self.log_sock, wait_for_one=False):
|
|
|
|
try:
|
|
|
|
msg = m.androidLog.message
|
|
|
|
if any(err in msg for err in ("ERROR_CRC", "ERROR_ECC", "ERROR_STREAM_UNDERFLOW", "APPLY FAILED")):
|
|
|
|
csid = msg.split("CSID:")[-1].split(" ")[0]
|
|
|
|
evt = CSID_MAP.get(csid, None)
|
|
|
|
if evt is not None:
|
|
|
|
self.events.add(evt)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
pass
|
2021-05-22 05:53:30 +08:00
|
|
|
|
2021-01-15 08:03:16 +08:00
|
|
|
# TODO: fix simulator
|
|
|
|
if not SIMULATION:
|
|
|
|
if not NOSENSOR:
|
2021-06-24 08:01:17 +08:00
|
|
|
if not self.sm['liveLocationKalman'].gpsOK and (self.distance_traveled > 1000):
|
2021-01-15 08:03:16 +08:00
|
|
|
# Not show in first 1 km to allow for driving out of garage. This event shows after 5 minutes
|
|
|
|
self.events.add(EventName.noGps)
|
2022-04-07 07:43:32 +08:00
|
|
|
|
2021-01-20 09:15:16 +08:00
|
|
|
if self.sm['modelV2'].frameDropPerc > 20:
|
2021-01-15 08:03:16 +08:00
|
|
|
self.events.add(EventName.modeldLagging)
|
2021-06-04 00:26:53 +08:00
|
|
|
if self.sm['liveLocationKalman'].excessiveResets:
|
|
|
|
self.events.add(EventName.localizerMalfunction)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
def data_sample(self):
|
|
|
|
"""Receive data from sockets and update carState"""
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Update carState from CAN
|
|
|
|
can_strs = messaging.drain_sock_raw(self.can_sock, wait_for_one=True)
|
|
|
|
CS = self.CI.update(self.CC, can_strs)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
self.sm.update(0)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-01-10 19:13:58 +08:00
|
|
|
if not self.initialized:
|
2022-04-07 07:43:32 +08:00
|
|
|
all_valid = CS.canValid and self.sm.all_checks()
|
2022-07-12 08:20:56 +08:00
|
|
|
timed_out = self.sm.frame * DT_CTRL > (6. if REPLAY else 3.5)
|
|
|
|
if all_valid or timed_out or SIMULATION:
|
2022-01-10 19:13:58 +08:00
|
|
|
if not self.read_only:
|
|
|
|
self.CI.init(self.CP, self.can_sock, self.pm.sock['sendcan'])
|
2022-01-21 20:13:34 +08:00
|
|
|
|
2022-08-17 13:02:03 +08:00
|
|
|
self.initialized = True
|
|
|
|
self.set_initial_state()
|
2022-01-10 19:13:58 +08:00
|
|
|
Params().put_bool("ControlsReady", True)
|
2021-05-01 04:55:17 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Check for CAN timeout
|
|
|
|
if not can_strs:
|
2022-08-26 11:30:43 +08:00
|
|
|
self.can_rcv_timeout_counter += 1
|
|
|
|
self.can_rcv_timeout = True
|
2020-01-18 04:48:30 +08:00
|
|
|
else:
|
2022-08-26 11:30:43 +08:00
|
|
|
self.can_rcv_timeout = False
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# When the panda and controlsd do not agree on controls_allowed
|
|
|
|
# we want to disengage openpilot. However the status from the panda goes through
|
|
|
|
# another socket other than the CAN messages and one can arrive earlier than the other.
|
|
|
|
# Therefore we allow a mismatch for two samples, then we trigger the disengagement.
|
|
|
|
if not self.enabled:
|
|
|
|
self.mismatch_counter = 0
|
2020-03-31 09:41:56 +08:00
|
|
|
|
2021-10-08 23:54:34 +08:00
|
|
|
# All pandas not in silent mode must have controlsAllowed when openpilot is enabled
|
2022-01-13 18:53:04 +08:00
|
|
|
if self.enabled and any(not ps.controlsAllowed for ps in self.sm['pandaStates']
|
2021-10-29 19:04:26 +08:00
|
|
|
if ps.safetyModel not in IGNORED_SAFETY_MODES):
|
|
|
|
self.mismatch_counter += 1
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-06-20 05:11:23 +08:00
|
|
|
self.distance_traveled += CS.vEgo * DT_CTRL
|
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
return CS
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
def state_transition(self, CS):
|
2020-05-13 06:06:48 +08:00
|
|
|
"""Compute conditional state transitions and execute actions on state transitions"""
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-11-12 10:53:48 +08:00
|
|
|
self.v_cruise_helper.update_v_cruise(CS, self.enabled, self.is_metric)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2021-11-17 05:11:32 +08:00
|
|
|
# decrement the soft disable timer at every step, as it's reset on
|
2020-05-13 06:06:48 +08:00
|
|
|
# entrance in SOFT_DISABLING state
|
|
|
|
self.soft_disable_timer = max(0, self.soft_disable_timer - 1)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
self.current_alert_types = [ET.PERMANENT]
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-04-05 09:02:27 +08:00
|
|
|
# ENABLED, SOFT DISABLING, PRE ENABLING, OVERRIDING
|
2020-05-13 06:06:48 +08:00
|
|
|
if self.state != State.disabled:
|
|
|
|
# user and immediate disable always have priority in a non-disabled state
|
2020-05-15 06:21:21 +08:00
|
|
|
if self.events.any(ET.USER_DISABLE):
|
2020-05-13 06:06:48 +08:00
|
|
|
self.state = State.disabled
|
2020-05-15 06:21:21 +08:00
|
|
|
self.current_alert_types.append(ET.USER_DISABLE)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
elif self.events.any(ET.IMMEDIATE_DISABLE):
|
2020-05-13 06:06:48 +08:00
|
|
|
self.state = State.disabled
|
2020-05-15 06:21:21 +08:00
|
|
|
self.current_alert_types.append(ET.IMMEDIATE_DISABLE)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
|
|
|
else:
|
2020-05-13 06:06:48 +08:00
|
|
|
# ENABLED
|
|
|
|
if self.state == State.enabled:
|
2020-05-15 06:21:21 +08:00
|
|
|
if self.events.any(ET.SOFT_DISABLE):
|
2020-05-13 06:06:48 +08:00
|
|
|
self.state = State.softDisabling
|
2021-12-12 09:07:37 +08:00
|
|
|
self.soft_disable_timer = int(SOFT_DISABLE_TIME / DT_CTRL)
|
2020-05-15 06:21:21 +08:00
|
|
|
self.current_alert_types.append(ET.SOFT_DISABLE)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-09-14 14:20:45 +08:00
|
|
|
elif self.events.any(ET.OVERRIDE_LATERAL) or self.events.any(ET.OVERRIDE_LONGITUDINAL):
|
2022-04-05 09:02:27 +08:00
|
|
|
self.state = State.overriding
|
2022-09-14 14:20:45 +08:00
|
|
|
self.current_alert_types += [ET.OVERRIDE_LATERAL, ET.OVERRIDE_LONGITUDINAL]
|
2022-04-05 09:02:27 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# SOFT DISABLING
|
|
|
|
elif self.state == State.softDisabling:
|
2020-05-15 06:21:21 +08:00
|
|
|
if not self.events.any(ET.SOFT_DISABLE):
|
2020-05-13 06:06:48 +08:00
|
|
|
# no more soft disabling condition, so go back to ENABLED
|
|
|
|
self.state = State.enabled
|
|
|
|
|
2022-01-25 13:27:56 +08:00
|
|
|
elif self.soft_disable_timer > 0:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.current_alert_types.append(ET.SOFT_DISABLE)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
|
|
|
elif self.soft_disable_timer <= 0:
|
|
|
|
self.state = State.disabled
|
|
|
|
|
|
|
|
# PRE ENABLING
|
|
|
|
elif self.state == State.preEnabled:
|
2022-11-30 05:11:03 +08:00
|
|
|
if not self.events.any(ET.PRE_ENABLE):
|
2020-05-13 06:06:48 +08:00
|
|
|
self.state = State.enabled
|
2020-06-29 11:29:42 +08:00
|
|
|
else:
|
|
|
|
self.current_alert_types.append(ET.PRE_ENABLE)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-04-05 09:02:27 +08:00
|
|
|
# OVERRIDING
|
|
|
|
elif self.state == State.overriding:
|
2022-04-06 02:36:18 +08:00
|
|
|
if self.events.any(ET.SOFT_DISABLE):
|
|
|
|
self.state = State.softDisabling
|
|
|
|
self.soft_disable_timer = int(SOFT_DISABLE_TIME / DT_CTRL)
|
|
|
|
self.current_alert_types.append(ET.SOFT_DISABLE)
|
2022-09-14 14:20:45 +08:00
|
|
|
elif not (self.events.any(ET.OVERRIDE_LATERAL) or self.events.any(ET.OVERRIDE_LONGITUDINAL)):
|
2022-04-05 09:02:27 +08:00
|
|
|
self.state = State.enabled
|
|
|
|
else:
|
2022-09-14 14:20:45 +08:00
|
|
|
self.current_alert_types += [ET.OVERRIDE_LATERAL, ET.OVERRIDE_LONGITUDINAL]
|
2022-04-05 09:02:27 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# DISABLED
|
|
|
|
elif self.state == State.disabled:
|
2020-05-15 06:21:21 +08:00
|
|
|
if self.events.any(ET.ENABLE):
|
|
|
|
if self.events.any(ET.NO_ENTRY):
|
|
|
|
self.current_alert_types.append(ET.NO_ENTRY)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2020-01-18 04:48:30 +08:00
|
|
|
else:
|
2020-05-15 06:21:21 +08:00
|
|
|
if self.events.any(ET.PRE_ENABLE):
|
2020-05-13 06:06:48 +08:00
|
|
|
self.state = State.preEnabled
|
2022-09-14 14:20:45 +08:00
|
|
|
elif self.events.any(ET.OVERRIDE_LATERAL) or self.events.any(ET.OVERRIDE_LONGITUDINAL):
|
2022-04-05 09:02:27 +08:00
|
|
|
self.state = State.overriding
|
2020-05-13 06:06:48 +08:00
|
|
|
else:
|
|
|
|
self.state = State.enabled
|
2020-05-15 06:21:21 +08:00
|
|
|
self.current_alert_types.append(ET.ENABLE)
|
2022-11-12 10:53:48 +08:00
|
|
|
self.v_cruise_helper.initialize_v_cruise(CS)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-04-05 08:13:29 +08:00
|
|
|
# Check if openpilot is engaged and actuators are enabled
|
|
|
|
self.enabled = self.state in ENABLED_STATES
|
|
|
|
self.active = self.state in ACTIVE_STATES
|
2020-05-15 06:21:21 +08:00
|
|
|
if self.active:
|
|
|
|
self.current_alert_types.append(ET.WARNING)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
def state_control(self, CS):
|
2022-03-14 11:58:44 +08:00
|
|
|
"""Given the state, this function returns a CarControl packet"""
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2021-03-12 13:08:51 +08:00
|
|
|
# Update VehicleModel
|
2022-10-26 06:51:39 +08:00
|
|
|
lp = self.sm['liveParameters']
|
|
|
|
x = max(lp.stiffnessFactor, 0.1)
|
|
|
|
sr = max(lp.steerRatio, 0.1)
|
2021-03-12 13:08:51 +08:00
|
|
|
self.VM.update_params(x, sr)
|
|
|
|
|
Live torque (#25456)
* wip torqued
* add basic logic
* setup in manager
* check sanity and publish msg
* add first order filter to outputs
* wire up controlsd, and update gains
* rename intercept to offset
* add cloudlog, live values are not updated
* fix bugs, do not reset points for now
* fix crashes
* rename to main
* fix bugs, works offline
* fix float in cereal bug
* add latacc filter
* randomly choose points, approx for iid
* add variable decay
* local param to capnp instead of dict
* verify works in replay
* use torqued output in controlsd
* use in controlsd; use points from past routes
* controlsd bugfix
* filter before updating gains, needs to be replaced
* save all points to ensure smooth transition across routes, revert friction factor to 1.5
* add filters to prevent noisy low-speed data points; improve fit sanity
* add engaged buffer
* revert lat_acc thresh
* use paramsd realtime process config
* make latacc-to-torque generic, and overrideable
* move freq to 4Hz, avoid storing in np.array, don't publish points in the message
* float instead of np
* remove constant while storing pts
* rename slope, offset to lat_accet_factor, offset
* resolve issues
* use camelcase in all capnp params
* use camelcase everywhere
* reduce latacc threshold or sanity, add car_sane todo, save points properly
* add and check tag
* write param to disk at end of route
* remove args
* rebase op, cereal
* save on exit
* restore default handler
* cpu usage check
* add to process replay
* handle reset better, reduce unnecessary computation
* always publish raw values - useful for debug
* regen routes
* update refs
* checks on cache restore
* check tuning vals too
* clean that up
* reduce cpu usage
* reduce cpu usage by 75%
* cleanup
* optimize further
* handle reset condition better, don't put points in init, use only in corolla
* bump cereal after rebasing
* update refs
* Update common/params.cc
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove unnecessary checks
* Update RELEASES.md
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
old-commit-hash: 4fa62f146426f76c9c1c2867d9729b33ec612b59
2022-09-20 06:19:26 +08:00
|
|
|
# Update Torque Params
|
|
|
|
if self.CP.lateralTuning.which() == 'torque':
|
|
|
|
torque_params = self.sm['liveTorqueParameters']
|
|
|
|
if self.sm.all_checks(['liveTorqueParameters']) and torque_params.useParams:
|
|
|
|
self.LaC.update_live_torque_params(torque_params.latAccelFactorFiltered, torque_params.latAccelOffsetFiltered, torque_params.frictionCoefficientFiltered)
|
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
lat_plan = self.sm['lateralPlan']
|
|
|
|
long_plan = self.sm['longitudinalPlan']
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
CC = car.CarControl.new_message()
|
|
|
|
CC.enabled = self.enabled
|
|
|
|
# Check which actuators can be enabled
|
|
|
|
CC.latActive = self.active and not CS.steerFaultTemporary and not CS.steerFaultPermanent and \
|
2022-09-30 04:52:41 +08:00
|
|
|
CS.vEgo > self.CP.minSteerSpeed and not CS.standstill
|
2022-11-30 09:34:10 +08:00
|
|
|
CC.longActive = self.enabled and not self.events.any(ET.OVERRIDE_LONGITUDINAL) and self.CP.openpilotLongitudinalControl
|
2022-03-14 11:58:44 +08:00
|
|
|
|
|
|
|
actuators = CC.actuators
|
2021-09-07 11:14:01 +08:00
|
|
|
actuators.longControlState = self.LoC.long_control_state
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
if CS.leftBlinker or CS.rightBlinker:
|
|
|
|
self.last_blinker_frame = self.sm.frame
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# State specific actions
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
if not CC.latActive:
|
2020-05-13 06:06:48 +08:00
|
|
|
self.LaC.reset()
|
2022-03-14 11:58:44 +08:00
|
|
|
if not CC.longActive:
|
2020-05-13 06:06:48 +08:00
|
|
|
self.LoC.reset(v_pid=CS.vEgo)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2021-06-12 05:33:17 +08:00
|
|
|
if not self.joystick_mode:
|
2021-08-31 00:32:52 +08:00
|
|
|
# accel PID loop
|
2022-11-12 10:53:48 +08:00
|
|
|
pid_accel_limits = self.CI.get_pid_accel_limits(self.CP, CS.vEgo, self.v_cruise_helper.v_cruise_kph * CV.KPH_TO_MS)
|
2022-02-18 16:45:00 +08:00
|
|
|
t_since_plan = (self.sm.frame - self.sm.rcv_frame['longitudinalPlan']) * DT_CTRL
|
2022-04-28 15:42:52 +08:00
|
|
|
actuators.accel = self.LoC.update(CC.longActive, CS, long_plan, pid_accel_limits, t_since_plan)
|
2021-03-12 13:08:51 +08:00
|
|
|
|
2021-06-12 05:33:17 +08:00
|
|
|
# Steering PID loop and lateral MPC
|
2022-05-13 07:59:52 +08:00
|
|
|
self.desired_curvature, self.desired_curvature_rate = get_lag_adjusted_curvature(self.CP, CS.vEgo,
|
|
|
|
lat_plan.psis,
|
|
|
|
lat_plan.curvatures,
|
|
|
|
lat_plan.curvatureRates)
|
2022-10-26 06:51:39 +08:00
|
|
|
actuators.steer, actuators.steeringAngleDeg, lac_log = self.LaC.update(CC.latActive, CS, self.VM, lp,
|
2022-07-25 05:56:55 +08:00
|
|
|
self.last_actuators, self.steer_limited, self.desired_curvature,
|
2022-05-13 07:59:52 +08:00
|
|
|
self.desired_curvature_rate, self.sm['liveLocationKalman'])
|
2021-06-12 05:33:17 +08:00
|
|
|
else:
|
|
|
|
lac_log = log.ControlsState.LateralDebugState.new_message()
|
2022-03-14 11:58:44 +08:00
|
|
|
if self.sm.rcv_frame['testJoystick'] > 0:
|
|
|
|
if CC.longActive:
|
|
|
|
actuators.accel = 4.0*clip(self.sm['testJoystick'].axes[0], -1, 1)
|
2021-06-12 05:33:17 +08:00
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
if CC.latActive:
|
|
|
|
steer = clip(self.sm['testJoystick'].axes[1], -1, 1)
|
|
|
|
# max angle is 45 for angle-based cars
|
|
|
|
actuators.steer, actuators.steeringAngleDeg = steer, steer * 45.
|
2021-06-12 05:33:17 +08:00
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
lac_log.active = self.active
|
2021-06-12 05:33:17 +08:00
|
|
|
lac_log.steeringAngleDeg = CS.steeringAngleDeg
|
2022-03-14 11:58:44 +08:00
|
|
|
lac_log.output = actuators.steer
|
|
|
|
lac_log.saturated = abs(actuators.steer) >= 0.9
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Send a "steering required alert" if saturation count has reached the limit
|
2022-07-12 03:52:03 +08:00
|
|
|
if lac_log.active and not CS.steeringPressed and self.CP.lateralTuning.which() == 'torque' and not self.joystick_mode:
|
2022-07-02 10:14:21 +08:00
|
|
|
undershooting = abs(lac_log.desiredLateralAccel) / abs(1e-3 + lac_log.actualLateralAccel) > 1.2
|
|
|
|
turning = abs(lac_log.desiredLateralAccel) > 1.0
|
|
|
|
good_speed = CS.vEgo > 5
|
|
|
|
max_torque = abs(self.last_actuators.steer) > 0.99
|
|
|
|
if undershooting and turning and good_speed and max_torque:
|
|
|
|
self.events.add(EventName.steerSaturated)
|
2022-12-08 19:01:57 +08:00
|
|
|
elif lac_log.active and lac_log.saturated:
|
2022-01-10 18:27:31 +08:00
|
|
|
dpath_points = lat_plan.dPathPoints
|
|
|
|
if len(dpath_points):
|
2021-03-12 13:08:51 +08:00
|
|
|
# Check if we deviated from the path
|
2021-12-02 05:53:42 +08:00
|
|
|
# TODO use desired vs actual curvature
|
2022-08-12 17:30:19 +08:00
|
|
|
if self.CP.steerControlType == car.CarParams.SteerControlType.angle:
|
|
|
|
steering_value = actuators.steeringAngleDeg
|
|
|
|
else:
|
|
|
|
steering_value = actuators.steer
|
|
|
|
|
|
|
|
left_deviation = steering_value > 0 and dpath_points[0] < -0.20
|
|
|
|
right_deviation = steering_value < 0 and dpath_points[0] > 0.20
|
2021-03-12 13:08:51 +08:00
|
|
|
|
|
|
|
if left_deviation or right_deviation:
|
|
|
|
self.events.add(EventName.steerSaturated)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2021-08-20 01:00:03 +08:00
|
|
|
# Ensure no NaNs/Infs
|
|
|
|
for p in ACTUATOR_FIELDS:
|
2021-09-07 09:45:59 +08:00
|
|
|
attr = getattr(actuators, p)
|
2022-05-12 20:08:11 +08:00
|
|
|
if not isinstance(attr, SupportsFloat):
|
2021-09-07 09:45:59 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not math.isfinite(attr):
|
2021-08-20 01:00:03 +08:00
|
|
|
cloudlog.error(f"actuators.{p} not finite {actuators.to_dict()}")
|
|
|
|
setattr(actuators, p, 0.0)
|
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
return CC, lac_log
|
2020-04-17 02:38:31 +08:00
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
def publish_logs(self, CS, start_time, CC, lac_log):
|
2020-05-13 06:06:48 +08:00
|
|
|
"""Send actuators and hud commands to the car, send controlsstate and MPC logging"""
|
|
|
|
|
2022-03-17 12:12:11 +08:00
|
|
|
# Orientation and angle rates can be useful for carcontroller
|
|
|
|
# Only calibrated (car) frame is relevant for the carcontroller
|
|
|
|
orientation_value = list(self.sm['liveLocationKalman'].calibratedOrientationNED.value)
|
2022-01-04 19:04:03 +08:00
|
|
|
if len(orientation_value) > 2:
|
2022-03-17 12:12:11 +08:00
|
|
|
CC.orientationNED = orientation_value
|
|
|
|
angular_rate_value = list(self.sm['liveLocationKalman'].angularVelocityCalibrated.value)
|
|
|
|
if len(angular_rate_value) > 2:
|
|
|
|
CC.angularVelocity = angular_rate_value
|
2021-11-13 04:21:22 +08:00
|
|
|
|
2022-10-12 07:27:46 +08:00
|
|
|
CC.cruiseControl.override = self.enabled and not CC.longActive and self.CP.openpilotLongitudinalControl
|
2021-09-29 03:30:50 +08:00
|
|
|
CC.cruiseControl.cancel = CS.cruiseState.enabled and (not self.enabled or not self.CP.pcmCruise)
|
2021-06-12 05:33:17 +08:00
|
|
|
if self.joystick_mode and self.sm.rcv_frame['testJoystick'] > 0 and self.sm['testJoystick'].buttons[0]:
|
|
|
|
CC.cruiseControl.cancel = True
|
|
|
|
|
2022-07-07 15:24:03 +08:00
|
|
|
speeds = self.sm['longitudinalPlan'].speeds
|
|
|
|
if len(speeds):
|
|
|
|
CC.cruiseControl.resume = self.enabled and CS.cruiseState.standstill and speeds[-1] > 0.1
|
|
|
|
|
2022-01-04 19:04:03 +08:00
|
|
|
hudControl = CC.hudControl
|
2022-11-12 10:53:48 +08:00
|
|
|
hudControl.setSpeed = float(self.v_cruise_helper.v_cruise_cluster_kph * CV.KPH_TO_MS)
|
2022-01-04 19:04:03 +08:00
|
|
|
hudControl.speedVisible = self.enabled
|
|
|
|
hudControl.lanesVisible = self.enabled
|
|
|
|
hudControl.leadVisible = self.sm['longitudinalPlan'].hasLead
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-01-04 19:04:03 +08:00
|
|
|
hudControl.rightLaneVisible = True
|
|
|
|
hudControl.leftLaneVisible = True
|
2020-05-13 06:06:48 +08:00
|
|
|
|
|
|
|
recent_blinker = (self.sm.frame - self.last_blinker_frame) * DT_CTRL < 5.0 # 5s blinker cooldown
|
|
|
|
ldw_allowed = self.is_ldw_enabled and CS.vEgo > LDW_MIN_SPEED and not recent_blinker \
|
2022-09-30 04:52:41 +08:00
|
|
|
and not CC.latActive and self.sm['liveCalibration'].calStatus == Calibration.CALIBRATED
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-01-21 20:13:34 +08:00
|
|
|
model_v2 = self.sm['modelV2']
|
2022-01-04 19:04:03 +08:00
|
|
|
desire_prediction = model_v2.meta.desirePrediction
|
|
|
|
if len(desire_prediction) and ldw_allowed:
|
2022-09-07 04:05:47 +08:00
|
|
|
right_lane_visible = model_v2.laneLineProbs[2] > 0.5
|
|
|
|
left_lane_visible = model_v2.laneLineProbs[1] > 0.5
|
2022-01-04 19:04:03 +08:00
|
|
|
l_lane_change_prob = desire_prediction[Desire.laneChangeLeft - 1]
|
|
|
|
r_lane_change_prob = desire_prediction[Desire.laneChangeRight - 1]
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-01-04 19:04:03 +08:00
|
|
|
lane_lines = model_v2.laneLines
|
|
|
|
l_lane_close = left_lane_visible and (lane_lines[1].y[0] > -(1.08 + CAMERA_OFFSET))
|
|
|
|
r_lane_close = right_lane_visible and (lane_lines[2].y[0] < (1.08 - CAMERA_OFFSET))
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-01-04 19:04:03 +08:00
|
|
|
hudControl.leftLaneDepart = bool(l_lane_change_prob > LANE_DEPARTURE_THRESHOLD and l_lane_close)
|
|
|
|
hudControl.rightLaneDepart = bool(r_lane_change_prob > LANE_DEPARTURE_THRESHOLD and r_lane_close)
|
|
|
|
|
|
|
|
if hudControl.rightLaneDepart or hudControl.leftLaneDepart:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.ldw)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-01-18 05:57:53 +08:00
|
|
|
clear_event_types = set()
|
|
|
|
if ET.WARNING not in self.current_alert_types:
|
|
|
|
clear_event_types.add(ET.WARNING)
|
|
|
|
if self.enabled:
|
|
|
|
clear_event_types.add(ET.NO_ENTRY)
|
|
|
|
|
2022-05-17 12:19:38 +08:00
|
|
|
alerts = self.events.create_alerts(self.current_alert_types, [self.CP, CS, self.sm, self.is_metric, self.soft_disable_timer])
|
2021-12-11 11:20:07 +08:00
|
|
|
self.AM.add_many(self.sm.frame, alerts)
|
2022-01-18 05:57:53 +08:00
|
|
|
current_alert = self.AM.process_alerts(self.sm.frame, clear_event_types)
|
2022-01-07 14:15:32 +08:00
|
|
|
if current_alert:
|
|
|
|
hudControl.visualAlert = current_alert.visual_alert
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-05-01 04:55:17 +08:00
|
|
|
if not self.read_only and self.initialized:
|
2020-05-13 06:06:48 +08:00
|
|
|
# send car controls over can
|
2021-12-16 20:08:20 +08:00
|
|
|
self.last_actuators, can_sends = self.CI.apply(CC)
|
2020-05-13 06:06:48 +08:00
|
|
|
self.pm.send('sendcan', can_list_to_can_capnp(can_sends, msgtype='sendcan', valid=CS.canValid))
|
2021-12-16 20:08:20 +08:00
|
|
|
CC.actuatorsOutput = self.last_actuators
|
2022-07-25 05:56:55 +08:00
|
|
|
self.steer_limited = abs(CC.actuators.steer - CC.actuatorsOutput.steer) > 1e-2
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-02-04 11:57:30 +08:00
|
|
|
force_decel = (self.sm['driverMonitoringState'].awarenessStatus < 0.) or \
|
2020-10-23 07:28:54 +08:00
|
|
|
(self.state == State.softDisabling)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-03-12 13:08:51 +08:00
|
|
|
# Curvature & Steering angle
|
2022-10-26 06:51:39 +08:00
|
|
|
lp = self.sm['liveParameters']
|
2021-12-17 09:34:12 +08:00
|
|
|
|
2022-10-26 06:51:39 +08:00
|
|
|
steer_angle_without_offset = math.radians(CS.steeringAngleDeg - lp.angleOffsetDeg)
|
|
|
|
curvature = -self.VM.calc_curvature(steer_angle_without_offset, CS.vEgo, lp.roll)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
|
|
|
# controlsState
|
|
|
|
dat = messaging.new_message('controlsState')
|
|
|
|
dat.valid = CS.canValid
|
|
|
|
controlsState = dat.controlsState
|
2022-01-07 14:15:32 +08:00
|
|
|
if current_alert:
|
|
|
|
controlsState.alertText1 = current_alert.alert_text_1
|
|
|
|
controlsState.alertText2 = current_alert.alert_text_2
|
|
|
|
controlsState.alertSize = current_alert.alert_size
|
|
|
|
controlsState.alertStatus = current_alert.alert_status
|
|
|
|
controlsState.alertBlinkingRate = current_alert.alert_rate
|
|
|
|
controlsState.alertType = current_alert.alert_type
|
|
|
|
controlsState.alertSound = current_alert.audible_alert
|
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.canMonoTimes = list(CS.canMonoTimes)
|
2021-02-04 11:57:30 +08:00
|
|
|
controlsState.longitudinalPlanMonoTime = self.sm.logMonoTime['longitudinalPlan']
|
|
|
|
controlsState.lateralPlanMonoTime = self.sm.logMonoTime['lateralPlan']
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.enabled = self.enabled
|
|
|
|
controlsState.active = self.active
|
2021-03-12 13:08:51 +08:00
|
|
|
controlsState.curvature = curvature
|
2022-05-13 07:59:52 +08:00
|
|
|
controlsState.desiredCurvature = self.desired_curvature
|
|
|
|
controlsState.desiredCurvatureRate = self.desired_curvature_rate
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.state = self.state
|
2020-05-15 06:21:21 +08:00
|
|
|
controlsState.engageable = not self.events.any(ET.NO_ENTRY)
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.longControlState = self.LoC.long_control_state
|
|
|
|
controlsState.vPid = float(self.LoC.v_pid)
|
2022-11-12 10:53:48 +08:00
|
|
|
controlsState.vCruise = float(self.v_cruise_helper.v_cruise_kph)
|
|
|
|
controlsState.vCruiseCluster = float(self.v_cruise_helper.v_cruise_cluster_kph)
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.upAccelCmd = float(self.LoC.pid.p)
|
|
|
|
controlsState.uiAccelCmd = float(self.LoC.pid.i)
|
|
|
|
controlsState.ufAccelCmd = float(self.LoC.pid.f)
|
|
|
|
controlsState.cumLagMs = -self.rk.remaining * 1000.
|
|
|
|
controlsState.startMonoTime = int(start_time * 1e9)
|
|
|
|
controlsState.forceDecel = bool(force_decel)
|
2022-08-26 11:30:43 +08:00
|
|
|
controlsState.canErrorCounter = self.can_rcv_timeout_counter
|
2022-11-16 09:18:13 +08:00
|
|
|
controlsState.experimentalMode = self.params.get_bool("ExperimentalMode") and self.CP.openpilotLongitudinalControl
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-01-04 19:04:03 +08:00
|
|
|
lat_tuning = self.CP.lateralTuning.which()
|
2021-06-12 05:33:17 +08:00
|
|
|
if self.joystick_mode:
|
|
|
|
controlsState.lateralControlState.debugState = lac_log
|
|
|
|
elif self.CP.steerControlType == car.CarParams.SteerControlType.angle:
|
2021-03-12 13:08:51 +08:00
|
|
|
controlsState.lateralControlState.angleState = lac_log
|
2022-01-04 19:04:03 +08:00
|
|
|
elif lat_tuning == 'pid':
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.lateralControlState.pidState = lac_log
|
2022-04-20 10:34:31 +08:00
|
|
|
elif lat_tuning == 'torque':
|
|
|
|
controlsState.lateralControlState.torqueState = lac_log
|
2022-01-04 19:04:03 +08:00
|
|
|
elif lat_tuning == 'indi':
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.lateralControlState.indiState = lac_log
|
2022-01-04 19:04:03 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
self.pm.send('controlsState', dat)
|
|
|
|
|
|
|
|
# carState
|
2020-05-15 06:21:21 +08:00
|
|
|
car_events = self.events.to_msg()
|
2020-05-13 06:06:48 +08:00
|
|
|
cs_send = messaging.new_message('carState')
|
|
|
|
cs_send.valid = CS.canValid
|
|
|
|
cs_send.carState = CS
|
2020-05-15 06:21:21 +08:00
|
|
|
cs_send.carState.events = car_events
|
2020-05-13 06:06:48 +08:00
|
|
|
self.pm.send('carState', cs_send)
|
|
|
|
|
|
|
|
# carEvents - logged every second or on change
|
2020-05-15 06:21:21 +08:00
|
|
|
if (self.sm.frame % int(1. / DT_CTRL) == 0) or (self.events.names != self.events_prev):
|
|
|
|
ce_send = messaging.new_message('carEvents', len(self.events))
|
|
|
|
ce_send.carEvents = car_events
|
2020-05-13 06:06:48 +08:00
|
|
|
self.pm.send('carEvents', ce_send)
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events_prev = self.events.names.copy()
|
2020-05-13 06:06:48 +08:00
|
|
|
|
|
|
|
# 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.carParams = self.CP
|
|
|
|
self.pm.send('carParams', cp_send)
|
|
|
|
|
|
|
|
# carControl
|
|
|
|
cc_send = messaging.new_message('carControl')
|
|
|
|
cc_send.valid = CS.canValid
|
|
|
|
cc_send.carControl = CC
|
|
|
|
self.pm.send('carControl', cc_send)
|
|
|
|
|
|
|
|
# copy CarControl to pass to CarInterface on the next iteration
|
|
|
|
self.CC = CC
|
|
|
|
|
|
|
|
def step(self):
|
|
|
|
start_time = sec_since_boot()
|
|
|
|
self.prof.checkpoint("Ratekeeper", ignore=True)
|
|
|
|
|
2022-10-26 06:51:39 +08:00
|
|
|
self.is_metric = self.params.get_bool("IsMetric")
|
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Sample data from sockets and get a carState
|
|
|
|
CS = self.data_sample()
|
2022-04-06 12:05:45 +08:00
|
|
|
cloudlog.timestamp("Data sampled")
|
2020-05-13 06:06:48 +08:00
|
|
|
self.prof.checkpoint("Sample")
|
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
self.update_events(CS)
|
2022-04-06 12:05:45 +08:00
|
|
|
cloudlog.timestamp("Events updated")
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2021-05-01 04:55:17 +08:00
|
|
|
if not self.read_only and self.initialized:
|
2020-05-13 06:06:48 +08:00
|
|
|
# Update control state
|
2020-05-15 06:21:21 +08:00
|
|
|
self.state_transition(CS)
|
2020-05-13 06:06:48 +08:00
|
|
|
self.prof.checkpoint("State transition")
|
2020-01-18 04:48:30 +08:00
|
|
|
|
|
|
|
# Compute actuators (runs PID loops and lateral MPC)
|
2022-03-14 11:58:44 +08:00
|
|
|
CC, lac_log = self.state_control(CS)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
self.prof.checkpoint("State Control")
|
2020-01-18 04:48:30 +08:00
|
|
|
|
|
|
|
# Publish data
|
2022-03-14 11:58:44 +08:00
|
|
|
self.publish_logs(CS, start_time, CC, lac_log)
|
2020-05-13 06:06:48 +08:00
|
|
|
self.prof.checkpoint("Sent")
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-03-16 11:51:07 +08:00
|
|
|
self.CS_prev = CS
|
2021-10-01 17:09:56 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
def controlsd_thread(self):
|
|
|
|
while True:
|
|
|
|
self.step()
|
|
|
|
self.rk.monitor_time()
|
|
|
|
self.prof.display()
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-11-12 10:53:48 +08:00
|
|
|
|
2020-01-18 04:48:30 +08:00
|
|
|
def main(sm=None, pm=None, logcan=None):
|
2020-05-13 06:06:48 +08:00
|
|
|
controls = Controls(sm, pm, logcan)
|
|
|
|
controls.controlsd_thread()
|
2020-01-18 04:48:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|