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

Revision history [back]

Create a member variable in your class and assign latests received message to this member variable. Then simply use the member variable in your loop.

  # subscriber
  import rclpy
  from rclpy.node import Node
  from std_msgs.msg import String

  class MinimalSubscriber(Node):
    def __init__(self):
      super().__init__('minimal_subscriber')
      self.subscription = self.create_subscription(
          String,
          'topic',
          self.listener_callback,
          10)

      self.latest_msg = String()
      self.run_loop()

    def run_loop(self):
      while True:
        # use self.latest_msg here
        pass


    def listener_callback(self, msg):
      self.get_logger().info('I heard: "%s"' % msg.data)
      self.latest_msg = msg


  def main(args=None):
    rclpy.init(args=args)

    minimal_subscriber = MinimalSubscriber()

    rclpy.spin(minimal_subscriber)

    minimal_subscriber.destroy_node()
    rclpy.shutdown()


  if __name__ == '__main__':
    main()

Create Edit; in the original answer I did no understand the question. The run() function includes a member variable in your class and assign latests received message definetly runnjng while loop, so the object creation never completes since the function is executed in constructor. Instead of blocking while loop you may want to this member variable. Then simply use the member variable in your loop. timer callback for periodic loops. Below is an example;

  # subscriber
  import rclpy
 import rclpy.node
import rclpy.qos

from rclpy.node import Node
  from std_msgs.msg import String

  geometry_msgs.msg import Point


class MinimalSubscriber(Node):
ROS2Pub(rclpy.node.Node):

    def __init__(self):
      super().__init__('minimal_subscriber')
      self.subscription = self.create_subscription(
          String,
          'topic',
          self.listener_callback,
          10)

      self.latest_msg = String()
      self.run_loop()
__init__(self, *args):
        super(ROS2Pub, self).__init__("topic_pub")
        self.my_pub = self.create_publisher(
            Point, "points", rclpy.qos.qos_profile_sensor_data)
        self.timer = self.create_timer(0.1, self.timer_callback)
        self.get_logger().info("Started the node")

    def run_loop(self):
      while True:
        # use self.latest_msg here
        pass


    timer_callback(self):
        msg = Point()
        msg.x = 10.0
        self.my_pub.publish(msg)
        self.get_logger().info("Published a point with x of %s" % msg.x)


def listener_callback(self, msg):
      self.get_logger().info('I heard: "%s"' % msg.data)
      self.latest_msg = msg


  def main(args=None):
    rclpy.init(args=args)

    minimal_subscriber = MinimalSubscriber()

    rclpy.spin(minimal_subscriber)

    minimal_subscriber.destroy_node()
main():
    rclpy.init()
    node = ROS2Pub()
    rclpy.spin(node)
    rclpy.shutdown()


 if __name__ == '__main__':
    main()