forked from sairishi10/MoPAT_Design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_format.py
73 lines (63 loc) · 1.59 KB
/
node_format.py
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
71
72
73
#MoPAT Design Lab - NAME
#Node name(ROS2) - Start Date
'''
Node definition
Subscribed topics:
List all topics it subscribes to
Published topics:
List all topics it publishes
Work:
The actual working of the node algo in 1 line
'''
#Import libraries
#ROS
import rclpy
from rclpy.node import Node
#ROS Messages
from std_msgs.msg import String
#Others - optional
import sys
import time
#MoPAT algorithm files - optional
#Any other function
def my_func(arg):
'''
Definition
Arguments:
args : xxxxx
Returns:
rets : xxxxx
'''
class our_node(Node):
def __init__(self):
super().__init__('node_name')
#Create publishers
self.pub1 = self.create_publisher(String, 'topic_name', 2) #(msg_type, topic_name, queue_size)
#Create subscribers - need not create a variable
self.create_subscription(String, 'topic_name', self.sub1_cb, 2) #(msg_type, topic_name, callback function, queue_size)
#Class variables
self.msg = String()
def run(self):
#YOUR CODE HERE
self.msg.data = "Hello Bitches!"
#Everytime you want to publish use
pub1.publish(self.msg)
#Callback functions
def sub1_cb(self, msg):
'''
Definition
Arguments:
msg : ROS message type
'''
print(msg.data)
def main():
rclpy.init()
#Create object
my_node = our_node()
#Spin will keep repeating the node until exit
rclpy.spin(my_node.run())
#Close node
my_node.destroy_node()
rclpy.shutdown()
if __name__ == "__main__":
main()