Skip to content

Commit

Permalink
builds
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishKittur committed Nov 14, 2024
2 parents 2195db3 + a6c19be commit 49101e1
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 76 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ set(CMAKE_INCLUDE_HEADERS_IN_COMPILE_COMMANDS ON)
### just like "include what you use" and do "find what you use"

## hytech's upstream libraries
find_package(fmt REQUIRED)
find_package(spdlog REQUIRED)



### generated via nix-proto from hytech's generated CAN protobufs
Expand Down Expand Up @@ -49,7 +52,6 @@ include(create_package)
# created libraries #
#####################


# utils
add_library(drivebrain_common_utils SHARED
drivebrain_core_impl/drivebrain_common_utils/src/ProtobufUtils.cpp
Expand All @@ -62,6 +64,7 @@ target_include_directories(drivebrain_common_utils PUBLIC

target_link_libraries(drivebrain_common_utils PUBLIC
protobuf::libprotobuf
spdlog::spdlog
)

make_cmake_package(drivebrain_common_utils drivebrain)
Expand Down Expand Up @@ -100,6 +103,7 @@ target_link_libraries(drivebrain_comms PUBLIC
db_service_grpc_cpp::db_service_grpc_cpp
libvncxx::libvncxx
drivebrain_estimation
spdlog::spdlog
)

make_cmake_package(drivebrain_comms drivebrain)
Expand Down Expand Up @@ -262,4 +266,3 @@ install(TARGETS
alpha_build
RUNTIME
DESTINATION ${CMAKE_INSTALL_BINDIR})

4 changes: 2 additions & 2 deletions db-core.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ pkgs, stdenv, cmake, boost, pkg-config, cmake_macros, protobuf, drivebrain_core_msgs_proto_cpp, hytech_np_proto_cpp, nlohmann_json, db-core-src }:
{ pkgs, stdenv, cmake, boost, pkg-config, cmake_macros, protobuf, drivebrain_core_msgs_proto_cpp, hytech_np_proto_cpp, nlohmann_json, db-core-src, spdlog, fmt }:
stdenv.mkDerivation {
name = "db-core";
src = db-core-src;
nativeBuildInputs = [ cmake pkg-config cmake_macros ];
propagatedBuildInputs = [ boost protobuf drivebrain_core_msgs_proto_cpp hytech_np_proto_cpp nlohmann_json cmake_macros ];
propagatedBuildInputs = [ boost protobuf drivebrain_core_msgs_proto_cpp hytech_np_proto_cpp nlohmann_json cmake_macros spdlog fmt ];
}
4 changes: 2 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{ pkgs, stdenv, cmake, boost, pkg-config, lz4 ,zstd, protobuf, nlohmann_json, foxglove-ws-protocol-cpp, cmake_macros, hytech_np_proto_cpp, dbcppp, gtest, drivebrain_core_msgs_proto_cpp, mcap, db_service_grpc_cpp, grpc, vn_lib, drivebrain_core, simulink_automation, ... }:
{ pkgs, stdenv, cmake, boost, pkg-config, lz4 ,zstd, protobuf, nlohmann_json, foxglove-ws-protocol-cpp, cmake_macros, hytech_np_proto_cpp, dbcppp, gtest, drivebrain_core_msgs_proto_cpp, mcap, db_service_grpc_cpp, grpc, vn_lib, drivebrain_core, simulink_automation, spdlog, fmt, ... }:

stdenv.mkDerivation {
name = "drivebrain_software";
src = ./.;
nativeBuildInputs = [ cmake pkg-config ];
propagatedBuildInputs = [ protobuf lz4 zstd boost cmake_macros nlohmann_json foxglove-ws-protocol-cpp hytech_np_proto_cpp dbcppp gtest drivebrain_core_msgs_proto_cpp mcap db_service_grpc_cpp grpc vn_lib drivebrain_core simulink_automation ];
propagatedBuildInputs = [ protobuf lz4 zstd boost cmake_macros nlohmann_json foxglove-ws-protocol-cpp hytech_np_proto_cpp dbcppp gtest drivebrain_core_msgs_proto_cpp mcap db_service_grpc_cpp grpc vn_lib drivebrain_core simulink_automation spdlog fmt ];
dontStrip = true;
cmakeFlags = [ "-DCMAKE_FIND_DEBUG_MODE=ON"];
}
23 changes: 15 additions & 8 deletions drivebrain_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#include <cstdlib>

#include <iostream>
#include <sstream>

#include <spdlog/spdlog.h>

// TODO first application will have

// - [x] message queue that can send messages between the CAN driver and the controller
Expand All @@ -43,7 +47,7 @@ std::atomic<bool> stop_signal{false};
// Signal handler function
void signalHandler(int signal)
{
std::cout << "Interrupt signal (" << signal << ") received. Cleaning up..." << std::endl;
spdlog::info("Interrupt signal ({}) received. Cleaning up...", signal);
stop_signal.store(true); // Set running to false to exit the main loop or gracefully terminate
}

Expand Down Expand Up @@ -82,7 +86,9 @@ int main(int argc, char *argv[])
po::notify(vm);

if (vm.count("help")) {
std::cout << desc << "\n";
std::stringstream ss;
ss << desc;
spdlog::info("{}", ss.str());
return 1;
}
if (vm.count("dbc-path")) {
Expand Down Expand Up @@ -137,26 +143,27 @@ int main(int argc, char *argv[])
DBInterfaceImpl db_service_inst(message_logger);
std::thread db_service_thread([&db_service_inst]()
{
std::cout <<"started db service thread" <<std::endl;
spdlog::info("started db service thread");

try {
while (!stop_signal.load()) {
// Run the io_context as long as stop_signal is false
db_service_inst.run_server(); // Run at least one handler, or return immediately if none
}
} catch (const std::exception& e) {
std::cerr << "Error in drivebrain service thread: " << e.what() << std::endl;
spdlog::error("Error in drivebrain service thread: {}", e.what());
}
});
// what we will do here is have a temporary super-loop.
// in this thread we will block on having anything in the rx queue, everything by default goes into the foxglove server (TODO)
// if we receive the pedals message, we step the controller and get its output to put intot he tx queue
std::thread io_context_thread([&io_context]()
{
std::cout <<"started io context thread" <<std::endl;
spdlog::info("Started io context thread");
try {
io_context.run();
} catch (const std::exception& e) {
std::cerr << "Error in io_context: " << e.what() << std::endl;
spdlog::error("Error in io_context: {}", e.what());
} });

std::thread process_thread([&rx_queue, &eth_tx_queue, &controller, &state_estimator]()
Expand Down Expand Up @@ -243,11 +250,11 @@ int main(int argc, char *argv[])
std::this_thread::sleep_for(std::chrono::seconds(1));
}
process_thread.join();
std::cout << "joined main process" << std::endl;
spdlog::info("joined main process");
io_context.stop();
io_context_thread.join();
db_service_inst.stop_server();
db_service_thread.join();
std::cout << "joined io context" << std::endl;
spdlog::info("joined io context");
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <ProtobufUtils.hpp>
#include <spdlog/spdlog.h>

namespace util
{
Expand All @@ -11,7 +12,7 @@ namespace util
google::protobuf::DescriptorPool::generated_pool()->FindFileByName(name);
if (!file_descriptor)
{
std::cerr << "File descriptor not found!" << std::endl;
spdlog::error("File descriptor for '{}' not found!", name);
}
else
{
Expand Down Expand Up @@ -63,4 +64,4 @@ namespace util
}
return fdSet;
}
}
}
28 changes: 14 additions & 14 deletions drivebrain_core_impl/drivebrain_comms/src/CANComms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <sys/socket.h>
#include <unistd.h>
#include <variant>
// logging includes
#include "spdlog/spdlog.h"

