2020-01-18 04:48:30 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
2021-03-12 13:08:51 +08:00
|
|
|
import math
|
2023-08-21 09:50:58 +08:00
|
|
|
import time
|
2023-12-14 11:00:22 +08:00
|
|
|
import threading
|
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
|
|
|
import cereal.messaging as messaging
|
2024-02-22 03:58:04 +08:00
|
|
|
|
|
|
|
from cereal import car, log
|
2024-06-07 05:31:56 +08:00
|
|
|
from msgq.visionipc import VisionIpcClient, VisionStreamType
|
2024-02-22 03:58:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
from openpilot.common.conversions import Conversions as CV
|
2024-03-20 03:39:09 +08:00
|
|
|
from openpilot.common.git import get_short_branch
|
2024-02-22 03:58:04 +08:00
|
|
|
from openpilot.common.numpy_fast import clip
|
|
|
|
from openpilot.common.params import Params
|
2024-07-16 02:14:04 +08:00
|
|
|
from openpilot.common.realtime import config_realtime_process, Priority, Ratekeeper, DT_CTRL
|
2023-12-07 09:27:51 +08:00
|
|
|
from openpilot.common.swaglog import cloudlog
|
2024-02-22 03:58:04 +08:00
|
|
|
|
2024-08-17 15:54:51 +08:00
|
|
|
from opendbc.car.car_helpers import get_car_interface
|
2024-02-22 03:58:04 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.alertmanager import AlertManager, set_offroad_alert
|
2024-08-06 08:27:28 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.drive_helpers import VCruiseHelper, clip_curvature, get_startup_event
|
2024-02-22 03:58:04 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.events import Events, ET
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.latcontrol import LatControl, MIN_LATERAL_CONTROL_SPEED
|
|
|
|
from openpilot.selfdrive.controls.lib.latcontrol_pid import LatControlPID
|
|
|
|
from openpilot.selfdrive.controls.lib.latcontrol_angle import LatControlAngle, STEER_ANGLE_SATURATION_THRESHOLD
|
|
|
|
from openpilot.selfdrive.controls.lib.latcontrol_torque import LatControlTorque
|
2024-02-22 03:58:04 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.longcontrol import LongControl
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.vehicle_model import VehicleModel
|
2024-08-14 12:11:16 +08:00
|
|
|
from openpilot.selfdrive.locationd.helpers import PoseCalibrator, Pose
|
2024-02-22 03:58:04 +08:00
|
|
|
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.system.hardware import HARDWARE
|
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
|
2024-01-22 04:09:48 +08:00
|
|
|
CAMERA_OFFSET = 0.04
|
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
|
2023-06-02 01:26:08 +08:00
|
|
|
TESTING_CLOSET = "TESTING_CLOSET" in os.environ
|
2023-05-08 06:43:45 +08:00
|
|
|
IGNORE_PROCESSES = {"loggerd", "encoderd", "statsd"}
|
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
|
2024-01-22 04:09:48 +08:00
|
|
|
Desire = log.Desire
|
|
|
|
LaneChangeState = log.LaneChangeState
|
|
|
|
LaneChangeDirection = log.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:
|
2023-10-14 14:27:04 +08:00
|
|
|
def __init__(self, CI=None):
|
2024-03-05 01:53:42 +08:00
|
|
|
self.params = Params()
|
|
|
|
|
2024-05-21 16:18:10 +08:00
|
|
|
if CI is None:
|
|
|
|
cloudlog.info("controlsd is waiting for CarParams")
|
2024-07-09 04:50:31 +08:00
|
|
|
self.CP = messaging.log_from_bytes(self.params.get("CarParams", block=True), car.CarParams)
|
2024-05-21 16:18:10 +08:00
|
|
|
cloudlog.info("controlsd got CarParams")
|
2024-02-22 05:18:43 +08:00
|
|
|
|
2024-05-21 16:18:10 +08:00
|
|
|
# Uses car interface helper functions, altering state won't be considered by card for actuation
|
|
|
|
self.CI = get_car_interface(self.CP)
|
|
|
|
else:
|
|
|
|
self.CI, self.CP = CI, CI.CP
|
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
|
2024-02-03 13:23:32 +08:00
|
|
|
self.branch = get_short_branch()
|
2022-07-04 23:13:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Setup sockets
|
2024-02-22 05:18:43 +08:00
|
|
|
self.pm = messaging.PubMaster(['controlsState', 'carControl', 'onroadEvents'])
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2024-08-14 12:11:16 +08:00
|
|
|
if self.params.get_bool("UbloxAvailable"):
|
|
|
|
self.gps_location_service = "gpsLocationExternal"
|
|
|
|
else:
|
|
|
|
self.gps_location_service = "gpsLocation"
|
|
|
|
self.gps_packets = [self.gps_location_service]
|
2023-09-27 07:47:21 +08:00
|
|
|
self.sensor_packets = ["accelerometer", "gyroscope"]
|
2022-06-02 21:20:51 +08:00
|
|
|
self.camera_packets = ["roadCameraState", "driverCameraState", "wideRoadCameraState"]
|
2021-05-20 12:28:16 +08:00
|
|
|
|
2022-06-02 21:20:51 +08:00
|
|
|
self.log_sock = messaging.sub_sock('androidLog')
|
2021-05-20 12:28:16 +08:00
|
|
|
|
2024-05-21 16:18:10 +08:00
|
|
|
# TODO: de-couple controlsd with card/conflate on carState without introducing controls mismatches
|
|
|
|
self.car_state_sock = messaging.sub_sock('carState', timeout=20)
|
|
|
|
|
2024-08-14 12:11:16 +08:00
|
|
|
ignore = self.sensor_packets + self.gps_packets + ['testJoystick']
|
2023-10-14 14:27:04 +08:00
|
|
|
if SIMULATION:
|
|
|
|
ignore += ['driverCameraState', 'managerState']
|
2024-05-23 06:18:06 +08:00
|
|
|
if REPLAY:
|
|
|
|
# no vipc in replay will make them ignored anyways
|
|
|
|
ignore += ['roadCameraState', 'wideRoadCameraState']
|
2023-10-14 14:27:04 +08:00
|
|
|
self.sm = messaging.SubMaster(['deviceState', 'pandaStates', 'peripheralState', 'modelV2', 'liveCalibration',
|
2024-08-14 12:11:16 +08:00
|
|
|
'carOutput', 'driverMonitoringState', 'longitudinalPlan', 'livePose',
|
2023-10-14 14:27:04 +08:00
|
|
|
'managerState', 'liveParameters', 'radarState', 'liveTorqueParameters',
|
2024-08-14 12:11:16 +08:00
|
|
|
'testJoystick'] + self.camera_packets + self.sensor_packets + self.gps_packets,
|
2024-02-13 02:11:37 +08:00
|
|
|
ignore_alive=ignore, ignore_avg_freq=ignore+['radarState', 'testJoystick'], ignore_valid=['testJoystick', ],
|
|
|
|
frequency=int(1/DT_CTRL))
|
2022-10-18 14:04:06 +08:00
|
|
|
|
2023-12-13 06:14:51 +08:00
|
|
|
self.joystick_mode = self.params.get_bool("JoystickDebugMode")
|
2022-04-07 02:31:31 +08:00
|
|
|
|
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")
|
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
|
|
|
|
2022-10-05 08:23:41 +08:00
|
|
|
# cleanup old params
|
2023-12-02 15:57:54 +08:00
|
|
|
if not self.CP.experimentalLongitudinalAvailable:
|
2022-10-26 06:51:39 +08:00
|
|
|
self.params.remove("ExperimentalLongitudinalEnabled")
|
2023-07-18 10:56:38 +08:00
|
|
|
if not self.CP.openpilotLongitudinalControl:
|
|
|
|
self.params.remove("ExperimentalMode")
|
2022-10-05 08:23:41 +08:00
|
|
|
|
2024-05-21 16:18:10 +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
|
|
|
|
2024-08-14 12:11:16 +08:00
|
|
|
self.pose_calibrator = PoseCalibrator()
|
|
|
|
self.calibrated_pose: Pose|None = None
|
|
|
|
|
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)
|
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
|
|
|
|
self.soft_disable_timer = 0
|
|
|
|
self.mismatch_counter = 0
|
2021-11-11 05:01:42 +08:00
|
|
|
self.cruise_mismatch_counter = 0
|
2020-05-13 06:06:48 +08:00
|
|
|
self.last_blinker_frame = 0
|
2023-02-03 09:12:13 +08:00
|
|
|
self.last_steering_pressed_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
|
2023-06-10 06:36:15 +08:00
|
|
|
self.not_running_prev = None
|
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
|
2023-02-10 11:27:51 +08:00
|
|
|
self.experimental_mode = False
|
2024-03-13 15:57:57 +08:00
|
|
|
self.personality = self.read_personality_param()
|
2024-05-16 05:32:52 +08:00
|
|
|
self.v_cruise_helper = VCruiseHelper(self.CP)
|
2023-05-27 04:07:35 +08:00
|
|
|
self.recalibrating_seen = False
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2023-02-10 07:37:39 +08:00
|
|
|
self.can_log_mono_time = 0
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2024-03-05 01:53:42 +08:00
|
|
|
self.startup_event = get_startup_event(car_recognized, not self.CP.passive, 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)
|
2023-12-04 06:09:37 +08:00
|
|
|
elif self.CP.passive:
|
2021-03-26 12:37:38 +08:00
|
|
|
self.events.add(EventName.dashcamMode, static=True)
|
2020-05-15 06:21:21 +08:00
|
|
|
|
2024-05-21 16:18:10 +08:00
|
|
|
# controlsd is driven by carState, expected at 100Hz
|
2020-05-15 06:21:21 +08:00
|
|
|
self.rk = Ratekeeper(100, print_delay_threshold=None)
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2022-08-17 13:02:03 +08:00
|
|
|
def set_initial_state(self):
|
|
|
|
if REPLAY:
|
2024-05-15 05:01:38 +08:00
|
|
|
controls_state = self.params.get("ReplayControlsState")
|
2022-08-17 13:02:03 +08:00
|
|
|
if controls_state is not None:
|
2023-07-21 12:56:57 +08:00
|
|
|
with log.ControlsState.from_bytes(controls_state) as controls_state:
|
2024-05-16 05:32:52 +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):
|
2023-12-02 14:48:29 +08:00
|
|
|
"""Compute onroadEvents from carState"""
|
2020-05-13 06:06:48 +08:00
|
|
|
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.clear()
|
|
|
|
|
2023-12-13 06:14:51 +08:00
|
|
|
# Add joystick event, static on cars, dynamic on nonCars
|
|
|
|
if self.joystick_mode:
|
|
|
|
self.events.add(EventName.joystickDebug)
|
|
|
|
self.startup_event = None
|
|
|
|
|
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
|
2023-12-04 06:09:37 +08:00
|
|
|
if self.CP.passive:
|
2022-09-23 04:43:27 +08:00
|
|
|
return
|
|
|
|
|
2024-05-16 05:32:52 +08:00
|
|
|
# Block resume if cruise never previously enabled
|
|
|
|
resume_pressed = any(be.type in (ButtonType.accelCruise, ButtonType.resumeCruise) for be in CS.buttonEvents)
|
|
|
|
if not self.CP.pcmCruise and not self.v_cruise_helper.v_cruise_initialized and resume_pressed:
|
|
|
|
self.events.add(EventName.resumeBlocked)
|
|
|
|
|
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)
|
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:
|
2023-02-23 06:11:29 +08:00
|
|
|
if self.sm['peripheralState'].fanSpeedRpm < 500 and self.sm['deviceState'].fanSpeedPercentDesired > 50:
|
|
|
|
# allow enough time for the fan controller in the panda to recover from stalls
|
|
|
|
if (self.sm.frame - self.last_functional_fan_frame) * DT_CTRL > 15.0:
|
2022-11-23 03:46:28 +08:00
|
|
|
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
|
2023-05-16 00:29:57 +08:00
|
|
|
if cal_status != log.LiveCalibrationData.Status.calibrated:
|
|
|
|
if cal_status == log.LiveCalibrationData.Status.uncalibrated:
|
2020-05-15 06:21:21 +08:00
|
|
|
self.events.add(EventName.calibrationIncomplete)
|
2023-05-16 00:29:57 +08:00
|
|
|
elif cal_status == log.LiveCalibrationData.Status.recalibrating:
|
2023-05-27 04:07:35 +08:00
|
|
|
if not self.recalibrating_seen:
|
|
|
|
set_offroad_alert("Offroad_Recalibration", True)
|
|
|
|
self.recalibrating_seen = True
|
2023-05-16 00:29:57 +08:00
|
|
|
self.events.add(EventName.calibrationRecalibrating)
|
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
|
2024-01-22 04:09:48 +08:00
|
|
|
if self.sm['modelV2'].meta.laneChangeState == LaneChangeState.preLaneChange:
|
|
|
|
direction = self.sm['modelV2'].meta.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)
|
2024-01-22 04:09:48 +08:00
|
|
|
elif self.sm['modelV2'].meta.laneChangeState in (LaneChangeState.laneChangeStarting,
|
2022-01-10 18:26:58 +08:00
|
|
|
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
|
|
|
|
2024-06-05 10:16:55 +08:00
|
|
|
# safety mismatch allows some time for pandad to set the safety mode and publish it back from panda
|
2024-02-19 06:43:42 +08:00
|
|
|
if (safety_mismatch and self.sm.frame*DT_CTRL > 10.) 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}
|
2024-02-10 13:44:23 +08:00
|
|
|
if self.sm.recv_frame['managerState'] and (not_running - IGNORE_PROCESSES):
|
2022-04-30 04:36:25 +08:00
|
|
|
self.events.add(EventName.processNotRunning)
|
2023-06-10 06:36:15 +08:00
|
|
|
if not_running != self.not_running_prev:
|
|
|
|
cloudlog.event("process_not_running", not_running=not_running, error=True)
|
|
|
|
self.not_running_prev = not_running
|
2022-04-30 04:36:25 +08:00
|
|
|
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)
|
2023-05-13 02:33:48 +08:00
|
|
|
if not REPLAY and self.rk.lagging:
|
2022-04-29 10:51:42 +08:00
|
|
|
self.events.add(EventName.controlsdLagging)
|
2024-05-19 16:07:12 +08:00
|
|
|
if len(self.sm['radarState'].radarErrors) or ((not self.rk.lagging or REPLAY) and 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
|
2023-08-11 09:58:22 +08:00
|
|
|
has_disable_events = self.events.contains(ET.NO_ENTRY) and (self.events.contains(ET.SOFT_DISABLE) or self.events.contains(ET.IMMEDIATE_DISABLE))
|
2022-07-15 10:55:18 +08:00
|
|
|
no_system_errors = (not has_disable_events) or (len(self.events) == num_events)
|
2024-06-07 07:46:27 +08:00
|
|
|
if not self.sm.all_checks() 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)
|
2024-06-07 07:46:27 +08:00
|
|
|
else:
|
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],
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2023-12-13 06:14:51 +08:00
|
|
|
if not (self.CP.notCar and self.joystick_mode):
|
2024-08-14 12:11:16 +08:00
|
|
|
if not self.sm['livePose'].posenetOK:
|
2023-12-08 02:53:33 +08:00
|
|
|
self.events.add(EventName.posenetInvalid)
|
2024-08-14 12:11:16 +08:00
|
|
|
if not self.sm['livePose'].inputsOK:
|
2023-12-08 02:53:33 +08:00
|
|
|
self.events.add(EventName.locationdTemporaryError)
|
|
|
|
if not self.sm['liveParameters'].valid and not TESTING_CLOSET and (not SIMULATION or REPLAY):
|
|
|
|
self.events.add(EventName.paramsdTemporaryError)
|
2021-10-20 22:16:40 +08:00
|
|
|
|
2023-09-27 07:47:21 +08:00
|
|
|
# conservative HW alert. if the data or frequency are off, locationd will throw an error
|
2024-02-10 13:44:23 +08:00
|
|
|
if any((self.sm.frame - self.sm.recv_frame[s])*DT_CTRL > 10. for s in self.sensor_packets):
|
2023-09-27 07:47:21 +08:00
|
|
|
self.events.add(EventName.sensorDataInvalid)
|
|
|
|
|
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
|
2024-04-27 11:04:10 +08:00
|
|
|
if (planner_fcw or model_fcw) and not (self.CP.notCar and self.joystick_mode):
|
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
|
2023-07-13 01:32:02 +08:00
|
|
|
if not SIMULATION or REPLAY:
|
2024-08-15 06:52:47 +08:00
|
|
|
# Not show in first 1.5 km to allow for driving out of garage. This event shows after 5 minutes
|
2024-08-14 12:11:16 +08:00
|
|
|
gps_ok = self.sm.recv_frame[self.gps_location_service] > 0 and (self.sm.frame - self.sm.recv_frame[self.gps_location_service]) * DT_CTRL < 2.0
|
|
|
|
if not gps_ok and self.sm['livePose'].inputsOK and (self.distance_traveled > 1500):
|
2023-09-27 07:47:21 +08:00
|
|
|
self.events.add(EventName.noGps)
|
2024-08-14 12:11:16 +08:00
|
|
|
if gps_ok:
|
2024-02-16 03:52:57 +08:00
|
|
|
self.distance_traveled = 0
|
|
|
|
self.distance_traveled += CS.vEgo * DT_CTRL
|
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)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
def data_sample(self):
|
2024-05-21 16:18:10 +08:00
|
|
|
"""Receive data from sockets"""
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2024-05-21 16:18:10 +08:00
|
|
|
car_state = messaging.recv_one(self.car_state_sock)
|
|
|
|
CS = car_state.carState if car_state else self.CS_prev
|
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()
|
2024-02-22 08:23:43 +08:00
|
|
|
timed_out = self.sm.frame * DT_CTRL > 6.
|
2023-07-13 01:32:02 +08:00
|
|
|
if all_valid or timed_out or (SIMULATION and not REPLAY):
|
2023-06-13 04:40:43 +08:00
|
|
|
available_streams = VisionIpcClient.available_streams("camerad", block=False)
|
|
|
|
if VisionStreamType.VISION_STREAM_ROAD not in available_streams:
|
|
|
|
self.sm.ignore_alive.append('roadCameraState')
|
2023-06-13 11:13:56 +08:00
|
|
|
if VisionStreamType.VISION_STREAM_WIDE_ROAD not in available_streams:
|
|
|
|
self.sm.ignore_alive.append('wideRoadCameraState')
|
2023-06-13 04:40:43 +08:00
|
|
|
|
2022-08-17 13:02:03 +08:00
|
|
|
self.initialized = True
|
|
|
|
self.set_initial_state()
|
2021-05-01 04:55:17 +08:00
|
|
|
|
2024-02-15 03:15:33 +08:00
|
|
|
cloudlog.event(
|
|
|
|
"controlsd.initialized",
|
|
|
|
dt=self.sm.frame*DT_CTRL,
|
|
|
|
timeout=timed_out,
|
|
|
|
canValid=CS.canValid,
|
|
|
|
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],
|
|
|
|
error=True,
|
|
|
|
)
|
2024-02-09 13:26:07 +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
|
|
|
|
2024-08-14 12:11:16 +08:00
|
|
|
# calibrate the live pose and save it for later use
|
|
|
|
if self.sm.updated["liveCalibration"]:
|
|
|
|
self.pose_calibrator.feed_live_calib(self.sm['liveCalibration'])
|
|
|
|
if self.sm.updated["livePose"]:
|
|
|
|
device_pose = Pose.from_live_pose(self.sm['livePose'])
|
|
|
|
self.calibrated_pose = self.pose_calibrator.build_calibrated_pose(device_pose)
|
|
|
|
|
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
|
|
|
|
2024-05-16 05:32:52 +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
|
2023-08-11 09:58:22 +08:00
|
|
|
if self.events.contains(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
|
|
|
|
2023-08-11 09:58:22 +08:00
|
|
|
elif self.events.contains(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:
|
2023-08-11 09:58:22 +08:00
|
|
|
if self.events.contains(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
|
|
|
|
2023-08-11 09:58:22 +08:00
|
|
|
elif self.events.contains(ET.OVERRIDE_LATERAL) or self.events.contains(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:
|
2023-08-11 09:58:22 +08:00
|
|
|
if not self.events.contains(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:
|
2023-08-11 09:58:22 +08:00
|
|
|
if not self.events.contains(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:
|
2023-08-11 09:58:22 +08:00
|
|
|
if self.events.contains(ET.SOFT_DISABLE):
|
2022-04-06 02:36:18 +08:00
|
|
|
self.state = State.softDisabling
|
|
|
|
self.soft_disable_timer = int(SOFT_DISABLE_TIME / DT_CTRL)
|
|
|
|
self.current_alert_types.append(ET.SOFT_DISABLE)
|
2023-08-11 09:58:22 +08:00
|
|
|
elif not (self.events.contains(ET.OVERRIDE_LATERAL) or self.events.contains(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:
|
2023-08-11 09:58:22 +08:00
|
|
|
if self.events.contains(ET.ENABLE):
|
|
|
|
if self.events.contains(ET.NO_ENTRY):
|
2020-05-15 06:21:21 +08:00
|
|
|
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:
|
2023-08-11 09:58:22 +08:00
|
|
|
if self.events.contains(ET.PRE_ENABLE):
|
2020-05-13 06:06:48 +08:00
|
|
|
self.state = State.preEnabled
|
2023-08-11 09:58:22 +08:00
|
|
|
elif self.events.contains(ET.OVERRIDE_LATERAL) or self.events.contains(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)
|
2024-05-16 05:32:52 +08:00
|
|
|
self.v_cruise_helper.initialize_v_cruise(CS, self.experimental_mode)
|
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:
|
2023-08-09 08:13:35 +08:00
|
|
|
self.LaC.update_live_torque_params(torque_params.latAccelFactorFiltered, torque_params.latAccelOffsetFiltered,
|
|
|
|
torque_params.frictionCoefficientFiltered)
|
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
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
long_plan = self.sm['longitudinalPlan']
|
2024-01-22 04:09:48 +08:00
|
|
|
model_v2 = self.sm['modelV2']
|
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
|
2023-01-11 12:51:10 +08:00
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
# Check which actuators can be enabled
|
2023-01-11 12:51:10 +08:00
|
|
|
standstill = CS.vEgo <= max(self.CP.minSteerSpeed, MIN_LATERAL_CONTROL_SPEED) or CS.standstill
|
2022-03-14 11:58:44 +08:00
|
|
|
CC.latActive = self.active and not CS.steerFaultTemporary and not CS.steerFaultPermanent and \
|
2023-01-11 12:51:10 +08:00
|
|
|
(not standstill or self.joystick_mode)
|
2023-08-11 09:58:22 +08:00
|
|
|
CC.longActive = self.enabled and not self.events.contains(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
|
|
|
|
2022-12-13 06:03:09 +08:00
|
|
|
# Enable blinkers while lane changing
|
2024-01-22 04:09:48 +08:00
|
|
|
if model_v2.meta.laneChangeState != LaneChangeState.off:
|
|
|
|
CC.leftBlinker = model_v2.meta.laneChangeDirection == LaneChangeDirection.left
|
|
|
|
CC.rightBlinker = model_v2.meta.laneChangeDirection == LaneChangeDirection.right
|
2022-12-13 06:03:09 +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:
|
2024-06-14 15:08:58 +08:00
|
|
|
self.LoC.reset()
|
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
|
2024-05-16 05:32:52 +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)
|
2024-06-14 15:08:58 +08:00
|
|
|
actuators.accel = self.LoC.update(CC.longActive, CS, long_plan.aTarget, long_plan.shouldStop, pid_accel_limits)
|
2021-03-12 13:08:51 +08:00
|
|
|
|
2021-06-12 05:33:17 +08:00
|
|
|
# Steering PID loop and lateral MPC
|
2024-01-22 04:09:48 +08:00
|
|
|
self.desired_curvature = clip_curvature(CS.vEgo, self.desired_curvature, model_v2.action.desiredCurvature)
|
|
|
|
actuators.curvature = self.desired_curvature
|
2022-10-26 06:51:39 +08:00
|
|
|
actuators.steer, actuators.steeringAngleDeg, lac_log = self.LaC.update(CC.latActive, CS, self.VM, lp,
|
2023-12-04 07:54:18 +08:00
|
|
|
self.steer_limited, self.desired_curvature,
|
2024-08-14 12:11:16 +08:00
|
|
|
self.calibrated_pose) # TODO what if not available
|
2021-06-12 05:33:17 +08:00
|
|
|
else:
|
|
|
|
lac_log = log.ControlsState.LateralDebugState.new_message()
|
2024-02-10 13:44:23 +08:00
|
|
|
if self.sm.recv_frame['testJoystick'] > 0:
|
2023-11-08 07:48:13 +08:00
|
|
|
# reset joystick if it hasn't been received in a while
|
2024-02-10 13:44:23 +08:00
|
|
|
should_reset_joystick = (self.sm.frame - self.sm.recv_frame['testJoystick'])*DT_CTRL > 0.2
|
2023-11-08 07:48:13 +08:00
|
|
|
if not should_reset_joystick:
|
|
|
|
joystick_axes = self.sm['testJoystick'].axes
|
|
|
|
else:
|
|
|
|
joystick_axes = [0.0, 0.0]
|
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
if CC.longActive:
|
2023-11-08 07:48:13 +08:00
|
|
|
actuators.accel = 4.0*clip(joystick_axes[0], -1, 1)
|
2021-06-12 05:33:17 +08:00
|
|
|
|
2022-03-14 11:58:44 +08:00
|
|
|
if CC.latActive:
|
2023-11-08 07:48:13 +08:00
|
|
|
steer = clip(joystick_axes[1], -1, 1)
|
2023-09-23 12:53:31 +08:00
|
|
|
# max angle is 45 for angle-based cars, max curvature is 0.02
|
2023-12-05 13:16:33 +08:00
|
|
|
actuators.steer, actuators.steeringAngleDeg, actuators.curvature = steer, steer * 90., steer * -0.02
|
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
|
|
|
|
2023-02-03 09:12:13 +08:00
|
|
|
if CS.steeringPressed:
|
|
|
|
self.last_steering_pressed_frame = self.sm.frame
|
|
|
|
recent_steer_pressed = (self.sm.frame - self.last_steering_pressed_frame)*DT_CTRL < 2.0
|
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
# Send a "steering required alert" if saturation count has reached the limit
|
2023-07-25 10:49:51 +08:00
|
|
|
if lac_log.active and not recent_steer_pressed and not self.CP.notCar:
|
2023-02-03 09:12:13 +08:00
|
|
|
if self.CP.lateralTuning.which() == 'torque' and not self.joystick_mode:
|
|
|
|
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
|
2024-05-11 12:00:01 +08:00
|
|
|
max_torque = abs(self.sm['carOutput'].actuatorsOutput.steer) > 0.99
|
2023-02-03 09:12:13 +08:00
|
|
|
if undershooting and turning and good_speed and max_torque:
|
|
|
|
lac_log.active and self.events.add(EventName.steerSaturated)
|
|
|
|
elif lac_log.saturated:
|
2024-01-22 04:09:48 +08:00
|
|
|
# TODO probably should not use dpath_points but curvature
|
|
|
|
dpath_points = model_v2.position.y
|
2023-02-03 09:12:13 +08:00
|
|
|
if len(dpath_points):
|
|
|
|
# Check if we deviated from the path
|
|
|
|
# TODO use desired vs actual curvature
|
|
|
|
if self.CP.steerControlType == car.CarParams.SteerControlType.angle:
|
|
|
|
steering_value = actuators.steeringAngleDeg
|
|
|
|
else:
|
|
|
|
steering_value = actuators.steer
|
2022-08-12 17:30:19 +08:00
|
|
|
|
2023-02-03 09:12:13 +08:00
|
|
|
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
|
|
|
|
2023-02-03 09:12:13 +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)
|
|
|
|
|
2024-03-13 16:05:02 +08:00
|
|
|
# decrement personality on distance button press
|
|
|
|
if self.CP.openpilotLongitudinalControl:
|
|
|
|
if any(not be.pressed and be.type == ButtonType.gapAdjustCruise for be in CS.buttonEvents):
|
|
|
|
self.personality = (self.personality - 1) % 3
|
|
|
|
self.params.put_nonblocking('LongitudinalPersonality', str(self.personality))
|
|
|
|
|
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
|
2024-08-14 12:11:16 +08:00
|
|
|
if self.calibrated_pose is not None:
|
|
|
|
CC.orientationNED = self.calibrated_pose.orientation.xyz.tolist()
|
|
|
|
CC.angularVelocity = self.calibrated_pose.angular_velocity.xyz.tolist()
|
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)
|
2024-02-10 13:44:23 +08:00
|
|
|
if self.joystick_mode and self.sm.recv_frame['testJoystick'] > 0 and self.sm['testJoystick'].buttons[0]:
|
2021-06-12 05:33:17 +08:00
|
|
|
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
|
2024-05-16 05:32:52 +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
|
2024-03-13 15:57:57 +08:00
|
|
|
hudControl.leadDistanceBars = self.personality + 1
|
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 \
|
2023-05-16 00:29:57 +08:00
|
|
|
and not CC.latActive and self.sm['liveCalibration'].calStatus == log.LiveCalibrationData.Status.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
|
2023-05-06 18:03:28 +08:00
|
|
|
l_lane_change_prob = desire_prediction[Desire.laneChangeLeft]
|
|
|
|
r_lane_change_prob = desire_prediction[Desire.laneChangeRight]
|
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
|
|
|
|
2023-12-04 06:09:37 +08:00
|
|
|
if not self.CP.passive and self.initialized:
|
2024-05-11 12:00:01 +08:00
|
|
|
CO = self.sm['carOutput']
|
2022-12-10 13:43:27 +08:00
|
|
|
if self.CP.steerControlType == car.CarParams.SteerControlType.angle:
|
2024-03-05 01:53:42 +08:00
|
|
|
self.steer_limited = abs(CC.actuators.steeringAngleDeg - CO.actuatorsOutput.steeringAngleDeg) > \
|
2022-12-10 13:43:27 +08:00
|
|
|
STEER_ANGLE_SATURATION_THRESHOLD
|
|
|
|
else:
|
2024-03-05 01:53:42 +08:00
|
|
|
self.steer_limited = abs(CC.actuators.steer - CO.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
|
|
|
|
|
2021-02-04 11:57:30 +08:00
|
|
|
controlsState.longitudinalPlanMonoTime = self.sm.logMonoTime['longitudinalPlan']
|
2024-01-22 04:09:48 +08:00
|
|
|
controlsState.lateralPlanMonoTime = self.sm.logMonoTime['modelV2']
|
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
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.state = self.state
|
2023-08-11 09:58:22 +08:00
|
|
|
controlsState.engageable = not self.events.contains(ET.NO_ENTRY)
|
2020-05-13 06:06:48 +08:00
|
|
|
controlsState.longControlState = self.LoC.long_control_state
|
2024-05-16 05:32:52 +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)
|
2023-02-10 11:27:51 +08:00
|
|
|
controlsState.experimentalMode = self.experimental_mode
|
2024-03-13 15:57:57 +08:00
|
|
|
controlsState.personality = self.personality
|
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
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
self.pm.send('controlsState', dat)
|
|
|
|
|
2023-12-02 14:48:29 +08:00
|
|
|
# onroadEvents - 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):
|
2023-12-02 14:48:29 +08:00
|
|
|
ce_send = messaging.new_message('onroadEvents', len(self.events))
|
2023-12-04 02:50:17 +08:00
|
|
|
ce_send.valid = True
|
2024-03-05 01:53:42 +08:00
|
|
|
ce_send.onroadEvents = self.events.to_msg()
|
2023-12-02 14:48:29 +08:00
|
|
|
self.pm.send('onroadEvents', 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
|
|
|
|
|
|
|
# carControl
|
|
|
|
cc_send = messaging.new_message('carControl')
|
|
|
|
cc_send.valid = CS.canValid
|
|
|
|
cc_send.carControl = CC
|
|
|
|
self.pm.send('carControl', cc_send)
|
|
|
|
|
|
|
|
def step(self):
|
2023-08-21 09:50:58 +08:00
|
|
|
start_time = time.monotonic()
|
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
|
|
|
|
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
|
|
|
|
2023-12-04 06:09:37 +08:00
|
|
|
if not self.CP.passive 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-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
|
|
|
|
|
|
|
# Publish data
|
2022-03-14 11:58:44 +08:00
|
|
|
self.publish_logs(CS, start_time, CC, lac_log)
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2024-05-21 16:18:10 +08:00
|
|
|
self.CS_prev = CS
|
|
|
|
|
2024-03-13 15:57:57 +08:00
|
|
|
def read_personality_param(self):
|
|
|
|
try:
|
|
|
|
return int(self.params.get('LongitudinalPersonality'))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return log.LongitudinalPersonality.standard
|
|
|
|
|
2023-12-14 11:00:22 +08:00
|
|
|
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
|
2024-03-13 15:57:57 +08:00
|
|
|
self.personality = self.read_personality_param()
|
2023-12-14 11:00:22 +08:00
|
|
|
if self.CP.notCar:
|
|
|
|
self.joystick_mode = self.params.get_bool("JoystickDebugMode")
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2020-05-13 06:06:48 +08:00
|
|
|
def controlsd_thread(self):
|
2023-12-14 11:00:22 +08:00
|
|
|
e = threading.Event()
|
|
|
|
t = threading.Thread(target=self.params_thread, args=(e, ))
|
|
|
|
try:
|
|
|
|
t.start()
|
|
|
|
while True:
|
|
|
|
self.step()
|
|
|
|
self.rk.monitor_time()
|
2024-06-13 08:16:30 +08:00
|
|
|
finally:
|
2023-12-14 11:00:22 +08:00
|
|
|
e.set()
|
|
|
|
t.join()
|
2020-01-18 04:48:30 +08:00
|
|
|
|
2022-11-12 10:53:48 +08:00
|
|
|
|
2023-10-14 14:27:04 +08:00
|
|
|
def main():
|
2024-03-05 01:53:42 +08:00
|
|
|
config_realtime_process(4, Priority.CTRL_HIGH)
|
2023-10-14 14:27:04 +08:00
|
|
|
controls = Controls()
|
2020-05-13 06:06:48 +08:00
|
|
|
controls.controlsd_thread()
|
2020-01-18 04:48:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|