ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

publish() to a closed topic when publishing a marker

asked 2018-03-19 11:19:21 -0500

arthur0304 gravatar image

updated 2018-03-19 11:31:32 -0500

Hello guys! I am trying to subscribe a message (/position) and publish a marker. The message(/position) contains series of (x,y,z) as below :

x: -253.021528805
y: -545.031320081
z: -465.328910428   
x: -305.591390007
y: -578.07464085
z: -511.685201552

Below is my code :

#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Point, Vector3
from visualization_msgs.msg import Marker

def callback(position):
  marker_pub = rospy.Publisher('route', Marker, queue_size = 100)

  while not rospy.is_shutdown() :
    # print(pos_vec)
    marker = Marker()
    marker.header.frame_id = '/map'
    marker.type = marker.LINE_STRIP
    marker.action = marker.ADD

    #Marker scale
    marker.scale.x = 0.1
    marker.scale.y = 0.1
    marker.scale.z = 0.1

    #Marker color
    marker.color.a = 1.0
    marker.color.r = 1.0
    marker.color.g = 1.0
    marker.color.b = 0.0

    #Marker orientation
    marker.pose.orientation.x = 0.0
    marker.pose.orientation.y = 0.0
    marker.pose.orientation.z = 0.0
    marker.pose.orientation.w = 1.0

    #Marker line points
    marker.points = []
    p = Point()
    p.x = position.x
    p.y = position.y
    p.z = position.z

    #Publish the marker

if __name__ == "__main__" :
rospy.Subscriber("/position", Vector3, callback)

However, I got the following errors :

[ERROR] [1521469185.321377]: bad callback: <function draw at 0x7f184ad9cb18>
Traceback (most recent call last):
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/", line 750, in _invoke_callback
File "/home/parallels/SDC_ws/src/simon/src/", line 45, in draw
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/", line 882, in publish
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/", line 1041, in publish
raise ROSException("publish() to a closed topic")
ROSException: publish() to a closed topic

I have searched the Internet but it seems very few people had encountered this kind of problem. Can anyone give me some hint? Thank you! Sorry for any grammar mistakes.

edit retag flag offensive close merge delete



This is probably not the answer, but please add a rospy.sleep(1.0) (or some similar period) to your while not rospy.is_shutdown() loop. Right now you're unnecessarily burning through 100% of your cpu cycles while publishing Marker msgs as fast as possible.

gvdhoorn gravatar image gvdhoorn  ( 2018-03-19 11:28:36 -0500 )edit

@gvdhoorn Thank you for your advice! You mean to change the *Publisher to the main function in this case? Is that what you mean?

arthur0304 gravatar image arthur0304  ( 2018-03-19 11:39:59 -0500 )edit

Create all your Publisher and Subscriber objects in your main, yes.

gvdhoorn gravatar image gvdhoorn  ( 2018-03-19 11:45:48 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-03-19 11:30:06 -0500

gvdhoorn gravatar image

On second thought: never add an infinite while-loop to a callback body. Ever.

Callbacks must never block (infinitely).

Also: don't create Publishers in callbacks. Do that in the initialisation phase/section of your program.

edit flag offensive delete link more


@gvdhoorn Thank you! I have solved the problem. Indeed the while loop should not be infinite.

arthur0304 gravatar image arthur0304  ( 2018-03-20 01:09:43 -0500 )edit

@arthur0304 Please explain how you solved your problem, not just mentioning it.

Kansai gravatar image Kansai  ( 2021-04-05 00:35:17 -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


Asked: 2018-03-19 11:19:21 -0500

Seen: 4,482 times

Last updated: Mar 19 '18