2 Star 23 Fork 8

Zeende/racecar_sim

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gazebo_odometry.py 2.85 KB
一键复制 编辑 原始数据 按行查看 历史
zeende 提交于 2021-03-18 16:28 +08:00 . the last
#!/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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zeende/racecar_sim.git
git@gitee.com:zeende/racecar_sim.git
zeende
racecar_sim
racecar_sim
master

搜索帮助