Skip to content

Commit

Permalink
faster update_strings
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Mar 26, 2023
1 parent 464f13c commit 6cd35ce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ class CANParser {
CANParser(int abus, const std::string& dbc_name, bool ignore_checksum, bool ignore_counter);
#ifndef DYNAMIC_CAPNP
void update_string(const std::string &data, bool sendcan);
void update_strings(const std::vector<std::string> &data, std::vector<SignalValue> &vals, bool sendcan);
void UpdateCans(uint64_t sec, const capnp::List<cereal::CanData>::Reader& cans);
#endif
void UpdateCans(uint64_t sec, const capnp::DynamicStruct::Reader& cans);
void UpdateValid(uint64_t sec);
void query_latest(std::vector<SignalValue> &vals);
void query_latest(std::vector<SignalValue> &vals, uint64_t last_ts = 0);
};

class CANPacker {
Expand Down
1 change: 1 addition & 0 deletions can/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ cdef extern from "common.h":
bool bus_timeout
CANParser(int, string, vector[MessageParseOptions], vector[SignalParseOptions])
void update_string(string&, bool)
void update_strings(vector[string]&, vector[SignalValue]&, bool)
void query_latest(vector[SignalValue]&)

cdef cppclass CANPacker:
Expand Down
16 changes: 14 additions & 2 deletions can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ void CANParser::update_string(const std::string &data, bool sendcan) {
UpdateValid(last_sec);
}

void CANParser::update_strings(const std::vector<std::string> &data, std::vector<SignalValue> &vals, bool sendcan) {
uint64_t current_sec = 0;
for (const auto &d : data) {
update_string(d, sendcan);
if (current_sec == 0) current_sec = last_sec;
}
query_latest(vals, current_sec);
}

void CANParser::UpdateCans(uint64_t sec, const capnp::List<cereal::CanData>::Reader& cans) {
//DEBUG("got %d messages\n", cans.size());

Expand Down Expand Up @@ -298,10 +307,13 @@ void CANParser::UpdateValid(uint64_t sec) {
can_valid = (can_invalid_cnt < CAN_INVALID_CNT) && _counters_valid;
}

void CANParser::query_latest(std::vector<SignalValue> &vals) {
void CANParser::query_latest(std::vector<SignalValue> &vals, uint64_t last_ts) {
if (last_ts == 0) {
last_ts = last_sec;
}
for (auto& kv : message_states) {
auto& state = kv.second;
if (last_sec != 0 && state.last_seen_nanos != last_sec) continue;
if (last_ts != 0 && state.last_seen_nanos < last_ts) continue;

for (int i = 0; i < state.parse_sigs.size(); i++) {
const Signal &sig = state.parse_sigs[i];
Expand Down
17 changes: 8 additions & 9 deletions can/parser_pyx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ cdef class CANParser:
message_options_v.push_back(mpo)

self.can = new cpp_CANParser(bus, dbc_name, message_options_v, signal_options_v)
self.update_vl()
self.update_strings({})

cdef unordered_set[uint32_t] update_vl(self):
cdef unordered_set[uint32_t] update_vl(self, vector[SignalValue]& new_vals):
cdef unordered_set[uint32_t] updated_addrs
cdef vector[SignalValue] new_vals
self.can.query_latest(new_vals)
for cv in new_vals:
# Cast char * directly to unicode
cv_name = <unicode>cv.name
Expand All @@ -121,18 +119,19 @@ cdef class CANParser:
for v in self.vl_all.values():
v.clear()

cdef vector[SignalValue] new_vals
self.can.update_string(dat, sendcan)
return self.update_vl()
self.can.query_latest(new_vals)
return self.update_vl(new_vals)

def update_strings(self, strings, sendcan=False):
for v in self.vl_all.values():
v.clear()

updated_addrs = set()
for s in strings:
self.can.update_string(s, sendcan)
updated_addrs.update(self.update_vl())
return updated_addrs
cdef vector[SignalValue] new_vals
self.can.update_strings(strings, new_vals, sendcan)
return self.update_vl(new_vals)

@property
def can_valid(self):
Expand Down

0 comments on commit 6cd35ce

Please sign in to comment.