From ea0d275b2ce0240c9b3169ba3c6b25c152fac9b5 Mon Sep 17 00:00:00 2001 From: MankaranSingh Date: Sun, 20 Aug 2023 19:35:33 +0530 Subject: [PATCH] use CanData struct from cereal --- SConstruct | 13 ++++++++++--- can/SConscript | 10 +--------- can/common.cc | 2 +- can/common.h | 6 +++--- can/dbc.cc | 4 ++-- can/msg.capnp | 18 ------------------ can/packer.cc | 2 +- can/parser.cc | 11 +++++------ can/tests/test_packer_parser.py | 4 ++-- 9 files changed, 25 insertions(+), 45 deletions(-) delete mode 100644 can/msg.capnp diff --git a/SConstruct b/SConstruct index e8c162e6c7..d8f40740bf 100644 --- a/SConstruct +++ b/SConstruct @@ -10,7 +10,7 @@ cereal_dir = Dir('.') python_path = sysconfig.get_paths()['include'] cpppath = [ - "can", + '#', '/usr/lib/include', python_path ] @@ -41,7 +41,7 @@ env = Environment( LDFLAGS=ldflags_asan, LINKFLAGS=ldflags_asan, LIBPATH=[ - "can/", + "#opendbc/can/", ], CFLAGS="-std=gnu11", CXXFLAGS=["-std=c++1z"], @@ -53,6 +53,11 @@ env = Environment( common = '' Export('env', 'zmq', 'arch', 'common') +cereal = [File('#cereal/libcereal.a')] +messaging = [File('#cereal/libmessaging.a')] +Export('cereal', 'messaging') + + envCython = env.Clone() envCython["CPPPATH"] += [np.get_include()] envCython["CCFLAGS"] += ["-Wno-#warnings", "-Wno-shadow", "-Wno-deprecated-declarations"] @@ -72,4 +77,6 @@ envCython["LIBS"] = python_libs Export('envCython') -SConscript(['can/SConscript']) + +SConscript(['cereal/SConscript']) +SConscript(['opendbc/can/SConscript']) diff --git a/can/SConscript b/can/SConscript index 49b43a8e07..f07234c111 100644 --- a/can/SConscript +++ b/can/SConscript @@ -1,15 +1,7 @@ -Import('env', 'envCython') +Import('env', 'envCython', 'cereal', 'common') import os -# Build Capnp Definitions -msg_capnp = env.Command( - target=['msg.capnp.c++', 'msg.capnp.h'], - source=["msg.capnp"], - action='capnp compile -oc++ $SOURCE' -) - -common = '' envDBC = env.Clone() dbc_file_path = '-DDBC_FILE_PATH=\'"%s"\'' % (envDBC.Dir("..").abspath) envDBC['CXXFLAGS'] += [dbc_file_path] diff --git a/can/common.cc b/can/common.cc index 735b85d241..0df9e04b90 100644 --- a/can/common.cc +++ b/can/common.cc @@ -1,4 +1,4 @@ -#include "common.h" +#include "opendbc/can/common.h" unsigned int honda_checksum(uint32_t address, const Signal &sig, const std::vector &d) { diff --git a/can/common.h b/can/common.h index 3b0af2e272..03b99e5598 100644 --- a/can/common.h +++ b/can/common.h @@ -10,10 +10,10 @@ #include #ifndef DYNAMIC_CAPNP -#include "msg.capnp.h" +#include "cereal/gen/cpp/log.capnp.h" #endif -#include "common_dbc.h" +#include "opendbc/can/common_dbc.h" #define INFO printf #define WARN printf @@ -81,7 +81,7 @@ class CANParser { #ifndef DYNAMIC_CAPNP void update_string(const std::string &data, bool sendcan); void update_strings(const std::vector &data, std::vector &vals, bool sendcan); - void UpdateCans(uint64_t sec, const capnp::List::Reader& cans); + void UpdateCans(uint64_t sec, const capnp::List::Reader& cans); #endif void UpdateCans(uint64_t sec, const capnp::DynamicStruct::Reader& cans); void UpdateValid(uint64_t sec); diff --git a/can/dbc.cc b/can/dbc.cc index 64d7858ab0..7abe6b679c 100644 --- a/can/dbc.cc +++ b/can/dbc.cc @@ -11,8 +11,8 @@ #include #include -#include "common.h" -#include "common_dbc.h" +#include "opendbc/can/common.h" +#include "opendbc/can/common_dbc.h" std::regex bo_regexp(R"(^BO_ (\w+) (\w+) *: (\w+) (\w+))"); std::regex sg_regexp(R"(^SG_ (\w+) : (\d+)\|(\d+)@(\d+)([\+|\-]) \(([0-9.+\-eE]+),([0-9.+\-eE]+)\) \[([0-9.+\-eE]+)\|([0-9.+\-eE]+)\] \"(.*)\" (.*))"); diff --git a/can/msg.capnp b/can/msg.capnp deleted file mode 100644 index 0ccff7b6ba..0000000000 --- a/can/msg.capnp +++ /dev/null @@ -1,18 +0,0 @@ -@0x8e2af1e708af8b8d; - -struct CanData { - address @0 :UInt32; - busTime @1 :UInt16; - dat @2 :Data; - src @3 :UInt8; -} - -struct Event { - logMonoTime @0 :UInt64; # nanoseconds - valid @1 :Bool = true; - - union { - can @2 :List(CanData); - sendcan @3 :List(CanData); - } -} diff --git a/can/packer.cc b/can/packer.cc index f7275a9e38..ad762828bc 100644 --- a/can/packer.cc +++ b/can/packer.cc @@ -5,7 +5,7 @@ #include #include -#include "common.h" +#include "opendbc/can/common.h" void set_value(std::vector &msg, const Signal &sig, int64_t ival) { diff --git a/can/parser.cc b/can/parser.cc index 44fc35a585..734681981d 100644 --- a/can/parser.cc +++ b/can/parser.cc @@ -8,9 +8,8 @@ #include #include -#include "common.h" -#include "msg.capnp.h" - +#include "cereal/logger/logger.h" +#include "opendbc/can/common.h" int64_t get_raw_value(const std::vector &msg, const Signal &sig) { int64_t ret = 0; @@ -172,7 +171,7 @@ void CANParser::update_string(const std::string &data, bool sendcan) { // extract the messages capnp::FlatArrayMessageReader cmsg(aligned_buf.slice(0, buf_size)); - Event::Reader event = cmsg.getRoot(); + cereal::Event::Reader event = cmsg.getRoot(); if (first_sec == 0) { first_sec = event.getLogMonoTime(); @@ -196,7 +195,7 @@ void CANParser::update_strings(const std::vector &data, std::vector query_latest(vals, current_sec); } -void CANParser::UpdateCans(uint64_t sec, const capnp::List::Reader& cans) { +void CANParser::UpdateCans(uint64_t sec, const capnp::List::Reader& cans) { //DEBUG("got %d messages\n", cans.size()); bool bus_empty = true; @@ -242,7 +241,7 @@ void CANParser::UpdateCans(uint64_t sec, const capnp::List::Reader& can #endif void CANParser::UpdateCans(uint64_t sec, const capnp::DynamicStruct::Reader& cmsg) { - // assume message struct is `CanData` and parse + // assume message struct is `cereal::CanData` and parse assert(cmsg.has("address") && cmsg.has("src") && cmsg.has("dat") && cmsg.has("busTime")); if (cmsg.get("src").as() != bus) { diff --git a/can/tests/test_packer_parser.py b/can/tests/test_packer_parser.py index f474370541..fc1898a332 100755 --- a/can/tests/test_packer_parser.py +++ b/can/tests/test_packer_parser.py @@ -2,7 +2,7 @@ import unittest import random -from cereal import log +import cereal.messaging as messaging from opendbc.can.parser import CANParser from opendbc.can.packer import CANPacker from opendbc.can.tests import TEST_DBC @@ -10,7 +10,7 @@ # Python implementation so we don't have to depend on boardd def can_list_to_can_capnp(can_msgs, msgtype='can', logMonoTime=None): - dat = log.Event.new_message() + dat = messaging.new_message() dat.init(msgtype, len(can_msgs)) if logMonoTime is not None: