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

function callback using std::bind in ros 2 subscription

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

Skyking gravatar image

Hi,

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",
  std::bind(
      &input_surface_indices_callback,
      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

Comments

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
3

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

Karsten gravatar image

I believe this is the same issue as reported in here: https://github.com/ros2/rclcpp/issues...

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

Comments

I was able to pass extra parameters to the callbacks in bouncy using the std::function method https://github.com/lucasw/v4l2ucp/blo... (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

3 followers

Stats

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

Seen: 4,221 times

Last updated: Apr 20 '18