Skip to content

Commit

Permalink
Merge pull request #23 from hytech-racing/system-wrapper-class
Browse files Browse the repository at this point in the history
creation of drivebrain app wrapper
  • Loading branch information
happyCupcake authored Jan 12, 2025
2 parents 4357ed9 + b5a64d0 commit 21d4044
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 208 deletions.
31 changes: 29 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ target_include_directories(drivebrain_estimation PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/drivebrain_core_impl/drivebrain_estimation/include>
)

make_cmake_package(drivebrain_estimation drivebrain)

add_library(drivebrain_app SHARED
drivebrain_app/src/DriveBrainApp.cpp
)

target_include_directories(drivebrain_app PUBLIC
$<INSTALL_INTERFACE:drivebrain_app/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/drivebrain_app/include>
)

target_link_libraries(drivebrain_app PUBLIC
drivebrain_core::drivebrain_core
drivebrain_control
drivebrain_comms
drivebrain_mcap_logger
Boost::program_options
)

target_link_libraries(drivebrain_estimation PUBLIC
drivebrain_core::drivebrain_core
drivebrain_common_utils
Expand All @@ -162,7 +181,7 @@ target_link_libraries(drivebrain_estimation PUBLIC
protobuf::libprotobuf
)

make_cmake_package(drivebrain_estimation drivebrain)
make_cmake_package(drivebrain_app drivebrain)

###############
# executables #
Expand Down Expand Up @@ -227,7 +246,14 @@ target_compile_features(test_vn PUBLIC cxx_std_11)
### system executable ###
### ###

add_executable(alpha_build drivebrain_app/main.cpp)
add_executable(alpha_build
drivebrain_app/main.cpp
)

# Add include directory for DriveBrainApp header
target_include_directories(alpha_build PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/drivebrain_app
)

enable_testing()

Expand All @@ -237,6 +263,7 @@ add_executable(alpha_test
)

target_link_libraries(alpha_build PUBLIC
drivebrain_app
drivebrain_core::drivebrain_core
drivebrain_control
drivebrain_comms
Expand Down
8 changes: 8 additions & 0 deletions cmake/drivebrain_appConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

find_dependency(drivebrain_core)
find_dependency(Boost COMPONENTS program_options)

include("${CMAKE_CURRENT_LIST_DIR}/drivebrain_appTargets.cmake")
81 changes: 81 additions & 0 deletions drivebrain_app/include/DriveBrainApp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// DriveBrainApp.hpp
#pragma once

#include <JsonFileHandler.hpp>
#include <CANComms.hpp>
#include <SimpleController.hpp>
#include <StateEstimator.hpp>
#include <MCUETHComms.hpp>
#include <VNComms.hpp>
#include <MsgLogger.hpp>
#include <MCAPProtobufLogger.hpp>
#include <mcap/writer.hpp>
#include <DrivebrainBase.hpp>
#include <foxglove_server.hpp>
#include <DBServiceImpl.hpp>

#include <thread>
#include <chrono>
#include <condition_variable>
#include <cassert>
#include <boost/program_options.hpp>
#include <boost/asio.hpp>
#include <memory>
#include <optional>
#include <csignal>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <spdlog/spdlog.h>
#include <atomic>

struct DriveBrainSettings {
bool run_db_service{true};
bool run_io_context{true};
bool run_process_loop{true};
};

class DriveBrainApp {
public:
DriveBrainApp(const std::string& param_path, const std::string& dbc_path, const DriveBrainSettings& settings = DriveBrainSettings{});
~DriveBrainApp();

void run();
void stop();

private:
// Private member functions
void _process_loop();

private:
// Private member variables
static std::atomic<bool> _stop_signal;
const std::string _param_path;
core::Logger _logger;
core::JsonFileHandler _config;
std::optional<std::string> _dbc_path;
boost::asio::io_context _io_context;

core::common::ThreadSafeDeque<std::shared_ptr<google::protobuf::Message>> _rx_queue;
core::common::ThreadSafeDeque<std::shared_ptr<google::protobuf::Message>> _tx_queue;
core::common::ThreadSafeDeque<std::shared_ptr<google::protobuf::Message>> _eth_tx_queue;
core::common::ThreadSafeDeque<std::shared_ptr<google::protobuf::Message>> _live_telem_queue;

std::vector<core::common::Configurable*> _configurable_components;
std::unique_ptr<common::MCAPProtobufLogger> _mcap_logger;
std::unique_ptr<control::SimpleController> _controller;
std::unique_ptr<estimation::Tire_Model_Codegen_MatlabModel> _matlab_math;
std::unique_ptr<core::FoxgloveWSServer> _foxglove_server;
std::shared_ptr<core::MsgLogger<std::shared_ptr<google::protobuf::Message>>> _message_logger;
std::unique_ptr<core::StateEstimator> _state_estimator;
std::unique_ptr<comms::CANDriver> _driver;
std::unique_ptr<comms::MCUETHComms> _eth_driver;
std::unique_ptr<comms::VNDriver> _vn_driver;
std::unique_ptr<DBInterfaceImpl> _db_service;

std::thread _process_thread;
std::thread _io_context_thread;
std::thread _db_service_thread;

const DriveBrainSettings _settings;
};
Loading

0 comments on commit 21d4044

Please sign in to comment.