From 872eacb529c06c2a307d31c9f30283f16df4e853 Mon Sep 17 00:00:00 2001 From: chiyu1468 Date: Thu, 7 Dec 2023 14:27:02 +0800 Subject: [PATCH] GlobalVariable Pass Telemetry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 這版捨棄 future promise 傳遞 改用全域變數傳遞資料 --- mavone/mavone.cpp | 135 ++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 71 deletions(-) diff --git a/mavone/mavone.cpp b/mavone/mavone.cpp index c5b6dac..4235379 100644 --- a/mavone/mavone.cpp +++ b/mavone/mavone.cpp @@ -31,14 +31,14 @@ struct MavInitParameter { struct systemHandlerInfo { int systemID; std::thread systemThread; - std::future> telemetryInfo_fut; - -}; - +}; +// 我無法解決跨執行緒的 promise 變數生命週期問題 +// 用最智障的全域變數 (砍掉 promise 結構簡單好多) +std::map> gTelemetryInfo; int initializeParameters(const std::string& filePath, MavInitParameter& initSetting) { @@ -85,57 +85,73 @@ int initializeParameters(const std::string& filePath, MavInitParameter& initSett * Important multi-threaded functions, * handling system initialization, receiving messages and sending messages. */ -void systemHandler(System& system, std::promise>& telemetryInfo_pro) { +void systemHandler(System& system) { // Initialization auto telemetry = Telemetry{system}; auto action = Action{system}; - std::cout << "System " << static_cast(system.get_system_id()) << " get in Thread.(debug)" << std::endl; // debug + std::cout << "System " << static_cast(system.get_system_id()) << " get in Thread. (debug)" << std::endl; // debug // Store telemetry information - std::map telemetryInfo; + std::map telemetryInfo; - // dev del S - std::cout << "mark tA" << std::endl; - std::future> telemetryInfo_future = telemetryInfo_pro.get_future(); // dev del + // Put all the subscriber init in this section + telemetry.subscribe_position([&system, &telemetryInfo](Telemetry::Position position) { + std::cout << "System ID: " << static_cast(system.get_system_id()) << " Altitude: " << position.relative_altitude_m << " m\n"; // debug - std::cout << "mark tB" << std::endl; - if (!telemetryInfo_future.valid()) { - std::cout << "telemetryInfo_pro is not valid" << std::endl; - } + // Store telemetry information + telemetryInfo["altitude"] = position.relative_altitude_m; + gTelemetryInfo[static_cast(system.get_system_id())] = telemetryInfo; + }); - telemetryInfo["test"] = 3.14; - telemetryInfo_pro.set_value(telemetryInfo); - std::cout << "mark tC" << std::endl; - if (!telemetryInfo_future.valid()) { - std::cout << "telemetryInfo_pro is not valid" << std::endl; - } + // Loop - - // dev del E + // Destroy - telemetry.subscribe_position([&system, &telemetryInfo, &telemetryInfo_pro](Telemetry::Position position) { - std::cout << "System ID: " << static_cast(system.get_system_id()) << " Altitude: " << position.relative_altitude_m << " m\n"; // debug - // Store telemetry information - telemetryInfo["altitude"] = position.relative_altitude_m; - // Add more telemetry information as needed + sleep_for(seconds(5)); + // Thread Terminate + std::cout << "Thread Out " << static_cast(system.get_system_id()) << std::endl; +} - // Set the telemetry information in the promise - std::cout << "mark tsA" << std::endl; - telemetryInfo_pro.set_value(telemetryInfo); - std::cout << "mark tsB" << std::endl; +// class systemHandler { - sleep_for(seconds(1)); // debug - }); +// private: +// Telemetry telemetry; +// Action action; - sleep_for(seconds(5)); // debug - std::cout << "Thread Out " << static_cast(system.get_system_id()) << std::endl; +// public: +// std::map telemetryInfo; +// std::map actionCommand; + +// // Constructor +// systemHandler(System& system) { +// // Initialization +// telemetry = Telemetry{system}; +// action = Action{system}; +// std::cout << "System " << static_cast(system.get_system_id()) << " captured. (debug)" << std::endl; // debug + + +// telemetry.subscribe_position([&system, &telemetryInfo](Telemetry::Position position) { +// std::cout << "System ID: " << static_cast(system.get_system_id()) << " Altitude: " << position.relative_altitude_m << " m\n"; // debug + +// // Store telemetry information +// telemetryInfo["altitude"] = position.relative_altitude_m; +// }); + +// } + + +// // Loop +// void operator()() { + +// std::cout << "Thread is running" << std::endl; +// } +// } -} int main(int argc, char** argv) { @@ -153,10 +169,6 @@ int main(int argc, char** argv) std::cerr << "Failed to open config file: " << argv[1] << std::endl; } - - - - // Start MAVSDK Server and connect to it. Mavsdk mavsdk; ConnectionResult connection_result = mavsdk.add_any_connection(initSetting.connectPort); @@ -189,35 +201,15 @@ int main(int argc, char** argv) System& new_system = *sys; std::cout << "system detect : " << static_cast(new_system.get_system_id()) << "(Debug)"<< std::endl; // debug - // - // std::map telemetryInfo; - auto telemetryInfo_pro = std::promise>{}; - // auto telemetryInfo_fut = telemetryInfo_pro.get_future(); - - - // dev del S - - auto telemetryInfo_pro1 = std::promise>{}; - auto telemetryInfo_fut1 = telemetryInfo_pro.get_future(); - - std::thread worker([&telemetryInfo_pro1]() { - // 執行任務並將結果回傳至 promise - std::map result; - result["test"]=3.14; - telemetryInfo_pro1.set_value(result); - }); - worker.join(); - // dev del E - - // Let handler progrem deal with System - std::thread systemHandleThread([&new_system, &telemetryInfo_pro]() {systemHandler(new_system, telemetryInfo_pro);}); + std::thread systemHandleThread([&new_system]() {systemHandler(new_system);}); systemHandlerInfos.push_back(systemHandlerInfo{ static_cast(new_system.get_system_id()), std::move(systemHandleThread), - std::move(telemetryInfo_fut1) + }); + }); // MySQL connection is here @@ -228,14 +220,15 @@ int main(int argc, char** argv) // -- 從 systemHandlerInfos 中 輪巡所有 system 檢查並處理是否有需要丟到 mysql 的資料 // -- 從 mysql 抓到指令 丟到 systemHandlerInfo - // for (const auto& info : systemHandlerInfos) { - // // Access info.systemID, info.systemHandleThread, info.telemetryInfo_fut - // // Perform operations on each object in systemHandlerInfos - // info.telemetryInfo_fut.wait(); - // auto x = info.telemetryInfo_fut.get(); - // std::cout << "mark D :" << info.systemID << "; " << x["altitude"] << std::endl; - // } + sleep_for(seconds(3)); + + std::cout << "mark A" << gTelemetryInfo[1]["altitude"] << std::endl; + + // make all thread join + for( auto& info : systemHandlerInfos){ + info.systemThread.join(); + } // ============================================================================== @@ -422,7 +415,7 @@ int main(int argc, char** argv) // } // Let it hover for a bit before landing again. - sleep_for(seconds(15)); + sleep_for(seconds(5)); // std::cout << "Landing...\n"; // const Action::Result land_result = action.land();