From 75157f9f6bd9f04ef85bc09f2ae4a9edbd890a13 Mon Sep 17 00:00:00 2001 From: Ilia-Loginov Date: Wed, 14 Aug 2024 13:16:42 +0400 Subject: [PATCH] code style --- test/mavsdk_tests/mavsdk_preparing.cpp | 95 ++++++++++++++------------ 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/test/mavsdk_tests/mavsdk_preparing.cpp b/test/mavsdk_tests/mavsdk_preparing.cpp index 5b9ba2935869..0744147299b7 100644 --- a/test/mavsdk_tests/mavsdk_preparing.cpp +++ b/test/mavsdk_tests/mavsdk_preparing.cpp @@ -21,8 +21,7 @@ using std::chrono::seconds; using std::this_thread::sleep_for; template -bool poll_condition_with_timeout_( - std::function fun, std::chrono::duration duration) +bool poll_condition_with_timeout(std::function fun, std::chrono::duration duration) { static constexpr unsigned check_resolution = 100; @@ -32,44 +31,49 @@ bool poll_condition_with_timeout_( while (!fun()) { std::this_thread::sleep_for(duration_us / check_resolution); const auto elapsed_time_us = std::chrono::duration_cast(std::chrono::steady_clock::now() - - start_time); + start_time); if (elapsed_time_us > duration_us) { std::cout << "Timeout, waiting for the vehicle for " - << elapsed_time_us.count() * std::chrono::steady_clock::period::num - / static_cast(std::chrono::steady_clock::period::den) - << " seconds\n"; + << elapsed_time_us.count() * std::chrono::steady_clock::period::num + / static_cast(std::chrono::steady_clock::period::den) + << " seconds\n"; return false; } } + return true; } int main(int argc, char **argv) { - const std::string arg_url {"--url"}; - const std::string arg_command {"--command"}; - const std::string command_set_param {"set_sys_autostart"}; - const std::string command_check {"check"}; - const std::string command_reboot {"reboot"}; - - std::string connection_url {}; - std::string command {}; - int param_value {}; + const std::string arg_url{"--url"}; + const std::string arg_command{"--command"}; + const std::string command_set_param{"set_sys_autostart"}; + const std::string command_check{"check"}; + const std::string command_reboot{"reboot"}; + + std::string connection_url{}; + std::string command{}; + int param_value{}; + mavsdk::Mavsdk mavsdk{}; + for (int i = 0; i < argc; ++i) { const std::string argv_string(argv[i]); + if (argv_string == arg_url && (argc > (i + 1))) { connection_url = argv[i + 1]; i++; - } - else if (argv_string == arg_command && (argc > (i + 1))) { + + } else if (argv_string == arg_command && (argc > (i + 1))) { command = argv[i + 1]; i++; if (command == command_set_param) { - if ((argc > (i + 1))){ + if ((argc > (i + 1))) { param_value = std::atoi(argv[i + 1]); i++; + } else { command.erase(); } @@ -83,80 +87,81 @@ int main(int argc, char **argv) return 1; } - Mavsdk mavsdk{}; - std::cout << "connection url " << connection_url << std::endl; + std::cout << "Connection url " << connection_url << std::endl; ConnectionResult connection_result = mavsdk.add_any_connection(connection_url); - if (connection_result != ConnectionResult::Success) - { + + if (connection_result != ConnectionResult::Success) { std::cerr << "Connect was failed" << std::endl; return 1; } + std::cout << "Waiting for system connect" << std::endl; - if(!poll_condition_with_timeout_( - [&]() { return mavsdk.systems().size() > 0; }, std::chrono::seconds(25))) - { + + auto res_polling = poll_condition_with_timeout([&]() { return mavsdk.systems().size() > 0;}, std::chrono::seconds(25)); + + if (not res_polling) { std::cerr << "Polling was failed" << std::endl; return 1; } auto system = mavsdk.systems().at(0); - if(!system) - { + if (!system) { std::cerr << "The system wasn't be found" << std::endl; return 1; } - if (command_check == command) - { + if (command_check == command) { std::cout << "Success! The connection was checked." << std::endl; return 0; } - if (command_set_param == command) - { + if (command_set_param == command) { //TODO: For some reason when we set the new value it sometimes takes several attempts to change a parameter. int attempt = 1; const int max_attemp = 5; const auto param_name = "SYS_AUTOSTART"; - for(auto i = 0; i <= max_attemp; i++) - { + + for (attempt = 0; attempt <= max_attemp; attempt++) { using namespace std::chrono_literals; auto param = Param(system); - if (param.set_param_int(param_name, param_value) != Param::Result::Success) - { + if (param.set_param_int(param_name, param_value) != Param::Result::Success) { std::cerr << "Fail setting param: " << param_name << ", value: " - << param_value <