Ask Your Question

function callback using std::bind in ros 2 subscription

asked 2018-04-20 07:19:37 -0500

Skyking gravatar image


Using std::bind with rclcpp::Node::create_subscription() throws compilation errors.

For example, I cannot use

sub_input_ = node->create_subscription<sensor_msgs::msg::Imu>("input",
      this, <argument1>,<argument2>));

However using lamdas as a callback function is ok in this case. Any help/information on this is welcome.

edit retag flag offensive close merge delete


What about showing us such a "compilation error"? We cannot help you without you showing us what happened.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-20 07:33:39 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-20 10:43:48 -0500

Karsten gravatar image

I believe this is the same issue as reported in here:

std::bind with additional arguments is not getting resolved correctly. However, I believe if you extract your std::bind to a std::function and then pass the function instance to the callback it will work.

std::function<void(std::shared_ptr<sensor_msgs::msg::Imu>)> fnc = std::bind(
   &input_surface_indices_callback, this, std::placeholders::_1, <argument1>, <argument2>);
sub_input_ = node->create_subscription<sensor_msgs::msg::Imu>("input", fnc);

I am assuming here that your callback has a signature like:

void input_surface_indices_callback(std::shared_ptr<sensor_msgs::msg::Imu>, Argument1T arg1, Argument2T arg2);

Could you try that out and see if it works for you?

edit flag offensive delete link more


I was able to pass extra parameters to the callbacks in bouncy using the std::function method (though I'm probably going replace the subscribers with service/s later)

lucasw gravatar image lucasw  ( 2018-11-01 11:00:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-04-20 07:19:37 -0500

Seen: 2,249 times

Last updated: Apr 20 '18