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

publisher not publishing on topic

asked 2013-06-23 23:53:06 -0500

vivek rk gravatar image

updated 2013-06-24 00:00:43 -0500

prasanna.kumar gravatar image

im trying to publish a message on a topic once but it never publishes the data at all. This is the code in main

ros::init(argc, argv, "keeper_env");
   ros::NodeHandle n;
   ros::Publisher chatter_pub = n.advertise<std_msgs::Empty>("toggle_led", 1000);
   std_msgs::Empty msg;
   chatter_pub.publish(msg);

even putting it in a finite while loop dosent work

int i=0;
        ros::Rate rat(20);
    while(i<10)
    {   
        chatter_pub.publish(msg);
        ros::spinOnce();
        i++;
                rat.sleep();
    }

but if i put it in an infitite loop then it publishes the data.

while(ros::ok())
    {   
        chatter_pub.publish(msg);
        ros::spinOnce();

    }

If i put it in a if condition in that infinite loop then it still dosent publish. i dont want it to be called infintely. i just need it once. can someone please help me

int i=0;
while(ros::ok())
    {   

        if(i==0)
        {   
            printf("Doses it go iun here \n");
            chatter_pub.publish(msg);
            i++;
        }
        }
edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
10

answered 2013-06-24 03:11:53 -0500

dornhege gravatar image

@Philip is right. The publisher need some time to connect to the subscribers. Your finite loop is just to short for that. I'm not sure what you expect from you condition case in the infinite loop as that is never true.

The proper solution to your problem is to use pub.getNumSubscribers() and wait until that is > 0. Then publish.

edit flag offensive delete link more

Comments

Ah, thanks for the hint about getNumSubscribers! I wasn't aware of that :-)

Philip gravatar image Philip  ( 2013-06-24 04:22:34 -0500 )edit

In case you run into this issue with rospy: The corresponding function is called get_num_connections().

aschaefer gravatar image aschaefer  ( 2018-12-05 16:52:06 -0500 )edit
5

answered 2013-06-24 02:09:48 -0500

Philip gravatar image

My first guess: you try to publish while the publisher is not fully set up. Can you try waiting for a second (e.g. using usleep(1000*1000)) between creating your publisher and sending the first message?

edit flag offensive delete link more
0

answered 2021-02-09 22:25:47 -0500

DangTran gravatar image

Hi, I just want to add a more explicit code on how to use pub.getNumSubscribers() answered by @dornhege.

If you want to publish a message, you have to make sure the connection between subscriber and publisher is already established. This can be done by:

ros::Publisher chatter_pub = n.advertise<std_msgs::Empty>("toggle_led", 1000)
while (publisher.getNumSubscribers() < 1) {
    ros::WallDuration sleep_t(0.5);    
    sleep_t.sleep();
}
// After connection are guaranteed to be established, we can publish messages now.
std_msgs::Empty msg;
chatter_pub.publish(msg);
chatter_pub.publish(msg); // Can even publish multiple times

If using while-loop, you probably do not have to worry about this problem. Because the connection is likely to be guaranteed.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-06-23 23:53:06 -0500

Seen: 12,285 times

Last updated: Feb 09 '21