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
```
This commit is contained in:
-T.K.- 2024-11-11 13:26:16 -08:00
parent e762a10086
commit c4514d235c
1 changed files with 22 additions and 20 deletions

View File

@ -12,30 +12,32 @@ RL_Sim::RL_Sim()
param_client = this->create_client<rcl_interfaces::srv::GetParameters>("/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<rcl_interfaces::srv::GetParameters::Request>();
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<std::string>("robot_name", "");
// this->get_parameter("robot_name", this->robot_name);
// this->declare_parameter<std::string>("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);