From c4514d235cbc215cfe17bbd1d6311d21fa911922 Mon Sep 17 00:00:00 2001 From: "-T.K.-" Date: Mon, 11 Nov 2024 13:26:16 -0800 Subject: [PATCH] FIX: fix std::future_error when running `ros2 run rl_sar rl_sim`, get: ```bash terminate called after throwing an instance of 'std::future_error' what(): std::future_error: No associated state ``` --- src/rl_sar/src/rl_sim.cpp | 42 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/rl_sar/src/rl_sim.cpp b/src/rl_sar/src/rl_sim.cpp index 57cf071..061fc19 100644 --- a/src/rl_sar/src/rl_sim.cpp +++ b/src/rl_sar/src/rl_sim.cpp @@ -12,30 +12,32 @@ RL_Sim::RL_Sim() param_client = this->create_client("/param_node/get_parameters"); while (!param_client->wait_for_service(std::chrono::seconds(1))) { + if (!rclcpp::ok()) { + std::cout << LOGGER::ERROR << "Interrupted while waiting for param_node service. Exiting." << std::endl; + return; + } std::cout << LOGGER::WARNING << "Waiting for param_node service to be available..." << std::endl; } auto request = std::make_shared(); - request->names.push_back("robot_name"); - request->names.push_back("gazebo_model_name"); - auto future = param_client->async_send_request(request); - rclcpp::spin_until_future_complete(this->get_node_base_interface(), future); - if (future.get()->values.size() < 2) - { - std::cout << LOGGER::WARNING << "Failed to get all parameters from param_node" << std::endl; - } - else - { - this->robot_name = future.get()->values[0].string_value; - this->gazebo_model_name = future.get()->values[1].string_value; - std::cout << LOGGER::INFO << "Get param robot_name: " << this->robot_name << std::endl; - std::cout << LOGGER::INFO << "Get param gazebo_model_name: " << this->gazebo_model_name << std::endl; - } + request->names = {"robot_name", "gazebo_model_name"}; - // // get params from this node - // this->declare_parameter("robot_name", ""); - // this->get_parameter("robot_name", this->robot_name); - // this->declare_parameter("gazebo_model_name", ""); - // this->get_parameter("gazebo_model_name", this->gazebo_model_name); + // Use a timeout for the future + auto future = param_client->async_send_request(request); + auto status = rclcpp::spin_until_future_complete(this->get_node_base_interface(), future, std::chrono::seconds(5)); + + if (status == rclcpp::FutureReturnCode::SUCCESS) { + auto result = future.get(); + if (result->values.size() < 2) { + std::cout << LOGGER::ERROR << "Failed to get all parameters from param_node" << std::endl; + } else { + this->robot_name = result->values[0].string_value; + this->gazebo_model_name = result->values[1].string_value; + std::cout << LOGGER::INFO << "Get param robot_name: " << this->robot_name << std::endl; + std::cout << LOGGER::INFO << "Get param gazebo_model_name: " << this->gazebo_model_name << std::endl; + } + } else { + std::cout << LOGGER::ERROR << "Failed to call param_node service" << std::endl; + } // read params from yaml this->ReadYaml(this->robot_name);