2020-05-09 09:08:11 +08:00
|
|
|
#!/usr/bin/env python3
|
2020-08-24 20:17:41 +08:00
|
|
|
import os
|
2023-06-16 06:04:22 +08:00
|
|
|
import json
|
2022-01-17 19:06:37 +08:00
|
|
|
import queue
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
from collections import OrderedDict, namedtuple
|
2021-03-29 12:22:31 +08:00
|
|
|
from pathlib import Path
|
2020-08-24 20:17:41 +08:00
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
import cereal.messaging as messaging
|
2020-01-18 03:03:22 +08:00
|
|
|
from cereal import log
|
2023-09-22 09:35:14 +08:00
|
|
|
from cereal.services import SERVICE_LIST
|
2023-08-21 11:49:55 +08: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-06 06:58:00 +08:00
|
|
|
from openpilot.common.realtime import DT_HW
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.selfdrive.controls.lib.alertmanager import set_offroad_alert
|
|
|
|
from openpilot.system.hardware import HARDWARE, TICI, AGNOS
|
|
|
|
from openpilot.system.loggerd.config import get_available_percent
|
2024-05-21 13:39:25 +08:00
|
|
|
from openpilot.system.statsd import statlog
|
2023-12-07 09:27:51 +08:00
|
|
|
from openpilot.common.swaglog import cloudlog
|
2024-06-06 06:58:00 +08:00
|
|
|
from openpilot.system.hardware.power_monitoring import PowerMonitoring
|
|
|
|
from openpilot.system.hardware.fan_controller import TiciFanController
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.system.version import terms_version, training_version
|
2020-08-24 20:17:41 +08:00
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
ThermalStatus = log.DeviceState.ThermalStatus
|
|
|
|
NetworkType = log.DeviceState.NetworkType
|
|
|
|
NetworkStrength = log.DeviceState.NetworkStrength
|
2020-01-18 03:03:22 +08:00
|
|
|
CURRENT_TAU = 15. # 15s time constant
|
2021-09-04 08:19:11 +08:00
|
|
|
TEMP_TAU = 5. # 5s time constant
|
2020-04-21 05:15:01 +08:00
|
|
|
DISCONNECT_TIMEOUT = 5. # wait 5 seconds before going offroad after disconnect so you get an alert
|
2023-09-22 09:35:14 +08:00
|
|
|
PANDA_STATES_TIMEOUT = round(1000 / SERVICE_LIST['pandaStates'].frequency * 1.5) # 1.5x the expected pandaState frequency
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-09-04 08:19:11 +08:00
|
|
|
ThermalBand = namedtuple("ThermalBand", ['min_temp', 'max_temp'])
|
2023-08-09 08:13:35 +08:00
|
|
|
HardwareState = namedtuple("HardwareState", ['network_type', 'network_info', 'network_strength', 'network_stats',
|
|
|
|
'network_metered', 'nvme_temps', 'modem_temps'])
|
2020-10-15 21:47:38 +08:00
|
|
|
|
2021-09-04 08:19:11 +08: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-10 11:15:13 +08:00
|
|
|
ThermalStatus.red: ThermalBand(88.0, 107.),
|
2021-09-04 08:19:11 +08:00
|
|
|
ThermalStatus.danger: ThermalBand(94.0, None),
|
|
|
|
})
|
2020-03-13 02:55:33 +08:00
|
|
|
|
2021-09-04 08:19:11 +08:00
|
|
|
# Override to highest thermal band when offroad and above this temp
|
2023-06-24 08:23:48 +08:00
|
|
|
OFFROAD_DANGER_TEMP = 75
|
2021-09-04 08:19:11 +08:00
|
|
|
|
2024-02-25 08:41:23 +08:00
|
|
|
prev_offroad_states: dict[str, tuple[bool, str | None]] = {}
|
2021-03-30 05:37:52 +08:00
|
|
|
|
2024-02-25 08:41:23 +08:00
|
|
|
tz_by_type: dict[str, int] | None = None
|
2022-03-15 05:45:50 +08:00
|
|
|
def populate_tz_by_type():
|
|
|
|
global tz_by_type
|
|
|
|
tz_by_type = {}
|
|
|
|
for n in os.listdir("/sys/devices/virtual/thermal"):
|
|
|
|
if not n.startswith("thermal_zone"):
|
|
|
|
continue
|
|
|
|
with open(os.path.join("/sys/devices/virtual/thermal", n, "type")) as f:
|
2023-08-12 06:33:49 +08:00
|
|
|
tz_by_type[f.read().strip()] = int(n.removeprefix("thermal_zone"))
|
2022-03-15 05:45:50 +08:00
|
|
|
|
2020-08-24 20:17:41 +08:00
|
|
|
def read_tz(x):
|
|
|
|
if x is None:
|
2020-02-29 12:27:41 +08:00
|
|
|
return 0
|
2020-08-24 20:17:41 +08:00
|
|
|
|
2022-03-15 05:45:50 +08:00
|
|
|
if isinstance(x, str):
|
|
|
|
if tz_by_type is None:
|
|
|
|
populate_tz_by_type()
|
|
|
|
x = tz_by_type[x]
|
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
try:
|
2020-12-30 14:32:03 +08:00
|
|
|
with open(f"/sys/devices/virtual/thermal/thermal_zone{x}/temp") as f:
|
2020-08-24 20:17:41 +08:00
|
|
|
return int(f.read())
|
2020-01-18 03:03:22 +08:00
|
|
|
except FileNotFoundError:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2020-08-24 20:17:41 +08:00
|
|
|
def read_thermal(thermal_config):
|
2023-12-04 02:50:17 +08:00
|
|
|
dat = messaging.new_message('deviceState', valid=True)
|
2021-02-17 13:39:32 +08:00
|
|
|
dat.deviceState.cpuTempC = [read_tz(z) / thermal_config.cpu[1] for z in thermal_config.cpu[0]]
|
|
|
|
dat.deviceState.gpuTempC = [read_tz(z) / thermal_config.gpu[1] for z in thermal_config.gpu[0]]
|
|
|
|
dat.deviceState.memoryTempC = read_tz(thermal_config.mem[0]) / thermal_config.mem[1]
|
2021-11-24 08:08:20 +08:00
|
|
|
dat.deviceState.pmicTempC = [read_tz(z) / thermal_config.pmic[1] for z in thermal_config.pmic[0]]
|
2020-01-18 03:03:22 +08:00
|
|
|
return dat
|
|
|
|
|
2020-03-13 02:55:33 +08:00
|
|
|
|
2024-02-25 08:41:23 +08:00
|
|
|
def set_offroad_alert_if_changed(offroad_alert: str, show_alert: bool, extra_text: str | None=None):
|
2020-10-15 21:47:38 +08: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)
|
|
|
|
|
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
def hw_state_thread(end_event, hw_queue):
|
|
|
|
"""Handles non critical hardware state, and sends over queue"""
|
|
|
|
count = 0
|
2022-03-03 03:01:06 +08:00
|
|
|
prev_hw_state = None
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2022-04-06 05:29:07 +08:00
|
|
|
modem_version = None
|
|
|
|
modem_nv = None
|
2022-04-12 06:33:58 +08:00
|
|
|
modem_configured = False
|
2023-07-24 11:36:13 +08:00
|
|
|
modem_restarted = False
|
|
|
|
modem_missing_count = 0
|
2022-04-06 05:29:07 +08:00
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
while not end_event.is_set():
|
|
|
|
# these are expensive calls. update every 10s
|
2024-06-06 06:58:00 +08:00
|
|
|
if (count % int(10. / DT_HW)) == 0:
|
2022-01-17 19:06:37 +08:00
|
|
|
try:
|
|
|
|
network_type = HARDWARE.get_network_type()
|
2022-03-03 03: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 19:06:37 +08:00
|
|
|
|
2022-04-06 05:29:07 +08:00
|
|
|
# Log modem version once
|
2022-06-02 21:20:51 +08:00
|
|
|
if AGNOS and ((modem_version is None) or (modem_nv is None)):
|
2023-08-25 07:30:54 +08:00
|
|
|
modem_version = HARDWARE.get_modem_version()
|
|
|
|
modem_nv = HARDWARE.get_modem_nv()
|
2022-04-06 05:29:07 +08:00
|
|
|
|
|
|
|
if (modem_version is not None) and (modem_nv is not None):
|
|
|
|
cloudlog.event("modem version", version=modem_version, nv=modem_nv)
|
2023-07-24 11:36:13 +08: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-06 05:29:07 +08:00
|
|
|
|
2022-07-28 20:58:23 +08:00
|
|
|
tx, rx = HARDWARE.get_modem_data_usage()
|
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
hw_state = HardwareState(
|
|
|
|
network_type=network_type,
|
|
|
|
network_info=HARDWARE.get_network_info(),
|
2022-07-28 20:58:23 +08: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 19:06:37 +08:00
|
|
|
nvme_temps=HARDWARE.get_nvme_temperatures(),
|
2022-03-03 03:01:06 +08:00
|
|
|
modem_temps=modem_temps,
|
2022-01-17 19:06:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
hw_queue.put_nowait(hw_state)
|
|
|
|
except queue.Full:
|
|
|
|
pass
|
|
|
|
|
2022-04-12 06:33:58 +08:00
|
|
|
# TODO: remove this once the config is in AGNOS
|
|
|
|
if not modem_configured and len(HARDWARE.get_sim_info().get('sim_id', '')) > 0:
|
|
|
|
cloudlog.warning("configuring modem")
|
|
|
|
HARDWARE.configure_modem()
|
|
|
|
modem_configured = True
|
|
|
|
|
2022-03-03 03:01:06 +08:00
|
|
|
prev_hw_state = hw_state
|
2022-01-17 19:06:37 +08:00
|
|
|
except Exception:
|
2022-03-03 03:01:06 +08:00
|
|
|
cloudlog.exception("Error getting hardware state")
|
2022-01-17 19:06:37 +08:00
|
|
|
|
|
|
|
count += 1
|
2024-06-06 06:58:00 +08:00
|
|
|
time.sleep(DT_HW)
|
2020-12-30 14:32:03 +08:00
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
|
2024-06-06 06:58:00 +08:00
|
|
|
def hardware_thread(end_event, hw_queue) -> None:
|
2022-01-17 19:06:37 +08:00
|
|
|
pm = messaging.PubMaster(['deviceState'])
|
2024-09-04 05:40:23 +08:00
|
|
|
sm = messaging.SubMaster(["peripheralState", "gpsLocationExternal", "selfdriveState", "pandaStates"], poll="pandaStates")
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
2024-02-25 08:41:23 +08:00
|
|
|
onroad_conditions: dict[str, bool] = {
|
2020-10-15 21:47:38 +08:00
|
|
|
"ignition": False,
|
|
|
|
}
|
2024-02-25 08:41:23 +08:00
|
|
|
startup_conditions: dict[str, bool] = {}
|
|
|
|
startup_conditions_prev: dict[str, bool] = {}
|
2020-10-15 21:47:38 +08:00
|
|
|
|
2024-02-25 08:41:23 +08:00
|
|
|
off_ts: float | None = None
|
|
|
|
started_ts: float | None = None
|
2020-01-18 03:03:22 +08:00
|
|
|
started_seen = False
|
2024-02-25 08:41:23 +08:00
|
|
|
startup_blocked_ts: float | None = None
|
2023-06-16 08:34:01 +08:00
|
|
|
thermal_status = ThermalStatus.yellow
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
last_hw_state = HardwareState(
|
|
|
|
network_type=NetworkType.none,
|
2022-07-28 20:58:23 +08:00
|
|
|
network_info=None,
|
2022-03-09 18:36:52 +08:00
|
|
|
network_metered=False,
|
2022-01-17 19:06:37 +08:00
|
|
|
network_strength=NetworkStrength.unknown,
|
2022-07-28 20:58:23 +08:00
|
|
|
network_stats={'wwanTx': -1, 'wwanRx': -1},
|
2022-01-17 19:06:37 +08:00
|
|
|
nvme_temps=[],
|
|
|
|
modem_temps=[],
|
|
|
|
)
|
2020-02-04 03:28:38 +08:00
|
|
|
|
2024-06-06 06:58:00 +08: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-18 03:03:22 +08:00
|
|
|
should_start_prev = False
|
2021-10-04 17:30:11 +08:00
|
|
|
in_car = False
|
2022-01-11 02:54:07 +08:00
|
|
|
engaged_prev = False
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
params = Params()
|
2020-12-30 14:32:03 +08:00
|
|
|
power_monitor = PowerMonitoring()
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-07-14 21:53:39 +08:00
|
|
|
HARDWARE.initialize_hardware()
|
2021-02-24 00:04:10 +08:00
|
|
|
thermal_config = HARDWARE.get_thermal_config()
|
2020-08-24 20:17:41 +08:00
|
|
|
|
2022-03-03 00:35:58 +08:00
|
|
|
fan_controller = None
|
2021-09-01 03:29:53 +08:00
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
while not end_event.is_set():
|
|
|
|
sm.update(PANDA_STATES_TIMEOUT)
|
2021-10-04 17:30:11 +08:00
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
pandaStates = sm['pandaStates']
|
2021-10-04 17:30:11 +08:00
|
|
|
peripheralState = sm['peripheralState']
|
2023-07-27 05:31:53 +08:00
|
|
|
peripheral_panda_present = peripheralState.pandaType != log.PandaState.PandaType.unknown
|
2021-10-04 17:30:11 +08:00
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
if sm.updated['pandaStates'] and len(pandaStates) > 0:
|
2020-04-21 05:15:01 +08:00
|
|
|
|
2022-02-08 06: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-07 07:01:42 +08:00
|
|
|
|
2021-10-04 17:30:11 +08:00
|
|
|
in_car = pandaState.harnessStatus != log.PandaState.HarnessStatus.notConnected
|
2021-03-17 21:19:55 +08:00
|
|
|
|
2020-04-08 10:18:14 +08:00
|
|
|
# Setup fan handler on first connect to panda
|
2023-07-27 05:31:53 +08:00
|
|
|
if fan_controller is None and peripheral_panda_present:
|
2021-09-01 03:29:53 +08:00
|
|
|
if TICI:
|
2022-03-03 00:35:58 +08:00
|
|
|
fan_controller = TiciFanController()
|
2020-04-08 10:18:14 +08:00
|
|
|
|
2024-02-10 13:44:23 +08:00
|
|
|
elif (time.monotonic() - sm.recv_time['pandaStates']) > DISCONNECT_TIMEOUT:
|
2022-06-03 04:39:41 +08:00
|
|
|
if onroad_conditions["ignition"]:
|
|
|
|
onroad_conditions["ignition"] = False
|
|
|
|
cloudlog.error("panda timed out onroad")
|
|
|
|
|
2024-07-11 17:49:42 +08:00
|
|
|
# Run at 2Hz, plus either edge of ignition
|
|
|
|
ign_edge = (started_ts is not None) != onroad_conditions["ignition"]
|
2024-06-06 06:58:00 +08:00
|
|
|
if (sm.frame % round(SERVICE_LIST['pandaStates'].frequency * DT_HW) != 0) and not ign_edge:
|
2024-03-23 06:57:36 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
msg = read_thermal(thermal_config)
|
|
|
|
msg.deviceState.deviceType = HARDWARE.get_device_type()
|
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
try:
|
|
|
|
last_hw_state = hw_queue.get_nowait()
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
2020-02-04 03:28:38 +08:00
|
|
|
|
2021-02-20 04:19:29 +08:00
|
|
|
msg.deviceState.freeSpacePercent = get_available_percent(default=100.0)
|
2021-02-17 13:39:32 +08:00
|
|
|
msg.deviceState.memoryUsagePercent = int(round(psutil.virtual_memory().percent))
|
2021-06-30 04:55:18 +08:00
|
|
|
msg.deviceState.gpuUsagePercent = int(round(HARDWARE.get_gpu_usage_percent()))
|
2024-02-21 12: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 19:06:37 +08:00
|
|
|
|
|
|
|
msg.deviceState.networkType = last_hw_state.network_type
|
2022-03-09 18:36:52 +08:00
|
|
|
msg.deviceState.networkMetered = last_hw_state.network_metered
|
2022-01-17 19:06:37 +08:00
|
|
|
msg.deviceState.networkStrength = last_hw_state.network_strength
|
2022-07-28 20:58:23 +08:00
|
|
|
msg.deviceState.networkStats = last_hw_state.network_stats
|
2022-01-17 19:06:37 +08: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 19:10:08 +08:00
|
|
|
|
2021-10-16 08:19:45 +08:00
|
|
|
msg.deviceState.screenBrightnessPercent = HARDWARE.get_screen_brightness()
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2023-06-16 08:34:01 +08:00
|
|
|
# this subset is only used for offroad
|
2022-10-04 12:19:51 +08:00
|
|
|
temp_sources = [
|
|
|
|
msg.deviceState.memoryTempC,
|
|
|
|
max(msg.deviceState.cpuTempC),
|
|
|
|
max(msg.deviceState.gpuTempC),
|
|
|
|
]
|
|
|
|
offroad_comp_temp = offroad_temp_filter.update(max(temp_sources))
|
|
|
|
|
|
|
|
# this drives the thermal status while onroad
|
|
|
|
temp_sources.append(max(msg.deviceState.pmicTempC))
|
|
|
|
all_comp_temp = all_temp_filter.update(max(temp_sources))
|
2023-06-16 08:08:47 +08:00
|
|
|
msg.deviceState.maxTempC = all_comp_temp
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2022-03-03 00:35:58 +08:00
|
|
|
if fan_controller is not None:
|
2022-10-04 12:19:51 +08:00
|
|
|
msg.deviceState.fanSpeedPercentDesired = fan_controller.update(all_comp_temp, onroad_conditions["ignition"])
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2023-08-21 09:50:58 +08: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-04 12:19:51 +08:00
|
|
|
if is_offroad_for_5_min and offroad_comp_temp > OFFROAD_DANGER_TEMP:
|
2023-06-16 11:12:24 +08:00
|
|
|
# if device is offroad and already hot without the extra onroad load,
|
|
|
|
# we want to cool down first before increasing load
|
2020-01-18 03:03:22 +08:00
|
|
|
thermal_status = ThermalStatus.danger
|
2021-01-17 06:23:06 +08:00
|
|
|
else:
|
2021-09-04 08:19:11 +08:00
|
|
|
current_band = THERMAL_BANDS[thermal_status]
|
|
|
|
band_idx = list(THERMAL_BANDS.keys()).index(thermal_status)
|
2022-10-04 12:19:51 +08:00
|
|
|
if current_band.min_temp is not None and all_comp_temp < current_band.min_temp:
|
2021-09-04 08:19:11 +08:00
|
|
|
thermal_status = list(THERMAL_BANDS.keys())[band_idx - 1]
|
2022-10-04 12:19:51 +08:00
|
|
|
elif current_band.max_temp is not None and all_comp_temp > current_band.max_temp:
|
2021-09-04 08:19:11 +08:00
|
|
|
thermal_status = list(THERMAL_BANDS.keys())[band_idx + 1]
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
# **** starting logic ****
|
|
|
|
|
2021-11-12 07: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 21:36:37 +08:00
|
|
|
startup_conditions["not_uninstalling"] = not params.get_bool("DoUninstall")
|
2020-10-15 21:47:38 +08:00
|
|
|
startup_conditions["accepted_terms"] = params.get("HasAcceptedTerms") == terms_version
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
# with 2% left, we killall, otherwise the phone will take a long time to boot
|
2021-02-20 04:19:29 +08:00
|
|
|
startup_conditions["free_space"] = msg.deviceState.freeSpacePercent > 2
|
2024-01-20 06:05:03 +08:00
|
|
|
startup_conditions["completed_training"] = params.get("CompletedTrainingVersion") == training_version
|
2021-04-07 21:36:37 +08:00
|
|
|
startup_conditions["not_driver_view"] = not params.get_bool("IsDriverViewEnabled")
|
|
|
|
startup_conditions["not_taking_snapshot"] = not params.get_bool("IsTakingSnapshot")
|
2023-06-13 07:33:33 +08:00
|
|
|
|
|
|
|
# must be at an engageable thermal band to go onroad
|
|
|
|
startup_conditions["device_temp_engageable"] = thermal_status < ThermalStatus.red
|
|
|
|
|
2024-01-09 07:09:38 +08:00
|
|
|
# ensure device is fully booted
|
|
|
|
startup_conditions["device_booted"] = startup_conditions.get("device_booted", False) or HARDWARE.booted()
|
|
|
|
|
2023-06-13 07:33:33 +08:00
|
|
|
# if the temperature enters the danger zone, go offroad to cool down
|
2021-12-05 14:17:44 +08:00
|
|
|
onroad_conditions["device_temp_good"] = thermal_status < ThermalStatus.danger
|
2023-06-24 08:12:01 +08: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-18 03:03:22 +08:00
|
|
|
|
2022-03-21 08:28:09 +08:00
|
|
|
# TODO: this should move to TICI.initialize_hardware, but we currently can't import params there
|
2021-05-18 04:35:24 +08:00
|
|
|
if TICI:
|
2022-03-21 08:28:09 +08: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-18 04:35:24 +08:00
|
|
|
|
2020-12-30 14:32:03 +08:00
|
|
|
# Handle offroad/onroad transition
|
2021-12-05 14: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 16:40:58 +08:00
|
|
|
if should_start != should_start_prev or (count == 0):
|
2022-01-11 02:54:07 +08:00
|
|
|
params.put_bool("IsEngaged", False)
|
|
|
|
engaged_prev = False
|
2021-04-12 16:40:58 +08:00
|
|
|
HARDWARE.set_power_save(not should_start)
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2024-09-04 05:40:23 +08:00
|
|
|
if sm.updated['selfdriveState']:
|
|
|
|
engaged = sm['selfdriveState'].enabled
|
2022-01-11 02:54:07 +08:00
|
|
|
if engaged != engaged_prev:
|
|
|
|
params.put_bool("IsEngaged", engaged)
|
|
|
|
engaged_prev = engaged
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open('/dev/kmsg', 'w') as kmsg:
|
2024-06-06 06:58:00 +08:00
|
|
|
kmsg.write(f"<3>[hardware] engaged: {engaged}\n")
|
2022-01-11 02:54:07 +08:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2021-04-12 16:40:58 +08:00
|
|
|
if should_start:
|
2020-01-18 03:03:22 +08:00
|
|
|
off_ts = None
|
|
|
|
if started_ts is None:
|
2023-08-21 09:50:58 +08:00
|
|
|
started_ts = time.monotonic()
|
2020-01-18 03:03:22 +08:00
|
|
|
started_seen = True
|
2023-06-16 09:16:59 +08:00
|
|
|
if startup_blocked_ts is not None:
|
2023-08-21 09:50:58 +08:00
|
|
|
cloudlog.event("Startup after block", block_duration=(time.monotonic() - startup_blocked_ts),
|
2023-06-16 09:16:59 +08:00
|
|
|
startup_conditions=startup_conditions, onroad_conditions=onroad_conditions,
|
|
|
|
startup_conditions_prev=startup_conditions_prev, error=True)
|
|
|
|
startup_blocked_ts = None
|
2020-01-18 03:03:22 +08:00
|
|
|
else:
|
2021-12-05 14:17:44 +08:00
|
|
|
if onroad_conditions["ignition"] and (startup_conditions != startup_conditions_prev):
|
2022-10-05 11:58:07 +08:00
|
|
|
cloudlog.event("Startup blocked", startup_conditions=startup_conditions, onroad_conditions=onroad_conditions, error=True)
|
|
|
|
startup_conditions_prev = startup_conditions.copy()
|
2023-08-21 09:50:58 +08:00
|
|
|
startup_blocked_ts = time.monotonic()
|
2020-12-30 14:32:03 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
started_ts = None
|
|
|
|
if off_ts is None:
|
2023-08-21 09:50:58 +08:00
|
|
|
off_ts = time.monotonic()
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2020-03-11 13:18:48 +08:00
|
|
|
# Offroad power monitoring
|
2022-11-22 15: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-17 13: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 21:33:46 +08: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 20:58:23 +08:00
|
|
|
|
2022-06-27 21:33:46 +08: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 17:56:27 +08:00
|
|
|
|
|
|
|
# Check if we need to shut down
|
2022-09-01 12:13:53 +08:00
|
|
|
if power_monitor.should_shutdown(onroad_conditions["ignition"], in_car, off_ts, started_seen):
|
2022-01-14 21:13:32 +08:00
|
|
|
cloudlog.warning(f"shutting device down, offroad since {off_ts}")
|
|
|
|
params.put_bool("DoShutdown", True)
|
2020-03-11 13:18:48 +08:00
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
msg.deviceState.started = started_ts is not None
|
|
|
|
msg.deviceState.startedMonoTime = int(1e9*(started_ts or 0))
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-05-19 15:19:49 +08:00
|
|
|
last_ping = params.get("LastAthenaPingTime")
|
|
|
|
if last_ping is not None:
|
|
|
|
msg.deviceState.lastAthenaPingTime = int(last_ping)
|
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
msg.deviceState.thermalStatus = thermal_status
|
|
|
|
pm.send("deviceState", msg)
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2022-01-17 19:06:37 +08:00
|
|
|
# Log to statsd
|
2022-01-10 22:21:48 +08: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 19:06:37 +08: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 22:21:48 +08:00
|
|
|
statlog.gauge("fan_speed_percent_desired", msg.deviceState.fanSpeedPercentDesired)
|
|
|
|
statlog.gauge("screen_brightness_percent", msg.deviceState.screenBrightnessPercent)
|
|
|
|
|
2021-04-09 13:50:11 +08:00
|
|
|
# report to server once every 10 minutes
|
2023-06-16 06:04:22 +08:00
|
|
|
rising_edge_started = should_start and not should_start_prev
|
2024-06-06 06:58:00 +08:00
|
|
|
if rising_edge_started or (count % int(600. / DT_HW)) == 0:
|
2023-06-16 06:04:22 +08: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-18 03:03:22 +08:00
|
|
|
|
2024-01-18 09:45:22 +08:00
|
|
|
params.put_bool_nonblocking("NetworkMetered", msg.deviceState.networkMetered)
|
2024-01-18 08:30:08 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
count += 1
|
2023-06-16 06:04:22 +08:00
|
|
|
should_start_prev = should_start
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
|
2022-01-17 19:06:37 +08: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-06 06:58:00 +08:00
|
|
|
threading.Thread(target=hardware_thread, args=(end_event, hw_queue)),
|
2022-01-17 19:06:37 +08: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-18 03:03:22 +08:00
|
|
|
|
2020-03-13 02:55:33 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|