ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

Example of ROS2 C++ blocking action request

asked 2021-12-28 09:26:13 -0500

AndyZe gravatar image

updated 2021-12-28 09:26:25 -0500

I'm looking for a C++ example of a blocking action request. Basically I seek the C++ version of this Python answer: https://answers.ros.org/question/3772...

In other words, I want to do something like this:

my_action_client_->async_send_goal(my_request, send_goal_options);

// block until complete here
edit retag flag offensive close merge delete

Comments

Hi @AndyZe, here you can find the C++ version of the Fibonnaci example shown in the previous answer. https://github.com/ros2/demos/tree/fo... Hope that's what you are looking for

osilva gravatar image osilva  ( 2021-12-28 09:43:35 -0500 )edit

Yep, thanks. It doesn't block until the action finishes, though.

    this->client_ptr_->async_send_goal(goal_msg, send_goal_options);
  }
AndyZe gravatar image AndyZe  ( 2021-12-28 09:45:04 -0500 )edit

I suspect the answer is to use async_get_result() but the syntax is a little tricky. I wish there were an example.

AndyZe gravatar image AndyZe  ( 2021-12-28 10:20:58 -0500 )edit
1

There is a little bit of information in the source code

/// Asynchronously get the result for an active goal.
  /**
   * \throws exceptions::UnknownGoalHandleError If the goal unknown or already reached a terminal
   *   state.
   * \param[in] goal_handle The goal handle for which to get the result.
   * \param[in] result_callback Optional callback that is called when the result is received.
   * \return A future that is set to the goal result when the goal is finished.
   */
  std::shared_future<WrappedResult>
  async_get_result(
    typename GoalHandle::SharedPtr goal_handle,
    ResultCallback result_callback = nullptr)
osilva gravatar image osilva  ( 2021-12-28 10:33:39 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-12-28 11:01:34 -0500

AndyZe gravatar image
  auto goal_handle = my_action_client_->async_send_goal(request, send_goal_options);

  auto result = my_action_client_->async_get_result(goal_handle.get());

  if (result.get().code == rclcpp_action::ResultCode::SUCCEEDED)
  {
    return true;
  }
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-12-28 09:26:13 -0500

Seen: 466 times

Last updated: Dec 28 '21