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

why do we need a timer to call an action once?

asked 2023-01-19 13:02:34 -0500

uwrtnm gravatar image

I was going through the ros2 docs and im a little confused on why do we need to use a wall_timer() if we want to call an action once. Why can't we just call the send_goal() function without the use of a timer()

namespace action_tutorials_cpp
{
class FibonacciActionClient : public rclcpp::Node
{
public:
  using Fibonacci = action_tutorials_interfaces::action::Fibonacci;
  using GoalHandleFibonacci = rclcpp_action::ClientGoalHandle<Fibonacci>;

  explicit FibonacciActionClient(const rclcpp::NodeOptions & options)
  : Node("fibonacci_action_client", options)
  {
    this->client_ptr_ = rclcpp_action::create_client<Fibonacci>(
      this,
      "fibonacci");

    this->timer_ = this->create_wall_timer(
      std::chrono::milliseconds(500),
      std::bind(&FibonacciActionClient::send_goal, this));
  }

  void send_goal()
  {
    using namespace std::placeholders;

    this->timer_->cancel();

    if (!this->client_ptr_->wait_for_action_server()) {
      RCLCPP_ERROR(this->get_logger(), "Action server not available after waiting");
      rclcpp::shutdown();
    }

    auto goal_msg = Fibonacci::Goal();
    goal_msg.order = 10;

    RCLCPP_INFO(this->get_logger(), "Sending goal");

    auto send_goal_options = rclcpp_action::Client<Fibonacci>::SendGoalOptions();
    send_goal_options.goal_response_callback =
      std::bind(&FibonacciActionClient::goal_response_callback, this, _1);
    send_goal_options.feedback_callback =
      std::bind(&FibonacciActionClient::feedback_callback, this, _1, _2);
    send_goal_options.result_callback =
      std::bind(&FibonacciActionClient::result_callback, this, _1);
    this->client_ptr_->async_send_goal(goal_msg, send_goal_options);
  }
edit retag flag offensive close merge delete

Comments

Have you tried running this without the timer? nitpick: consider adding a link to the docs rather than the code.

achille gravatar image achille  ( 2023-01-24 16:44:33 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-01-24 21:21:39 -0500

ChuiV gravatar image

While I'm not the one who wrote this example code, I can still speculate. I think the demo is to show two main points:

  1. How to send an action goal.
  2. Action goals can be sent inside a callback. (ie a timer, subscription, or service callback).

Another possible reason for the timer is to provide time for the dds discovery process to happen prior to sending any action requests. If the action goal has been published prior to the action server being discovered and set-up, then the goal will never reach the server.

edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2023-01-19 13:02:34 -0500

Seen: 108 times

Last updated: Jan 24 '23