2020-05-09 09:08:11 +08:00
|
|
|
#!/usr/bin/env python3
|
2020-01-18 03:03:22 +08:00
|
|
|
import datetime
|
2020-08-24 20:17:41 +08:00
|
|
|
import os
|
2020-08-19 02:52:01 +08:00
|
|
|
import time
|
2021-03-29 12:22:31 +08:00
|
|
|
from pathlib import Path
|
2021-12-29 01:07:00 +08:00
|
|
|
from typing import Dict, NoReturn, Optional, Tuple
|
2021-09-04 08:19:11 +08:00
|
|
|
from collections import namedtuple, OrderedDict
|
2020-08-24 20:17:41 +08:00
|
|
|
|
|
|
|
import psutil
|
2020-01-18 03:03:22 +08:00
|
|
|
from smbus2 import SMBus
|
2020-08-24 20:17:41 +08:00
|
|
|
|
|
|
|
import cereal.messaging as messaging
|
2020-01-18 03:03:22 +08:00
|
|
|
from cereal import log
|
|
|
|
from common.filter_simple import FirstOrderFilter
|
2021-09-04 08:19:11 +08:00
|
|
|
from common.numpy_fast import interp
|
2021-05-05 02:32:40 +08:00
|
|
|
from common.params import Params, ParamKeyType
|
2020-08-24 20:17:41 +08:00
|
|
|
from common.realtime import DT_TRML, sec_since_boot
|
2021-03-26 04:30:09 +08:00
|
|
|
from common.dict_helpers import strip_deprecated_keys
|
2020-08-12 07:23:57 +08:00
|
|
|
from selfdrive.controls.lib.alertmanager import set_offroad_alert
|
2021-09-01 03:29:53 +08:00
|
|
|
from selfdrive.controls.lib.pid import PIController
|
|
|
|
from selfdrive.hardware import EON, TICI, PC, HARDWARE
|
2020-01-18 03:03:22 +08:00
|
|
|
from selfdrive.loggerd.config import get_available_percent
|
2020-08-24 20:17:41 +08:00
|
|
|
from selfdrive.swaglog import cloudlog
|
2020-12-02 22:20:57 +08:00
|
|
|
from selfdrive.thermald.power_monitoring import PowerMonitoring
|
2021-12-05 14:15:55 +08:00
|
|
|
from selfdrive.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
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-09-04 08:19:11 +08:00
|
|
|
ThermalBand = namedtuple("ThermalBand", ['min_temp', 'max_temp'])
|
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),
|
|
|
|
ThermalStatus.red: ThermalBand(80.0, 107.),
|
|
|
|
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
|
2021-09-29 17:45:02 +08:00
|
|
|
OFFROAD_DANGER_TEMP = 79.5 if TICI else 70.0
|
2021-09-04 08:19:11 +08:00
|
|
|
|
|
|
|
prev_offroad_states: Dict[str, Tuple[bool, Optional[str]]] = {}
|
2021-03-30 05:37:52 +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
|
|
|
|
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):
|
2021-02-17 13:39:32 +08:00
|
|
|
dat = messaging.new_message('deviceState')
|
|
|
|
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]
|
|
|
|
dat.deviceState.ambientTempC = read_tz(thermal_config.ambient[0]) / thermal_config.ambient[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
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
def setup_eon_fan():
|
|
|
|
os.system("echo 2 > /sys/module/dwc3_msm/parameters/otg_switch")
|
|
|
|
|
2020-03-13 02:55:33 +08:00
|
|
|
|
2021-09-04 08:19:11 +08:00
|
|
|
last_eon_fan_val = None
|
2020-01-18 03:03:22 +08:00
|
|
|
def set_eon_fan(val):
|
2021-03-30 05:37:52 +08:00
|
|
|
global last_eon_fan_val
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
if last_eon_fan_val is None or last_eon_fan_val != val:
|
|
|
|
bus = SMBus(7, force=True)
|
2021-03-30 05:37:52 +08:00
|
|
|
try:
|
|
|
|
i = [0x1, 0x3 | 0, 0x3 | 0x08, 0x3 | 0x10][val]
|
|
|
|
bus.write_i2c_block_data(0x3d, 0, [i])
|
2021-12-25 03:18:39 +08:00
|
|
|
except OSError:
|
2021-03-30 05:37:52 +08:00
|
|
|
# tusb320
|
|
|
|
if val == 0:
|
|
|
|
bus.write_i2c_block_data(0x67, 0xa, [0])
|
|
|
|
else:
|
|
|
|
bus.write_i2c_block_data(0x67, 0xa, [0x20])
|
|
|
|
bus.write_i2c_block_data(0x67, 0x8, [(val - 1) << 6])
|
2020-01-18 03:03:22 +08:00
|
|
|
bus.close()
|
|
|
|
last_eon_fan_val = val
|
|
|
|
|
2020-03-13 02:55:33 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
# temp thresholds to control fan speed - high hysteresis
|
|
|
|
_TEMP_THRS_H = [50., 65., 80., 10000]
|
|
|
|
# temp thresholds to control fan speed - low hysteresis
|
|
|
|
_TEMP_THRS_L = [42.5, 57.5, 72.5, 10000]
|
|
|
|
# fan speed options
|
|
|
|
_FAN_SPEEDS = [0, 16384, 32768, 65535]
|
|
|
|
|
|
|
|
|
2021-09-01 03:29:53 +08:00
|
|
|
def handle_fan_eon(controller, max_cpu_temp, fan_speed, ignition):
|
2020-01-18 03:03:22 +08:00
|
|
|
new_speed_h = next(speed for speed, temp_h in zip(_FAN_SPEEDS, _TEMP_THRS_H) if temp_h > max_cpu_temp)
|
|
|
|
new_speed_l = next(speed for speed, temp_l in zip(_FAN_SPEEDS, _TEMP_THRS_L) if temp_l > max_cpu_temp)
|
|
|
|
|
|
|
|
if new_speed_h > fan_speed:
|
|
|
|
# update speed if using the high thresholds results in fan speed increment
|
|
|
|
fan_speed = new_speed_h
|
|
|
|
elif new_speed_l < fan_speed:
|
|
|
|
# update speed if using the low thresholds results in fan speed decrement
|
|
|
|
fan_speed = new_speed_l
|
|
|
|
|
2020-03-13 02:55:33 +08:00
|
|
|
set_eon_fan(fan_speed // 16384)
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
return fan_speed
|
|
|
|
|
2020-01-24 08:41:29 +08:00
|
|
|
|
2021-09-01 03:29:53 +08:00
|
|
|
def handle_fan_uno(controller, max_cpu_temp, fan_speed, ignition):
|
2020-02-12 06:03:40 +08:00
|
|
|
new_speed = int(interp(max_cpu_temp, [40.0, 80.0], [0, 80]))
|
2020-01-24 08:41:29 +08:00
|
|
|
|
|
|
|
if not ignition:
|
|
|
|
new_speed = min(30, new_speed)
|
|
|
|
|
|
|
|
return new_speed
|
|
|
|
|
2020-03-13 02:55:33 +08:00
|
|
|
|
2021-11-16 00:52:42 +08:00
|
|
|
last_ignition = False
|
2021-09-01 03:29:53 +08:00
|
|
|
def handle_fan_tici(controller, max_cpu_temp, fan_speed, ignition):
|
2021-11-16 00:52:42 +08:00
|
|
|
global last_ignition
|
|
|
|
|
2021-09-01 03:29:53 +08:00
|
|
|
controller.neg_limit = -(80 if ignition else 30)
|
|
|
|
controller.pos_limit = -(30 if ignition else 0)
|
|
|
|
|
2021-11-16 00:52:42 +08:00
|
|
|
if ignition != last_ignition:
|
|
|
|
controller.reset()
|
|
|
|
|
2021-09-01 03:29:53 +08:00
|
|
|
fan_pwr_out = -int(controller.update(
|
2021-12-09 09:46:01 +08:00
|
|
|
setpoint=75,
|
2021-09-01 03:29:53 +08:00
|
|
|
measurement=max_cpu_temp,
|
|
|
|
feedforward=interp(max_cpu_temp, [60.0, 100.0], [0, -80])
|
|
|
|
))
|
|
|
|
|
2021-11-16 00:52:42 +08:00
|
|
|
last_ignition = ignition
|
2021-09-01 03:29:53 +08:00
|
|
|
return fan_pwr_out
|
|
|
|
|
|
|
|
|
2020-10-15 21:47:38 +08:00
|
|
|
def set_offroad_alert_if_changed(offroad_alert: str, show_alert: bool, extra_text: Optional[str]=None):
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-12-29 01:07:00 +08:00
|
|
|
def thermald_thread() -> NoReturn:
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
pm = messaging.PubMaster(['deviceState'])
|
2020-12-30 14:32:03 +08:00
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
pandaState_timeout = int(1000 * 2.5 * DT_TRML) # 2.5x the expected pandaState frequency
|
2021-10-08 23:54:34 +08:00
|
|
|
pandaState_sock = messaging.sub_sock('pandaStates', timeout=pandaState_timeout)
|
2021-10-04 17:30:11 +08:00
|
|
|
sm = messaging.SubMaster(["peripheralState", "gpsLocationExternal", "managerState"])
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
fan_speed = 0
|
|
|
|
count = 0
|
|
|
|
|
2021-12-29 01:07:00 +08:00
|
|
|
onroad_conditions: Dict[str, bool] = {
|
2020-10-15 21:47:38 +08:00
|
|
|
"ignition": False,
|
|
|
|
}
|
2021-12-29 01:07:00 +08:00
|
|
|
startup_conditions: Dict[str, bool] = {}
|
|
|
|
startup_conditions_prev: Dict[str, bool] = {}
|
2020-10-15 21:47:38 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
off_ts = None
|
|
|
|
started_ts = None
|
|
|
|
started_seen = False
|
|
|
|
thermal_status = ThermalStatus.green
|
|
|
|
usb_power = True
|
|
|
|
|
2020-02-04 03:28:38 +08:00
|
|
|
network_type = NetworkType.none
|
2020-03-08 03:14:04 +08:00
|
|
|
network_strength = NetworkStrength.unknown
|
2021-05-17 19:10:08 +08:00
|
|
|
network_info = None
|
2021-07-12 21:52:09 +08:00
|
|
|
modem_version = None
|
2021-06-22 21:29:02 +08:00
|
|
|
registered_count = 0
|
2021-09-22 05:39:07 +08:00
|
|
|
nvme_temps = None
|
2021-09-23 17:33:47 +08:00
|
|
|
modem_temps = None
|
2020-02-04 03:28:38 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
current_filter = FirstOrderFilter(0., CURRENT_TAU, DT_TRML)
|
2021-09-04 08:19:11 +08:00
|
|
|
temp_filter = FirstOrderFilter(0., TEMP_TAU, DT_TRML)
|
2021-02-17 13:39:32 +08:00
|
|
|
pandaState_prev = None
|
2020-01-18 03:03:22 +08:00
|
|
|
should_start_prev = False
|
2021-10-04 17:30:11 +08:00
|
|
|
in_car = False
|
2020-04-08 10:18:14 +08:00
|
|
|
handle_fan = None
|
|
|
|
is_uno = False
|
2021-02-24 22:25:06 +08:00
|
|
|
ui_running_prev = False
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
params = Params()
|
2020-12-30 14:32:03 +08:00
|
|
|
power_monitor = PowerMonitoring()
|
2020-04-21 05:15:01 +08:00
|
|
|
no_panda_cnt = 0
|
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
|
|
|
|
2021-09-01 03:29:53 +08:00
|
|
|
# TODO: use PI controller for UNO
|
|
|
|
controller = PIController(k_p=0, k_i=2e-3, neg_limit=-80, pos_limit=0, rate=(1 / DT_TRML))
|
|
|
|
|
2021-09-23 13:25:08 +08:00
|
|
|
# Leave flag for loggerd to indicate device was left onroad
|
2021-06-05 13:04:26 +08:00
|
|
|
if params.get_bool("IsOnroad"):
|
2021-09-23 13:25:08 +08:00
|
|
|
params.put_bool("BootedOnroad", True)
|
2021-06-05 13:04:26 +08:00
|
|
|
|
2021-10-04 17:30:11 +08:00
|
|
|
while True:
|
2021-10-08 23:54:34 +08:00
|
|
|
pandaStates = messaging.recv_sock(pandaState_sock, wait=True)
|
2021-10-04 17:30:11 +08:00
|
|
|
|
|
|
|
sm.update(0)
|
|
|
|
peripheralState = sm['peripheralState']
|
|
|
|
|
2020-08-24 20:17:41 +08:00
|
|
|
msg = read_thermal(thermal_config)
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-10-08 23:54:34 +08:00
|
|
|
if pandaStates is not None and len(pandaStates.pandaStates) > 0:
|
|
|
|
pandaState = pandaStates.pandaStates[0]
|
2020-04-21 05:15:01 +08:00
|
|
|
|
|
|
|
# If we lose connection to the panda, wait 5 seconds before going offroad
|
2021-10-04 17:30:11 +08:00
|
|
|
if pandaState.pandaType == log.PandaState.PandaType.unknown:
|
2020-04-21 05:15:01 +08:00
|
|
|
no_panda_cnt += 1
|
|
|
|
if no_panda_cnt > DISCONNECT_TIMEOUT / DT_TRML:
|
2021-12-05 14:17:44 +08:00
|
|
|
if onroad_conditions["ignition"]:
|
2020-04-21 05:15:01 +08:00
|
|
|
cloudlog.error("Lost panda connection while onroad")
|
2021-12-05 14:17:44 +08:00
|
|
|
onroad_conditions["ignition"] = False
|
2020-04-21 05:15:01 +08:00
|
|
|
else:
|
|
|
|
no_panda_cnt = 0
|
2021-12-05 14:17:44 +08:00
|
|
|
onroad_conditions["ignition"] = pandaState.ignitionLine or pandaState.ignitionCan
|
2020-04-07 07:01:42 +08:00
|
|
|
|
2021-10-04 17:30:11 +08:00
|
|
|
in_car = pandaState.harnessStatus != log.PandaState.HarnessStatus.notConnected
|
|
|
|
usb_power = peripheralState.usbPowerMode != log.PeripheralState.UsbPowerMode.client
|
2021-03-17 21:19:55 +08:00
|
|
|
|
2020-04-08 10:18:14 +08:00
|
|
|
# Setup fan handler on first connect to panda
|
2021-10-04 17:30:11 +08:00
|
|
|
if handle_fan is None and peripheralState.pandaType != log.PandaState.PandaType.unknown:
|
|
|
|
is_uno = peripheralState.pandaType == log.PandaState.PandaType.uno
|
2020-04-08 10:18:14 +08:00
|
|
|
|
2021-09-01 03:29:53 +08:00
|
|
|
if TICI:
|
|
|
|
cloudlog.info("Setting up TICI fan handler")
|
|
|
|
handle_fan = handle_fan_tici
|
|
|
|
elif is_uno or PC:
|
2020-04-08 10:18:14 +08:00
|
|
|
cloudlog.info("Setting up UNO fan handler")
|
|
|
|
handle_fan = handle_fan_uno
|
|
|
|
else:
|
|
|
|
cloudlog.info("Setting up EON fan handler")
|
|
|
|
setup_eon_fan()
|
|
|
|
handle_fan = handle_fan_eon
|
|
|
|
|
2020-04-07 07:01:42 +08:00
|
|
|
# Handle disconnect
|
2021-02-17 13:39:32 +08:00
|
|
|
if pandaState_prev is not None:
|
2021-10-04 17:30:11 +08:00
|
|
|
if pandaState.pandaType == log.PandaState.PandaType.unknown and \
|
|
|
|
pandaState_prev.pandaType != log.PandaState.PandaType.unknown:
|
2021-05-05 02:32:40 +08:00
|
|
|
params.clear_all(ParamKeyType.CLEAR_ON_PANDA_DISCONNECT)
|
2021-02-17 13:39:32 +08:00
|
|
|
pandaState_prev = pandaState
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-09-22 05:39:07 +08:00
|
|
|
# these are expensive calls. update every 10s
|
2020-02-18 02:38:39 +08:00
|
|
|
if (count % int(10. / DT_TRML)) == 0:
|
2020-02-18 02:37:38 +08:00
|
|
|
try:
|
2020-08-26 20:57:17 +08:00
|
|
|
network_type = HARDWARE.get_network_type()
|
|
|
|
network_strength = HARDWARE.get_network_strength(network_type)
|
2021-05-17 19:10:08 +08:00
|
|
|
network_info = HARDWARE.get_network_info() # pylint: disable=assignment-from-none
|
2021-10-07 11:32:22 +08:00
|
|
|
nvme_temps = HARDWARE.get_nvme_temperatures()
|
2021-09-23 17:33:47 +08:00
|
|
|
modem_temps = HARDWARE.get_modem_temperatures()
|
2021-06-22 21:29:02 +08:00
|
|
|
|
2021-07-12 21:52:09 +08:00
|
|
|
# Log modem version once
|
|
|
|
if modem_version is None:
|
|
|
|
modem_version = HARDWARE.get_modem_version() # pylint: disable=assignment-from-none
|
|
|
|
if modem_version is not None:
|
|
|
|
cloudlog.warning(f"Modem version: {modem_version}")
|
|
|
|
|
2021-06-22 21:29:02 +08:00
|
|
|
if TICI and (network_info.get('state', None) == "REGISTERED"):
|
|
|
|
registered_count += 1
|
|
|
|
else:
|
|
|
|
registered_count = 0
|
|
|
|
|
|
|
|
if registered_count > 10:
|
2021-06-25 17:00:40 +08:00
|
|
|
cloudlog.warning(f"Modem stuck in registered state {network_info}. nmcli conn up lte")
|
|
|
|
os.system("nmcli conn up lte")
|
2021-06-23 18:40:21 +08:00
|
|
|
registered_count = 0
|
|
|
|
|
2020-02-22 07:02:24 +08:00
|
|
|
except Exception:
|
2020-03-08 03:14:04 +08:00
|
|
|
cloudlog.exception("Error getting network status")
|
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-08-20 10:14:51 +08:00
|
|
|
msg.deviceState.cpuUsagePercent = [int(round(n)) for n in psutil.cpu_percent(percpu=True)]
|
2021-06-30 04:55:18 +08:00
|
|
|
msg.deviceState.gpuUsagePercent = int(round(HARDWARE.get_gpu_usage_percent()))
|
2021-02-17 13:39:32 +08:00
|
|
|
msg.deviceState.networkType = network_type
|
|
|
|
msg.deviceState.networkStrength = network_strength
|
2021-05-17 19:10:08 +08:00
|
|
|
if network_info is not None:
|
|
|
|
msg.deviceState.networkInfo = network_info
|
2021-09-22 05:39:07 +08:00
|
|
|
if nvme_temps is not None:
|
|
|
|
msg.deviceState.nvmeTempC = nvme_temps
|
2021-09-23 17:33:47 +08:00
|
|
|
if modem_temps is not None:
|
|
|
|
msg.deviceState.modemTempC = 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()
|
2021-02-17 13:39:32 +08:00
|
|
|
msg.deviceState.batteryPercent = HARDWARE.get_battery_capacity()
|
|
|
|
msg.deviceState.batteryCurrent = HARDWARE.get_battery_current()
|
|
|
|
msg.deviceState.usbOnline = HARDWARE.get_usb_present()
|
|
|
|
current_filter.update(msg.deviceState.batteryCurrent / 1e6)
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-09-04 08:19:11 +08:00
|
|
|
max_comp_temp = temp_filter.update(
|
|
|
|
max(max(msg.deviceState.cpuTempC), msg.deviceState.memoryTempC, max(msg.deviceState.gpuTempC))
|
|
|
|
)
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2020-04-08 10:18:14 +08:00
|
|
|
if handle_fan is not None:
|
2021-12-05 14:17:44 +08:00
|
|
|
fan_speed = handle_fan(controller, max_comp_temp, fan_speed, onroad_conditions["ignition"])
|
2021-02-17 13:39:32 +08:00
|
|
|
msg.deviceState.fanSpeedPercentDesired = fan_speed
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2020-09-01 17:25:04 +08:00
|
|
|
is_offroad_for_5_min = (started_ts is None) and ((not started_seen) or (off_ts is None) or (sec_since_boot() - off_ts > 60 * 5))
|
2021-09-04 08:19:11 +08:00
|
|
|
if is_offroad_for_5_min and max_comp_temp > OFFROAD_DANGER_TEMP:
|
|
|
|
# If device is offroad we want to cool down before going onroad
|
|
|
|
# since going onroad increases load and can make temps go over 107
|
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)
|
|
|
|
if current_band.min_temp is not None and max_comp_temp < current_band.min_temp:
|
|
|
|
thermal_status = list(THERMAL_BANDS.keys())[band_idx - 1]
|
|
|
|
elif current_band.max_temp is not None and max_comp_temp > current_band.max_temp:
|
|
|
|
thermal_status = list(THERMAL_BANDS.keys())[band_idx + 1]
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
# **** starting logic ****
|
|
|
|
|
2021-12-05 14:15:55 +08:00
|
|
|
# Ensure date/time are valid
|
2020-03-27 10:49:58 +08:00
|
|
|
now = datetime.datetime.utcnow()
|
2020-11-20 18:47:39 +08:00
|
|
|
startup_conditions["time_valid"] = (now.year > 2020) or (now.year == 2020 and now.month >= 10)
|
2020-10-15 21:47:38 +08:00
|
|
|
set_offroad_alert_if_changed("Offroad_InvalidTime", (not startup_conditions["time_valid"]))
|
2020-01-18 03:03:22 +08:00
|
|
|
|
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
|
2020-11-04 11:56:25 +08:00
|
|
|
startup_conditions["completed_training"] = params.get("CompletedTrainingVersion") == training_version or \
|
2021-06-19 07:01:22 +08:00
|
|
|
params.get_bool("Passive")
|
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")
|
2020-01-18 03:03:22 +08:00
|
|
|
# if any CPU gets above 107 or the battery gets above 63, kill all processes
|
|
|
|
# controls will warn with CPU above 95 or battery above 60
|
2021-12-05 14:17:44 +08:00
|
|
|
onroad_conditions["device_temp_good"] = thermal_status < ThermalStatus.danger
|
|
|
|
set_offroad_alert_if_changed("Offroad_TemperatureTooHigh", (not onroad_conditions["device_temp_good"]))
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-05-18 04:35:24 +08:00
|
|
|
if TICI:
|
2021-10-27 10:34:08 +08:00
|
|
|
set_offroad_alert_if_changed("Offroad_StorageMissing", (not Path("/data/media").is_mount()))
|
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):
|
2021-06-05 13:04:26 +08:00
|
|
|
params.put_bool("IsOnroad", should_start)
|
2021-04-12 16:40:58 +08:00
|
|
|
params.put_bool("IsOffroad", not should_start)
|
|
|
|
HARDWARE.set_power_save(not should_start)
|
2020-01-18 03:03:22 +08:00
|
|
|
|
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:
|
|
|
|
started_ts = sec_since_boot()
|
|
|
|
started_seen = True
|
|
|
|
else:
|
2021-12-05 14:17:44 +08:00
|
|
|
if onroad_conditions["ignition"] and (startup_conditions != startup_conditions_prev):
|
|
|
|
cloudlog.event("Startup blocked", startup_conditions=startup_conditions, onroad_conditions=onroad_conditions)
|
2020-12-30 14:32:03 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
started_ts = None
|
|
|
|
if off_ts is None:
|
|
|
|
off_ts = sec_since_boot()
|
|
|
|
|
2020-03-11 13:18:48 +08:00
|
|
|
# Offroad power monitoring
|
2021-12-05 14:17:44 +08:00
|
|
|
power_monitor.calculate(peripheralState, 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())
|
2021-11-24 08:08:20 +08:00
|
|
|
current_power_draw = HARDWARE.get_current_power_draw() # pylint: disable=assignment-from-none
|
|
|
|
msg.deviceState.powerDrawW = current_power_draw if current_power_draw is not None else 0
|
2020-08-17 17:56:27 +08:00
|
|
|
|
|
|
|
# Check if we need to disable charging (handled by boardd)
|
2021-12-05 14:17:44 +08:00
|
|
|
msg.deviceState.chargingDisabled = power_monitor.should_disable_charging(onroad_conditions["ignition"], in_car, off_ts)
|
2020-08-17 17:56:27 +08:00
|
|
|
|
|
|
|
# Check if we need to shut down
|
2021-12-05 14:17:44 +08:00
|
|
|
if power_monitor.should_shutdown(peripheralState, onroad_conditions["ignition"], in_car, off_ts, started_seen):
|
2020-08-19 02:52:01 +08:00
|
|
|
cloudlog.info(f"shutting device down, offroad since {off_ts}")
|
|
|
|
# TODO: add function for blocking cloudlog instead of sleep
|
|
|
|
time.sleep(10)
|
2021-02-24 00:04:10 +08:00
|
|
|
HARDWARE.shutdown()
|
2020-03-11 13:18:48 +08:00
|
|
|
|
2021-02-24 22:25:06 +08:00
|
|
|
# If UI has crashed, set the brightness to reasonable non-zero value
|
2021-10-04 17:30:11 +08:00
|
|
|
ui_running = "ui" in (p.name for p in sm["managerState"].processes if p.running)
|
|
|
|
if ui_running_prev and not ui_running:
|
|
|
|
HARDWARE.set_screen_brightness(20)
|
|
|
|
ui_running_prev = ui_running
|
2021-02-24 22:25:06 +08:00
|
|
|
|
2021-02-17 13:39:32 +08:00
|
|
|
msg.deviceState.chargingError = current_filter.x > 0. and msg.deviceState.batteryPercent < 90 # if current is positive, then battery is being discharged
|
|
|
|
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
|
|
|
|
2021-03-09 09:00:58 +08:00
|
|
|
if EON and not is_uno:
|
|
|
|
set_offroad_alert_if_changed("Offroad_ChargeDisabled", (not usb_power))
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
should_start_prev = should_start
|
2020-10-21 21:14:29 +08:00
|
|
|
startup_conditions_prev = startup_conditions.copy()
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-04-09 13:50:11 +08:00
|
|
|
# report to server once every 10 minutes
|
|
|
|
if (count % int(600. / DT_TRML)) == 0:
|
2021-04-28 15:09:03 +08:00
|
|
|
if EON and started_ts is None and msg.deviceState.memoryUsagePercent > 40:
|
|
|
|
cloudlog.event("High offroad memory usage", mem=msg.deviceState.memoryUsagePercent)
|
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
cloudlog.event("STATUS_PACKET",
|
2020-03-13 02:55:33 +08:00
|
|
|
count=count,
|
2021-10-08 23:54:34 +08:00
|
|
|
pandaStates=(strip_deprecated_keys(pandaStates.to_dict()) if pandaStates else None),
|
2021-10-04 17:30:11 +08:00
|
|
|
peripheralState=strip_deprecated_keys(peripheralState.to_dict()),
|
|
|
|
location=(strip_deprecated_keys(sm["gpsLocationExternal"].to_dict()) if sm.alive["gpsLocationExternal"] else None),
|
2021-03-26 04:30:09 +08:00
|
|
|
deviceState=strip_deprecated_keys(msg.to_dict()))
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
2021-12-29 01:07:00 +08:00
|
|
|
def main() -> NoReturn:
|
2020-01-18 03:03:22 +08:00
|
|
|
thermald_thread()
|
|
|
|
|
2020-03-13 02:55:33 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|