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

function callback using std::bind in ros 2 subscription

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

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 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

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

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 -0600 )edit

It's a pity that this function broke in ros2. This gave me headaches for days, thanks a lot for your solution, worked as a charm!

merosss gravatar image merosss  ( 2022-12-29 09:53:45 -0600 )edit

Question Tools



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

Seen: 3,493 times

Last updated: Apr 20 '18