最新消息:

ROS与navigation教程-发布里程计消息

ROS1/一代机器人系统 少儿编程 1679浏览 0评论
ROS与navigation教程

ROS与navigation教程-发布里程计消息

说明:

  • 介绍导航包发布里程信息的示例。
  • 它涵盖了通过ROS发布nav_msgs/Odometry消息,以及从“odom”坐标系到tf上的“base_link”坐标系的变换。

(1)发布里程信息

导航包使用TF来确定机器人在地图中的位置,并将传感器数据与静态地图相关联。然而,TF不提供关于机器人的速度的任何信息。因此,导航包要求任何里程源都通过ROS发布TF变换和nav_msgs/Odometry消息。本教程解释了nav_msgs/Odometry消息,并提供了用于发布消息并通过ROS和tf进行变换的示例代码。

(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对应于机器人在坐标系中的估计位置以及用于该位态估计的确定性的可选协方差。该消息中的twist对应于机器人在子坐标系中的速度,通常是移动基站的坐标系,以及用于该速度估计的确定性的可选协方差。

(3)使用tf发布Odometry变换

如变换配置教程中所述,“TF”软件库负责管理与机器人相关的变换树的各坐标系之间的关系。因此,任何里程源都必须发布有关其管理的坐标系的信息。

(4)编写代码

在本节中,我们将编写一些用于通过ROS发布 nav_msgs/Odometry 消息的示例代码,并使用tf进行变换,这种变换只是在一个循环中驱动的仿真的机器人。我们首先将代码全部显示出来,下面是一个一个一个的解释。

将依赖关系添加到包的manifest.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消息,我们需要包括相关的头文件。

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

我们需要创建一个 ros::Publisher 和一个 tf::TransformBroadcaster来分别使用ROS和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的速率发布里程信息,使得显示更简洁些,大多数系统将以更高的速率发布里程信息。

//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);

我们通常会尝试在我们的系统中使用所有消息的3D版本,以允许2D和3D组件在适当的情况下协同工作,并将消息数量保持在最低限度。因此,有必要将我们的yaw(偏航值)变为四元数。幸运的是,tf提供的功能允许从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";

在这里,我们将创建一个TransformStamped消息,通过tf发送。我们要发布在current_time时刻的从"odom"坐标系到“base_link”坐标系的变换。因此,我们将相应地设置消息头和child_frame_id,确保使用“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);

将我们的里程数据中填入变换消息中,然后使用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消息,以便导航包可以从中获取速度信息。我们将消息的header设置为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;

这将使用里程数据填充消息,并发送出去。我们将消息的child_frame_id设置为“base_link”坐标系,因为我们要发送速度信息到这个坐标系。

您必须 登录 才能发表评论!