ROS2无法建立起odom->base_footprint之间的tf2变换
-
使用里程计发布odom话题,并建立odom->base_footprint的之间tf2变换,代码如下:
import rclpy from rclpy.node import Node from nav_msgs.msg import Odometry import tf2_ros from tf2_ros import TransformBroadcaster from geometry_msgs.msg import TransformStamped import math class OdometryPublisher(Node): def __init__(self): super().__init__('odom_tf') self.publisher = self.create_publisher(Odometry, 'odom', 10) self.timer = self.create_timer(0.1, self.publish_odometry) self.odom_frame_id = 'odom' self.child_frame_id = 'base_link' self.odom_broadcaster = TransformBroadcaster(self) # 初始化里程计变量 self.x = 1.0 self.y = -30.0 self.theta = 1.0 self.vx = 0.0 # 例子中使用的线速度 self.vtheta = 0.0 # 例子中使用的角速度 def publish_odometry(self): # 更新里程计信息 self.x += self.vx * math.cos(self.theta) self.y += self.vx * math.sin(self.theta) self.theta += self.vtheta # 发布TransformStamped消息 odom_trans = TransformStamped() odom_trans.header.stamp = self.get_clock().now().to_msg() odom_trans.header.frame_id = self.odom_frame_id odom_trans.child_frame_id = self.child_frame_id odom_trans.transform.translation.x = self.x odom_trans.transform.translation.y = self.y odom_trans.transform.rotation.w = math.cos(self.theta / 2) odom_trans.transform.rotation.z = math.sin(self.theta / 2) self.odom_broadcaster.sendTransform(odom_trans) # 发布Odometry消息 odom_msg = Odometry() odom_msg.header.stamp = self.get_clock().now().to_msg() odom_msg.header.frame_id = self.odom_frame_id odom_msg.child_frame_id = self.child_frame_id odom_msg.pose.pose.position.x = self.x odom_msg.pose.pose.position.y = self.y odom_msg.pose.pose.orientation.w = math.cos(self.theta / 2) odom_msg.pose.pose.orientation.z = math.sin(self.theta / 2) odom_msg.twist.twist.linear.x = self.vx odom_msg.twist.twist.angular.z = self.vtheta self.publisher.publish(odom_msg) def main(args=None): rclpy.init(args=args) node = OdometryPublisher() rclpy.spin(node) rclpy.shutdown() if __name__ == '__main__': main()
tf树也显示了连接:
但是无法建立map->odom之间的联系,而且在rviz2里面以odom为Fixed Frame,也没有雷达点云信息
如果使用终端发布固定tf2变换,就可以建立odom与雷达之间的联系ros2 run tf2_ros static_transform_publisher 0 0 0 0 0 0 odom base_footprint
而且也建立了map->odom之间的tf2变换,也可以进行导航,只不过机器人无法相对于map进行移动