From 894a8131d4f4e1949d44dad7570c5c97e7a48a09 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Tue, 10 Mar 2026 18:14:23 -0400 Subject: [PATCH] Bulk OTa --- examples/rpc_call/conanfile.txt | 4 +-- examples/rpc_call/main.cpp | 30 ++++++++++++++-------- examples/rpc_call/rpc/RemoteManagement.cpp | 16 +++++++----- examples/servo/conanfile.txt | 4 +-- examples/servo/main.cpp | 11 +++++--- include/actuators/Actuator.h | 6 ++--- include/actuators/PositionalActuator1D.h | 2 +- 7 files changed, 45 insertions(+), 28 deletions(-) diff --git a/examples/rpc_call/conanfile.txt b/examples/rpc_call/conanfile.txt index a61eff3..f578d99 100644 --- a/examples/rpc_call/conanfile.txt +++ b/examples/rpc_call/conanfile.txt @@ -1,8 +1,8 @@ [requires] -libcontrol/1.0.0 +libcontrol/1.0.3 flatbuffers/24.12.23 spdlog/1.16.0 -librpc/1.1.7 +librpc/1.1.8 [generators] CMakeDeps diff --git a/examples/rpc_call/main.cpp b/examples/rpc_call/main.cpp index 38d9652..3c3ad9e 100644 --- a/examples/rpc_call/main.cpp +++ b/examples/rpc_call/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -10,19 +11,27 @@ #include "rpc/RemoteManagement.h" void progress_monitor_task(std::shared_ptr rm, uint8_t module_id) { + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist6(10, 100); + + std::cout << dist6(rng) << std::endl; while (true) { - std::this_thread::sleep_for(std::chrono::seconds(5)); - std::cout << "Module " << (int)module_id << ": " << rm->ota_progress() * 100 << "% complete\n"; - if (rm->ota_progress() >= 1.0) return; + std::this_thread::sleep_for(std::chrono::milliseconds(5000 + dist6(rng))); + std::cout << "Module " << (int)module_id << ": " << rm->ota_progress() * 100 << "% complete" + << std::endl; + if (rm->ota_progress() >= 1.0) + return; } } -void task(std::shared_ptr controller, uint8_t module_id, const std::string& filepath) { +void task(std::shared_ptr controller, uint8_t module_id, + const std::string &filepath) { auto rm = std::make_shared(module_id, filepath, controller); std::thread t(progress_monitor_task, rm, module_id); rm->perform_ota(); t.join(); - std::cout << "Done updating " << (int)module_id << "\n"; + std::cout << "Done updating " << (int)module_id << std::endl; } int main() { @@ -31,10 +40,12 @@ int main() { std::this_thread::sleep_for(std::chrono::seconds(5)); std::vector to_update{}; - for (const auto& maybe_module : robot_controller->getModules()) { + for (const auto &maybe_module : robot_controller->getModules()) { if (const auto &module = maybe_module.lock()) { - if (module->get_leader() != module->get_device_id()) { continue; } - std::cout << "Updating module " << (int)module->get_device_id(); + if (module->get_leader() != module->get_device_id()) { + continue; + } + std::cout << "Updating module " << (int)module->get_device_id() << std::endl; to_update.emplace_back(module->get_device_id()); } } @@ -44,8 +55,7 @@ int main() { return 0; } - std::string filename = - "/Users/jslightham/Documents/Classes/capstone/firmware/build/firmware.bin"; + std::string filename = "/home/jslightham/Documents/capstone/firmware/build/firmware.bin"; std::vector threads; for (int i = 0; i < to_update.size(); i++) { threads.emplace_back(task, robot_controller, to_update[i], filename); diff --git a/examples/rpc_call/rpc/RemoteManagement.cpp b/examples/rpc_call/rpc/RemoteManagement.cpp index 9ab0382..e6f573c 100644 --- a/examples/rpc_call/rpc/RemoteManagement.cpp +++ b/examples/rpc_call/rpc/RemoteManagement.cpp @@ -23,7 +23,7 @@ bool RemoteManagement::perform_ota() { m_file.seekg(0, std::ios::end); std::streamsize total_size = m_file.tellg(); m_file.seekg(0, std::ios::beg); - m_total_packets = total_size/OTA_CHUNK_SIZE; + m_total_packets = total_size / OTA_CHUNK_SIZE; // std::cout << "Total number of chunks: " << total_size/OTA_CHUNK_SIZE << std::endl; while (m_file) { @@ -62,11 +62,15 @@ void RemoteManagement::restart() { bool RemoteManagement::start_ota() { // std::cout << "Starting OTA" << std::endl; - const auto maybe = m_robot_controller->remote_call(4, m_module_id, {}); - if (maybe) { - // std::cout << "Got valid response" << std::endl; - m_sequence_num = 1; - return (*maybe)->at(0) > 0; + int attempts = 0; + while (attempts < MAX_PACKET_TX_ATTEMPTS) { + const auto maybe = m_robot_controller->remote_call(4, m_module_id, {}); + if (maybe) { + // std::cout << "Got valid response" << std::endl; + m_sequence_num = 1; + return (*maybe)->at(0) > 0; + } + attempts++; } return false; } diff --git a/examples/servo/conanfile.txt b/examples/servo/conanfile.txt index a61eff3..f578d99 100644 --- a/examples/servo/conanfile.txt +++ b/examples/servo/conanfile.txt @@ -1,8 +1,8 @@ [requires] -libcontrol/1.0.0 +libcontrol/1.0.3 flatbuffers/24.12.23 spdlog/1.16.0 -librpc/1.1.7 +librpc/1.1.8 [generators] CMakeDeps diff --git a/examples/servo/main.cpp b/examples/servo/main.cpp index 7b2bdc2..9a28b48 100644 --- a/examples/servo/main.cpp +++ b/examples/servo/main.cpp @@ -24,17 +24,20 @@ int main() { std::cout << "Found " << controller->getModules().size() << " modules" << std::endl; for (const auto &maybe_module : controller->getModules()) { if (const auto &module = maybe_module.lock()) { - if (module->get_type() == ModuleType_DC_MOTOR) { + if (module->get_type() == ModuleType_SERVO_2 || + module->get_type() == ModuleType_SERVO_1) { + std::cout << "Actuating " << (int)module->get_device_id() << " leader " + << (int)module->get_leader() << std::endl; int randomNumber = dist(gen); if (module->get_position() > 90) { - module->actuate(70 - randomNumber); + module->actuate(50 - randomNumber); } else { - module->actuate(110 + randomNumber); + module->actuate(150 + randomNumber); } + std::this_thread::sleep_for(std::chrono::milliseconds(250)); } } } - std::this_thread::sleep_for(std::chrono::milliseconds(100)); } return 0; diff --git a/include/actuators/Actuator.h b/include/actuators/Actuator.h index 90c7ddf..27c9654 100644 --- a/include/actuators/Actuator.h +++ b/include/actuators/Actuator.h @@ -10,13 +10,13 @@ class Actuator : public Module { public: - explicit Actuator(uint8_t device_id) : Module(device_id) {}; + explicit Actuator(uint8_t device_id) : Module(device_id){}; - Actuator(uint8_t device_id, ModuleType module_type) : Module(device_id, module_type) {}; + Actuator(uint8_t device_id, ModuleType module_type) : Module(device_id, module_type){}; Actuator(uint8_t device_id, ModuleType module_type, Messaging::ConnectionType connection_type, uint8_t leader) - : Module(device_id, module_type, connection_type, leader) {}; + : Module(device_id, module_type, connection_type, leader){}; protected: virtual std::vector get_actuation_message() = 0; diff --git a/include/actuators/PositionalActuator1D.h b/include/actuators/PositionalActuator1D.h index 215f8ce..a7644b8 100644 --- a/include/actuators/PositionalActuator1D.h +++ b/include/actuators/PositionalActuator1D.h @@ -16,7 +16,7 @@ class PositionalActuator1D final : public Actuator { public: PositionalActuator1D(uint8_t device_id, ModuleType type) : Actuator(device_id, type), - m_acm_builder(std::make_unique()) {}; + m_acm_builder(std::make_unique()){}; double get_position() override; void actuate(double position) override;