From 37e2ea9cf3e3cbf6988124da08ec6bb52ebe25e7 Mon Sep 17 00:00:00 2001 From: deanlee Date: Thu, 1 Aug 2024 16:03:31 +0800 Subject: [PATCH] dd --- opendbc/can/common.h | 3 +- opendbc/can/common.pxd | 2 +- opendbc/can/parser_pyx.pyx | 68 +++++++++++--------------------------- 3 files changed, 22 insertions(+), 51 deletions(-) diff --git a/opendbc/can/common.h b/opendbc/can/common.h index 354d485af7..e9ddca969e 100644 --- a/opendbc/can/common.h +++ b/opendbc/can/common.h @@ -65,7 +65,6 @@ class CANParser { private: const int bus; const DBC *dbc = NULL; - std::unordered_map message_states; public: bool can_valid = false; @@ -75,11 +74,11 @@ class CANParser { uint64_t last_nonempty_nanos = 0; uint64_t bus_timeout_threshold = 0; uint64_t can_invalid_cnt = CAN_INVALID_CNT; + std::unordered_map message_states; CANParser(int abus, const std::string& dbc_name, const std::vector> &messages); CANParser(int abus, const std::string& dbc_name, bool ignore_checksum, bool ignore_counter); - MessageState *messageState(uint32_t address) { return &message_states.at(address); } std::set update(const std::vector &can_data, bool sendcan); protected: diff --git a/opendbc/can/common.pxd b/opendbc/can/common.pxd index 284b8657f4..10508afa28 100644 --- a/opendbc/can/common.pxd +++ b/opendbc/can/common.pxd @@ -82,8 +82,8 @@ cdef extern from "common.h": cdef cppclass CANParser: bool can_valid bool bus_timeout + unordered_map[uint32_t, MessageState] message_states CANParser(int, string, vector[pair[uint32_t, int]]) except + - MessageState *messageState(uint32_t address) set[uint32_t] update(vector[CanData]&, bool) except + cdef cppclass CANPacker: diff --git a/opendbc/can/parser_pyx.pyx b/opendbc/can/parser_pyx.pyx index 4fe44130b5..55ca535fa7 100644 --- a/opendbc/can/parser_pyx.pyx +++ b/opendbc/can/parser_pyx.pyx @@ -7,52 +7,10 @@ from libcpp.vector cimport vector from libc.stdint cimport uint32_t from .common cimport CANParser as cpp_CANParser -from .common cimport MessageState as cpp_MessageState from .common cimport dbc_lookup, DBC, CanData, CanFrame import numbers from collections import defaultdict -from collections.abc import Mapping - -cdef class MessageState: - cdef cpp_MessageState *state - cdef list signal_names - - @property - def names(self): - return self.signal_names - - def value(self, name): - return self.state.values.at(name).value - - def all_values(self, name): - return self.state.values.at(name).all_values - - def ts_nanos(self, name): - return self.state.values.at(name).ts_nanos - - @staticmethod - cdef create(cpp_MessageState *s): - state = MessageState() - state.state = s - state.signal_names = [it.first.decode("utf-8") for it in s.values] - return state - - -class ValueDict(Mapping): - def __init__(self, MessageState state, fetch_func): - self.state = state - self.fetch_func = fetch_func - - def __getitem__(self, key): - return self.fetch_func(self.state, key) - - def __iter__(self): - return iter(self.state.names) - - def __len__(self): - return len(self.state.names) - cdef class CANParser: cdef: @@ -88,14 +46,14 @@ cdef class CANParser: self.can = new cpp_CANParser(bus, dbc_name, message_v) - # Populate dictionaries with ValueDict + # Populate value dictionaries + self._update() for address, _ in message_v: m = self.dbc.addr_to_msg.at(address) name = m.name.decode("utf8") - state = MessageState.create(self.can.messageState(address)) - self.vl[name] = self.vl[address] = ValueDict(state, MessageState.value) - self.vl_all[name] = self.vl_all[address] = ValueDict(state, MessageState.all_values) - self.ts_nanos[name] = self.ts_nanos[address] = ValueDict(state, MessageState.ts_nanos) + self.vl[name] = self.vl[address] + self.vl_all[name] = self.vl_all[address] + self.ts_nanos[name] = self.ts_nanos[address] def __dealloc__(self): if self.can: @@ -127,7 +85,21 @@ cdef class CANParser: except TypeError: raise RuntimeError("invalid parameter") - return self.can.update(can_data_array, sendcan) + update_addresses = self.can.update(can_data_array, sendcan) + self._update() + return update_addresses + + cdef _update(self): + for state in self.can.message_states: + address = state.first + vl = self.vl.setdefault(address, {}) + vl_all = self.vl_all.setdefault(address, {}) + ts_nanos = self.ts_nanos.setdefault(address, {}) + for v in state.second.values: + name = v.first + vl[name] = v.second.value + vl_all[name] = v.second.all_values + ts_nanos[name] = v.second.ts_nanos @property def can_valid(self):