0
$\begingroup$

Rosanswers logo

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?


Originally posted by laurent on ROS Answers with karma: 21 on 2016-07-20

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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()

Originally posted by ahendrix with karma: 47576 on 2016-07-20

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by laurent on 2016-07-20:
Ahh that makes perfect sense thank you so much!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.