-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber_node.py
More file actions
34 lines (28 loc) · 896 Bytes
/
subscriber_node.py
File metadata and controls
34 lines (28 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Now let's create a subscriber (listener)
# subscriber_node.py
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class ListenerNode(Node):
def __init__(self):
# Give our node a name
super().__init__('listener')
# Create a subscriber:
# - It will subscribe to String messages
# - On the topic 'greetings'
# - And call listener_callback when a message arrives
self.subscription = self.create_subscription(
String,
'greetings',
self.listener_callback,
10)
def listener_callback(self, msg):
# When we receive a message, log it
self.get_logger().info(f'I heard: {msg.data}')
# How to run the subscriber:
def main_subscriber():
rclpy.init()
node = ListenerNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()