-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher_node.py
More file actions
41 lines (33 loc) · 1.11 KB
/
publisher_node.py
File metadata and controls
41 lines (33 loc) · 1.11 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
# First, let's create a simple publisher (talker)
# publisher_node.py
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class TalkerNode(Node):
def __init__(self):
# Give our node a name
super().__init__('talker')
# Create a publisher:
# - It will publish String messages
# - On the topic 'greetings'
# - Queue size of 10 messages
self.publisher = self.create_publisher(String, 'greetings', 10)
# Create a timer that calls our function every second
self.timer = self.create_timer(1.0, self.timer_callback)
self.count = 0
def timer_callback(self):
# Create a message
msg = String()
msg.data = f'Hello World: {self.count}'
# Publish the message
self.publisher.publish(msg)
# Let us know it was sent
self.get_logger().info(f'Publishing: {msg.data}')
self.count += 1
# How to run the publisher:
def main_publisher():
rclpy.init()
node = TalkerNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()