-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgazebo_true_odom.py
executable file
·54 lines (41 loc) · 1.81 KB
/
gazebo_true_odom.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
#!/usr/bin/env python
import sys
import rospy
from nav_msgs.msg import Odometry
from gazebo_msgs.srv import GetLinkState
class LinkStateToOdometry:
def getLinkState(self): # Position subscriber callback function
XX_state = self.gazebo_link_state(self.link_name, 'world')
self.Odometry.pose.pose = XX_state.link_state.pose
self.Odometry.twist.twist = XX_state.link_state.twist
self.Odometry.pose.pose.position.z += 0.4
# Add time stamp
self.Odometry.header.stamp = rospy.Time.now()
return
def start(self):
rate = rospy.Rate(50.0) # 50Hz
while not rospy.is_shutdown():
rate.sleep()
self.getLinkState()
self.pub1.publish(self.Odometry)
return
def __init__(self, link_name="base_link", topic_name="odometry", robot_name="X1", frame="world", child_frame="X1/base_link"):
node_name = topic_name + "_" + robot_name
rospy.init_node(node_name)
self.link_name = robot_name + "::" + robot_name + "/" + link_name
self.pubTopic1 = "/" + robot_name + "/" + topic_name
self.pub1 = rospy.Publisher(self.pubTopic1, Odometry, queue_size=10)
# Initialize Odometry message object
self.Odometry = Odometry()
self.Odometry.header.seq = 1
self.Odometry.header.frame_id = frame
self.Odometry.child_frame_id = child_frame
# Initialize Gazebo LinkState service
rospy.wait_for_service('/gazebo/get_link_state')
self.gazebo_link_state = rospy.ServiceProxy('/gazebo/get_link_state', GetLinkState, persistent=True)
if __name__ == '__main__':
publish_tool = LinkStateToOdometry(robot_name=sys.argv[1], child_frame=sys.argv[1] + "/base_link")
try:
publish_tool.start()
except rospy.ROSInterruptException:
pass