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

Problems calling services within classes

asked 2012-09-21 10:28:50 -0500

Ammar gravatar image

updated 2012-09-22 04:44:22 -0500

Hello Community, I have a couple of services and subscribers that are functions of a class that I created. The services are being advertised correctly and I can see them in rosservice list. However when the service gets called, it spins indefinitely never calling the appropriate callback function. However the subscribers work perfectly well within the same class. I am also able to call the same function (service callbacks) through normal function calls on the object. Could someone please suggest to me what the problem could be here. Here is how I advertise the service in my main() method:

trader::Trader a;
ros::ServiceServer auction_service = n.advertiseService(PACKAGE_NAME + "/auction", &trader::Trader::auctionCB, &a);

Here is the function:

bool Trader::auctionCB(multiagent_coordination_msgs::Auction::Request &req, multiagent_coordination_msgs::Auction::Response &res){return true;}

Here is the function declaration in the .h file:

        bool auctionCB(multiagent_coordination_msgs::Auction::Request &req, multiagent_coordination_msgs::Auction::Response &res);

As I said the subscribers work perfectly. Thanks Ammar

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2012-09-22 09:11:49 -0500

dornhege gravatar image

The declarations look fine to me. The only guess I might have: Do the Trader and ServiceServer maybe go out of scope?

Besides that, you should produce a complete minimal working example.

edit flag offensive delete link more

Comments

Thats an interesting observation. The service is being advertised in main(). Do I need to have an object of the class (Trader) present when I call the service? I just assumed the service can be called normally in other class methods.

Ammar gravatar image Ammar  ( 2012-09-22 13:53:18 -0500 )edit

I'm not really sure, what you mean, but as you declare the service once someone call it, it will call the auctionCB method in the Trader a object instance. This has to exist to not fail.

dornhege gravatar image dornhege  ( 2012-09-23 23:37:19 -0500 )edit
0

answered 2016-11-06 18:18:48 -0500

updated 2016-11-06 18:19:29 -0500

If you are calling the advertiseService function within that class, you may call it like so:

ros::ServiceServer auction_service = n.advertiseService(PACKAGE_NAME + "/auction", &trader::Trader::auctionCB, this);

Should work!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2012-09-21 10:28:50 -0500

Seen: 1,736 times

Last updated: Nov 06 '16