2020-01-18 02:28:44 +08:00
|
|
|
"""Utilities for reading real time clocks and keeping soft real time constraints."""
|
2020-09-17 06:33:12 +08:00
|
|
|
import gc
|
|
|
|
import os
|
2020-01-18 02:28:44 +08:00
|
|
|
import time
|
|
|
|
import multiprocessing
|
|
|
|
|
2020-11-30 18:21:06 +08:00
|
|
|
from common.hardware import PC, TICI
|
2020-11-12 04:14:51 +08:00
|
|
|
from common.clock import sec_since_boot # pylint: disable=no-name-in-module, import-error
|
2020-01-18 02:28:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
# time step for each process
|
|
|
|
DT_CTRL = 0.01 # controlsd
|
|
|
|
DT_MDL = 0.05 # model
|
|
|
|
DT_TRML = 0.5 # thermald and manager
|
|
|
|
|
2020-11-30 18:21:06 +08:00
|
|
|
# driver monitoring
|
|
|
|
if TICI:
|
|
|
|
DT_DMON = 0.05
|
|
|
|
else:
|
|
|
|
DT_DMON = 0.1
|
|
|
|
|
2020-01-18 02:28:44 +08:00
|
|
|
|
2020-09-17 06:33:12 +08:00
|
|
|
class Priority:
|
|
|
|
MIN_REALTIME = 52 # highest android process priority is 51
|
|
|
|
CTRL_LOW = MIN_REALTIME
|
|
|
|
CTRL_HIGH = MIN_REALTIME + 1
|
2020-06-06 05:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
def set_realtime_priority(level):
|
2020-09-17 06:33:12 +08:00
|
|
|
if not PC:
|
|
|
|
os.sched_setscheduler(0, os.SCHED_FIFO, os.sched_param(level))
|
2020-06-06 05:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
def set_core_affinity(core):
|
2020-09-17 06:33:12 +08:00
|
|
|
if not PC:
|
|
|
|
os.sched_setaffinity(0, [core,])
|
|
|
|
|
|
|
|
|
2020-10-03 08:18:18 +08:00
|
|
|
def config_realtime_process(core, priority):
|
2020-09-17 06:33:12 +08:00
|
|
|
gc.disable()
|
|
|
|
set_realtime_priority(priority)
|
2020-09-18 06:49:14 +08:00
|
|
|
set_core_affinity(core)
|
2020-01-18 02:28:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Ratekeeper():
|
|
|
|
def __init__(self, rate, print_delay_threshold=0.):
|
|
|
|
"""Rate in Hz for ratekeeping. print_delay_threshold must be nonnegative."""
|
|
|
|
self._interval = 1. / rate
|
|
|
|
self._next_frame_time = sec_since_boot() + self._interval
|
|
|
|
self._print_delay_threshold = print_delay_threshold
|
|
|
|
self._frame = 0
|
|
|
|
self._remaining = 0
|
|
|
|
self._process_name = multiprocessing.current_process().name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def frame(self):
|
|
|
|
return self._frame
|
|
|
|
|
|
|
|
@property
|
|
|
|
def remaining(self):
|
|
|
|
return self._remaining
|
|
|
|
|
|
|
|
# Maintain loop rate by calling this at the end of each loop
|
|
|
|
def keep_time(self):
|
|
|
|
lagged = self.monitor_time()
|
|
|
|
if self._remaining > 0:
|
|
|
|
time.sleep(self._remaining)
|
|
|
|
return lagged
|
|
|
|
|
|
|
|
# this only monitor the cumulative lag, but does not enforce a rate
|
|
|
|
def monitor_time(self):
|
|
|
|
lagged = False
|
|
|
|
remaining = self._next_frame_time - sec_since_boot()
|
|
|
|
self._next_frame_time += self._interval
|
|
|
|
if self._print_delay_threshold is not None and remaining < -self._print_delay_threshold:
|
|
|
|
print("%s lagging by %.2f ms" % (self._process_name, -remaining * 1000))
|
|
|
|
lagged = True
|
|
|
|
self._frame += 1
|
|
|
|
self._remaining = remaining
|
|
|
|
return lagged
|