-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_publisher_node.py
More file actions
70 lines (49 loc) · 2.12 KB
/
example_publisher_node.py
File metadata and controls
70 lines (49 loc) · 2.12 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
65
66
67
68
69
70
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 ExamplePublisherNode(Node):
def __init__(self):
# TODO: Replace "example_publisher_node" with the name of the publisher node
super().__init__('example_publisher_node')
# Creates a publisher attached to this node and posting to "example_topic"
self.publisher_handle = self.create_publisher(
# 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',
# NOTE: This parameter sets how many messages are to be sent
10,
)
timer_period = 1 / 10 # seconds
# Creates a timer that publishes to "example_topic" via a fixed time period
self.timer = self.create_timer(timer_period, self.publish_data_onto_topic)
"""
Callback function for publishing data to 'example_topic'
"""
def publish_data_onto_topic(self):
# Create the message packet
msg = Int32()
msg.data = 42
# Publish it on the topic "example_topic"
self.publisher_handle.publish(msg)
def main(args=None):
# Initialize ROS communication for this node
rclpy.init(args=args)
# Initialize the node
node = ExamplePublisherNode()
# 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()