Advertisement
Guest User

listener.py

a guest
Feb 2nd, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import socket
  2. import sys
  3. import json
  4.  
  5. import rclpy
  6. from rclpy.node import Node
  7.  
  8. from std_msgs.msg import String
  9.  
  10. from types import SimpleNamespace
  11.  
  12.  
  13. class MinimalSubscriber(Node):
  14.  
  15.     def __init__(self):
  16.         super().__init__('minimal_subscriber')
  17.         self.subscription = self.create_subscription(
  18.             String,
  19.             'topic',
  20.             self.listener_callback,
  21.             10)
  22.         self.subscription  # prevent unused variable warning
  23.  
  24.     def listener_callback(self, msg):
  25.         self.get_logger().info('I heard: "%s"' % msg.data)
  26.         x = json.loads(msg.data, object_hook=lambda d: SimpleNamespace(**d))
  27.         print(x.id, x.action)
  28.  
  29.  
  30. def main(args=None):
  31.     rclpy.init(args=args)
  32.  
  33.     minimal_subscriber = MinimalSubscriber()
  34.  
  35.     rclpy.spin(minimal_subscriber)
  36.  
  37.     # Destroy the node explicitly
  38.     # (optional - otherwise it will be done automatically
  39.     # when the garbage collector destroys the node object)
  40.     minimal_subscriber.destroy_node()
  41.     rclpy.shutdown()
  42.  
  43.  
  44. if __name__ == '__main__':
  45.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement