简体   繁体   English

取消订阅ROS主题-Python

[英]Unsubscribing from ROS Topic - Python

So I have a Class and in its init function, I subscribe to a camera, whose callback function is created in my class. 因此,我有一个Class,在其init函数中,我预订了一个摄像头,该摄像头的回调函数在我的类中创建。 ie: 即:

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? 谁能帮助我,我将如何使用该功能?

I don't understand exactly what you have to do, but when you subscribe to a topic, you can write something like this: 我不完全了解您需要做什么,但是当您订阅某个主题时,可以编写如下内容:

sub = rospy.subscriber("/cameras/left_hand_camera/image",Image,self.callback_viewer)

Then when you have to unsubscribe you just have to do: 然后,当您必须退订时,您只需要执行以下操作:

sub.unregister()

Hope this answer your question. 希望这个回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM