代码拉取完成,页面将自动刷新
#!/usr/bin/env python
'''
This script makes Gazebo less fail by translating gazebo status messages to odometry data.
Since Gazebo also publishes data faster than normal odom data, this script caps the update to 20hz.
Winter Guerra
'''
import rospy
from nav_msgs.msg import Odometry
from geometry_msgs.msg import Pose, Twist, Transform, TransformStamped
from gazebo_msgs.msg import LinkStates
from std_msgs.msg import Header
import numpy as np
import math
import tf2_ros
class OdometryNode:
# Set publishers
pub_odom = rospy.Publisher('/odom', Odometry, queue_size=1)
def __init__(self):
# init internals
self.last_received_pose = Pose()
self.last_received_twist = Twist()
self.last_recieved_stamp = None
# Set the update rate
rospy.Timer(rospy.Duration(.05), self.timer_callback) # 20hz
self.tf_pub = tf2_ros.TransformBroadcaster()
# Set subscribers
rospy.Subscriber('/gazebo/link_states', LinkStates, self.sub_robot_pose_update)
def sub_robot_pose_update(self, msg):
# Find the index of the racecar
try:
arrayIndex = msg.name.index('racecar::base_footprint')
except ValueError as e:
# Wait for Gazebo to startup
pass
else:
# Extract our current position information
self.last_received_pose = msg.pose[arrayIndex]
self.last_received_twist = msg.twist[arrayIndex]
self.last_recieved_stamp = rospy.Time.now()
def timer_callback(self, event):
if self.last_recieved_stamp is None:
return
cmd = Odometry()
cmd.header.stamp = self.last_recieved_stamp
cmd.header.frame_id = 'odom'
cmd.child_frame_id = 'base_footprint'
cmd.pose.pose = self.last_received_pose
cmd.twist.twist = self.last_received_twist
cmd.pose.covariance =[1e-3, 0, 0, 0, 0, 0,
0, 1e-3, 0, 0, 0, 0,
0, 0, 1e6, 0, 0, 0,
0, 0, 0, 1e6, 0, 0,
0, 0, 0, 0, 1e6, 0,
0, 0, 0, 0, 0, 1e3]
cmd.twist.covariance = [1e-9, 0, 0, 0, 0, 0,
0, 1e-3, 1e-9, 0, 0, 0,
0, 0, 1e6, 0, 0, 0,
0, 0, 0, 1e6, 0, 0,
0, 0, 0, 0, 1e6, 0,
0, 0, 0, 0, 0, 1e-9]
self.pub_odom.publish(cmd)
tf = TransformStamped(
header=Header(
frame_id=cmd.header.frame_id,
stamp=cmd.header.stamp
),
child_frame_id=cmd.child_frame_id,
transform=Transform(
translation=cmd.pose.pose.position,
rotation=cmd.pose.pose.orientation
)
)
self.tf_pub.sendTransform(tf)
# Start the node
if __name__ == '__main__':
rospy.init_node("gazebo_odometry_node")
node = OdometryNode()
rospy.spin()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。