From 3d85ae97a61a535c2411e210a46155350b8492a4 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Sat, 12 Jul 2025 23:33:54 -0400 Subject: [PATCH] RMT running in integration --- components/config/ConfigManager.cpp | 1 + components/rpc/CommunicationRouter.cpp | 32 +++++++++++++++----- components/rpc/include/CommunicationRouter.h | 12 +++++--- components/rpc/include/MessagingInterface.h | 4 +-- main/LoopManager.cpp | 4 +-- main/main.cpp | 9 ------ 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/components/config/ConfigManager.cpp b/components/config/ConfigManager.cpp index 3079f80..53b29ca 100644 --- a/components/config/ConfigManager.cpp +++ b/components/config/ConfigManager.cpp @@ -26,6 +26,7 @@ void ConfigManager::init_config() { nvs_close(config_handle); } +// todo: we should probably cache some of these things uint16_t ConfigManager::get_module_id() { nvs_handle config_handle; nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle); diff --git a/components/rpc/CommunicationRouter.cpp b/components/rpc/CommunicationRouter.cpp index fbfe57d..cbdbeea 100644 --- a/components/rpc/CommunicationRouter.cpp +++ b/components/rpc/CommunicationRouter.cpp @@ -1,9 +1,9 @@ #include "CommunicationRouter.h" -#include #include -#include -#include +#include "mDNSDiscoveryService.h" +#include "MPIMessageBuilder.h" +#include "WifiManager.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" @@ -49,9 +49,13 @@ CommunicationRouter::~CommunicationRouter() { that->m_data_link_manager->start_receive_frames(channel); while (true) { // todo: very c style function calls - that->m_data_link_manager->receive(reinterpret_cast(buffer), 512, &bytes_received, channel); + const auto err = that->m_data_link_manager->receive(reinterpret_cast(buffer), 512, &bytes_received, channel); that->m_data_link_manager->start_receive_frames(channel); + if (ESP_OK != err) { + continue; + } + that->route(reinterpret_cast(buffer), bytes_received); std::cout << "RMT callback" << std::endl; @@ -63,14 +67,15 @@ int CommunicationRouter::send_msg(char* buffer, const size_t length) const { return 0; } +// todo: the number of things this is doing in so many different places is crazy... void CommunicationRouter::update_leader() { RIPRow_public table[RIP_MAX_ROUTES]; - size_t table_size = 0; + size_t table_size = RIP_MAX_ROUTES; this->m_data_link_manager->get_routing_table(table, &table_size); // Leader election (just get the highest id in rip) std::vector connected_module_ids; - uint8_t max = ConfigManager::get_module_id(); + uint8_t max = m_module_id; for (int i = 0; i < table_size; i++) { const auto id = table[i].info.board_id; connected_module_ids.emplace_back(id); @@ -79,17 +84,28 @@ void CommunicationRouter::update_leader() { } } + // Leader has changed, we may need to change PC connection state + if (this->m_leader != max) { + if (max == m_module_id) { + m_pc_connection->connect(); + } else if (this->m_leader == m_module_id) { + m_pc_connection->disconnect(); + } + } + this->m_leader = max; mDNSDiscoveryService::set_connected_boards(connected_module_ids); + + this->m_last_leader_updated = std::chrono::system_clock::now(); } void CommunicationRouter::route(uint8_t* buffer, const size_t length) const { const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer); - if (mpi_message->destination() == ConfigManager::get_module_id()) { + if (mpi_message->destination() == m_module_id) { this->m_rx_callback(reinterpret_cast(buffer), 512); - } else if (mpi_message->destination() == PC_ADDR && this->m_leader == ConfigManager::get_module_id()) { + } else if (mpi_message->destination() == PC_ADDR && this->m_leader == m_module_id) { this->m_tcp_server->send_msg(reinterpret_cast(buffer), 512); } else { this->m_data_link_manager->send(mpi_message->destination(), buffer, length, FrameType::MOTOR_TYPE, 0); diff --git a/components/rpc/include/CommunicationRouter.h b/components/rpc/include/CommunicationRouter.h index f35712b..8b34397 100644 --- a/components/rpc/include/CommunicationRouter.h +++ b/components/rpc/include/CommunicationRouter.h @@ -9,6 +9,7 @@ #include #include #include +#include #include "freertos/FreeRTOS.h" @@ -29,21 +30,23 @@ class CommunicationRouter { }; public: - explicit CommunicationRouter(const std::function &rx_callback) + CommunicationRouter(const std::function &rx_callback, std::unique_ptr&& pc_connection) : m_tcp_rx_queue(std::make_shared>>(10)), m_rx_callback(rx_callback), m_tcp_server(std::make_unique(TCP_PORT, m_tcp_rx_queue)), + m_data_link_manager(std::make_unique(ConfigManager::get_module_id(), MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()])), + m_pc_connection(std::move(pc_connection)), m_module_id(ConfigManager::get_module_id()), m_last_leader_updated(std::chrono::system_clock::now()){ update_leader(); xTaskCreate(router_thread, "communication_router", 2048, this, 3, &this->m_router_thread); - const auto num_channels = MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_id()]; + const auto num_channels = MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()]; this->m_link_layer_threads.resize(num_channels); for (uint8_t i = 0; i < num_channels; i++) { auto *params = new link_layer_thread_params(this, i); - xTaskCreate(link_layer_thread, "communication_router_rmt", 2048, ¶ms, 3, &this->m_link_layer_threads[i]); + xTaskCreate(link_layer_thread, "communication_router_rmt", 3096, params, 3, &this->m_link_layer_threads[i]); } } @@ -66,7 +69,8 @@ private: std::vector m_link_layer_threads; std::unique_ptr m_tcp_server; std::unique_ptr m_data_link_manager; - uint8_t m_leader; + std::unique_ptr m_pc_connection; // todo: change to dependency inject + uint8_t m_leader = 0; uint8_t m_module_id; std::chrono::time_point m_last_leader_updated; }; diff --git a/components/rpc/include/MessagingInterface.h b/components/rpc/include/MessagingInterface.h index 49bb4eb..f87b183 100644 --- a/components/rpc/include/MessagingInterface.h +++ b/components/rpc/include/MessagingInterface.h @@ -13,9 +13,9 @@ class MessagingInterface { public: - MessagingInterface() + explicit MessagingInterface(std::unique_ptr&& pc_connection) : m_mpi_rx_queue(xQueueCreate(MAX_RX_BUFFER_SIZE, RX_QUEUE_SIZE)), - m_router(std::make_unique([this](char* buffer, const int size) { handleRecv(buffer, size); })), + m_router(std::make_unique([this](char* buffer, const int size) { handleRecv(buffer, size); }, std::move(pc_connection))), m_map_semaphore(xSemaphoreCreateMutex()) {}; ~MessagingInterface(); diff --git a/main/LoopManager.cpp b/main/LoopManager.cpp index 2e22141..6691d19 100644 --- a/main/LoopManager.cpp +++ b/main/LoopManager.cpp @@ -11,7 +11,7 @@ #include "esp_log.h" [[noreturn]] void LoopManager::control_loop() { - const auto messaging_interface = std::make_unique(); + const auto messaging_interface = std::make_unique(std::make_unique()); char buffer[512]; while (true) { @@ -20,7 +20,7 @@ char s[21] = {'H', 'e', 'l', 'l', 'o', ' ', 'f', 'r', 'o', 'm', ' ', 't', 'h', 'e', ' ', 'B', 'O', 'A', 'R', 'D', '!' }; - messaging_interface->send(s, 21, 0, 2, true); + messaging_interface->send(s, 21, 5, 2, true); ESP_LOGI("MEM", "Free internal RAM: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT)); ESP_LOGI("MEM", "Free PSRAM: %d", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); diff --git a/main/main.cpp b/main/main.cpp index c91db5d..445ba8a 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,17 +1,13 @@ -//mdns and other stuff main file #include #include #include "sdkconfig.h" #include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" #include "WifiManager.h" #include "mDNSDiscoveryService.h" #include "TCPServer.h" #include "ConfigManager.h" -#include "constants/tcp.h" #include "LoopManager.h" #include "esp_log.h" @@ -20,11 +16,6 @@ extern "C" [[noreturn]] void app_main(void) { ESP_LOGI("MEM", "Free PSRAM: %d", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); ConfigManager::init_config(); - - const auto manager = std::make_unique(); - manager->connect(); - mDNSDiscoveryService::setup(); - LoopManager::control_loop(); }