2020-05-08 21:08:11 -04:00
|
|
|
#!/usr/bin/env python3
|
2024-12-16 17:10:47 -08:00
|
|
|
import fcntl
|
2020-08-24 14:17:41 +02:00
|
|
|
import os
|
2023-06-15 15:04:22 -07:00
|
|
|
import json
|
2022-01-17 12:06:37 +01:00
|
|
|
import queue
|
2024-12-13 16:50:34 -08:00
|
|
|
import struct
|
2022-01-17 12:06:37 +01:00
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
from collections import OrderedDict, namedtuple
|
2021-03-28 21:22:31 -07:00
|
|
|
from pathlib import Path
|
2020-08-24 14:17:41 +02:00
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
|
|
import cereal.messaging as messaging
|
2020-01-17 11:03:22 -08:00
|
|
|
from cereal import log
|
2023-09-21 18:35:14 -07:00
|
|
|
from cereal.services import SERVICE_LIST
|
2023-08-20 20:49:55 -07:00
|
|
|
from openpilot.common.dict_helpers import strip_deprecated_keys
|
|
|
|
|
from openpilot.common.filter_simple import FirstOrderFilter
|
|
|
|
|
from openpilot.common.params import Params
|
2024-06-05 15:58:00 -07:00
|
|
|
from openpilot.common.realtime import DT_HW
|
2024-09-06 17:16:32 -07:00
|
|
|
from openpilot.selfdrive.selfdrived.alertmanager import set_offroad_alert
|
2023-08-20 20:49:55 -07:00
|
|
|
from openpilot.system.hardware import HARDWARE, TICI, AGNOS
|
|
|
|
|
from openpilot.system.loggerd.config import get_available_percent
|
2024-05-20 22:39:25 -07:00
|
|
|
from openpilot.system.statsd import statlog
|
2023-12-06 17:27:51 -08:00
|
|
|
from openpilot.common.swaglog import cloudlog
|
2024-06-05 15:58:00 -07:00
|
|
|
from openpilot.system.hardware.power_monitoring import PowerMonitoring
|
|
|
|
|
from openpilot.system.hardware.fan_controller import TiciFanController
|
2023-08-20 20:49:55 -07:00
|
|
|
from openpilot.system.version import terms_version, training_version
|
2020-08-24 14:17:41 +02:00
|
|
|
|
2021-02-16 21:39:32 -08:00
|
|
|
ThermalStatus = log.DeviceState.ThermalStatus
|
|
|
|
|
NetworkType = log.DeviceState.NetworkType
|
|
|
|
|
NetworkStrength = log.DeviceState.NetworkStrength
|
2020-01-17 11:03:22 -08:00
|
|
|
CURRENT_TAU = 15. # 15s time constant
|
2021-09-04 02:19:11 +02:00
|
|
|
TEMP_TAU = 5. # 5s time constant
|
2020-04-20 14:15:01 -07:00
|
|
|
DISCONNECT_TIMEOUT = 5. # wait 5 seconds before going offroad after disconnect so you get an alert
|
2023-09-21 18:35:14 -07:00
|
|
|
PANDA_STATES_TIMEOUT = round(1000 / SERVICE_LIST['pandaStates'].frequency * 1.5) # 1.5x the expected pandaState frequency
|
2025-05-27 20:20:38 -07:00
|
|
|
ONROAD_CYCLE_TIME = 1 # seconds to wait offroad after requesting an onroad cycle
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2021-09-04 02:19:11 +02:00
|
|
|
ThermalBand = namedtuple("ThermalBand", ['min_temp', 'max_temp'])
|
2023-08-08 17:13:35 -07:00
|
|
|
HardwareState = namedtuple("HardwareState", ['network_type', 'network_info', 'network_strength', 'network_stats',
|
|
|
|
|
'network_metered', 'nvme_temps', 'modem_temps'])
|
2020-10-15 15:47:38 +02:00
|
|
|
|
2021-09-04 02:19:11 +02:00
|
|
|
# List of thermal bands. We will stay within this region as long as we are within the bounds.
|
|
|
|
|
# When exiting the bounds, we'll jump to the lower or higher band. Bands are ordered in the dict.
|
|
|
|
|
THERMAL_BANDS = OrderedDict({
|
|
|
|
|
ThermalStatus.green: ThermalBand(None, 80.0),
|
|
|
|
|
ThermalStatus.yellow: ThermalBand(75.0, 96.0),
|
2023-06-09 20:15:13 -07:00
|
|
|
ThermalStatus.red: ThermalBand(88.0, 107.),
|
2021-09-04 02:19:11 +02:00
|
|
|
ThermalStatus.danger: ThermalBand(94.0, None),
|
|
|
|
|
})
|
2020-03-12 11:55:33 -07:00
|
|
|
|
2021-09-04 02:19:11 +02:00
|
|
|
# Override to highest thermal band when offroad and above this temp
|
2023-06-23 17:23:48 -07:00
|
|
|
OFFROAD_DANGER_TEMP = 75
|
2021-09-04 02:19:11 +02:00
|
|
|
|
2024-02-25 00:41:23 +00:00
|
|
|
prev_offroad_states: dict[str, tuple[bool, str | None]] = {}
|
2021-03-29 23:37:52 +02:00
|
|
|
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2020-03-12 11:55:33 -07:00
|
|
|
|
2024-02-25 00:41:23 +00:00
|
|
|
def set_offroad_alert_if_changed(offroad_alert: str, show_alert: bool, extra_text: str | None=None):
|
2020-10-15 15:47:38 +02:00
|
|
|
if prev_offroad_states.get(offroad_alert, None) == (show_alert, extra_text):
|
|
|
|
|
return
|
|
|
|
|
prev_offroad_states[offroad_alert] = (show_alert, extra_text)
|
|
|
|
|
set_offroad_alert(offroad_alert, show_alert, extra_text)
|
|
|
|
|
|
2024-12-13 16:50:34 -08:00
|
|
|
def touch_thread(end_event):
|
2024-12-16 17:10:47 -08:00
|
|
|
count = 0
|
|
|
|
|
|
2024-12-13 16:50:34 -08:00
|
|
|
pm = messaging.PubMaster(["touch"])
|
|
|
|
|
|
|
|
|
|
event_format = "llHHi"
|
|
|
|
|
event_size = struct.calcsize(event_format)
|
|
|
|
|
event_frame = []
|
|
|
|
|
|
|
|
|
|
with open("/dev/input/by-path/platform-894000.i2c-event", "rb") as event_file:
|
2024-12-16 17:10:47 -08:00
|
|
|
fcntl.fcntl(event_file, fcntl.F_SETFL, os.O_NONBLOCK)
|
2024-12-13 16:50:34 -08:00
|
|
|
while not end_event.is_set():
|
2024-12-16 17:10:47 -08:00
|
|
|
if (count % int(1. / DT_HW)) == 0:
|
|
|
|
|
event = event_file.read(event_size)
|
|
|
|
|
if event:
|
|
|
|
|
(sec, usec, etype, code, value) = struct.unpack(event_format, event)
|
|
|
|
|
if etype != 0 or code != 0 or value != 0:
|
|
|
|
|
touch = log.Touch.new_message()
|
|
|
|
|
touch.sec = sec
|
|
|
|
|
touch.usec = usec
|
|
|
|
|
touch.type = etype
|
|
|
|
|
touch.code = code
|
|
|
|
|
touch.value = value
|
|
|
|
|
event_frame.append(touch)
|
|
|
|
|
else: # end of frame, push new log
|
|
|
|
|
msg = messaging.new_message('touch', len(event_frame), valid=True)
|
|
|
|
|
msg.touch = event_frame
|
|
|
|
|
pm.send('touch', msg)
|
|
|
|
|
event_frame = []
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
time.sleep(DT_HW)
|
2024-12-13 16:50:34 -08:00
|
|
|
|
2020-10-15 15:47:38 +02:00
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
def hw_state_thread(end_event, hw_queue):
|
|
|
|
|
"""Handles non critical hardware state, and sends over queue"""
|
|
|
|
|
count = 0
|
2022-03-02 11:01:06 -08:00
|
|
|
prev_hw_state = None
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2022-04-05 14:29:07 -07:00
|
|
|
modem_version = None
|
2022-04-11 15:33:58 -07:00
|
|
|
modem_configured = False
|
2023-07-23 20:36:13 -07:00
|
|
|
modem_restarted = False
|
|
|
|
|
modem_missing_count = 0
|
2022-04-05 14:29:07 -07:00
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
while not end_event.is_set():
|
|
|
|
|
# these are expensive calls. update every 10s
|
2024-06-05 15:58:00 -07:00
|
|
|
if (count % int(10. / DT_HW)) == 0:
|
2022-01-17 12:06:37 +01:00
|
|
|
try:
|
|
|
|
|
network_type = HARDWARE.get_network_type()
|
2022-03-02 11:01:06 -08:00
|
|
|
modem_temps = HARDWARE.get_modem_temperatures()
|
|
|
|
|
if len(modem_temps) == 0 and prev_hw_state is not None:
|
|
|
|
|
modem_temps = prev_hw_state.modem_temps
|
2022-01-17 12:06:37 +01:00
|
|
|
|
2022-04-05 14:29:07 -07:00
|
|
|
# Log modem version once
|
2024-11-20 16:01:48 -08:00
|
|
|
if AGNOS and (modem_version is None):
|
2023-08-24 16:30:54 -07:00
|
|
|
modem_version = HARDWARE.get_modem_version()
|
2022-04-05 14:29:07 -07:00
|
|
|
|
2024-11-20 16:01:48 -08:00
|
|
|
if modem_version is not None:
|
|
|
|
|
cloudlog.event("modem version", version=modem_version)
|
2023-07-23 20:36:13 -07:00
|
|
|
else:
|
|
|
|
|
if not modem_restarted:
|
|
|
|
|
# TODO: we may be able to remove this with a MM update
|
|
|
|
|
# ModemManager's probing on startup can fail
|
|
|
|
|
# rarely, restart the service to probe again.
|
|
|
|
|
modem_missing_count += 1
|
|
|
|
|
if modem_missing_count > 3:
|
|
|
|
|
modem_restarted = True
|
|
|
|
|
cloudlog.event("restarting ModemManager")
|
|
|
|
|
os.system("sudo systemctl restart --no-block ModemManager")
|
2022-04-05 14:29:07 -07:00
|
|
|
|
2022-07-28 14:58:23 +02:00
|
|
|
tx, rx = HARDWARE.get_modem_data_usage()
|
|
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
hw_state = HardwareState(
|
|
|
|
|
network_type=network_type,
|
|
|
|
|
network_info=HARDWARE.get_network_info(),
|
2022-07-28 14:58:23 +02:00
|
|
|
network_strength=HARDWARE.get_network_strength(network_type),
|
|
|
|
|
network_stats={'wwanTx': tx, 'wwanRx': rx},
|
|
|
|
|
network_metered=HARDWARE.get_network_metered(network_type),
|
2022-01-17 12:06:37 +01:00
|
|
|
nvme_temps=HARDWARE.get_nvme_temperatures(),
|
2022-03-02 11:01:06 -08:00
|
|
|
modem_temps=modem_temps,
|
2022-01-17 12:06:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
hw_queue.put_nowait(hw_state)
|
|
|
|
|
except queue.Full:
|
|
|
|
|
pass
|
|
|
|
|
|
2024-11-05 21:20:17 -08:00
|
|
|
if not modem_configured and HARDWARE.get_modem_version() is not None:
|
2022-04-11 15:33:58 -07:00
|
|
|
cloudlog.warning("configuring modem")
|
|
|
|
|
HARDWARE.configure_modem()
|
|
|
|
|
modem_configured = True
|
|
|
|
|
|
2022-03-02 11:01:06 -08:00
|
|
|
prev_hw_state = hw_state
|
2022-01-17 12:06:37 +01:00
|
|
|
except Exception:
|
2022-03-02 11:01:06 -08:00
|
|
|
cloudlog.exception("Error getting hardware state")
|
2022-01-17 12:06:37 +01:00
|
|
|
|
|
|
|
|
count += 1
|
2024-06-05 15:58:00 -07:00
|
|
|
time.sleep(DT_HW)
|
2020-12-29 22:32:03 -08:00
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
|
2024-06-05 15:58:00 -07:00
|
|
|
def hardware_thread(end_event, hw_queue) -> None:
|
2022-01-17 12:06:37 +01:00
|
|
|
pm = messaging.PubMaster(['deviceState'])
|
2024-09-03 14:40:23 -07:00
|
|
|
sm = messaging.SubMaster(["peripheralState", "gpsLocationExternal", "selfdriveState", "pandaStates"], poll="pandaStates")
|
2020-01-17 11:03:22 -08:00
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
2024-02-25 00:41:23 +00:00
|
|
|
onroad_conditions: dict[str, bool] = {
|
2020-10-15 15:47:38 +02:00
|
|
|
"ignition": False,
|
2025-05-27 20:20:38 -07:00
|
|
|
"not_onroad_cycle": True,
|
2020-10-15 15:47:38 +02:00
|
|
|
}
|
2024-02-25 00:41:23 +00:00
|
|
|
startup_conditions: dict[str, bool] = {}
|
|
|
|
|
startup_conditions_prev: dict[str, bool] = {}
|
2020-10-15 15:47:38 +02:00
|
|
|
|
2024-02-25 00:41:23 +00:00
|
|
|
off_ts: float | None = None
|
|
|
|
|
started_ts: float | None = None
|
2020-01-17 11:03:22 -08:00
|
|
|
started_seen = False
|
2024-02-25 00:41:23 +00:00
|
|
|
startup_blocked_ts: float | None = None
|
2023-06-15 17:34:01 -07:00
|
|
|
thermal_status = ThermalStatus.yellow
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
last_hw_state = HardwareState(
|
|
|
|
|
network_type=NetworkType.none,
|
2022-07-28 14:58:23 +02:00
|
|
|
network_info=None,
|
2022-03-09 11:36:52 +01:00
|
|
|
network_metered=False,
|
2022-01-17 12:06:37 +01:00
|
|
|
network_strength=NetworkStrength.unknown,
|
2022-07-28 14:58:23 +02:00
|
|
|
network_stats={'wwanTx': -1, 'wwanRx': -1},
|
2022-01-17 12:06:37 +01:00
|
|
|
nvme_temps=[],
|
|
|
|
|
modem_temps=[],
|
|
|
|
|
)
|
2020-02-03 11:28:38 -08:00
|
|
|
|
2024-06-05 15:58:00 -07:00
|
|
|
all_temp_filter = FirstOrderFilter(0., TEMP_TAU, DT_HW, initialized=False)
|
|
|
|
|
offroad_temp_filter = FirstOrderFilter(0., TEMP_TAU, DT_HW, initialized=False)
|
2020-01-17 11:03:22 -08:00
|
|
|
should_start_prev = False
|
2021-10-04 11:30:11 +02:00
|
|
|
in_car = False
|
2022-01-10 19:54:07 +01:00
|
|
|
engaged_prev = False
|
2025-05-27 20:20:38 -07:00
|
|
|
offroad_cycle_count = 0
|
2020-01-17 11:03:22 -08:00
|
|
|
|
|
|
|
|
params = Params()
|
2020-12-29 22:32:03 -08:00
|
|
|
power_monitor = PowerMonitoring()
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2021-07-14 15:53:39 +02:00
|
|
|
HARDWARE.initialize_hardware()
|
2021-02-23 17:04:10 +01:00
|
|
|
thermal_config = HARDWARE.get_thermal_config()
|
2020-08-24 14:17:41 +02:00
|
|
|
|
2022-03-02 17:35:58 +01:00
|
|
|
fan_controller = None
|
2021-08-31 21:29:53 +02:00
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
while not end_event.is_set():
|
|
|
|
|
sm.update(PANDA_STATES_TIMEOUT)
|
2021-10-04 11:30:11 +02:00
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
pandaStates = sm['pandaStates']
|
2021-10-04 11:30:11 +02:00
|
|
|
peripheralState = sm['peripheralState']
|
2023-07-26 14:31:53 -07:00
|
|
|
peripheral_panda_present = peripheralState.pandaType != log.PandaState.PandaType.unknown
|
2021-10-04 11:30:11 +02:00
|
|
|
|
2025-05-27 20:20:38 -07:00
|
|
|
# handle requests to cycle system started state
|
|
|
|
|
if params.get_bool("OnroadCycleRequested"):
|
|
|
|
|
params.put_bool("OnroadCycleRequested", False)
|
|
|
|
|
offroad_cycle_count = sm.frame
|
|
|
|
|
onroad_conditions["not_onroad_cycle"] = (sm.frame - offroad_cycle_count) >= ONROAD_CYCLE_TIME * SERVICE_LIST['pandaStates'].frequency
|
|
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
if sm.updated['pandaStates'] and len(pandaStates) > 0:
|
2020-04-20 14:15:01 -07:00
|
|
|
|
2022-02-07 14:32:29 -08:00
|
|
|
# Set ignition based on any panda connected
|
|
|
|
|
onroad_conditions["ignition"] = any(ps.ignitionLine or ps.ignitionCan for ps in pandaStates if ps.pandaType != log.PandaState.PandaType.unknown)
|
|
|
|
|
|
|
|
|
|
pandaState = pandaStates[0]
|
2020-04-06 16:01:42 -07:00
|
|
|
|
2021-10-04 11:30:11 +02:00
|
|
|
in_car = pandaState.harnessStatus != log.PandaState.HarnessStatus.notConnected
|
2021-03-17 14:19:55 +01:00
|
|
|
|
2020-04-07 19:18:14 -07:00
|
|
|
# Setup fan handler on first connect to panda
|
2023-07-26 14:31:53 -07:00
|
|
|
if fan_controller is None and peripheral_panda_present:
|
2021-08-31 21:29:53 +02:00
|
|
|
if TICI:
|
2022-03-02 17:35:58 +01:00
|
|
|
fan_controller = TiciFanController()
|
2020-04-07 19:18:14 -07:00
|
|
|
|
2024-02-09 21:44:23 -08:00
|
|
|
elif (time.monotonic() - sm.recv_time['pandaStates']) > DISCONNECT_TIMEOUT:
|
2022-06-02 13:39:41 -07:00
|
|
|
if onroad_conditions["ignition"]:
|
|
|
|
|
onroad_conditions["ignition"] = False
|
|
|
|
|
cloudlog.error("panda timed out onroad")
|
|
|
|
|
|
2024-07-11 02:49:42 -07:00
|
|
|
# Run at 2Hz, plus either edge of ignition
|
2025-05-27 20:20:38 -07:00
|
|
|
ign_edge = (started_ts is not None) != all(onroad_conditions.values())
|
2024-06-05 15:58:00 -07:00
|
|
|
if (sm.frame % round(SERVICE_LIST['pandaStates'].frequency * DT_HW) != 0) and not ign_edge:
|
2024-03-22 15:57:36 -07:00
|
|
|
continue
|
|
|
|
|
|
2024-12-07 15:03:30 -08:00
|
|
|
msg = messaging.new_message('deviceState', valid=True)
|
|
|
|
|
msg.deviceState = thermal_config.get_msg()
|
2024-03-22 15:57:36 -07:00
|
|
|
msg.deviceState.deviceType = HARDWARE.get_device_type()
|
|
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
try:
|
|
|
|
|
last_hw_state = hw_queue.get_nowait()
|
|
|
|
|
except queue.Empty:
|
|
|
|
|
pass
|
2020-02-03 11:28:38 -08:00
|
|
|
|
2021-02-19 12:19:29 -08:00
|
|
|
msg.deviceState.freeSpacePercent = get_available_percent(default=100.0)
|
2021-02-16 21:39:32 -08:00
|
|
|
msg.deviceState.memoryUsagePercent = int(round(psutil.virtual_memory().percent))
|
2021-06-29 22:55:18 +02:00
|
|
|
msg.deviceState.gpuUsagePercent = int(round(HARDWARE.get_gpu_usage_percent()))
|
2024-02-20 20:15:46 -08:00
|
|
|
online_cpu_usage = [int(round(n)) for n in psutil.cpu_percent(percpu=True)]
|
|
|
|
|
offline_cpu_usage = [0., ] * (len(msg.deviceState.cpuTempC) - len(online_cpu_usage))
|
|
|
|
|
msg.deviceState.cpuUsagePercent = online_cpu_usage + offline_cpu_usage
|
2022-01-17 12:06:37 +01:00
|
|
|
|
|
|
|
|
msg.deviceState.networkType = last_hw_state.network_type
|
2022-03-09 11:36:52 +01:00
|
|
|
msg.deviceState.networkMetered = last_hw_state.network_metered
|
2022-01-17 12:06:37 +01:00
|
|
|
msg.deviceState.networkStrength = last_hw_state.network_strength
|
2022-07-28 14:58:23 +02:00
|
|
|
msg.deviceState.networkStats = last_hw_state.network_stats
|
2022-01-17 12:06:37 +01:00
|
|
|
if last_hw_state.network_info is not None:
|
|
|
|
|
msg.deviceState.networkInfo = last_hw_state.network_info
|
|
|
|
|
|
|
|
|
|
msg.deviceState.nvmeTempC = last_hw_state.nvme_temps
|
|
|
|
|
msg.deviceState.modemTempC = last_hw_state.modem_temps
|
2021-05-17 13:10:08 +02:00
|
|
|
|
2021-10-15 17:19:45 -07:00
|
|
|
msg.deviceState.screenBrightnessPercent = HARDWARE.get_screen_brightness()
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2023-06-15 17:34:01 -07:00
|
|
|
# this subset is only used for offroad
|
2022-10-03 21:19:51 -07:00
|
|
|
temp_sources = [
|
|
|
|
|
msg.deviceState.memoryTempC,
|
2024-12-07 15:15:49 -08:00
|
|
|
max(msg.deviceState.cpuTempC, default=0.),
|
|
|
|
|
max(msg.deviceState.gpuTempC, default=0.),
|
2022-10-03 21:19:51 -07:00
|
|
|
]
|
|
|
|
|
offroad_comp_temp = offroad_temp_filter.update(max(temp_sources))
|
|
|
|
|
|
|
|
|
|
# this drives the thermal status while onroad
|
2024-12-07 15:15:49 -08:00
|
|
|
temp_sources.append(max(msg.deviceState.pmicTempC, default=0.))
|
2022-10-03 21:19:51 -07:00
|
|
|
all_comp_temp = all_temp_filter.update(max(temp_sources))
|
2023-06-15 17:08:47 -07:00
|
|
|
msg.deviceState.maxTempC = all_comp_temp
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2022-03-02 17:35:58 +01:00
|
|
|
if fan_controller is not None:
|
2022-10-03 21:19:51 -07:00
|
|
|
msg.deviceState.fanSpeedPercentDesired = fan_controller.update(all_comp_temp, onroad_conditions["ignition"])
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2023-08-20 18:50:58 -07:00
|
|
|
is_offroad_for_5_min = (started_ts is None) and ((not started_seen) or (off_ts is None) or (time.monotonic() - off_ts > 60 * 5))
|
2022-10-03 21:19:51 -07:00
|
|
|
if is_offroad_for_5_min and offroad_comp_temp > OFFROAD_DANGER_TEMP:
|
2023-06-15 20:12:24 -07:00
|
|
|
# if device is offroad and already hot without the extra onroad load,
|
|
|
|
|
# we want to cool down first before increasing load
|
2020-01-17 11:03:22 -08:00
|
|
|
thermal_status = ThermalStatus.danger
|
2021-01-16 14:23:06 -08:00
|
|
|
else:
|
2021-09-04 02:19:11 +02:00
|
|
|
current_band = THERMAL_BANDS[thermal_status]
|
|
|
|
|
band_idx = list(THERMAL_BANDS.keys()).index(thermal_status)
|
2022-10-03 21:19:51 -07:00
|
|
|
if current_band.min_temp is not None and all_comp_temp < current_band.min_temp:
|
2021-09-04 02:19:11 +02:00
|
|
|
thermal_status = list(THERMAL_BANDS.keys())[band_idx - 1]
|
2022-10-03 21:19:51 -07:00
|
|
|
elif current_band.max_temp is not None and all_comp_temp > current_band.max_temp:
|
2021-09-04 02:19:11 +02:00
|
|
|
thermal_status = list(THERMAL_BANDS.keys())[band_idx + 1]
|
2020-01-17 11:03:22 -08:00
|
|
|
|
|
|
|
|
# **** starting logic ****
|
|
|
|
|
|
2021-11-11 15:32:25 -08:00
|
|
|
startup_conditions["up_to_date"] = params.get("Offroad_ConnectivityNeeded") is None or params.get_bool("DisableUpdates") or params.get_bool("SnoozeUpdate")
|
2021-04-07 15:36:37 +02:00
|
|
|
startup_conditions["not_uninstalling"] = not params.get_bool("DoUninstall")
|
2020-10-15 15:47:38 +02:00
|
|
|
startup_conditions["accepted_terms"] = params.get("HasAcceptedTerms") == terms_version
|
2020-01-17 11:03:22 -08:00
|
|
|
|
|
|
|
|
# with 2% left, we killall, otherwise the phone will take a long time to boot
|
2021-02-19 12:19:29 -08:00
|
|
|
startup_conditions["free_space"] = msg.deviceState.freeSpacePercent > 2
|
2024-01-19 14:05:03 -08:00
|
|
|
startup_conditions["completed_training"] = params.get("CompletedTrainingVersion") == training_version
|
2021-04-07 15:36:37 +02:00
|
|
|
startup_conditions["not_driver_view"] = not params.get_bool("IsDriverViewEnabled")
|
|
|
|
|
startup_conditions["not_taking_snapshot"] = not params.get_bool("IsTakingSnapshot")
|
2023-06-12 16:33:33 -07:00
|
|
|
|
|
|
|
|
# must be at an engageable thermal band to go onroad
|
|
|
|
|
startup_conditions["device_temp_engageable"] = thermal_status < ThermalStatus.red
|
|
|
|
|
|
2024-01-08 15:09:38 -08:00
|
|
|
# ensure device is fully booted
|
|
|
|
|
startup_conditions["device_booted"] = startup_conditions.get("device_booted", False) or HARDWARE.booted()
|
|
|
|
|
|
2023-06-12 16:33:33 -07:00
|
|
|
# if the temperature enters the danger zone, go offroad to cool down
|
2021-12-04 22:17:44 -08:00
|
|
|
onroad_conditions["device_temp_good"] = thermal_status < ThermalStatus.danger
|
2023-06-23 17:12:01 -07:00
|
|
|
extra_text = f"{offroad_comp_temp:.1f}C"
|
|
|
|
|
show_alert = (not onroad_conditions["device_temp_good"] or not startup_conditions["device_temp_engageable"]) and onroad_conditions["ignition"]
|
|
|
|
|
set_offroad_alert_if_changed("Offroad_TemperatureTooHigh", show_alert, extra_text=extra_text)
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2022-03-20 17:28:09 -07:00
|
|
|
# TODO: this should move to TICI.initialize_hardware, but we currently can't import params there
|
2024-11-05 14:27:33 -08:00
|
|
|
if TICI and HARDWARE.get_device_type() == "tici":
|
2022-03-20 17:28:09 -07:00
|
|
|
if not os.path.isfile("/persist/comma/living-in-the-moment"):
|
|
|
|
|
if not Path("/data/media").is_mount():
|
|
|
|
|
set_offroad_alert_if_changed("Offroad_StorageMissing", True)
|
|
|
|
|
else:
|
|
|
|
|
# check for bad NVMe
|
|
|
|
|
try:
|
|
|
|
|
with open("/sys/block/nvme0n1/device/model") as f:
|
|
|
|
|
model = f.read().strip()
|
|
|
|
|
if not model.startswith("Samsung SSD 980") and params.get("Offroad_BadNvme") is None:
|
|
|
|
|
set_offroad_alert_if_changed("Offroad_BadNvme", True)
|
|
|
|
|
cloudlog.event("Unsupported NVMe", model=model, error=True)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
2021-05-17 13:35:24 -07:00
|
|
|
|
2020-12-29 22:32:03 -08:00
|
|
|
# Handle offroad/onroad transition
|
2021-12-04 22:17:44 -08:00
|
|
|
should_start = all(onroad_conditions.values())
|
|
|
|
|
if started_ts is None:
|
|
|
|
|
should_start = should_start and all(startup_conditions.values())
|
|
|
|
|
|
2021-04-12 01:40:58 -07:00
|
|
|
if should_start != should_start_prev or (count == 0):
|
2022-01-10 19:54:07 +01:00
|
|
|
params.put_bool("IsEngaged", False)
|
|
|
|
|
engaged_prev = False
|
2021-04-12 01:40:58 -07:00
|
|
|
HARDWARE.set_power_save(not should_start)
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2024-09-03 14:40:23 -07:00
|
|
|
if sm.updated['selfdriveState']:
|
|
|
|
|
engaged = sm['selfdriveState'].enabled
|
2022-01-10 19:54:07 +01:00
|
|
|
if engaged != engaged_prev:
|
|
|
|
|
params.put_bool("IsEngaged", engaged)
|
|
|
|
|
engaged_prev = engaged
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open('/dev/kmsg', 'w') as kmsg:
|
2024-06-05 15:58:00 -07:00
|
|
|
kmsg.write(f"<3>[hardware] engaged: {engaged}\n")
|
2022-01-10 19:54:07 +01:00
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
2021-04-12 01:40:58 -07:00
|
|
|
if should_start:
|
2020-01-17 11:03:22 -08:00
|
|
|
off_ts = None
|
|
|
|
|
if started_ts is None:
|
2023-08-20 18:50:58 -07:00
|
|
|
started_ts = time.monotonic()
|
2020-01-17 11:03:22 -08:00
|
|
|
started_seen = True
|
2023-06-15 18:16:59 -07:00
|
|
|
if startup_blocked_ts is not None:
|
2023-08-20 18:50:58 -07:00
|
|
|
cloudlog.event("Startup after block", block_duration=(time.monotonic() - startup_blocked_ts),
|
2023-06-15 18:16:59 -07:00
|
|
|
startup_conditions=startup_conditions, onroad_conditions=onroad_conditions,
|
|
|
|
|
startup_conditions_prev=startup_conditions_prev, error=True)
|
|
|
|
|
startup_blocked_ts = None
|
2020-01-17 11:03:22 -08:00
|
|
|
else:
|
2021-12-04 22:17:44 -08:00
|
|
|
if onroad_conditions["ignition"] and (startup_conditions != startup_conditions_prev):
|
2022-10-04 20:58:07 -07:00
|
|
|
cloudlog.event("Startup blocked", startup_conditions=startup_conditions, onroad_conditions=onroad_conditions, error=True)
|
|
|
|
|
startup_conditions_prev = startup_conditions.copy()
|
2023-08-20 18:50:58 -07:00
|
|
|
startup_blocked_ts = time.monotonic()
|
2020-12-29 22:32:03 -08:00
|
|
|
|
2020-01-17 11:03:22 -08:00
|
|
|
started_ts = None
|
|
|
|
|
if off_ts is None:
|
2023-08-20 18:50:58 -07:00
|
|
|
off_ts = time.monotonic()
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2020-03-10 22:18:48 -07:00
|
|
|
# Offroad power monitoring
|
2022-11-21 23:43:24 -08:00
|
|
|
voltage = None if peripheralState.pandaType == log.PandaState.PandaType.unknown else peripheralState.voltage
|
|
|
|
|
power_monitor.calculate(voltage, onroad_conditions["ignition"])
|
2021-02-16 21:39:32 -08:00
|
|
|
msg.deviceState.offroadPowerUsageUwh = power_monitor.get_power_used()
|
|
|
|
|
msg.deviceState.carBatteryCapacityUwh = max(0, power_monitor.get_car_battery_capacity())
|
2022-06-27 15:33:46 +02:00
|
|
|
current_power_draw = HARDWARE.get_current_power_draw()
|
|
|
|
|
statlog.sample("power_draw", current_power_draw)
|
|
|
|
|
msg.deviceState.powerDrawW = current_power_draw
|
2022-07-28 14:58:23 +02:00
|
|
|
|
2022-06-27 15:33:46 +02:00
|
|
|
som_power_draw = HARDWARE.get_som_power_draw()
|
|
|
|
|
statlog.sample("som_power_draw", som_power_draw)
|
|
|
|
|
msg.deviceState.somPowerDrawW = som_power_draw
|
2020-08-17 11:56:27 +02:00
|
|
|
|
|
|
|
|
# Check if we need to shut down
|
2022-08-31 21:13:53 -07:00
|
|
|
if power_monitor.should_shutdown(onroad_conditions["ignition"], in_car, off_ts, started_seen):
|
2022-01-14 14:13:32 +01:00
|
|
|
cloudlog.warning(f"shutting device down, offroad since {off_ts}")
|
|
|
|
|
params.put_bool("DoShutdown", True)
|
2020-03-10 22:18:48 -07:00
|
|
|
|
2021-02-16 21:39:32 -08:00
|
|
|
msg.deviceState.started = started_ts is not None
|
|
|
|
|
msg.deviceState.startedMonoTime = int(1e9*(started_ts or 0))
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2021-05-19 00:19:49 -07:00
|
|
|
last_ping = params.get("LastAthenaPingTime")
|
|
|
|
|
if last_ping is not None:
|
|
|
|
|
msg.deviceState.lastAthenaPingTime = int(last_ping)
|
|
|
|
|
|
2021-02-16 21:39:32 -08:00
|
|
|
msg.deviceState.thermalStatus = thermal_status
|
|
|
|
|
pm.send("deviceState", msg)
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
# Log to statsd
|
2022-01-10 15:21:48 +01:00
|
|
|
statlog.gauge("free_space_percent", msg.deviceState.freeSpacePercent)
|
|
|
|
|
statlog.gauge("gpu_usage_percent", msg.deviceState.gpuUsagePercent)
|
|
|
|
|
statlog.gauge("memory_usage_percent", msg.deviceState.memoryUsagePercent)
|
|
|
|
|
for i, usage in enumerate(msg.deviceState.cpuUsagePercent):
|
|
|
|
|
statlog.gauge(f"cpu{i}_usage_percent", usage)
|
|
|
|
|
for i, temp in enumerate(msg.deviceState.cpuTempC):
|
|
|
|
|
statlog.gauge(f"cpu{i}_temperature", temp)
|
|
|
|
|
for i, temp in enumerate(msg.deviceState.gpuTempC):
|
|
|
|
|
statlog.gauge(f"gpu{i}_temperature", temp)
|
|
|
|
|
statlog.gauge("memory_temperature", msg.deviceState.memoryTempC)
|
|
|
|
|
for i, temp in enumerate(msg.deviceState.pmicTempC):
|
|
|
|
|
statlog.gauge(f"pmic{i}_temperature", temp)
|
2022-01-17 12:06:37 +01:00
|
|
|
for i, temp in enumerate(last_hw_state.nvme_temps):
|
|
|
|
|
statlog.gauge(f"nvme_temperature{i}", temp)
|
|
|
|
|
for i, temp in enumerate(last_hw_state.modem_temps):
|
|
|
|
|
statlog.gauge(f"modem_temperature{i}", temp)
|
2022-01-10 15:21:48 +01:00
|
|
|
statlog.gauge("fan_speed_percent_desired", msg.deviceState.fanSpeedPercentDesired)
|
|
|
|
|
statlog.gauge("screen_brightness_percent", msg.deviceState.screenBrightnessPercent)
|
|
|
|
|
|
2021-04-08 22:50:11 -07:00
|
|
|
# report to server once every 10 minutes
|
2023-06-15 15:04:22 -07:00
|
|
|
rising_edge_started = should_start and not should_start_prev
|
2024-06-05 15:58:00 -07:00
|
|
|
if rising_edge_started or (count % int(600. / DT_HW)) == 0:
|
2023-06-15 15:04:22 -07:00
|
|
|
dat = {
|
|
|
|
|
'count': count,
|
|
|
|
|
'pandaStates': [strip_deprecated_keys(p.to_dict()) for p in pandaStates],
|
|
|
|
|
'peripheralState': strip_deprecated_keys(peripheralState.to_dict()),
|
|
|
|
|
'location': (strip_deprecated_keys(sm["gpsLocationExternal"].to_dict()) if sm.alive["gpsLocationExternal"] else None),
|
|
|
|
|
'deviceState': strip_deprecated_keys(msg.to_dict())
|
|
|
|
|
}
|
|
|
|
|
cloudlog.event("STATUS_PACKET", **dat)
|
|
|
|
|
|
|
|
|
|
# save last one before going onroad
|
|
|
|
|
if rising_edge_started:
|
|
|
|
|
try:
|
|
|
|
|
params.put("LastOffroadStatusPacket", json.dumps(dat))
|
|
|
|
|
except Exception:
|
|
|
|
|
cloudlog.exception("failed to save offroad status")
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2024-01-17 17:45:22 -08:00
|
|
|
params.put_bool_nonblocking("NetworkMetered", msg.deviceState.networkMetered)
|
2024-01-17 16:30:08 -08:00
|
|
|
|
2020-01-17 11:03:22 -08:00
|
|
|
count += 1
|
2023-06-15 15:04:22 -07:00
|
|
|
should_start_prev = should_start
|
2020-01-17 11:03:22 -08:00
|
|
|
|
|
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
def main():
|
|
|
|
|
hw_queue = queue.Queue(maxsize=1)
|
|
|
|
|
end_event = threading.Event()
|
|
|
|
|
|
|
|
|
|
threads = [
|
|
|
|
|
threading.Thread(target=hw_state_thread, args=(end_event, hw_queue)),
|
2024-06-05 15:58:00 -07:00
|
|
|
threading.Thread(target=hardware_thread, args=(end_event, hw_queue)),
|
2022-01-17 12:06:37 +01:00
|
|
|
]
|
|
|
|
|
|
2024-12-13 16:50:34 -08:00
|
|
|
if TICI:
|
|
|
|
|
threads.append(threading.Thread(target=touch_thread, args=(end_event,)))
|
|
|
|
|
|
2022-01-17 12:06:37 +01:00
|
|
|
for t in threads:
|
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
if not all(t.is_alive() for t in threads):
|
|
|
|
|
break
|
|
|
|
|
finally:
|
|
|
|
|
end_event.set()
|
|
|
|
|
|
|
|
|
|
for t in threads:
|
|
|
|
|
t.join()
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2020-03-12 11:55:33 -07:00
|
|
|
|
2020-01-17 11:03:22 -08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|