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

Ros topic published string cannot be captured

asked 2014-03-21 02:55:54 -0600

ljk gravatar image

Dear my friends,

Recently, I have encountered the following problem.

I want my ros node to publish a string message if other node is sending request for this message and I want to differentiate each message sent, which means for each request only one string is sent out: I use the following command to publish the string (python code)

pub=rospy.Publisher('/act',String)
action_str='hello'
pub.publish(action_str)

I also use rostopic echo /act to see whether the string has been sent, but i see nothing when running the ros node

But if I repeatedly send publish the same string using for loop (about 2000 loops), now I can see the message on the screen, but this is not what i want, I just want to publish once not multiple times.

I guess the issue here is due to the communication latency of ros?

Any help and suggestions are much much appreciated!!!

Juekun

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2014-03-21 03:12:57 -0600

dornhege gravatar image

Publishers should be constructed and kept alive for the runtime of the program. The issue you are seeing is not so much the latency of ROS, but more the fact that it takes some time to setup the connection to other nodes. Once that's happened messages should arrive quickly.

The publish() call itself doesn't guarantee that messages are delivered anywhere. They will only be sent to connected nodes. Between the three lines of code this won't have happened yet, so it's sent to nowhere.

edit flag offensive delete link more

Comments

so, is there a way to fix this? I have the same problem and I need to send messages once

Pinchi_ gravatar image Pinchi_  ( 2014-03-21 05:39:54 -0600 )edit

That depends on how the node works. If it keeps running, just keep the publisher somewhere and don't recreated it every time. If the node will quit after the publish, wait until a subscriber has connected to the publisher and publish then.

dornhege gravatar image dornhege  ( 2014-03-21 05:43:14 -0600 )edit

Sorry, I'm really new on ROS and I don't follow. I created a node, then I created a publisher, and then I published a message. I do a spinOnce() and then my program ends. I do all this in my main scope. Are you sugesting that I make my program wait after the spinOnce()?

Pinchi_ gravatar image Pinchi_  ( 2014-03-21 05:53:49 -0600 )edit

Depending on what you intent to do a service might be better. To sent one message out that should arrive: Yes, you must wait until someone has connect to send the message to.

dornhege gravatar image dornhege  ( 2014-03-21 05:57:51 -0600 )edit

Thank you kindly for your answers, but I already have someone connected. I have a subscriber on a Arduino, so I first initialize that subscriber and then I use the code I described.

Pinchi_ gravatar image Pinchi_  ( 2014-03-21 06:06:15 -0600 )edit

Just starting both programs won't make them instantly connect. That takes some (short) time. If you publish immediately after creating the publisher it's almost certain that they won't have connected.

dornhege gravatar image dornhege  ( 2014-03-21 06:11:56 -0600 )edit

Thank you for your answers! I added a sleep() between the initialization of the publisher and the publish() method and it worked! Thank you for your patience

Pinchi_ gravatar image Pinchi_  ( 2014-03-21 07:42:16 -0600 )edit

Thanks a lot for your discussions! I found another solution to this problem To enable the latch property of the publisher so that the message published will be retained until some subscriber fetches it. pub=rospy.Publisher('/act',String,latch=True)

ljk gravatar image ljk  ( 2014-03-24 02:39:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-03-21 02:55:54 -0600

Seen: 1,669 times

Last updated: Mar 21 '14