Skip to content

Commit

Permalink
Move thermald hardware calls into HW abstraction layer
Browse files Browse the repository at this point in the history
* abstracted away hardware calls

* oopsie

* remove bugs

* remove bugs #2

* fix unit test

* removed print

Co-authored-by: Comma Device <device@comma.ai>
  • Loading branch information
2 people authored and sshane committed Jan 17, 2021
1 parent 0f2eb5a commit 2599563
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 22 deletions.
24 changes: 24 additions & 0 deletions common/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ def get_sim_info(self):
def get_network_strength(self, network_type):
return NetworkStrength.unknown

def get_battery_capacity(self):
return 100

def get_battery_status(self):
return ""

def get_battery_current(self):
return 0

def get_battery_voltage(self):
return 0

def get_battery_charging(self):
return True

def set_battery_charging(self, on):
pass

def get_usb_present(self):
return False

def get_current_power_draw(self):
return 0


if EON:
HARDWARE = cast(HardwareBase, Android())
Expand Down
29 changes: 29 additions & 0 deletions common/hardware_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,32 @@ def get_cdma_level(cdmadbm, cdmaecio):
network_strength = max(network_strength, ns)

return network_strength

def get_battery_capacity(self):
return self.read_param_file("/sys/class/power_supply/battery/capacity", int, 100)

def get_battery_status(self):
# This does not correspond with actual charging or not.
# If a USB cable is plugged in, it responds with 'Charging', even when charging is disabled
return self.read_param_file("/sys/class/power_supply/battery/status", lambda x: x.strip(), '')

def get_battery_current(self):
return self.read_param_file("/sys/class/power_supply/battery/current_now", int)

def get_battery_voltage(self):
return self.read_param_file("/sys/class/power_supply/battery/voltage_now", int)

def get_battery_charging(self):
# This does correspond with actually charging
return self.read_param_file("/sys/class/power_supply/battery/charge_type", lambda x: x.strip() != "N/A", True)

def set_battery_charging(self, on):
with open('/sys/class/power_supply/battery/charging_enabled', 'w') as f:
f.write(f"{1 if on else 0}\n")

def get_usb_present(self):
return self.read_param_file("/sys/class/power_supply/usb/present", lambda x: bool(int(x)), False)

def get_current_power_draw(self):
# We don't have a good direct way to measure this on android
return None
40 changes: 40 additions & 0 deletions common/hardware_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def get_cmdline():
cmdline = f.read()
return {kv[0]: kv[1] for kv in [s.split('=') for s in cmdline.split(' ')] if len(kv) == 2}

@staticmethod
def read_param_file(path, parser, default=0):
try:
with open(path) as f:
return parser(f.read())
except Exception:
return default

@abstractmethod
def get_sound_card_online(self):
pass
Expand Down Expand Up @@ -39,3 +47,35 @@ def get_sim_info(self):
@abstractmethod
def get_network_strength(self, network_type):
pass

@abstractmethod
def get_battery_capacity(self):
pass

@abstractmethod
def get_battery_status(self):
pass

@abstractmethod
def get_battery_current(self):
pass

@abstractmethod
def get_battery_voltage(self):
pass

@abstractmethod
def get_battery_charging(self):
pass

@abstractmethod
def set_battery_charging(self, on):
pass

@abstractmethod
def get_usb_present(self):
pass

@abstractmethod
def get_current_power_draw(self):
pass
26 changes: 26 additions & 0 deletions common/hardware_tici.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ def get_sim_info(self):

def get_network_strength(self, network_type):
return NetworkStrength.unknown

# We don't have a battery, so let's use some sane constants
def get_battery_capacity(self):
return 100

def get_battery_status(self):
return ""

def get_battery_current(self):
return 0

def get_battery_voltage(self):
return 0

def get_battery_charging(self):
return True

def set_battery_charging(self, on):
pass

def get_usb_present(self):
# Not sure if relevant on tici, but the file exists
return self.read_param_file("/sys/class/power_supply/usb/present", lambda x: bool(int(x)), False)

def get_current_power_draw(self):
return (self.read_param_file("/sys/class/hwmon/hwmon1/power1_input", int) / 1e6)
25 changes: 14 additions & 11 deletions selfdrive/thermald/power_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from cereal import log
from common.realtime import sec_since_boot
from common.params import Params, put_nonblocking
from common.hardware import TICI
from common.hardware import HARDWARE
from selfdrive.swaglog import cloudlog

