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

Unsubscribing from ROS Topic - Python

asked 2016-07-20 13:05:32 -0500

laurent gravatar image

So I have a Class and in its init function, I subscribe to a camera, whose callback function is created in my class. i.e:

class example(object):
  def __init__(self):
    rospy.subscriber("/cameras/left_hand_camera/image",Image,self.callback_viewer)
  def callback_viewer(self,data):
    try:
      cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      print(e)
    cv2.imshow("window", cv_image)

So for the purposes of my project, I need to create another class, which, in addition to doing some other stuff, unsubscribe to all the topics it is currently subscribing to. but I don't know how to use the unsubscriber function listed here

Can anyone help me with that, how would i use that function?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-07-20 13:23:36 -0500

ahendrix gravatar image

In order to use the unregister message on a subscriber object, you need a subscriber object. This subscriber object is returned by rospy.subscriber, so I'd recommend that you do:

class example(object):
  def __init__(self):
    # save the subscriber object to a class member
    self.sub = rospy.subscriber("/cameras/left_hand_camera/image",Image,self.callback_viewer)

  def callback_viewer(self,data):
    try:
      cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      print(e)
    cv2.imshow("window", cv_image)

  def unsubscribe(self):
    # use the saved subscriber object to unregister the subscriber
    self.sub.unregister()
edit flag offensive delete link more

Comments

Ahh that makes perfect sense thank you so much!

laurent gravatar image laurent  ( 2016-07-20 13:28:39 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-20 13:05:32 -0500

Seen: 5,254 times

Last updated: Jul 20 '16