Tutorial1
阅读原文时间:2023年07月09日阅读:2

一 Introduction to tf2

本部分是关于tf2简单介绍,比如tf2能做什么,并使用一个turtlesim的例子来显示tf2在多机器人中的一些能力.同时也包括一些工具的使用,比如tf2_echo, view_frames, and rviz.

1.安装Demo

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

2.Running the Demo

$ roslaunch turtle_tf2 turtle_tf2_demo.launch

看到下面两个turtles,如下

在启动的界面使用方向键来控制中间的turtle运动,会看到另外一个turtle跟随运动.

3.上面都做了什么呢?

在这个demo里面使用tf2库来创建了三个坐标系:世界坐标系,turtle1坐标系,turtle2坐标系.

本教程使用了一个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坐标系的一个变换等同于如下:

:

$ 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
#include
#include
#include
#include

std::string static_turtle_name;

int main(int argc, char** argv)
{
ros::init(argc, argv, "my_static_tf2_broadcaster");
if(argc != )
{
ROS_ERROR("Invalid number of parameters\nusage: static_turtle_tf2_broadcaster child_frame_name x y z roll pitch yaw ");
return -;
}
if(strcmp(argv[], "world") == )
{
ROS_ERROR("Your static turtle name cannot be 'world' ");
return -;
}
static_turtle_name = argv[];
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[]);
static_transformStamped.transform.translation.y = atof(argv[]);
static_transformStamped.transform.translation.z = atof(argv[]);
tf2::Quaternion quat;
quat.setRPY(atof(argv[]), atof(argv[]), atof(argv[]));

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 ;  

}

修改CMakeLists.txt文件

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

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

然后运行之

$ rosrun learning_tf2 static_turtle_tf2_broadcaster mystaticturtle

rostopic echo /tf_static
transforms:


  • header:
    seq:
    stamp:
    secs:
    nsecs:
    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. 

比如

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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章