0
$\begingroup$

Rosanswers logo

I have a publisher node and I would like to get a callback when a subscriber connects. I followed the example in the documentation, but I get a compilation warning and no callback. For example, I start a rostopic echo before I launch my node, and I do receive the message, but the publisher is never notified that a subscriber connected.

Here is the warning:

In function ‘int main(int, char**)’: .../AppTestCallbackSubscription.cpp:20:88: warning: the address of ‘void connectCallback(const ros::SingleSubscriberPublisher&)’ will always evaluate as ‘true’ [-Waddress]

Here is the code:

#include <ros/ros.h>
#include <std_msgs/String.h>

//typedef boost::function<void(const SingleSubscriberPublisher&)> SubscriberStatusCallback;
void connectCallback(const ros::SingleSubscriberPublisher& pub)
{
    ROS_INFO("Subscriber connected");
}

int main(int argc, char** argv)
{
    const std::string TOPIC_NAME = "/foo";
    
    ros::init(argc, argv, "MyPublisher");

    ros::NodeHandle nh("~");
    
    // None of these two lines triggers the callback
    //ros::Publisher pub = nh.advertise<std_msgs::String>(TOPIC_NAME, 10, connectCallback, ros::SubscriberStatusCallback(), ros::VoidConstPtr(), false);
    ros::Publisher pub = nh.advertise<std_msgs::String>(TOPIC_NAME, 10, connectCallback);
      
    // Gives time to the subscribers to connect
    ros::Time maxTime(ros::Time::now()+ros::Duration(1)); // Will wait at most 1000 ms
    while(ros::Time::now()<maxTime)
    {
        ros::spinOnce();
    }

    std_msgs::String msg;
    msg.data = "Hello World!";
    pub.publish(msg);
    
    ROS_INFO("Message published");

    return 0;
}

Originally posted by Benoit Larochelle on ROS Answers with karma: 867 on 2012-11-14

Post score: 1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

You are passing the callback pointer address as the bool latch parameter (3rd parameter in the advertise function) and the compiler is telling you that this will always evaluate to true. Here's the documentation where I got this from.

I'm not sure how to get the behavior you want, but what you did is definitely not the way to go.


Originally posted by georgebrindeiro with karma: 1264 on 2012-11-14

This answer was NOT ACCEPTED on the original site

Post score: 3


Original comments

Comment by Lorenz on 2012-11-14:
Wow. Good catch. I would say to get the right method selected, either specify all parameters or at least the disconnect callback. Maybe, adding a & in front of connectCallback could also help. Or an explicit type cast.

Comment by Benoit Larochelle on 2012-11-14:
I was also afraid of compiler ambiguity so I already tried passing in all parameters (commented out line above). No improvement though. Adding a & in front also did not work. However, an explicit type cast did the trick! @Lorenz, maybe you could write it as a new answer and I'll give you kudos :-)

Comment by Benoit Larochelle on 2012-11-14:
Also, I'll file a ticket to update the documentation because it I did a copy-paste from it and it clearly didn't work

Comment by Benoit Larochelle on 2012-11-14:
I put in this issue: https://github.com/ros/ros_comm/issues/23

Comment by Lorenz on 2012-11-14:
I think the karma should go to @georgebrinbeiro :) Btw. I just tried and I guess the problem is that SubscriberStatusCallback is a boost::function and the compiler doesn't cast to it correctly. Explicitly creating the right type is better than casting: SubscriberStatusCallback(connectCallback).

$\endgroup$
0
$\begingroup$

Rosanswers logo

Here is the solution found by @Lorenz and fixed (in the documentation) by @Dirk Thomas.

An explicit type cast is required in front of the callback function:

Instead of: handle.advertise<std_msgs::Empty>("my_topic", 1, connectCallback);

Use: handle.advertise<std_msgs::Empty>("my_topic", 1, (ros::SubscriberStatusCallback)connectCallback);


Originally posted by Benoit Larochelle with karma: 867 on 2012-12-11

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.