Tutorial2
一.写一个tf2的broadcaster
本教程关于怎样broadcast一个机器人的坐标系到tf2上.
1.创建一个learning_tf2包
catkin_create_pkg learning_tf2 tf2 tf2_ros roscpp rospy turtlesim
本文要broadcast变化中的turtles的坐标系.
新建文件src/turtle_tf2_broadcaster.cpp
//
// Created by gary on 2019/9/4.
// #include <ros/ros.h>
#include <tf2/LinearMath/Quaternion.h>
#include <tf2_ros/transform_broadcaster.h>
#include <geometry_msgs/TransformStamped.h>
#include <turtlesim/Pose.h> std::string turtle_name; void poseCallback(const turtlesim::PoseConstPtr& msg)
{
static tf2_ros::TransformBroadcaster br;
geometry_msgs::TransformStamped transformStamped; transformStamped.header.stamp = ros::Time::now();
transformStamped.header.frame_id = "world";
transformStamped.child_frame_id = turtle_name;
transformStamped.transform.translation.x = msg->x;
transformStamped.transform.translation.y = msg->y;
transformStamped.transform.translation.z = 0.0; tf2::Quaternion q;
q.setRPY(, , msg->theta);
transformStamped.transform.rotation.x = q.x();
transformStamped.transform.rotation.y = q.y();
transformStamped.transform.rotation.z = q.z();
transformStamped.transform.rotation.w = q.w();
br.sendTransform(transformStamped);
} int main(int argc, char**argv)
{
ros::init(argc, argv, "my_tf2_broadcaster");
ros::NodeHandle private_node("~");
if(!private_node.hasParam("turtle"))
{
if(argc != )
{
ROS_ERROR("need turtle name as argument");
return -;
}
turtle_name = argv[];
}
else
{
private_node.getParam("turtle", turtle_name);
}
ros::NodeHandle node;
ros::Subscriber sub = node.subscribe(turtle_name + "/pose", , &poseCallback); ros::spin();
return ;
}
添加一个launch文件
start_demo.launch
<launch>
<!-- Turtlesim Node-->
<node pkg="turtlesim" type="turtlesim_node" name="sim"/> <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>
<!-- Axes -->
<param name="scale_linear" value="" type="double"/>
<param name="scale_angular" value="" type="double"/> <node pkg="learning_tf2" type="turtle_tf2_broadcaster"
args="/turtle1" name="turtle1_tf2_broadcaster" />
<node pkg="learning_tf2" type="turtle_tf2_broadcaster"
args="/turtle2" name="turtle2_tf2_broadcaster" /> </launch>
修改CMakeLists.txt,添加如下
add_executable(turtle_tf2_broadcaster src/turtle_tf2_broadcaster.cpp)
target_link_libraries(turtle_tf2_broadcaster
${catkin_LIBRARIES}
) ## Mark other files for installation (e.g. launch and bag files, etc.)
install(FILES
start_demo.launch
# myfile2
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
然后编译之.
启动:
$ roslaunch learning_tf2 start_demo.launch
启动之后,就可以在turtle sim中看到一个turtle.
2.查看结果
$ rosrun tf tf_echo /world /turtle1
通过上面的命令可以看到turtle1在world中的位置信息.目前turtle2的信息没有.
二. 写一个tf2的listener
创建文件 src/turtle_tf2_listener.cpp
//
// Created by gary on 2019/9/4.
//
#include <ros/ros.h>
#include <tf2_ros/transform_listener.h>
#include <geometry_msgs/TransformStamped.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Spawn.h> int main(int argc, char** argv)
{
ros::init(argc, argv, "my_tf2_listener");
ros::NodeHandle node; ros::service::waitForService("spawn");
ros::ServiceClient spawner = node.serviceClient<turtlesim::Spawn>("spawn");
turtlesim::Spawn turtle;
turtle.request.x = ;
turtle.request.y = ;
turtle.request.theta = ;
turtle.request.name = "turtle2"; spawner.call(turtle);
ros::Publisher turtle_vel =
node.advertise<geometry_msgs::Twist>("turtle2/cmd_vel", );
tf2_ros::Buffer tfBuffer;
tf2_ros::TransformListener tfListener(tfBuffer); ros::Rate rate(10.0); while(node.ok())
{
geometry_msgs::TransformStamped transformStamped;
try
{
transformStamped = tfBuffer.lookupTransform("turtle2", "turtle1",ros::Time());
}
catch (tf2::TransformException &ex)
{
ROS_WARN("%s",ex.what());
ros::Duration(1.0).sleep();
continue;
}
geometry_msgs::Twist vel_msg;
vel_msg.angular.z = 4.0 * atan2(transformStamped.transform.translation.y,
transformStamped.transform.translation.x);
vel_msg.linear.x = 0.5 * sqrt(pow(transformStamped.transform.translation.x,) +
pow(transformStamped.transform.translation.y, ));
turtle_vel.publish(vel_msg);
rate.sleep(); }
return ;
}
CMakeLists.txt
add_executable(turtle_tf2_listener src/turtle_tf2_listener.cpp)
target_link_libraries(turtle_tf2_listener
${catkin_LIBRARIES}
)
tf2_ros::Buffer tfBuffer;
tf2_ros::TransformListener tfListener(tfBuffer);
说明一下:
创建了一个TransformListener对象,一旦TransformListener被创建,它就开始接收tf2的变换,且会默认保存10s的变换.
transformStamped = tfBuffer.lookupTransform("turtle2", "turtle1",
ros::Time(0));
参数说明:
- We want the transform to this frame (target frame) ...
- ... from this frame (source frame).
The time at which we want to transform. Providing ros::Time(0) will just get us the latest available transform.
- Duration before timeout. (optional, default=ros::Duration(0.0))
在launch文件中添加如下
<launch>
<!--Turtlesim Node-->
<node pkg = "turtlesim" type = "turtlesim_node" name="sim"/>
<node pkg = "turtlesim" type = "turtle_teleop_key" name = "teleop" output = "screen"/> <!-- Axes -->
<param name="scale_linear" value="" type="double"/>
<param name="scale_angular" value="" type="double"/> <node pkg = "learning_tf2" type = "turtle_tf2_broadcaster"
args = "/turtle1" name ="turtle1_tf2_broadcaster" output = "screen" /> <node pkg = "learning_tf2" type = "turtle_tf2_broadcaster"
args = "/turtle2" name ="turtle2_tf2_broadcaster" output = "screen" />
<node pkg="learning_tf2" type="turtle_tf2_listener"
name="listener" />
</launch>
启动launch文件
$ roslaunch learning_tf2 start_demo.launch
Tutorial2的更多相关文章
- RDF和Jena RDF API简介
这是官方文章<An Introduction to RDF and the Jena RDF API>的译文.原文是在刺猬的温驯这里看到的.其中的图片没法显示了,还有一段丢失了.于是我在此 ...
- Redis 学习笔记续
Redis - 数据类型 Redis支持5种类型的数据类型,它描述如下的: 字符串 Redis字符串是字节序列.Redis字符串是二进制安全的,这意味着他们有一个已知的长度没有任何特殊字符终止,所以你 ...
- Git引用
原文: http://gitbook.liuhui998.com/7_3.html git中,分支(branch), 远程跟踪分支(remote-tracking branch)以及标签(tag)都是 ...
- React JS快速入门教程
翻译至官方文档<Tutorial>http://facebook.github.io/react/docs/tutorial.html 转载请注明出处:http://blog.csdn.n ...
- React学习一
一.运行起来react 1.引入react库 引入对应的js即可,第三方cdn引入 <script src="https://cdnjs.cloudflare.com/ajax/lib ...
- Struts2 配置详解
1. web.xml 此文件的配置可以参看struts2的示例文档 <filter> <filter-name>struts2</filter-name> < ...
- C++混合编程之idlcpp教程Python篇(4)
上一篇在这 C++混合编程之idlcpp教程Python篇(3) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial2中,同样加入了三个文件 Pyt ...
- C++混合编程之idlcpp教程Lua篇(4)
上一篇在这 C++混合编程之idlcpp教程Lua篇(3) 与前面的工程相似,工程LuaTutorial2中,同样加入了三个文件 LuaTutorial2.cpp, Tutorial2.i, tut ...
- jQuery 图片剪裁插件初探之 Jcrop
主页:http://deepliquid.com/content/Jcrop.html 官方下载地址:http://deepliquid.com/content/Jcrop_Download.html ...
随机推荐
- [CSS]CSS中使用span和div遇到的问题
一. span和div的区别 1.span是行级元素,div是块级元素2.span占用的宽度是内容的宽度,而div默认是一行.所以一般在页面中,只有一行或不到一行文字用span,元素占据多行时用div ...
- 对Promise的研究4
Promise.reject() Promise.reject(reason)方法也会返回一个新的 Promise 实例,该实例的状态为rejected. const p = Promise.reje ...
- 对webpack的初步研究1
一.概念: 1.webpack的核心是用于现代JavaScript应用程序的静态模块捆绑器.当webpack处理您的应用程序时,它会在内部构建一个依赖关系图,它映射您的项目所需的每个模块并生成一个或多 ...
- c++11 默认函数的控制
1. 类与默认函数: C++中声明自定义的类,编译器会默认生成未定义的成员函数: 构造函数 拷贝构造函数 拷贝赋值函数(operator=) 移动构造函数 移动拷贝函数 析构函数 编译器还会提供全局默 ...
- 英语单词composing
composing 来源——书籍Python.Crash.Course.2015.11 Using Individual Values from a List You can use individu ...
- grunt教程
https://blog.csdn.net/sinat_38992528/article/details/79400595
- prim 模板
#include<cstdio> #include<vector> #include<cstring> #include<set> #define ma ...
- 为应用程序池 ''DefaultAppPool'' 提供服务的进程意外终止。进程 ID 是 ''xxx''问题的解决方法
网上提供了很多办法,都未解决. 解决过程一波三折,依次用了下列方法: 1.解决办法 点击“开始”-“控制面板”-“管理工具”-“组件服务”-“计算机”-“我的电脑”-“DCOM”选项, 选择其下的“I ...
- ps磨皮的方法
磨皮顾名思义就是将皮肤模糊掉,它可以去除皮肤上的一些斑点以及细纹,从而使皮肤看起来更加光滑.下面就是两张磨皮前后的对比图,很明显右边的人物的皮肤看起来要更好一些:给人的视觉效果也更年轻一点. 磨皮前后 ...
- css3爆炸轮播效果
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="UTF-8&qu ...