译:在ROS上发布Odometry信息

在ROS上发布Odometry信息

描述: 这一教程提供了一个为Navigation Stack发布Odometry消息的例子,不仅包含了如何发布nav_msgs/Odometry消息,还包括一个”odom”坐标帧到“base_link”坐标帧的变换。

1. 在ROS上发布Odometry信息


Navigation Stack使用tf来判断机器人的位置以及对传感器数据进行变换,但是tf并不包含任何关于机器人速度的信息。因此,Navigation Stack需要一个Odometry source来发布一个变换关系以及一个包含速度信息的nav_msgs/Odometry消息。本文将解释nav_msgs/Odometry消息并提供一个发布消息和变换的简单例子。

2. nav_msgs/Odometry消息


nav_msgs/Odometry消息中保存了机器人在自由空间的位置和速度估计。

# This represents an estimate of a position and velocity in free space.  
# The pose in this message should be specified in the coordinate frame given by header.frame_id.
# The twist in this message should be specified in the coordinate frame given by the child_frame_id
Header header
string child_frame_id
geometry_msgs/PoseWithCovariance pose
geometry_msgs/TwistWithCovariance twist

消息中的pose字段对应于Odometry帧中对机器人的估计位置,该字段包含了一个可选的位置估计的协方差字段。twist字段则对应于child 帧中机器人的速度,一般是移动基座(mobile base)的坐标帧,该字段包含了一个可选的速度估计的协方差字段。

3. 使用tf发布Odometry变换

变换配置教程,我们介绍了tf是通过变换树来管理坐标帧转换之间的关系的。因此,任何Odometry source都必须发布其管理的坐标帧相关的信息,阅读下面的内容前请先了解tf的用法。

4. 编码


这一部分我们将为一个虚拟的转圈机器人编写一个发布nav_msgs/Odometry消息以及变换关系的例子。

在你的package.xml文件中添加以下依赖:

<depend package="tf"/>
<depend package="nav_msgs"/>
#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <nav_msgs/Odometry.h>

int main(int argc, char** argv){
  ros::init(argc, argv, "odometry_publisher");

  ros::NodeHandle n;
  ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("odom", 50);
  tf::TransformBroadcaster odom_broadcaster;

  double x = 0.0;
  double y = 0.0;
  double th = 0.0;

  double vx = 0.1;
  double vy = -0.1;
  double vth = 0.1;

  ros::Time current_time, last_time;
  current_time = ros::Time::now();
  last_time = ros::Time::now();

  ros::Rate r(1.0);
  while(n.ok()){

    ros::spinOnce();               // check for incoming messages
    current_time = ros::Time::now();

    //compute odometry in a typical way given the velocities of the robot
    double dt = (current_time - last_time).toSec();
    double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
    double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
    double delta_th = vth * dt;

    x += delta_x;
    y += delta_y;
    th += delta_th;

    //since all odometry is 6DOF we'll need a quaternion created from yaw
    geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);

    //first, we'll publish the transform over tf
    geometry_msgs::TransformStamped odom_trans;
    odom_trans.header.stamp = current_time;
    odom_trans.header.frame_id = "odom";
    odom_trans.child_frame_id = "base_link";

    odom_trans.transform.translation.x = x;
    odom_trans.transform.translation.y = y;
    odom_trans.transform.translation.z = 0.0;
    odom_trans.transform.rotation = odom_quat;

    //send the transform
    odom_broadcaster.sendTransform(odom_trans);

    //next, we'll publish the odometry message over ROS
    nav_msgs::Odometry odom;
    odom.header.stamp = current_time;
    odom.header.frame_id = "odom";

    //set the position
    odom.pose.pose.position.x = x;
    odom.pose.pose.position.y = y;
    odom.pose.pose.position.z = 0.0;
    odom.pose.pose.orientation = odom_quat;

    //set the velocity
    odom.child_frame_id = "base_link";
    odom.twist.twist.linear.x = vx;
    odom.twist.twist.linear.y = vy;
    odom.twist.twist.angular.z = vth;

    //publish the message
    odom_pub.publish(odom);

    last_time = current_time;
    r.sleep();
  }
}
#include <tf/transform_broadcaster.h>
#include <nav_msgs/Odometry.h>

因为我们既要发布odom到base_link的变换,也要发布nav_msgs/Odometry消息,所以include这两个头文件。

  ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("odom", 50);
  tf::TransformBroadcaster odom_broadcaster;

创建发布器以及tf::TransformBroadcaster来发布消息和tf。

  double x = 0.0;
  double y = 0.0;
  double th = 0.0;

假设机器人从odom坐标帧的起始位置开始。

  double vx = 0.1;
  double vy = -0.1;
  double vth = 0.1;

设置一些速度,让base_link帧在odom帧中以x方向0.1m/s,y方向-0.1m/s以及th方向0.1rad/s进行运动,这或多或少会让我们的虚拟机器人转圈。

  ros::Rate r(1.0);

简单起见,这里以1Hz的频率发布Odometry信息,大多数系统会希望以更高的频率发布odometry。

    //compute odometry in a typical way given the velocities of the robot
    double dt = (current_time - last_time).toSec();
    double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
    double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
    double delta_th = vth * dt;

    x += delta_x;
    y += delta_y;
    th += delta_th;

这里我们基于设置的固定速度来更新Odometry信息,当然,真实的Odometry系统会结合计算的真实速度来更新。

    //since all odometry is 6DOF we'll need a quaternion created from yaw
    geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);

一般我们会在系统中使用所有信息的3D版本来兼容2D和3D的信息,并尽量减少所需要创建的消息数量。因此,我们需要将我们yaw数值转换为一个四元组(Quaternion)。幸运的是,tf提供了从yaw创建Quaternion的接口,并能从Quaternion中方便地操作yaw数据。

    //first, we'll publish the transform over tf
    geometry_msgs::TransformStamped odom_trans;
    odom_trans.header.stamp = current_time;
    odom_trans.header.frame_id = "odom";
    odom_trans.child_frame_id = "base_link";

这里我们创建利用tf来发送的TransformStamped消息,我们希望在current_time从odom帧变换为base_link帧,因此,我们需要向上面这段代码一样设置消息。

    odom_trans.transform.translation.x = x;
    odom_trans.transform.translation.y = y;
    odom_trans.transform.translation.z = 0.0;
    odom_trans.transform.rotation = odom_quat;

    //send the transform
    odom_broadcaster.sendTransform(odom_trans);

这里我们将根据odometry数据填充transform消息,然后通过TransformBroadcaster发布该变换。

    //next, we'll publish the odometry message over ROS
    nav_msgs::Odometry odom;
    odom.header.stamp = current_time;
    odom.header.frame_id = "odom";

我们还需要发布nav_msgs/Odometry消息以让navigation stack能接收速度信息,这里将消息头设为current_time以及“odom”坐标帧。

    //set the position
    odom.pose.pose.position.x = x;
    odom.pose.pose.position.y = y;
    odom.pose.pose.position.z = 0.0;
    odom.pose.pose.orientation = odom_quat;

    //set the velocity
    odom.child_frame_id = "base_link";
    odom.twist.twist.linear.x = vx;
    odom.twist.twist.linear.y = vy;
    odom.twist.twist.angular.z = vth;

这里在消息中填充了odometry数据,这里将消息的child_frame_id设为“base_link”,因为这是我们要将速度信息发往的坐标帧。

参考资料

  1. navigation/Tutorials/RobotSetup/Odom
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值