Skip to content

Commit

Permalink
CANParser: Initialize value dicts directly from DBC Msg (#1381)
Browse files Browse the repository at this point in the history
* init dict in __init__

* comment

* 0.0

* test all transitions

* this is more important and test same thing

---------

Co-authored-by: Shane Smiskol <[email protected]>
  • Loading branch information
deanlee and sshane authored Nov 2, 2024
1 parent a84fe18 commit fa5e761
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
1 change: 0 additions & 1 deletion opendbc/can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class CANParser {
bool can_valid = false;
bool bus_timeout = false;
uint64_t first_nanos = 0;
uint64_t last_nanos = 0;
uint64_t last_nonempty_nanos = 0;
uint64_t bus_timeout_threshold = 0;
uint64_t can_invalid_cnt = CAN_INVALID_CNT;
Expand Down
13 changes: 6 additions & 7 deletions opendbc/can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ CANParser::CANParser(int abus, const std::string& dbc_name, bool ignore_checksum
}

void CANParser::update(const std::vector<CanData> &can_data, std::vector<SignalValue> &vals) {
if (can_data.empty()) return;

// used to check for updated messages in earlier frames when we receive multiple at once
uint64_t current_nanos = 0;
for (const auto &c : can_data) {
if (first_nanos == 0) {
Expand All @@ -166,10 +169,9 @@ void CANParser::update(const std::vector<CanData> &can_data, std::vector<SignalV
if (current_nanos == 0) {
current_nanos = c.nanos;
}
last_nanos = c.nanos;

UpdateCans(c);
UpdateValid(last_nanos);
UpdateValid(c.nanos);
}
query_latest(vals, current_nanos);
}
Expand Down Expand Up @@ -241,13 +243,10 @@ void CANParser::UpdateValid(uint64_t nanos) {
can_valid = (can_invalid_cnt < CAN_INVALID_CNT) && _counters_valid;
}

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

Expand Down
9 changes: 5 additions & 4 deletions opendbc/can/parser_pyx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from libcpp.vector cimport vector
from libc.stdint cimport uint32_t

from .common cimport CANParser as cpp_CANParser
from .common cimport dbc_lookup, SignalValue, DBC, CanData
from .common cimport dbc_lookup, Msg, SignalValue, DBC, CanData

import numbers
from collections import defaultdict
Expand Down Expand Up @@ -53,15 +53,16 @@ cdef class CANParser:
self.addresses.add(address)

name = m.name.decode("utf8")
self.vl[address] = {}
signal_names = [sig.name.decode("utf-8") for sig in (<Msg*>m).sigs]

self.vl[address] = {name: 0.0 for name in signal_names}
self.vl[name] = self.vl[address]
self.vl_all[address] = defaultdict(list)
self.vl_all[name] = self.vl_all[address]
self.ts_nanos[address] = {}
self.ts_nanos[address] = {name: 0.0 for name in signal_names}
self.ts_nanos[name] = self.ts_nanos[address]

self.can = new cpp_CANParser(bus, dbc_name, message_v)
self.update_strings([])

def __dealloc__(self):
if self.can:
Expand Down
12 changes: 12 additions & 0 deletions opendbc/can/tests/test_packer_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ def test_parser_can_valid(self):
parser.update_strings([t, [msg]])
assert parser.can_valid

def test_parser_updated_list(self):
msgs = [("CAN_FD_MESSAGE", 10), ]
parser = CANParser(TEST_DBC, msgs, 0)
packer = CANPacker(TEST_DBC)

msg = packer.make_can_msg("CAN_FD_MESSAGE", 0, {})
ret = parser.update_strings([0, [msg]])
assert ret == {245}

ret = parser.update_strings([])
assert len(ret) == 0

def test_parser_counter_can_valid(self):
"""
Tests number of allowed bad counters + ensures CAN stays invalid
Expand Down

0 comments on commit fa5e761

Please sign in to comment.