Ask Your Question
0

Ros topic published string cannot be captured

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

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 -0500

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 imagePinchi_ ( 2014-03-21 05:39:54 -0500 )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 imagedornhege ( 2014-03-21 05:43:14 -0500 )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 imagePinchi_ ( 2014-03-21 05:53:49 -0500 )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 imagedornhege ( 2014-03-21 05:57:51 -0500 )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 imagePinchi_ ( 2014-03-21 06:06:15 -0500 )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 imagedornhege ( 2014-03-21 06:11:56 -0500 )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 imagePinchi_ ( 2014-03-21 07:42:16 -0500 )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 imageljk ( 2014-03-24 02:39:21 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 676 times

Last updated: Mar 21 '14