轉載自http://www.ncnynl.com/archives/201702/1328.html
ROS發布nav_msgs/Odometry消息,以及通過tf從“odom”坐標系到“base_link”坐標系的轉換。
在ROS上發布Odometry信息
導航包使用tf來確定機器人在世界中的位置,並將傳感器數據與靜態地圖相關聯。然而,tf不提供關於機器人的速度的任何信息。因此,導航包要求任何odometry源通過ROS發布包含速度信息的transform和nav_msgs/Odometry消息。
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. # 此消息中的姿勢應在由header.frame_id給定的坐標系中指定 # The twist in this message should be specified in the coordinate frame given by the child_frame_id # 這個消息中的twist應該在由child_frame_id給出的坐標系指定 Header header string child_frame_id geometry_msgs/PoseWithCovariance pose geometry_msgs/TwistWithCovariance twist
下面是更詳細的消息類型
std_msgs/Header header
string child_frame_id
geometry_msgs/PoseWithCovariance pose
geometry_msgs/TwistWithCovariance twist
該消息中的姿勢(pose)對應於機器人在世界坐標系中的估計位置以及該姿勢估計特定的可選協方差。
該消息中的速度(twist)對應於機器人在子坐標系的速度,通常是移動基站的坐標系,以及該速度估計特定的可選協方差。
使用tf發布Odometry變換
如變換配置教程中所討論的,“tf”軟件庫負責管理與變換樹中的機器人相關的坐標系之間的關系。
因此,任何odometry(里程)源都必須發布其管理的坐標系的相關信息。
編寫代碼
我們將編寫一些用於通過ROS發布 nav_msgs/Odometry消息的示例代碼,以及使用tf的虛擬機器人的變換。
catkin_create_pkg Odom tf nav_msgs roscpp rospy
#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組件在適當的時候一起工作,並將我們要創建的消息數量保持在最小。
- 因此,有必要將我們用於里程的偏航值轉換為四元數而發送出去。
- 幸運的是,tf提供了允許從偏航值容易地創建四元數並且容易地訪問四元數的偏航值的功能。
-
代碼:
//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);
-
解釋:這里我們從我們的odometry數據填充變換消息,然后使用我們的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消息,以便導航堆棧可以從中獲取速度信息。
- 我們將消息的頭部設置為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”坐標系,因為這是我們發送速度信息的坐標系。
CMakeLists.txt
add_executable(Odom_exam src/Odom_exam.cpp)
target_link_libraries(Odom_exam ${catkin_LIBRARIES})