当前位置:网站首页>TF learning notes in ROS
TF learning notes in ROS
2022-06-12 15:01:00 【From spring to winter】
List of articles
One 、turtle_tf_broadcaster.cpp
#include <tf/transform_broadcaster.h>
// Use TransformBroadcaster, We need to include header files :tf/transform_broadcaster.h
#include <turtlesim/Pose.h>
std::string turtle_name;
void poseCallback(const turtlesim::PoseConstPtr& msg)
//ConstPtr& Is a reference to a constant pointer
{
static tf::TransformBroadcaster br;
// Create a TransformBroadcaster , We need it to broadcast conversions
tf::Transform transform;
transform.setOrigin(tf::Vector3(msg->x, msg->y, 0.0));
// Create a transform object , We will turtle Of 2D The location information is transformed into 3D Of transform in
tf::Quaternion q;
q.setRPY(0, 0, msg->theta);
// Create a quaternion , hold turtle The Euler angle data in is converted to quaternions q
transform.setRotation(q);
// Here we set the rotation
br.sendTransform(tf::StampedTransform(transform, ros::Time::now( ), "world", turtle_name));
// This is the executive part ,TransformBroadcaster Publishing transformation requires four parameters
//1. Publish the transformation itself
//2. Give the published transformation a timestamp , You need to mark it in the present time , Use ros::Time::now() Get the present time
//3. Publish parent coordinate system , In the case of “world”
//4. Publish sub coordinate systems , In the case of “turtle_name”
}
int main(int argc, char** argv) //argc Indicates the number of commands received ,argv Indicates the contents of the incoming command
// for example : The compiled program is my.exe, In command execution my.exe, be : here argc Namely 1, Parameters received are 1 individual , The parameter argv[0] yes “my.exe”
// Executing orders my.exe 1 2 3 be : here argc Namely 4 individual , The parameter argv[0] yes “my.exe”, Parameters argv[1] yes “1”, Parameters argv[2] yes “2”, Parameters argv[3] yes “3”
{
ros::init(argc, argv, "my_tf_broadcaster");
if (argc != 2){
ROS_ERROR("need turtle name as argument"); return -1;};
//return If you don't answer anything , In fact, that is void Return of type function , Generally no longer execute return Subsequent statements ;
// If the function is executed successfully, it returns 0, Unsuccessful returns non-zero , Generally, non-zero values are used -1 Express ;
//return 0; Generally used at the end of the main function , Indicates that the program terminates normally , That is, tell the system that the program is normal .
//return 1 perhaps -1; Indicates that a surrogate value is returned , Usually used at the end of a subfunction , Indicates that the program terminated abnormally
turtle_name = argv[1];
// The parameter passed in names the previously defined turtle_name
ros::NodeHandle node;
ros::Subscriber sub = node.subscribe(turtle_name+"/pose", 10, &poseCallback);
ros::spin();
return 0;
};
Two 、turtle_tf_listener.cpp
#include <tf/transform_listener.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Spawn.h>
int main(int argc, char** argv)
{
ros::init(argc, argv, "my_tf_listener");
ros::NodeHandle node;
ros::service::waitForService("spawn");
ros::ServiceClient add_turtle =
node.serviceClient<turtlesim::Spawn>("spawn");
turtlesim::Spawn srv;
add_turtle.call(srv);
// Create a serviceClient That is, the client requesting the service
//add_turtle.call(srv), Client request spawn service , Generate another turtlebot
ros::Publisher turtle_vel =
node.advertise<geometry_msgs::Twist>("turtle2/cmd_vel", 10);
tf::TransformListener listener;
// Create a TransformListener object
ros::Rate rate(10.0);
while (node.ok())
{
tf::StampedTransform transform;
try
{
listener.lookupTransform("/turtle2", "/turtle1",
ros::Time(0), transform);
// We use listener Receive specific transformations , It contains four parameters :
// 1. /turtle1 To /turtle2 Coordinate system transformation
// 2. Use ros::Time(0) Get the latest available transform
// 3. transform Used to store transformation results
}
catch(tf::TransformException ex)
{
ROS_ERROR("%s", ex.what());
ros::Duration(1.0).sleep();
continue;
// Use try catch Statement block to determine whether there is an exception ;
//try Judge , If there is an abnormal entry catch sentence ,ex.what() return char Array
//continue: End this cycle , This cycle continue The latter part is no longer executed , Start the next cycle
// Pay attention to the distinction here break,break Is to permanently terminate the loop ,break The following is no longer executed ,while The loop is no longer executed
}
geometry_msgs::Twist vel_msg;
vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(), transform.getOrigin().x());
vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2)+
pow(transform.getOrigin().y(), 2));
//transform.getOrigin().x() obtain transform Transform values in , Or get turtle2 stay turtle1 In a coordinate system x value ;
//transform.getOrigin().y() obtain turtle2 stay turtle1 In a coordinate system y value ;
// This is mainly through turtle1 The distance and angle of turtle2 Linear velocity and angular velocity of
turtle_vel.publish(vel_msg);
// The new speed passes turtle2/cmd_cel Topic to post , to update turtle2 The position of
rate.sleep();
}
return 0;
};
3、 ... and 、 compile
1.CMakeList.txt
There are three places that need to be modified :
1.
2.
3.
2.package.xml
There is one place that needs to be modified :
3. To write launch file start_demo.launch

Four 、 perform
1. start-up launch
// perform
roscore
roslaunch learning_tf start_demo.launch

Launch After the file runs , You can see turtle1_tf_broadcaster、turtle2_tf_broadcaster Start broadcasting , meanwhile turtlesim The simulation node starts .
2. start-up listener
rosrun learning_tf turtle_tf_listener


start-up listener node ,listener Call in program spawn Service generation turtle2, At the same time turtle1、turtle2 Between tf Transformation , Caught once during operation error, That is, it was not carried out correctly at the beginning turtle1 And turtle2 Of tf Transformation , Before entering the next while The cycle can be carried out correctly tf Transformation .
according to tf Transformation , to turtle2 Issue speed command , Generate a chase process as shown in the figure above .
3. Start keyboard control node
rosrun turtlesim turtle_teleop_key

The cursor stays under the terminal running keyboard control , Use the up, down, left and right keys to control turtle1 motion ,turtle2 Changes will follow , As shown in the figure below :
Summary
Yesterday, Beijing welcomed 2020 The first snow in winter , The campus is beautiful ;
I think winter is more suitable for reading alone , Come on, everybody ~
边栏推荐
- 简单的爬虫框架:解析51job页面岗位信息
- 【Environment】1. Get the configuration in YML through the environment in the configuration class
- Function recursion example
- [wechat applet] 3 The first wechat applet
- h3c GR5200路由器上如何设置公网ip可以访问
- 左对齐,右对齐,随机数,goto,比较输出bool
- 【LDA】EM变分推理 粗略版笔记【待完善
- 产业端:618的新战场
- Autofac (2)
- Assertion of selenium webdriver
猜你喜欢

Writing method of JUnit multithreading

Simple crawler framework: parsing 51job page position information

野指针理解

tc菜单分割

Detailed explanation of factory pattern (simple factory pattern, factory method pattern, abstract factory pattern) Scala code demonstration

NETCORE combined with cap event bus to realize distributed transaction -- Introduction (1)
![[LDA] LDA theme model notes - mainly Dirichlet](/img/e0/bc96b141aa577106379fab63d9df40.png)
[LDA] LDA theme model notes - mainly Dirichlet

解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击

ROS初学者编写小乌龟以一定速度旋转一定角度的server

Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
随机推荐
Browser fingerprint interpretation
First set and follow set in vernacular
机器人前行、旋转的service编写
C语言打开中文路径文件
MySQL index and view
Selenium advanced
C main函数
C scanf function
如何写年终总结
Swap numbers, XOR, operator correlation
MH32F103ARPT6软硬件兼容替代STM32F103RCT6
阿里、腾讯、拼多多垂范,产业互联网的新逻辑渐显
关于互联网大厂裁员
SQL cross database injection
Thinking: what is asynchrony and thread safety
同花顺手机炒股开户安全吗
Serialization and deserialization mechanism in terms of games
【LDA】LDA主题模型笔记—主要是狄利克雷
野指针理解
Chapter I exercises of program construction and interpretation