-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_subscriber_node.py
More file actions
64 lines (44 loc) · 1.99 KB
/
example_subscriber_node.py
File metadata and controls
64 lines (44 loc) · 1.99 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import rclpy
from rclpy.node import Node
# Import necessary ROS data types for data transferring
# See links for reference:
# - Data message specification: http://wiki.ros.org/msg
# - Source files for message types: https://github.com/ros/std_msgs/tree/kinetic-devel/msg
from std_msgs.msg import Int32, Int32MultiArray
# TODO: Put external library imports below
class ExampleSubscriberNode(Node):
def __init__(self):
# TODO: Replace "example_subscriber_node" with the name of the subscriber node
super().__init__('example_subscriber_node')
# Creates a subscriber attached to this node and listening to "example_topic"
self.subscriber_handle = self.create_subscription(
# TODO: Replace "Int32" with the corresponding type of data sent over the topic
Int32,
# TODO: Replace "example_topic" with the name of the topic
'example_topic',
# TODO: Replace "on_subscriber_data_received" with the name of the callback function
self.on_subscriber_data_received,
# NOTE: This parameter sets how many messages are to be received
10,
)
"""
Callback function for when data is received via 'example_topic'
"""
def on_subscriber_data_received(self, msg):
self.get_logger().info(f'Received data: {msg.data}')
def main(args=None):
# Initialize ROS communication for this node
rclpy.init(args=args)
# Initialize the node
node = ExampleSubscriberNode()
# Start up the node
# NOTE: This line will block until a shutdown command is received (like Ctrl + C)
rclpy.spin(node)
# Destroy the node explicitly
# NOTE: optional - otherwise it will be done automatically when the garbage collector destroys it
node.destroy_node()
# Shutdown ROS communication for this node
rclpy.shutdown()
# Only run the 'main()' code when calling this script directly
if __name__ == '__main__':
main()