Ask Your Question
0

Unable to publish PoseStamped message

asked 2018-10-24 01:20:23 -0600

Subodh Malgonde gravatar image

updated 2018-10-24 01:26:48 -0600

I am trying to publish a PoseStamped message via a node running in Python. I am listening for this message on another tab in my terminal.

Python node:

import rospy
from geometry_msgs.msg import PoseStamped

rospy.init_node("mynode")

goal_publisher = rospy.Publisher("move_base_simple/goal", PoseStamped, queue_size=5)

goal = PoseStamped()

goal.header.seq = 1
goal.header.stamp = rospy.Time.now()
goal.header.frame_id = "map"

goal.pose.position.x = 1.0
goal.pose.position.y = 2.0
goal.pose.position.z = 0.0

goal.pose.orientation.x = 0.0
goal.pose.orientation.y = 0.0
goal.pose.orientation.z = 0.0
goal.pose.orientation.w = 1.0

goal_publisher.publish(goal)

rospy.spin()

So this my order of doing things:

  1. In the first tab of terminal run roscore
  2. In the second tab run rostopic echo /move_base_simple/goal
  3. In the third tab run the Python node which publishes a message

However no message is printed in the 2nd tab. What am I doing wrong? I am not getting any error messages.

EDIT: If I add a small delay before the goal_publisher.publish(goal) line then the message gets published.

rospy.sleep(1)
goal_publisher.publish(goal)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-10-24 02:03:46 -0600

gvdhoorn gravatar image

EDIT: If I add a small delay before the goal_publisher.publish(goal) line then the message gets published.

This is something that has come up many times and that makes me wonder whether we should add this to the wiki about Publishers and Subscibers in some form.

Setting up connections between publishers and subscibers takes time. Subscibers connect to publishers directly, not to the Master, nor does the Master buffer messages, so even if you start your subscriber before your publisher there is a non-zero amount of time needed for the two to connect.

If there are no connections, any message you publish will be dropped and won't reach anyone.

In your publisher program, you don't give any potentially interested subscribers any time to set up a connection, so it is most likely your PoseStamped message is dropped.

You have two options:

  1. publish the message periodically (so that even if there are no subscribers now, they'll receive one of the later messages published to the topic). That may or may not be appropriate for what you're trying to do (ie: for some messages / interactions it doesn't make sense to duplicate the same message)
  2. wait for subscribers to connect. With your edit you've already done that, but you did it time based. It's always nicer to do things state based, which in this case means: look at the nr of subscribers and if there is at least one (or any other number) then publish your message. You can use Topic::get_num_connections for that in rospy.
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-10-24 01:20:23 -0600

Seen: 929 times

Last updated: Oct 24 '18