// https://docs.kernel.org/networking/can.html

Expand Down Expand Up @@ -101,8 +103,7 @@ void comms::CANDriver::_do_read() {
_handle_recv_CAN_frame(_frame);
_do_read(); // Continue reading for the next frame
} else if (ec) {
std::cerr << "Error receiving CAN message: " << ec.message()
<< std::endl;
spdlog::error("Error receiving CAN message: {}", ec.message());
}
});
}
Expand All @@ -112,7 +113,7 @@ void comms::CANDriver::_send_message(const struct can_frame &frame) {
_socket, boost::asio::buffer(&frame, sizeof(frame)),
[this](boost::system::error_code ec, std::size_t /*bytes_transferred*/) {
if (ec) {
std::cerr << "Error sending CAN message: " << ec.message() << std::endl;
spdlog::error("Error sending CAN message: {}", ec.message());
}
});
}
Expand All @@ -137,7 +138,8 @@ comms::CANDriver::_get_pb_msg_by_name(const std::string &name) {
prototype_message.reset(
google::protobuf::MessageFactory::generated_factory()->GetPrototype(desc)->New());
if (!prototype_message) {
std::cerr << "Failed to create prototype message" << std::endl;
spdlog::error("Failed to create prototype message");

return nullptr;
}
// delete desc;
Expand Down Expand Up @@ -165,7 +167,7 @@ void comms::CANDriver::set_field_values_of_pb_msg(
if (it != field_values.end()) {
// Set field values based on field type
if (field->is_repeated()) {
std::cerr << "Unsupported field type for field: " << field_name << std::endl;
spdlog::warn("Unsupported field type for field: {}", field_name);
continue;
}
switch (field->type()) {
Expand Down Expand Up @@ -221,7 +223,7 @@ comms::CANDriver::FieldVariant
comms::CANDriver::get_field_value(std::shared_ptr<google::protobuf::Message> message,
const std::string &field_name) {
if (!message) {
std::cerr << "Message is null" << std::endl;
spdlog::error("Message is null");
return std::monostate{};
}

Expand All @@ -230,7 +232,7 @@ comms::CANDriver::get_field_value(std::shared_ptr<google::protobuf::Message> mes
const google::protobuf::FieldDescriptor *field = descriptor->FindFieldByName(field_name);

if (field == nullptr) {
std::cerr << "Field not found: " << field_name << std::endl;
spdlog::warn("Field not found: {}", field_name);
return std::monostate{};
}

Expand All @@ -255,18 +257,17 @@ comms::CANDriver::get_field_value(std::shared_ptr<google::protobuf::Message> mes
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
return reflection->GetEnum(*message, field)->name();
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
std::cerr << "Nested messages not supported" << std::endl;
spdlog::warn("Nested messages not supported");
return std::monostate{};
default:
std::cerr << "Unsupported field type" << std::endl;
spdlog::warn("Unsupported field type");
return std::monostate{};
}
}

std::optional<can_frame>
comms::CANDriver::_get_CAN_msg(std::shared_ptr<google::protobuf::Message> pb_msg) {
// - [x] get an associated CAN message based on the name of the input message

can_frame frame{};
std::string type_url = pb_msg->GetTypeName();
std::string messageTypeName = type_url.substr(type_url.find_last_of('.') + 1);
Expand All @@ -282,7 +283,7 @@ comms::CANDriver::_get_CAN_msg(std::shared_ptr<google::protobuf::Message> pb_msg
std::visit(
[&sig, &frame](const FieldVariant &arg) {
if (std::holds_alternative<std::monostate>(arg)) {
std::cout << "No value found or unsupported field" << std::endl;
spdlog::warn("No value found or unsupported field");
} else if (std::holds_alternative<float>(arg)) {
auto val = std::get<float>(arg);
sig.Encode(sig.PhysToRaw(val), frame.data);
Expand All @@ -299,14 +300,13 @@ comms::CANDriver::_get_CAN_msg(std::shared_ptr<google::protobuf::Message> pb_msg
auto val = std::get<bool>(arg);
sig.Encode(val, frame.data);
} else {
std::cout << "uh not supported yet" << std::endl;
spdlog::warn("uh not supported yet");
}
},
field_value);
}
} else {
std::cout << "WARNING: not creating a frame to send due to not finding frame name"
<< std::endl;
spdlog::warn("WARNING: not creating a frame to send due to not finding frame name");
return std::nullopt;
}

Expand Down
12 changes: 7 additions & 5 deletions drivebrain_core_impl/drivebrain_comms/src/DBServiceImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <DBServiceImpl.hpp>
#include "spdlog/spdlog.h"

grpc::Status DBInterfaceImpl::RequestStopLogging(grpc::ServerContext *context, const google::protobuf::Empty *rq, db_service::v1::service::LoggerStatus *response)
{
{
std::cout << "requested stopping of logging" << std::endl;
spdlog::warn("requested stopping of logging");
_logger_inst->stop_logging_to_file();
auto status = _logger_inst->get_logger_status();

Expand All @@ -15,7 +16,7 @@ grpc::Status DBInterfaceImpl::RequestStopLogging(grpc::ServerContext *context, c
grpc::Status DBInterfaceImpl::RequestStartLogging(grpc::ServerContext *context, const google::protobuf::Empty *rq, db_service::v1::service::LoggerStatus *response)
{
{
std::cout << "requested start of logging" << std::endl;
spdlog::warn("requested start of logging");
_logger_inst->start_logging_to_new_file();
auto status = _logger_inst->get_logger_status();
response->set_active_or_previous_log_file_name(std::get<0>(status));
Expand All @@ -26,7 +27,7 @@ grpc::Status DBInterfaceImpl::RequestStartLogging(grpc::ServerContext *context,
grpc::Status DBInterfaceImpl::RequestCurrentLoggerStatus(grpc::ServerContext *context, const google::protobuf::Empty *rq, db_service::v1::service::LoggerStatus *response)
{
{
std::cout << "requested status of logger" << std::endl;
spdlog::warn("requested status of logger");
auto status = _logger_inst->get_logger_status();
response->set_active_or_previous_log_file_name(std::get<0>(status));
response->set_currently_logging(std::get<1>(status));
Expand All @@ -39,7 +40,8 @@ DBInterfaceImpl::DBInterfaceImpl(std::shared_ptr<core::MsgLogger<std::shared_ptr
}
void DBInterfaceImpl::stop_server() {
if (_server) {
std::cout << "Shutting down the server..." << std::endl;
spdlog::warn("Shutting down the server...");

_server->Shutdown();
}
}
Expand All @@ -56,7 +58,7 @@ void DBInterfaceImpl::run_server()
builder.RegisterService(this);
// Finally assemble the server.
_server = builder.BuildAndStart();
std::cout << "Server listening on " << server_address << std::endl;
spdlog::warn("Server listening on ");

// Wait for the server to shutdown. Note that some other thread must be
// responsible for shutting down the server for this call to ever return.
Expand Down
4 changes: 3 additions & 1 deletion drivebrain_core_impl/drivebrain_comms/src/MCUETHComms.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <MCUETHComms.hpp>
#include "hytech_msgs.pb.h"
#include <spdlog/spdlog.h>


using boost::asio::ip::udp;
namespace comms
Expand Down Expand Up @@ -32,7 +34,7 @@ namespace comms
_running = false;
_input_deque_ref.cv.notify_all();
_output_thread.join();
std::cout <<"destructed MCU ETH COMMS" <<std::endl;
spdlog::warn("destructed MCU ETH COMMS");
}

void MCUETHComms::_handle_send_msg_from_queue()
Expand Down
Loading

0 comments on commit 49101e1

Please sign in to comment.