forked from hiro-wpi/Mobile_Robot_ROS_Control_Interfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_and_pub_template.py
executable file
·45 lines (34 loc) · 1.12 KB
/
sub_and_pub_template.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
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist
class SubAndPub():
def __init__(self):
# ROS
# subscribe to keyboard input
self.subsciber = rospy.Subscriber("key", String, self.get_key_callback)
# publish the control command
self.publisher = rospy.Publisher("cmd_vel", Twist, queue_size=1)
def get_key_callback(self, data):
# Initialize published message
twist = Twist()
# Map to command
key = data.data
if key == "w":
twist.linear.x = 1.0
if key == "s":
twist.linear.x = -1.0
if key == "a":
twist.angular.z = 1.0
if key == "d":
twist.angular.z = -1.0
# rospy.loginfo("Send twist commad: Lx: ", twist.linear.x, ", Az: ", twist.angular.z)
self.publisher.publish(twist)
if __name__ == "__main__":
# Launch ros node class
rospy.init_node('sub_and_pub_template_node')
sub_and_pub = SubAndPub()
# Set rate and run
r = rospy.Rate(60)
while not rospy.is_shutdown():
r.sleep()