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

boost::bind with Ros Service

asked 2015-07-24 11:23:07 -0600

Ongun Kanat gravatar image

I have a ROS Service GripperState

int32 gripperState
int32 paddleState

And In my main.cpp file I have this line since I need gripper data from robot needs to be passed to service

ros::ServiceServer gripperService = 
    n.advertiseService<my_robot::GripperState::Request, my_robot::GripperState::Response>
    ("GripperState",boost::bind(&gripperControl, node->getGripper(), 
    (my_robot::GripperState::Request &) _2,  (my_robot::GripperState::Response &) _3));

In GripperServer.cpp I have the definition of method gripperControl

bool gripperControl(ArGripper *gripper, my_robot::GripperState::Request &req, my_robot::GripperState::Response &res)
    int32_t gSt = gripper->getGripState();
    int32_t pSt = gripper->getPaddleState();
    ROS_INFO("GripperSt : %d, PaddleSt: %d", gSt, pSt);
    res.gripperState = 5;
    res.paddleState = 10;
    return true;

However it compiles. It doesn't work since boost doesn't pass the placeholders _2 and _3 as references.(I think it creates a copy of each parameter).

Is there any way to make this work?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-09-08 20:07:39 -0600

Benjamin Blumer gravatar image

First of all, I don't think those casts ( (my_robot::GripperState::Request &) ) before the _2 and _3 are necessary.

But, most importantly, I think you're numbering your arguments incorrectly. _2 should be _1 and _3 should be _2. Those underscored arguments refer to the args passed to the boost functor returned by boost::bind; so having 2 and 3 implies that it's receiving 3 arguments and it should ignore the first. When, in reality, ROS only passes it two arguments (the request and the response).

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-07-24 11:23:07 -0600

Seen: 1,533 times

Last updated: Sep 08 '15