Tutorial1

一 Introduction to tf2node

本部分是關於tf2簡單介紹,好比tf2能作什麼,並使用一個turtlesim的例子來顯示tf2在多機器人中的一些能力.同時也包括一些工具的使用,好比tf2_echo, view_frames, and rviz.ide

1.安裝Demo工具

sudo apt-get install ros-$ROS_DISTRO-turtle-tf2 ros-$ROS_DISTRO-tf2-tools ros-$ROS_DISTRO-tf

2.Running the Demospa

$ roslaunch turtle_tf2 turtle_tf2_demo.launch

看到下面兩個turtles,以下命令行

在啓動的界面使用方向鍵來控制中間的turtle運動,會看到另一個turtle跟隨運動.code

3.上面都作了什麼呢?orm

在這個demo裏面使用tf2庫來建立了三個座標系:世界座標系,turtle1座標系,turtle2座標系.blog

本教程使用了一個tf2 的broadcaster來發布turtle的座標系,以及一個tf2 的listener來計算兩個turtles座標系之間的差別.而後移動一個turtle來跟隨另外一個運動.教程

4.tf2 工具開發

4.1 使用view_frames

view_frames建立一個由tf2在ROS中發佈的座標系圖標.

$ rosrun tf2_tools view_frames.py

有一個tf2的listener來監聽ROS中發佈的frames,而後畫出由座標系組成的樹型結構:

$ evince frames.pdf

4.2 使用tf_echo

tf_echo得出在ROS中任意兩個座標系之間的transform.

rosrun tf tf_echo [reference_frame] [target_frame]

對於本demo中turtle2相對於trutle1座標系的一個變換等同於以下:

\large{$$\mathbf{T}_{turtle1\_turtle2} =\mathbf{T}_{turtle1\_world} *\mathbf{T}_{world\_turtle2}$$} :

$ rosrun tf tf_echo turtle1 turtle2

二 寫代碼實現一個tf2的靜態broadcaster

 1.建立一個包learning_tf2

$ catkin_create_pkg learning_tf2 tf2 tf2_ros roscpp rospy turtlesim

2.怎樣來broadcast一個transforms

怎樣broadcast座標系到tf2中.在本例中將broadcast變化中的turtles的座標系.

建立文件,包的src/static_turtle_tf2_broadcaster.cpp

#include <ros/ros.h>
#include <tf2_ros/static_transform_broadcaster.h>
#include <geometry_msgs/TransformStamped.h>
#include <cstdio>
#include <tf2/LinearMath/Quaternion.h>

std::string static_turtle_name;

int main(int argc, char** argv)
{
    ros::init(argc, argv, "my_static_tf2_broadcaster");
    if(argc != 8)
    {
        ROS_ERROR("Invalid number of parameters\nusage: static_turtle_tf2_broadcaster child_frame_name x y z roll pitch yaw ");
        return -1;
    }
    if(strcmp(argv[1], "world") == 0)
    {
        ROS_ERROR("Your static turtle name cannot be 'world' ");
        return -1;
    }
    static_turtle_name = argv[1];
    static tf2_ros::StaticTransformBroadcaster static_broadcaster;
    geometry_msgs::TransformStamped static_transformStamped;
    static_transformStamped.header.stamp = ros::Time::now();
    static_transformStamped.header.frame_id ="world";
    static_transformStamped.child_frame_id = static_turtle_name;
    static_transformStamped.transform.translation.x = atof(argv[2]);
    static_transformStamped.transform.translation.y = atof(argv[3]);
    static_transformStamped.transform.translation.z = atof(argv[4]);
    tf2::Quaternion quat;
    quat.setRPY(atof(argv[5]), atof(argv[6]), atof(argv[7]));

    static_transformStamped.transform.rotation.x = quat.x();
    static_transformStamped.transform.rotation.y = quat.y();
    static_transformStamped.transform.rotation.z = quat.z();
    static_transformStamped.transform.rotation.w = quat.w();

    static_broadcaster.sendTransform(static_transformStamped);
    ROS_INFO("Spinning until killed publishing %s to world",static_turtle_name.c_str());
    ros::spin();
    return 0;
}
View Code

修改CMakeLists.txt文件

add_executable(${PROJECT_NAME}_node src/static_turtle_tf2_broadcaster.cpp)

 target_link_libraries(${PROJECT_NAME}_node
   ${catkin_LIBRARIES}
 )
View Code

而後運行之

$ rosrun learning_tf2 static_turtle_tf2_broadcaster mystaticturtle 0 0 1 0 0 0
View Code
rostopic echo /tf_static 
transforms: 
  - 
    header: 
      seq: 0
      stamp: 
        secs: 1567585822
        nsecs: 250673262
      frame_id: "world"
    child_frame_id: "mystaticturtle"
    transform: 
      translation: 
        x: 0.0
        y: 0.0
        z: 1.0
      rotation: 
        x: 0.0
        y: 0.0
        z: 0.0
        w: 1.0
---

3.發佈靜態transform的合適方法

在實際的機器人開發使用中,基本不會使用上面的方式來發布靜態tf,應該使用一個可執行節點static_transform_publisher來執行,其要麼在命令行執行,要麼在launch文件中執行.

static_transform_publisher x y z yaw pitch roll frame_id child_frame_id

    Publish a static coordinate transform to tf2 using an x/y/z offset in meters and yaw/pitch/roll in radians. (yaw is rotation about Z, pitch is rotation about Y, and roll is rotation about X). 

static_transform_publisher x y z qx qy qz qw frame_id child_frame_id

    Publish a static coordinate transform to tf2 using an x/y/z offset in meters and quaternion. 

好比

<launch>
     <node pkg="tf2_ros" type="static_transform_publisher" name="link1_broadcaster" args="1 0 0 0 0 0 1 link1_parent link1" />
  </launch>

Unlike in tf, there is no period argument, and a latched topic is used.

相關文章
相關標籤/搜索