コード例 #1
0
ファイル: ros_adapter.py プロジェクト: robotlinker/core-1
  def __init__(
          self,
          ros_adapter,
          robot_name,
          pubsub_client,
          network,
          ros_topic,
          params):
    self.ros_adapter = ros_adapter
    self.ros_topic = ros_topic
    self.network = network

    self.latch = params.get('latch', False)
    self.max_hz_filter = _MaxHzFilter(params.get('max_hz', 0))

    self.latched_msg = None

    self.topic = pubsub_client.topic(pubsub_topic_name(robot_name, ros_topic))
    if not self.topic.exists():
      self.topic.create()

    self.subscription = self.topic.subscription(
        pubsub_subscription_name(self.topic.name, network))
    if not self.subscription.exists():
      self.subscription.create()

    self.publish_queue = Queue.Queue()

    # TODO(rodrigoq): add a way to unsubscribe on closing the app
    rospy.loginfo('creating ROS subscriber for %s...', ros_topic)
    self.ros_subscriber = shapeshifter.Subscriber(ros_topic, self.callback)
    # TODO(rodrigoq): we always use latched publishers because we register
    # lazily on the first message, meaning that subscribers otherwise miss the
    # first message. This could have unintended effects, so it would be better
    # to have some sort of delay/timeout on the first message.
    self.ros_publisher = shapeshifter.Publisher(ros_topic, self.ROS_QUEUE_SIZE,
                                                latch=True)

    self.pull_thread = threading.Thread(target=self.pull_pubsub_messages)
    # TODO(rodrigoq): add a way to cleanly stop the thread
    self.pull_thread.daemon = True
    self.pull_thread.start()

    self.publish_thread = threading.Thread(target=self.publish_pubsub_messages)
    # TODO(rodrigoq): add a way to cleanly stop the thread
    self.publish_thread.daemon = True
    self.publish_thread.start()
コード例 #2
0
  def testSubscriber_MessageHasConnectionHeader(self):
    """Checks messages received by Subscriber have connection header.

    Publishes a string with rospy.Publisher, receives it with
    shapeshifter.Subscriber, and checks it has _connection_header.
    """
    callback = mock.Mock()
    s = shapeshifter.Subscriber(self.id(), callback)

    p = rospy.Publisher(
        self.id(),
        std_msgs.msg.String,
        queue_size=QUEUE_SIZE,
        latch=True)
    p.publish(std_msgs.msg.String("hello"))

    (message,), _ = self.waitForCall(callback)
    self.assertTrue(hasattr(message, "_connection_header"),
                    "message has no _connection_header attribute")
コード例 #3
0
  def testSubscriber_MessageHasSerializedString(self):
    """Checks messages received by Subscriber have serialized contents.

    Publishes a string with rospy.Publisher, receives it with
    shapeshifter.Subscriber, and checks it contains a serialized string in
    _buff.
    """
    callback = mock.Mock()
    s = shapeshifter.Subscriber(self.id(), callback)

    p = rospy.Publisher(
        self.id(),
        std_msgs.msg.String,
        queue_size=QUEUE_SIZE,
        latch=True)
    p.publish(std_msgs.msg.String("hello"))

    (message,), _ = self.waitForCall(callback)
    ros_string = std_msgs.msg.String()
    ros_string.deserialize(message._buff)
    self.assertEqual(ros_string.data, "hello", "message contains wrong string")