Skip to content

Commit

Permalink
dd
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Aug 1, 2024
1 parent c49fae1 commit 37e2ea9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 51 deletions.
3 changes: 1 addition & 2 deletions opendbc/can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class CANParser {
private:
const int bus;
const DBC *dbc = NULL;
std::unordered_map<uint32_t, MessageState> message_states;

public:
bool can_valid = false;
Expand All @@ -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<uint32_t, MessageState> message_states;

CANParser(int abus, const std::string& dbc_name,
const std::vector<std::pair<uint32_t, int>> &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<uint32_t> update(const std::vector<CanData> &can_data, bool sendcan);

protected:
Expand Down
2 changes: 1 addition & 1 deletion opendbc/can/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 20 additions & 48 deletions opendbc/can/parser_pyx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = <unicode>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):
Expand Down

0 comments on commit 37e2ea9

Please sign in to comment.