From 071b620804652a40ba5c54a24c4afa897c73e2f5 Mon Sep 17 00:00:00 2001 From: deanlee Date: Fri, 2 Aug 2024 16:08:37 +0800 Subject: [PATCH] improve update() --- opendbc/can/common.h | 1 + opendbc/can/parser.cc | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/opendbc/can/common.h b/opendbc/can/common.h index 77d2a73dd9..b79bae6aad 100644 --- a/opendbc/can/common.h +++ b/opendbc/can/common.h @@ -83,6 +83,7 @@ class CANParser { std::set update(const std::vector &can_data); protected: + void clearAllValues(); void updateCans(const CanData &can, std::set &updated_addresses); void UpdateValid(uint64_t nanos); }; diff --git a/opendbc/can/parser.cc b/opendbc/can/parser.cc index db109965ad..cf19bc4cad 100644 --- a/opendbc/can/parser.cc +++ b/opendbc/can/parser.cc @@ -158,25 +158,34 @@ CANParser::CANParser(int abus, const std::string& dbc_name, bool ignore_checksum } std::set CANParser::update(const std::vector &can_data) { - // Clear all_vals in message_states - for (auto &[_, state] : message_states) { - for (auto &vals : state.all_vals) { - vals.clear(); - } + clearAllValues(); + std::set updated_addresses; + + if (can_data.empty()) { + return updated_addresses; + } + + if (first_nanos == 0) { + first_nanos = can_data.front().nanos; } - std::set updated_addresses; for (const auto &c : can_data) { - if (first_nanos == 0) { - first_nanos = c.nanos; - } last_nanos = std::max(last_nanos, c.nanos); updateCans(c, updated_addresses); UpdateValid(last_nanos); } + return updated_addresses; } +void CANParser::clearAllValues() { + for (auto &[_, state] : message_states) { + for (auto &vals : state.all_vals) { + vals.clear(); + } + } +} + void CANParser::updateCans(const CanData &can, std::set &updated_addresses) { //DEBUG("got %zu messages\n", can.frames.size());