Actions Client example constructor

Hi,

In the actions client example (5.2), theexplicit keyword and NodeOptions are introduced in the constructor:

 explicit MyActionClient(const rclcpp::NodeOptions & node_options = rclcpp::NodeOptions())
  : Node("my_action_client", node_options), goal_done_(false)
  {
     [...]
  }

Is there a specific reason why this differs from the previous examples?
I tried removing them and it seems this works as well:

 MyActionClient()
  : Node("my_action_client"), goal_done_(false)
  {
     [...]
  }

Thank you,
Johan

Similarly, is there a specific reason for the use of

   this->client_ptr_ = rclcpp_action::create_client<Move>(
       this->get_node_base_interface(),
       this->get_node_graph_interface(),
       this->get_node_logging_interface(),
       this->get_node_waitables_interface(),
       "move_robot_as");
       */

instead of:

    this->client_ptr_ =
        rclcpp_action::create_client<Move>(this, "move_robot_as");

The latter seems to work as well.

Thank you,
Johan

Hi @JRTG ,

The NodeOptions in the constructor is needed only when you use the following lines:

The latter, as you noted:

Is actually sufficient as well, as mentioned in the rclcpp documentation. I have also tried this and it works for me too, so I can verify this. Yet, I still do not understand when exactly I would need the 4 additional lines in the create_client function. This is how it is mentioned in the official documentation also : Writing an action server and client (C++) — ROS 2 Documentation: Foxy documentation

I understand that the explicit keyword is necessary but the NodeOptions is not.
Although, as indicated in the documentation, you may construct the constructor as follows:

 MyActionClient(const rclcpp::NodeOptions &node_options)
  : Node("my_action_client", node_options)
  {
     bool goal_done_ = false;   // you can add this line here to make the constructor line smaller
     [...]
  }

In my opinion, the concise version works the same way as the version with the four pointers to the node interfaces.

I would still like to know the reason as you do, from The Construct team.

Regards,
Girish

Hello @girishkumar.kannan ,

As far as I can tell, both the four pointer version as well as the concise version work, either with or without the NodeOptions in the constructor.

However I think I found the reason to declare the constructor with NodeOptions:

Components must have a constructor that takes rclcpp::NodeOptions as the only argument, for the component composition to work.

I tried to omit the NodeOptions in the Composition exercise 7.1 and it yields a compile error:

error: no matching function for call to 'my_components::MoveRobot::MoveRobot(const rclcpp::NodeOptions&)'
  146 |  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/user/ros2_ws/src/my_components/src/moverobot_component.cpp:18:1: note: candidate: 'my_components::MoveRobot::MoveRobot()'

See also this post for a similar problem.

Regards,
Johan

Hi @JRTG ,

Good find on the information.

I have not yet implemented actions (server or client) with node composition. That is probably the reason why I was unaware of this information and the use for NodeOptions.

Thanks for posting this!

Regards,
Girish

Hello @girishkumar.kannan ,

It’s unrelated to actions: any node that is to be used in compsition must have a constructor that has the following signature:

rclcpp::Node(const std::string &node_name, const rclcpp::NodeOptions &options=rclcpp::NodeOptions())

Re,
Johan

Hi @JRTG ,

Yes, I realized this soon after I posted my response. I forgot to edit/update it.

I realized that NodeOptions are required when I read through the unit on “Components” again.

I should have probably said that I have not implemented “Components” by myself. It was only during when I learned the unit on ROS2 Components. I have not tried to use Components in my programs yet (or in the course project).

Anyways, thanks for clarifying that point.

Regards,
Girish

EDIT: Perhaps this reason for using NodeOptions could be added in the course with succinct lines, if not present in the course already, by The Construct team.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.