From bcdf5dfa0b86145d048ee2cc7b88fe37ed90f653 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 8 Feb 2024 21:37:36 -0800 Subject: [PATCH] SubMaster cleanup --- messaging/__init__.py | 26 ++++++++++++-------------- messaging/tests/test_pub_sub_master.py | 8 ++++---- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/messaging/__init__.py b/messaging/__init__.py index 19152a4..5a6d26f 100644 --- a/messaging/__init__.py +++ b/messaging/__init__.py @@ -117,14 +117,14 @@ def recv_sock(sock: SubSocket, wait: bool = False) -> Optional[capnp.lib.capnp._ while 1: if wait and dat is None: - rcv = sock.receive() + recv = sock.receive() else: - rcv = sock.receive(non_blocking=True) + recv = sock.receive(non_blocking=True) - if rcv is None: # Timeout hit + if recv is None: # Timeout hit break - dat = rcv + dat = recv if dat is not None: dat = log_from_bytes(dat) @@ -160,8 +160,8 @@ class SubMaster: ignore_valid: Optional[List[str]] = None, addr: str = "127.0.0.1"): self.frame = -1 self.updated = {s: False for s in services} - self.rcv_time = {s: 0. for s in services} - self.rcv_frame = {s: 0 for s in services} + self.recv_time = {s: 0. for s in services} + self.recv_frame = {s: 0 for s in services} self.alive = {s: False for s in services} self.freq_ok = {s: False for s in services} self.recv_dts: Dict[str, Deque[float]] = {s: deque(maxlen=AVG_FREQ_HISTORY) for s in services} @@ -200,7 +200,7 @@ class SubMaster: return self.data[s] def _check_avg_freq(self, s): - return self.rcv_time[s] > 1e-5 and self.freq[s] > 1e-5 and (s not in self.non_polled_services) \ + return self.recv_time[s] > 1e-5 and self.freq[s] > 1e-5 and (s not in self.non_polled_services) \ and (s not in self.ignore_average_freq) def update(self, timeout: int = 1000) -> None: @@ -223,11 +223,9 @@ class SubMaster: s = msg.which() self.updated[s] = True - if self._check_avg_freq(s): - self.recv_dts[s].append(cur_time - self.rcv_time[s]) - - self.rcv_time[s] = cur_time - self.rcv_frame[s] = self.frame + self.recv_dts[s].append(cur_time - self.recv_time[s]) + self.recv_time[s] = cur_time + self.recv_frame[s] = self.frame self.data[s] = getattr(msg, s) self.logMonoTime[s] = msg.logMonoTime self.valid[s] = msg.valid @@ -241,7 +239,7 @@ class SubMaster: # arbitrary small number to avoid float comparison. If freq is 0, we can skip the check if self.freq[s] > 1e-5: # alive if delay is within 10x the expected frequency - self.alive[s] = (cur_time - self.rcv_time[s]) < (10. / self.freq[s]) + self.alive[s] = (cur_time - self.recv_time[s]) < (10. / self.freq[s]) # TODO: check if update frequency is high enough to not drop messages # freq_ok if average frequency is higher than 90% of expected frequency @@ -266,7 +264,7 @@ class SubMaster: def all_freq_ok(self, service_list: Optional[List[str]] = None) -> bool: if service_list is None: # check all service_list = list(self.alive.keys()) - return all(self.freq_ok[s] for s in service_list if s not in self.ignore_alive) + return all(self.freq_ok[s] for s in service_list if s not in self.ignore_average_freq) def all_valid(self, service_list: Optional[List[str]] = None) -> bool: if service_list is None: # check all diff --git a/messaging/tests/test_pub_sub_master.py b/messaging/tests/test_pub_sub_master.py index db79402..8d4dfbe 100755 --- a/messaging/tests/test_pub_sub_master.py +++ b/messaging/tests/test_pub_sub_master.py @@ -19,7 +19,7 @@ class TestSubMaster(unittest.TestCase): def test_init(self): sm = messaging.SubMaster(events) - for p in [sm.updated, sm.rcv_time, sm.rcv_frame, sm.alive, + for p in [sm.updated, sm.recv_time, sm.recv_frame, sm.alive, sm.sock, sm.freq, sm.data, sm.logMonoTime, sm.valid]: self.assertEqual(len(cast(Sized, p)), len(events)) @@ -29,11 +29,11 @@ class TestSubMaster(unittest.TestCase): self.assertEqual(sm.frame, -1) self.assertFalse(any(sm.updated.values())) self.assertFalse(any(sm.alive.values())) - self.assertTrue(all(t == 0. for t in sm.rcv_time.values())) - self.assertTrue(all(f == 0 for f in sm.rcv_frame.values())) + self.assertTrue(all(t == 0. for t in sm.recv_time.values())) + self.assertTrue(all(f == 0 for f in sm.recv_frame.values())) self.assertTrue(all(t == 0 for t in sm.logMonoTime.values())) - for p in [sm.updated, sm.rcv_time, sm.rcv_frame, sm.alive, + for p in [sm.updated, sm.recv_time, sm.recv_frame, sm.alive, sm.sock, sm.freq, sm.data, sm.logMonoTime, sm.valid]: self.assertEqual(len(cast(Sized, p)), len(socks))