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

Can 1 node subsrcibe MSG from a topic and publish MSG to other topic at the same time?

asked 2013-05-01 17:32:39 -0600

David_xiong gravatar image

updated 2014-04-20 14:09:26 -0600

ngrennan gravatar image

Hi all, I downloaded the face_recognition demo and pi_tracker demo, and I want to combine the two which means that I want the turtlebot recognized me at first and move as I wish. However, it means that I must publish MSG to contract with pi_tracker after being recognized, so in the face_rec_client.cpp, I added the code:

    ros::NodeHandle n1;
    ros::Publisher chatter_pub = n1.advertise<std_msgs::String>("chatter1", 1000);
    std_msgs::String msg;
    std::stringstream ss;
    ss << "You are Master!";'t = ss.str();

which means that if the turtlebot recognized me(David), it should publish a msg on topic "chatter1" as well as ROS_INFO the msg. NOW, THE QUESTION IS: it does print "You are Master" on screen, but I can't get it on topic "chatter1". so I think maybe I didn't publish the msg in the right way. anybody could help me? Thanks?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-05-01 17:59:18 -0600

Bill Smart gravatar image

My guess is that the problem is caused by chatter_pub going out of scope immediately after the publish call. You're creating chatter_pub fresh every time you use it in the body of the if statement, which is probably not a good idea. Move the declaration of chatter_pub out to a more global scope (outside of the if block) and see if this solves things. As it stands, I think that you're (automatically) destroying the publisher before it has a chance to do its work.

edit flag offensive delete link more

answered 2013-05-01 18:01:14 -0600

After a node advertises a topic, it sometimes takes a small amount of time for other subscribers to connect to the newly-advertised publisher. Any messages that are published before the subscribers are fully connected are "lost". See this thread for more discussion on this issue.

I recommend that you put the n1.advertise call somewhere near the start of your node. That way, subscribers are connected when the node first starts up. They should be connected by the time you need to actually publish your message. Alternatively, you can add a sleep/delay after the advertise call, to give subscribers a chance to connect before you publish your first message.

edit flag offensive delete link more

Question Tools


Asked: 2013-05-01 17:32:39 -0600

Seen: 387 times

Last updated: May 01 '13