Re-enable rmt

This commit is contained in:
2026-01-06 16:35:13 -05:00
parent 1ee447feaf
commit 70c313c3cc
3 changed files with 180 additions and 182 deletions

View File

@@ -1,162 +1,163 @@
#include <iostream> #include <iostream>
#include "CommunicationRouter.h"
#include "AngleControlMessageBuilder.h" #include "AngleControlMessageBuilder.h"
#include "include/wireless/mDNSDiscoveryService.h" #include "CommunicationRouter.h"
#include "MPIMessageBuilder.h" #include "MPIMessageBuilder.h"
#include "include/wireless/WifiManager.h"
#include "freertos/FreeRTOS.h"
#include "Tables.h"
#include "PtrQueue.h"
#include "OrientationDetection.h" #include "OrientationDetection.h"
#include "PtrQueue.h"
#include "Tables.h"
#include "constants/module.h"
#include "freertos/FreeRTOS.h"
#include "freertos/idf_additions.h"
#include "freertos/projdefs.h"
#include "include/wireless/WifiManager.h"
#include "include/wireless/mDNSDiscoveryService.h"
#define TAG "CommunicationRouter" #define TAG "CommunicationRouter"
CommunicationRouter::~CommunicationRouter() { CommunicationRouter::~CommunicationRouter() { vTaskDelete(m_router_thread); }
vTaskDelete(m_router_thread);
}
// todo: we really need to change all char to uint8_t everywhere // todo: we really need to change all char to uint8_t everywhere
// todo: get rid of copying going on, need to pass around sharedptrs/uniqueptrs // todo: get rid of copying going on, need to pass around sharedptrs/uniqueptrs
[[noreturn]] void CommunicationRouter::router_thread(void *args) { [[noreturn]] void CommunicationRouter::router_thread(void *args) {
const auto that = static_cast<CommunicationRouter *>(args); const auto that = static_cast<CommunicationRouter *>(args);
while (true) { while (true) {
const auto buffer = that->m_tcp_rx_queue->dequeue(); const auto buffer = that->m_tcp_rx_queue->dequeue();
ESP_LOGD(TAG, "Got message from TCP"); ESP_LOGD(TAG, "Got message from TCP");
that->route(buffer->data(), buffer->size()); that->route(buffer->data(), buffer->size());
} }
} }
[[noreturn]] void CommunicationRouter::link_layer_thread(void *args) { [[noreturn]] void CommunicationRouter::link_layer_thread(void *args) {
const auto* params = static_cast<link_layer_thread_params *>(args); const auto that = static_cast<CommunicationRouter *>(args);
const auto that = params->router;
const auto channel = params->channel;
delete params;
size_t bytes_received = 0; while (true) {
that->m_data_link_manager->start_receive_frames(channel); for (uint8_t channel = 0;
while (true) { channel <
char buffer[512]; MODULE_TO_NUM_CHANNELS_MAP[that->m_config_manager.get_module_type()];
const auto err = that->m_data_link_manager->receive(reinterpret_cast<uint8_t *>(buffer), 512, &bytes_received, channel); channel++) {
that->m_data_link_manager->start_receive_frames(channel); uint16_t frame_size = 0;
FrameHeader frame_header{};
if (ESP_OK != that->m_data_link_manager->async_receive_info(
&frame_size, &frame_header, channel) ||
0 == frame_size) {
continue;
}
// todo: do we only want one thread ever doing this? std::vector<uint8_t> data{};
if (std::chrono::system_clock::now() - that->m_last_leader_updated > std::chrono::seconds(15)) { data.resize(frame_size);
that->m_last_leader_updated = std::chrono::system_clock::now(); that->m_data_link_manager->async_receive(data.data(), frame_size,
ESP_LOGI(TAG, "Updating leader"); &frame_header, channel);
that->update_leader(); that->route(data.data(), frame_size);
}
if (ESP_OK != err || bytes_received < 1) {
continue;
}
ESP_LOGD(TAG, "Got message from RMT");
that->route(reinterpret_cast<uint8_t *>(buffer), bytes_received);
} }
vTaskDelay(pdMS_TO_TICKS(50));
}
} }
int CommunicationRouter::send_msg(char* buffer, const size_t length) const { int CommunicationRouter::send_msg(char *buffer, const size_t length) const {
ESP_LOGD(TAG, "Got message from application"); ESP_LOGD(TAG, "Got message from application");
route(reinterpret_cast<uint8_t *>(buffer), length); route(reinterpret_cast<uint8_t *>(buffer), length);
return 0; return 0;
} }
void CommunicationRouter::update_leader() { void CommunicationRouter::update_leader() {
RIPRow_public table[RIP_MAX_ROUTES]; RIPRow_public table[RIP_MAX_ROUTES];
size_t table_size = RIP_MAX_ROUTES; size_t table_size = RIP_MAX_ROUTES;
this->m_data_link_manager->get_routing_table(table, &table_size); this->m_data_link_manager->get_routing_table(table, &table_size);
// Leader election (just get the highest id in rip) // Leader election (just get the highest id in rip)
std::vector<int> connected_module_ids; std::vector<int> connected_module_ids;
uint8_t max = m_module_id; uint8_t max = m_module_id;
for (int i = 0; i < table_size; i++) { for (int i = 0; i < table_size; i++) {
const auto id = table[i].info.board_id; const auto id = table[i].info.board_id;
connected_module_ids.emplace_back(id); connected_module_ids.emplace_back(id);
if (id > max) { // todo: change this to be correct if (id > max) { // todo: change this to be correct
max = id; max = id;
}
} }
}
// Leader has changed, we may need to change PC connection state // Leader has changed, we may need to change PC connection state
if (this->m_leader != max) { if (this->m_leader != max) {
if (max == m_module_id) { if (max == m_module_id) {
m_pc_connection->connect(); m_pc_connection->connect();
m_lossless_server->startup(); m_lossless_server->startup();
m_lossy_server->startup(); m_lossy_server->startup();
} else if (this->m_leader == m_module_id) { } else if (this->m_leader == m_module_id) {
m_pc_connection->disconnect(); m_pc_connection->disconnect();
m_lossless_server->shutdown(); m_lossless_server->shutdown();
m_lossless_server->shutdown(); m_lossless_server->shutdown();
}
} }
}
this->m_leader = max; this->m_leader = max;
if (this->m_leader == m_module_id) { if (this->m_leader == m_module_id) {
this->m_discovery_service->set_connected_boards(connected_module_ids); this->m_discovery_service->set_connected_boards(connected_module_ids);
} }
} }
void CommunicationRouter::route(uint8_t* buffer, const size_t length) const { void CommunicationRouter::route(uint8_t *buffer, const size_t length) const {
flatbuffers::Verifier verifier(buffer, length); flatbuffers::Verifier verifier(buffer, length);
// This could be moved to just be called on wireline data to save cpu cycles. // This could be moved to just be called on wireline data to save cpu cycles.
if (bool ok = Messaging::VerifyMPIMessageBuffer(verifier); !ok) { if (bool ok = Messaging::VerifyMPIMessageBuffer(verifier); !ok) {
ESP_LOGW(TAG, "route: got an invalid MPI message, disregarding"); ESP_LOGW(TAG, "route: got an invalid MPI message, disregarding");
return; return;
} }
if (const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer); mpi_message->destination() == m_module_id) { if (const auto &mpi_message =
ESP_LOGD(TAG, "Routing to this module [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length); Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer);
this->m_rx_callback(reinterpret_cast<char *>(buffer), 512); mpi_message->destination() == m_module_id) {
} else if (mpi_message->destination() == PC_ADDR && this->m_leader == m_module_id) { this->m_rx_callback(reinterpret_cast<char *>(buffer), 512);
ESP_LOGD(TAG, "Routing to wifi [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length); } else if (mpi_message->destination() == PC_ADDR &&
if (mpi_message->is_durable()) { this->m_leader == m_module_id) {
this->m_lossless_server->send_msg(reinterpret_cast<char *>(buffer), 512); if (mpi_message->is_durable()) {
} else { this->m_lossless_server->send_msg(reinterpret_cast<char *>(buffer), 512);
this->m_lossy_server->send_msg(reinterpret_cast<char *>(buffer), 512);
}
} else if (mpi_message->destination() == PC_ADDR) {
ESP_LOGD(TAG, "Routing to wireline for wifi [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length);
this->m_data_link_manager->send(this->m_leader, buffer, length, FrameType::MOTOR_TYPE, 0);
}else {
ESP_LOGD(TAG, "Routing to wireline [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length);
this->m_data_link_manager->send(mpi_message->destination(), buffer, length, FrameType::MOTOR_TYPE, 0);
}
}
std::pair<std::vector<uint8_t>, std::vector<Orientation>> CommunicationRouter::get_physically_connected_modules() const {
std::vector<RIPRow_public> table;
table.resize(RIP_MAX_ROUTES);
size_t table_size = RIP_MAX_ROUTES * sizeof(RIPRow_public);
m_data_link_manager->get_routing_table(table.data(), &table_size);
std::vector<uint8_t> connected_module_ids;
std::vector<Orientation> connected_module_orientations;
connected_module_ids.resize(MAX_WIRED_CONNECTIONS);
connected_module_orientations.resize(MAX_WIRED_CONNECTIONS);
for (int i = 0; i < MAX_WIRED_CONNECTIONS; i++) {
connected_module_ids[i] = 0; // this is not the PC ID here, marking as nc.
}
for (int i = 0; i < table_size; i++) {
if (table[i].info.hops == 1 && table[i].channel < MAX_WIRED_CONNECTIONS) {
connected_module_ids[table[i].channel] = table[i].info.board_id;
}
}
if (const auto id = connected_module_ids[0]; 0 == id) {
connected_module_orientations[0] = Orientation_Deg0;
} else { } else {
connected_module_orientations[0] = OrientationDetection::get_orientation(0); this->m_lossy_server->send_msg(reinterpret_cast<char *>(buffer), 512);
} }
} else if (mpi_message->destination() == PC_ADDR) {
this->m_data_link_manager->send(this->m_leader, buffer, length,
FrameType::MOTOR_TYPE, 0);
} else {
this->m_data_link_manager->send(mpi_message->destination(), buffer, length,
FrameType::MOTOR_TYPE, 0);
}
}
return { connected_module_ids, connected_module_orientations }; std::pair<std::vector<uint8_t>, std::vector<Orientation>>
CommunicationRouter::get_physically_connected_modules() const {
std::vector<RIPRow_public> table;
table.resize(RIP_MAX_ROUTES);
size_t table_size = RIP_MAX_ROUTES * sizeof(RIPRow_public);
m_data_link_manager->get_routing_table(table.data(), &table_size);
std::vector<uint8_t> connected_module_ids;
std::vector<Orientation> connected_module_orientations;
connected_module_ids.resize(MAX_WIRED_CONNECTIONS);
connected_module_orientations.resize(MAX_WIRED_CONNECTIONS);
for (int i = 0; i < MAX_WIRED_CONNECTIONS; i++) {
connected_module_ids[i] = 0; // this is not the PC ID here, marking as nc.
}
for (int i = 0; i < table_size; i++) {
if (table[i].info.hops == 1 && table[i].channel < MAX_WIRED_CONNECTIONS) {
connected_module_ids[table[i].channel] = table[i].info.board_id;
}
}
if (const auto id = connected_module_ids[0]; 0 == id) {
connected_module_orientations[0] = Orientation_Deg0;
} else {
connected_module_orientations[0] = OrientationDetection::get_orientation(0);
}
return {connected_module_ids, connected_module_orientations};
} }
[[nodiscard]] uint8_t CommunicationRouter::get_leader() const { [[nodiscard]] uint8_t CommunicationRouter::get_leader() const {
return this->m_leader; return this->m_leader;
} }

View File

@@ -7,79 +7,77 @@
#ifndef COMMUNICATIONROUTER_H #ifndef COMMUNICATIONROUTER_H
#define COMMUNICATIONROUTER_H #define COMMUNICATIONROUTER_H
#include <chrono>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <chrono>
#include "CommunicationFactory.h" #include "CommunicationFactory.h"
#include "IDiscoveryService.h"
#include "ConfigManager.h" #include "ConfigManager.h"
#include "OrientationDetection.h"
#include "wireless/WifiManager.h"
#include "wireless/TCPServer.h"
#include "DataLinkManager.h" #include "DataLinkManager.h"
#include "constants/module.h" #include "IDiscoveryService.h"
#include "OrientationDetection.h"
#include "PtrQueue.h" #include "PtrQueue.h"
#include "constants/module.h"
#include "wireless/TCPServer.h"
#include "wireless/WifiManager.h"
class CommunicationRouter { class CommunicationRouter {
class link_layer_thread_params {
public:
link_layer_thread_params(CommunicationRouter* router, const uint8_t channel) : router(router), channel(channel) {};
CommunicationRouter *router;
uint8_t channel;
};
public: public:
explicit CommunicationRouter(const std::function<void(char*, int)> &rx_callback) explicit CommunicationRouter(
: m_tcp_rx_queue(std::make_shared<PtrQueue<std::vector<uint8_t>>>(10)), const std::function<void(char *, int)> &rx_callback)
m_rx_callback(rx_callback), : m_tcp_rx_queue(std::make_shared<PtrQueue<std::vector<uint8_t>>>(10)),
m_config_manager(ConfigManager::get_instance()), m_rx_callback(rx_callback),
m_pc_connection(CommunicationFactory::create_connection_manager(m_config_manager.get_communication_method())), m_config_manager(ConfigManager::get_instance()),
m_lossless_server(CommunicationFactory::create_lossless_server(m_config_manager.get_communication_method(), m_tcp_rx_queue)), m_pc_connection(CommunicationFactory::create_connection_manager(
m_lossy_server(CommunicationFactory::create_lossy_server(m_config_manager.get_communication_method(), m_tcp_rx_queue)), m_config_manager.get_communication_method())),
m_data_link_manager(std::make_unique<DataLinkManager>(m_config_manager.get_module_id(), MODULE_TO_NUM_CHANNELS_MAP[m_config_manager.get_module_type()])), m_lossless_server(CommunicationFactory::create_lossless_server(
m_module_id(m_config_manager.get_module_id()), m_config_manager.get_communication_method(), m_tcp_rx_queue)),
m_last_leader_updated(std::chrono::system_clock::now()), m_lossy_server(CommunicationFactory::create_lossy_server(
m_discovery_service(CommunicationFactory::create_discovery_service(m_config_manager.get_communication_method())){ m_config_manager.get_communication_method(), m_tcp_rx_queue)),
OrientationDetection::init(); m_data_link_manager(std::make_unique<DataLinkManager>(
update_leader(); m_config_manager.get_module_id(),
MODULE_TO_NUM_CHANNELS_MAP[m_config_manager.get_module_type()])),
m_module_id(m_config_manager.get_module_id()),
m_last_leader_updated(std::chrono::system_clock::now()),
m_discovery_service(CommunicationFactory::create_discovery_service(
m_config_manager.get_communication_method())) {
OrientationDetection::init();
update_leader();
xTaskCreate(router_thread, "router", 4096, this, 3, &this->m_router_thread); xTaskCreate(router_thread, "router", 4096, this, 3, &this->m_router_thread);
xTaskCreate(link_layer_thread, "router_rmt", 4096, this, 3,
&this->m_link_layer_thread);
}
// const auto num_channels = MODULE_TO_NUM_CHANNELS_MAP[m_config_manager.get_module_type()]; ~CommunicationRouter();
// this->m_link_layer_threads.resize(num_channels);
// for (int i = 0; i < num_channels; i++) {
// auto *params = new link_layer_thread_params(this, i);
// xTaskCreate(link_layer_thread, "router_rmt", 4096, params, 3, &this->m_link_layer_threads[i]);
// }
}
~CommunicationRouter(); [[noreturn]] static void router_thread(void *args);
[[noreturn]] static void link_layer_thread(void *args);
int send_msg(char *buffer, size_t length) const;
void update_leader();
void route(uint8_t *buffer, size_t length) const;
[[nodiscard]] std::pair<std::vector<uint8_t>, std::vector<Orientation>>
get_physically_connected_modules() const;
[[nodiscard]] uint8_t get_leader() const;
[[noreturn]] static void router_thread(void *args); // todo: does this really need to be here (so i can access from thread)?
[[noreturn]] static void link_layer_thread(void *args); std::shared_ptr<PtrQueue<std::vector<uint8_t>>>
int send_msg(char* buffer, size_t length) const; m_tcp_rx_queue; // todo: this should probably be thread safe
void update_leader(); std::function<void(char *, int)> m_rx_callback;
void route(uint8_t *buffer, size_t length) const;
[[nodiscard]] std::pair<std::vector<uint8_t>, std::vector<Orientation>> get_physically_connected_modules() const;
[[nodiscard]] uint8_t get_leader() const;
// todo: does this really need to be here (so i can access from thread)?
std::shared_ptr<PtrQueue<std::vector<uint8_t>>> m_tcp_rx_queue; // todo: this should probably be thread safe
std::function<void(char*, int)> m_rx_callback;
private: private:
TaskHandle_t m_router_thread = nullptr; TaskHandle_t m_router_thread = nullptr;
ConfigManager &m_config_manager; TaskHandle_t m_link_layer_thread = nullptr;
std::unique_ptr<IConnectionManager> m_pc_connection; ConfigManager &m_config_manager;
std::vector<TaskHandle_t> m_link_layer_threads; std::unique_ptr<IConnectionManager> m_pc_connection;
std::unique_ptr<IRPCServer> m_lossless_server; std::unique_ptr<IRPCServer> m_lossless_server;
std::unique_ptr<IRPCServer> m_lossy_server; std::unique_ptr<IRPCServer> m_lossy_server;
std::unique_ptr<DataLinkManager> m_data_link_manager; std::unique_ptr<DataLinkManager> m_data_link_manager;
uint8_t m_leader = 0; uint8_t m_leader = 0;
uint8_t m_module_id; uint8_t m_module_id;
std::chrono::time_point<std::chrono::system_clock> m_last_leader_updated; std::chrono::time_point<std::chrono::system_clock> m_last_leader_updated;
std::unique_ptr<IDiscoveryService> m_discovery_service; std::unique_ptr<IDiscoveryService> m_discovery_service;
}; };
#endif //COMMUNICATIONROUTER_H #endif // COMMUNICATIONROUTER_H

View File

@@ -12,7 +12,7 @@ if(DEFINED RMT_TEST AND RMT_TEST EQUAL 1)
idf_build_set_property(COMPILE_DEFINITIONS RMT_TEST APPEND) idf_build_set_property(COMPILE_DEFINITIONS RMT_TEST APPEND)
else() else()
list(REMOVE_ITEM ALL_SRCS list(REMOVE_ITEM ALL_SRCS
"${CMAKE_CURRENT_SOURCE_DIR}/main.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/main_rmt_test.cpp"
) )
endif() endif()
@@ -34,4 +34,3 @@ if(DEFINED RECEIVE_ONLY AND RECEIVE_ONLY)
message(STATUS "Building for receive only") message(STATUS "Building for receive only")
idf_build_set_property(COMPILE_DEFINITIONS RECEIVE_ONLY=${RECEIVE_ONLY} APPEND) idf_build_set_property(COMPILE_DEFINITIONS RECEIVE_ONLY=${RECEIVE_ONLY} APPEND)
endif() endif()