mirror of
https://github.com/BotChain-Robots/control.git
synced 2026-03-09 16:22:26 +01:00
Add more examples
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -50,3 +50,5 @@ CMakeCache.txt
|
|||||||
/examples/display/build
|
/examples/display/build
|
||||||
*.log
|
*.log
|
||||||
/examples/rpc_call/build
|
/examples/rpc_call/build
|
||||||
|
/examples/bandwidth/build
|
||||||
|
/examples/servo/build
|
||||||
|
|||||||
19
examples/bandwidth/CMakeLists.txt
Normal file
19
examples/bandwidth/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(BandwidthExample)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
find_package(libcontrol REQUIRED)
|
||||||
|
find_package(spdlog REQUIRED)
|
||||||
|
find_package(librpc REQUIRED)
|
||||||
|
|
||||||
|
add_executable(BandwidthExample main.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(BandwidthExample
|
||||||
|
PRIVATE
|
||||||
|
libcontrol::libcontrol
|
||||||
|
spdlog::spdlog
|
||||||
|
librpc::librpc
|
||||||
|
)
|
||||||
9
examples/bandwidth/CMakeUserPresets.json
Normal file
9
examples/bandwidth/CMakeUserPresets.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"version": 4,
|
||||||
|
"vendor": {
|
||||||
|
"conan": {}
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"build/CMakePresets.json"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
examples/bandwidth/README.md
Normal file
10
examples/bandwidth/README.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Servo Example
|
||||||
|
|
||||||
|
## Compiling
|
||||||
|
```
|
||||||
|
conan install . --build=missing --output-folder=build -s build_type=Release
|
||||||
|
cmake -S . -B "build" -DCMAKE_TOOLCHAIN_FILE="build/conan_toolchain.cmake" -DCMAKE_BUILD_TYPE="Release"
|
||||||
|
cmake --build "./build" --config "Release"
|
||||||
|
|
||||||
|
./build/BandwidthExample
|
||||||
|
```
|
||||||
4
examples/bandwidth/build.sh
Executable file
4
examples/bandwidth/build.sh
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
conan install . --build=missing --output-folder=build -s build_type=Release
|
||||||
|
cmake -S . -B "build" -DCMAKE_TOOLCHAIN_FILE="build/conan_toolchain.cmake" -DCMAKE_BUILD_TYPE="Release"
|
||||||
|
cmake --build "./build" --config "Release"
|
||||||
|
|
||||||
9
examples/bandwidth/conanfile.txt
Normal file
9
examples/bandwidth/conanfile.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[requires]
|
||||||
|
libcontrol/1.0.0
|
||||||
|
flatbuffers/24.12.23
|
||||||
|
spdlog/1.16.0
|
||||||
|
librpc/1.1.7
|
||||||
|
|
||||||
|
[generators]
|
||||||
|
CMakeDeps
|
||||||
|
CMakeToolchain
|
||||||
31
examples/bandwidth/main.cpp
Normal file
31
examples/bandwidth/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <format>
|
||||||
|
#include <iostream>
|
||||||
|
#include <random>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "libcontrol.h"
|
||||||
|
|
||||||
|
#define TARGET_MODULE 150
|
||||||
|
#define METADATA_TAG 7
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
const auto messaging_interface = std::make_unique<MessagingInterface>();
|
||||||
|
|
||||||
|
const auto modules = messaging_interface->find_connected_modules(std::chrono::seconds(3));
|
||||||
|
|
||||||
|
if (!modules.contains(TARGET_MODULE)) {
|
||||||
|
std::cout << "Could not find target module " << TARGET_MODULE << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> vec(1024, 'A');
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
messaging_interface->send(vec.data(), vec.size(), TARGET_MODULE, METADATA_TAG, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
//
|
//
|
||||||
// Created by Johnathon Slightham on 2026-02-16.
|
// Created by Johnathon Slightham on 2026-02-16.
|
||||||
//
|
//
|
||||||
@@ -21,6 +22,7 @@ class RemoteManagement {
|
|||||||
m_robot_controller(controller) {
|
m_robot_controller(controller) {
|
||||||
}
|
}
|
||||||
bool perform_ota();
|
bool perform_ota();
|
||||||
|
double ota_progress(); // 0 to 1 representing % progress.
|
||||||
void restart();
|
void restart();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -29,6 +31,7 @@ class RemoteManagement {
|
|||||||
bool ota_end();
|
bool ota_end();
|
||||||
|
|
||||||
uint16_t m_sequence_num = 0;
|
uint16_t m_sequence_num = 0;
|
||||||
|
uint16_t m_total_packets = 0;
|
||||||
uint8_t m_module_id;
|
uint8_t m_module_id;
|
||||||
std::ifstream m_file;
|
std::ifstream m_file;
|
||||||
std::unique_ptr<Flatbuffers::OTAPacketBuilder> m_builder;
|
std::unique_ptr<Flatbuffers::OTAPacketBuilder> m_builder;
|
||||||
|
|||||||
@@ -9,40 +9,51 @@
|
|||||||
#include "libcontrol.h"
|
#include "libcontrol.h"
|
||||||
#include "rpc/RemoteManagement.h"
|
#include "rpc/RemoteManagement.h"
|
||||||
|
|
||||||
|
void progress_monitor_task(std::shared_ptr<RemoteManagement> rm, uint8_t module_id) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void task(std::shared_ptr<RobotController> controller, uint8_t module_id, const std::string& filepath) {
|
||||||
|
auto rm = std::make_shared<RemoteManagement>(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";
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
// const auto messaging_interface = std::make_unique<MessagingInterface>();
|
|
||||||
|
|
||||||
// const auto modules = messaging_interface->find_connected_modules(std::chrono::seconds(3));
|
|
||||||
|
|
||||||
// std::cout << "Found " << modules.size() << " modules" << std::endl;
|
|
||||||
|
|
||||||
// for (const auto module : modules) {
|
|
||||||
// std::cout << "Found ID " << (int)module << std::endl;
|
|
||||||
// }
|
|
||||||
|
|
||||||
const auto robot_controller = std::make_shared<RobotController>();
|
const auto robot_controller = std::make_shared<RobotController>();
|
||||||
robot_controller->fetchDirectlyConnectedModules(true);
|
robot_controller->fetchDirectlyConnectedModules(true);
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
|
|
||||||
// const auto function_tag = 3;
|
std::vector<uint8_t> to_update{};
|
||||||
// const auto module = 99;
|
for (const auto& maybe_module : robot_controller->getModules()) {
|
||||||
// std::string msg = "Hello world!";
|
if (const auto &module = maybe_module.lock()) {
|
||||||
// std::vector<uint8_t> parameters(msg.begin(), msg.end());
|
if (module->get_leader() != module->get_device_id()) { continue; }
|
||||||
// auto maybe_return_value = messaging_interface->remote_call(function_tag, module, parameters);
|
std::cout << "Updating module " << (int)module->get_device_id();
|
||||||
|
to_update.emplace_back(module->get_device_id());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if (maybe_return_value) {
|
if (to_update.size() < 1) {
|
||||||
// auto return_value = std::move(*maybe_return_value);
|
std::cout << "No modules found to update" << std::endl;
|
||||||
// std::cout << "Got return value " << (char *)return_value->data() << std::endl;
|
return 0;
|
||||||
// } else {
|
}
|
||||||
// std::cout << "Function call time out" << std::endl;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
|
|
||||||
std::string filename =
|
std::string filename =
|
||||||
"/Users/jslightham/Documents/Classes/capstone/firmware/build/firmware.bin";
|
"/Users/jslightham/Documents/Classes/capstone/firmware/build/firmware.bin";
|
||||||
const auto rm = std::make_unique<RemoteManagement>(99, filename, robot_controller);
|
std::vector<std::thread> threads;
|
||||||
rm->perform_ota();
|
for (int i = 0; i < to_update.size(); i++) {
|
||||||
|
threads.emplace_back(task, robot_controller, to_update[i], filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &t : threads) {
|
||||||
|
t.join();
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ bool RemoteManagement::perform_ota() {
|
|||||||
m_file.seekg(0, std::ios::end);
|
m_file.seekg(0, std::ios::end);
|
||||||
std::streamsize total_size = m_file.tellg();
|
std::streamsize total_size = m_file.tellg();
|
||||||
m_file.seekg(0, std::ios::beg);
|
m_file.seekg(0, std::ios::beg);
|
||||||
|
m_total_packets = total_size/OTA_CHUNK_SIZE;
|
||||||
// std::cout << "Total number of chunks: " << total_size/OTA_CHUNK_SIZE << std::endl;
|
// std::cout << "Total number of chunks: " << total_size/OTA_CHUNK_SIZE << std::endl;
|
||||||
|
|
||||||
while (m_file) {
|
while (m_file) {
|
||||||
@@ -96,3 +97,14 @@ bool RemoteManagement::ota_end() {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double RemoteManagement::ota_progress() {
|
||||||
|
if (m_total_packets < 1) {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
if (m_sequence_num >= m_total_packets) {
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return static_cast<double>(m_sequence_num) / static_cast<double>(m_total_packets);
|
||||||
|
}
|
||||||
|
|||||||
19
examples/servo/CMakeLists.txt
Normal file
19
examples/servo/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(ServoExample)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
find_package(libcontrol REQUIRED)
|
||||||
|
find_package(spdlog REQUIRED)
|
||||||
|
find_package(librpc REQUIRED)
|
||||||
|
|
||||||
|
add_executable(ServoExample main.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(ServoExample
|
||||||
|
PRIVATE
|
||||||
|
libcontrol::libcontrol
|
||||||
|
spdlog::spdlog
|
||||||
|
librpc::librpc
|
||||||
|
)
|
||||||
9
examples/servo/CMakeUserPresets.json
Normal file
9
examples/servo/CMakeUserPresets.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"version": 4,
|
||||||
|
"vendor": {
|
||||||
|
"conan": {}
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"build/CMakePresets.json"
|
||||||
|
]
|
||||||
|
}
|
||||||
10
examples/servo/README.md
Normal file
10
examples/servo/README.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Servo Example
|
||||||
|
|
||||||
|
## Compiling
|
||||||
|
```
|
||||||
|
conan install . --build=missing --output-folder=build -s build_type=Release
|
||||||
|
cmake -S . -B "build" -DCMAKE_TOOLCHAIN_FILE="build/conan_toolchain.cmake" -DCMAKE_BUILD_TYPE="Release"
|
||||||
|
cmake --build "./build" --config "Release"
|
||||||
|
|
||||||
|
./build/ServoExample
|
||||||
|
```
|
||||||
4
examples/servo/build.sh
Executable file
4
examples/servo/build.sh
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
conan install . --build=missing --output-folder=build -s build_type=Release
|
||||||
|
cmake -S . -B "build" -DCMAKE_TOOLCHAIN_FILE="build/conan_toolchain.cmake" -DCMAKE_BUILD_TYPE="Release"
|
||||||
|
cmake --build "./build" --config "Release"
|
||||||
|
|
||||||
9
examples/servo/conanfile.txt
Normal file
9
examples/servo/conanfile.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[requires]
|
||||||
|
libcontrol/1.0.0
|
||||||
|
flatbuffers/24.12.23
|
||||||
|
spdlog/1.16.0
|
||||||
|
librpc/1.1.7
|
||||||
|
|
||||||
|
[generators]
|
||||||
|
CMakeDeps
|
||||||
|
CMakeToolchain
|
||||||
41
examples/servo/main.cpp
Normal file
41
examples/servo/main.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <format>
|
||||||
|
#include <iostream>
|
||||||
|
#include <random>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "libcontrol.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
const auto controller = std::make_unique<RobotController>();
|
||||||
|
|
||||||
|
controller->fetchDirectlyConnectedModules(true);
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
|
||||||
|
std::cout << "Found " << controller->getModules().size() << " modules" << std::endl;
|
||||||
|
|
||||||
|
std::random_device rd;
|
||||||
|
std::mt19937 gen(rd());
|
||||||
|
std::uniform_int_distribution<> dist(0, 10);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
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) {
|
||||||
|
int randomNumber = dist(gen);
|
||||||
|
if (module->get_position() > 90) {
|
||||||
|
module->actuate(70 - randomNumber);
|
||||||
|
} else {
|
||||||
|
module->actuate(110 + randomNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user