PANDA_OUTPUT_VOLTAGE = 5.28
Expand Down Expand Up @@ -127,42 +127,45 @@ def calculate(self, health):
# No ignition, we integrate the offroad power used by the device
is_uno = health.health.hwType == log.HealthData.HwType.uno
# Get current power draw somehow
current_power = 0
if TICI:
with open("/sys/class/hwmon/hwmon1/power1_input") as f:
current_power = int(f.read()) / 1e6
elif get_battery_status() == 'Discharging':
current_power = HARDWARE.get_current_power_draw()
if current_power is not None:
pass
elif HARDWARE.get_battery_status() == 'Discharging':
# If the battery is discharging, we can use this measurement
# On C2: this is low by about 10-15%, probably mostly due to UNO draw not being factored in
<<<<<<< HEAD
current_power = ((get_battery_voltage() / 1000000) * (get_battery_current() / 1000000))
elif (health.health.hwType in [log.HealthData.HwType.whitePanda, log.HealthData.HwType.greyPanda]) and (health.health.current > 1):
# If white/grey panda, use the integrated current measurements if the measurement is not 0
# If the measurement is 0, the current is 400mA or greater, and out of the measurement range of the panda
# This seems to be accurate to about 5%
current_power = (PANDA_OUTPUT_VOLTAGE * panda_current_to_actual_current(health.health.current))
=======
current_power = ((HARDWARE.get_battery_voltage() / 1000000) * (HARDWARE.get_battery_current() / 1000000))
>>>>>>> e64484ae... Move thermald hardware calls into HW abstraction layer (#2630)
elif (self.next_pulsed_measurement_time is not None) and (self.next_pulsed_measurement_time <= now):
# TODO: Figure out why this is off by a factor of 3/4???
FUDGE_FACTOR = 1.33

# Turn off charging for about 10 sec in a thread that does not get killed on SIGINT, and perform measurement here to avoid blocking thermal
def perform_pulse_measurement(now):
try:
set_battery_charging(False)
HARDWARE.set_battery_charging(False)
time.sleep(5)

# Measure for a few sec to get a good average
voltages = []
currents = []
for _ in range(6):
voltages.append(get_battery_voltage())
currents.append(get_battery_current())
voltages.append(HARDWARE.get_battery_voltage())
currents.append(HARDWARE.get_battery_current())
time.sleep(1)
current_power = ((mean(voltages) / 1000000) * (mean(currents) / 1000000))

self._perform_integration(now, current_power * FUDGE_FACTOR)

# Enable charging again
set_battery_charging(True)
HARDWARE.set_battery_charging(True)
except Exception:
cloudlog.exception("Pulsed power measurement failed")

Expand Down Expand Up @@ -233,6 +236,6 @@ def should_shutdown(self, health, offroad_timestamp, started_seen, LEON):
should_shutdown = False
# Wait until we have shut down charging before powering down
should_shutdown |= (not panda_charging and self.should_disable_charging(health, offroad_timestamp))
should_shutdown |= ((get_battery_capacity() < BATT_PERC_OFF) and (not get_battery_charging()) and ((now - offroad_timestamp) > 60))
should_shutdown |= ((HARDWARE.get_battery_capacity() < BATT_PERC_OFF) and (not HARDWARE.get_battery_charging()) and ((now - offroad_timestamp) > 60))
should_shutdown &= started_seen
return should_shutdown
17 changes: 6 additions & 11 deletions selfdrive/thermald/thermald.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
from selfdrive.loggerd.config import get_available_percent
from selfdrive.pandad import get_expected_signature
from selfdrive.swaglog import cloudlog
from selfdrive.thermald.power_monitoring import (PowerMonitoring,
get_battery_capacity,
get_battery_current,
get_battery_status,
get_battery_voltage,
get_usb_present)
from selfdrive.thermald.power_monitoring import PowerMonitoring
from selfdrive.version import get_git_branch, terms_version, training_version

ThermalConfig = namedtuple('ThermalConfig', ['cpu', 'gpu', 'mem', 'bat', 'ambient'])
Expand Down Expand Up @@ -259,11 +254,11 @@ def thermald_thread():
msg.thermal.cpuPerc = int(round(psutil.cpu_percent()))
msg.thermal.networkType = network_type
msg.thermal.networkStrength = network_strength
msg.thermal.batteryPercent = get_battery_capacity()
msg.thermal.batteryStatus = get_battery_status()
msg.thermal.batteryCurrent = get_battery_current()
msg.thermal.batteryVoltage = get_battery_voltage()
msg.thermal.usbOnline = get_usb_present()
msg.thermal.batteryPercent = HARDWARE.get_battery_capacity()
msg.thermal.batteryStatus = HARDWARE.get_battery_status()
msg.thermal.batteryCurrent = HARDWARE.get_battery_current()
msg.thermal.batteryVoltage = HARDWARE.get_battery_voltage()
msg.thermal.usbOnline = HARDWARE.get_usb_present()

# Fake battery levels on uno for frame
if (not EON) or is_uno:
Expand Down

0 comments on commit 2599563

Please sign in to comment.