GlobalVariable Pass Telemetry

這版捨棄 future promise 傳遞 改用全域變數傳遞資料
master
chiyu1468 2 years ago
parent e61388992a
commit 872eacb529

@ -31,14 +31,14 @@ struct MavInitParameter {
struct systemHandlerInfo { struct systemHandlerInfo {
int systemID; int systemID;
std::thread systemThread; std::thread systemThread;
std::future<std::map<std::string, float>> telemetryInfo_fut;
};
};
// 我無法解決跨執行緒的 promise 變數生命週期問題
// 用最智障的全域變數 (砍掉 promise 結構簡單好多)
std::map<int,std::map<std::string,float>> gTelemetryInfo;
int initializeParameters(const std::string& filePath, MavInitParameter& initSetting) { int initializeParameters(const std::string& filePath, MavInitParameter& initSetting) {
@ -85,57 +85,73 @@ int initializeParameters(const std::string& filePath, MavInitParameter& initSett
* Important multi-threaded functions, * Important multi-threaded functions,
* handling system initialization, receiving messages and sending messages. * handling system initialization, receiving messages and sending messages.
*/ */
void systemHandler(System& system, std::promise<std::map<std::string, float>>& telemetryInfo_pro) { void systemHandler(System& system) {
// Initialization // Initialization
auto telemetry = Telemetry{system}; auto telemetry = Telemetry{system};
auto action = Action{system}; auto action = Action{system};
std::cout << "System " << static_cast<int>(system.get_system_id()) << " get in Thread.(debug)" << std::endl; // debug std::cout << "System " << static_cast<int>(system.get_system_id()) << " get in Thread. (debug)" << std::endl; // debug
// Store telemetry information // Store telemetry information
std::map<std::string, float> telemetryInfo; std::map<std::string,float> telemetryInfo;
// dev del S // Put all the subscriber init in this section
std::cout << "mark tA" << std::endl; telemetry.subscribe_position([&system, &telemetryInfo](Telemetry::Position position) {
std::future<std::map<std::string, float>> telemetryInfo_future = telemetryInfo_pro.get_future(); // dev del std::cout << "System ID: " << static_cast<int>(system.get_system_id()) << " Altitude: " << position.relative_altitude_m << " m\n"; // debug
std::cout << "mark tB" << std::endl; // Store telemetry information
if (!telemetryInfo_future.valid()) { telemetryInfo["altitude"] = position.relative_altitude_m;
std::cout << "telemetryInfo_pro is not valid" << std::endl; gTelemetryInfo[static_cast<int>(system.get_system_id())] = telemetryInfo;
} });
telemetryInfo["test"] = 3.14; // Loop
telemetryInfo_pro.set_value(telemetryInfo);
std::cout << "mark tC" << std::endl;
if (!telemetryInfo_future.valid()) {
std::cout << "telemetryInfo_pro is not valid" << std::endl;
}
// Destroy
// dev del E
telemetry.subscribe_position([&system, &telemetryInfo, &telemetryInfo_pro](Telemetry::Position position) { sleep_for(seconds(5));
std::cout << "System ID: " << static_cast<int>(system.get_system_id()) << " Altitude: " << position.relative_altitude_m << " m\n"; // debug // Thread Terminate
std::cout << "Thread Out " << static_cast<int>(system.get_system_id()) << std::endl;
}
// Store telemetry information
telemetryInfo["altitude"] = position.relative_altitude_m;
// Add more telemetry information as needed
// Set the telemetry information in the promise // class systemHandler {
std::cout << "mark tsA" << std::endl;
telemetryInfo_pro.set_value(telemetryInfo);
std::cout << "mark tsB" << std::endl;
// private:
// Telemetry telemetry;
// Action action;
sleep_for(seconds(1)); // debug
});
sleep_for(seconds(5)); // debug // public:
// std::map<std::string,float> telemetryInfo;
// std::map<std::string,float> actionCommand;
std::cout << "Thread Out " << static_cast<int>(system.get_system_id()) << std::endl; // // Constructor
// systemHandler(System& system) {
// // Initialization
// telemetry = Telemetry{system};
// action = Action{system};
// std::cout << "System " << static_cast<int>(system.get_system_id()) << " captured. (debug)" << std::endl; // debug
// telemetry.subscribe_position([&system, &telemetryInfo](Telemetry::Position position) {
// std::cout << "System ID: " << static_cast<int>(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) 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; std::cerr << "Failed to open config file: " << argv[1] << std::endl;
} }
// Start MAVSDK Server and connect to it. // Start MAVSDK Server and connect to it.
Mavsdk mavsdk; Mavsdk mavsdk;
ConnectionResult connection_result = mavsdk.add_any_connection(initSetting.connectPort); ConnectionResult connection_result = mavsdk.add_any_connection(initSetting.connectPort);
@ -189,35 +201,15 @@ int main(int argc, char** argv)
System& new_system = *sys; System& new_system = *sys;
std::cout << "system detect : " << static_cast<int>(new_system.get_system_id()) << "(Debug)"<< std::endl; // debug std::cout << "system detect : " << static_cast<int>(new_system.get_system_id()) << "(Debug)"<< std::endl; // debug
//
// std::map<string, float> telemetryInfo;
auto telemetryInfo_pro = std::promise<std::map<std::string, float>>{};
// auto telemetryInfo_fut = telemetryInfo_pro.get_future();
// dev del S
auto telemetryInfo_pro1 = std::promise<std::map<std::string, float>>{};
auto telemetryInfo_fut1 = telemetryInfo_pro.get_future();
std::thread worker([&telemetryInfo_pro1]() {
// 執行任務並將結果回傳至 promise
std::map<std::string, float> result;
result["test"]=3.14;
telemetryInfo_pro1.set_value(result);
});
worker.join();
// dev del E
// Let handler progrem deal with System // 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{ systemHandlerInfos.push_back(systemHandlerInfo{
static_cast<int>(new_system.get_system_id()), static_cast<int>(new_system.get_system_id()),
std::move(systemHandleThread), std::move(systemHandleThread),
std::move(telemetryInfo_fut1)
}); });
}); });
// MySQL connection is here // MySQL connection is here
@ -228,14 +220,15 @@ int main(int argc, char** argv)
// -- 從 systemHandlerInfos 中 輪巡所有 system 檢查並處理是否有需要丟到 mysql 的資料 // -- 從 systemHandlerInfos 中 輪巡所有 system 檢查並處理是否有需要丟到 mysql 的資料
// -- 從 mysql 抓到指令 丟到 systemHandlerInfo // -- 從 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. // Let it hover for a bit before landing again.
sleep_for(seconds(15)); sleep_for(seconds(5));
// std::cout << "Landing...\n"; // std::cout << "Landing...\n";
// const Action::Result land_result = action.land(); // const Action::Result land_result = action.land();

Loading…
Cancel
Save