Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cereal/messaging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from cereal import log
from cereal.services import SERVICE_LIST
from openpilot.common.util import MovingAverage
from openpilot.common.utils import MovingAverage

NO_TRAVERSAL_LIMIT = 2**64-1

Expand Down
2 changes: 1 addition & 1 deletion common/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from setproctitle import getproctitle

from openpilot.common.util import MovingAverage
from openpilot.common.utils import MovingAverage
from openpilot.system.hardware import PC


Expand Down
46 changes: 0 additions & 46 deletions common/util.py

This file was deleted.

48 changes: 48 additions & 0 deletions common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,57 @@
LOG_COMPRESSION_LEVEL = 10 # little benefit up to level 15. level ~17 is a small step change


def sudo_write(val: str, path: str) -> None:
try:
with open(path, 'w') as f:
f.write(str(val))
except PermissionError:
os.system(f"sudo chmod a+w {path}")
try:
with open(path, 'w') as f:
f.write(str(val))
except PermissionError:
# fallback for debugfs files
os.system(f"sudo su -c 'echo {val} > {path}'")


def sudo_read(path: str) -> str:
try:
return subprocess.check_output(f"sudo cat {path}", shell=True, encoding='utf8').strip()
except Exception:
return ""


class MovingAverage:
def __init__(self, window_size: int):
self.window_size: int = window_size
self.buffer: list[float] = [0.0] * window_size
self.index: int = 0
self.count: int = 0
self.sum: float = 0.0

def add_value(self, new_value: float):
# Update the sum: subtract the value being replaced and add the new value
self.sum -= self.buffer[self.index]
self.buffer[self.index] = new_value
self.sum += new_value

# Update the index in a circular manner
self.index = (self.index + 1) % self.window_size

# Track the number of added values (for partial windows)
self.count = min(self.count + 1, self.window_size)

def get_average(self) -> float:
if self.count == 0:
return float('nan')
return self.sum / self.count


class CallbackReader:
"""Wraps a file, but overrides the read method to also
call a callback function with the number of bytes read so far."""

def __init__(self, f, callback, *args):
self.f = f
self.callback = callback
Expand Down
2 changes: 1 addition & 1 deletion system/hardware/tici/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path

from cereal import log
from openpilot.common.util import sudo_read, sudo_write
from openpilot.common.utils import sudo_read, sudo_write
from openpilot.common.gpio import gpio_set, gpio_init, get_irqs_for_action
from openpilot.system.hardware.base import HardwareBase, LPABase, ThermalConfig, ThermalZone
from openpilot.system.hardware.tici import iwlist
Expand Down
2 changes: 1 addition & 1 deletion system/sensord/sensord.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import cereal.messaging as messaging
from cereal.services import SERVICE_LIST
from openpilot.common.util import sudo_write
from openpilot.common.utils import sudo_write
from openpilot.common.realtime import config_realtime_process, Ratekeeper
from openpilot.common.swaglog import cloudlog
from openpilot.common.gpio import gpiochip_get_ro_value_fd, gpioevent_data
Expand Down
Loading