需求:发布两个坐标系的关系
流程:
1.包含头文件
2.设置编码 节点初始化
3.创建发布者对象
包含:#include “tf2_ros/static_transform_broadcaster.h”
tf2_ros::StaticTransformBroadcaster pub;
4.组织发布消息
包含:#include “geometry_msgs/TransformStamped.h”
geometry_msgs::TransformStamped tfs;
4.1.需要欧拉角转换
包含:#include “tf2/LinearMath/Quaternion.h”
tf2::Quaternion qtn;//创建欧拉角对象
//向该对象设置欧拉角,这个对象可以将欧拉角转换成四元素
qtn.setRPY(0,0,0);//欧拉角的单位是弧度
tfs.transform.rotation.x = qtn.getX();
tfs.transform.rotation.y = qtn.getY();
tfs.transform.rotation.z = qtn.getZ();
tfs.transform.rotation.w = qtn.getW();
5.发布数据
tfs.transform.rotation.x = qtn.getX();
6.spin()
ros::spin();
#include "ros/ros.h"
#include "tf2_ros/static_transform_broadcaster.h"
#include "geometry_msgs/TransformStamped.h"
#include "tf2/LinearMath/Quaternion.h"
/*
需求:发布两个坐标系的关系
流程:
1.包含头文件
2.设置编码 节点初始化
3.创建发布者对象
4.组织发布消息
5.发布数据
6.spin()
*/
int main(int argc, char *argv[])
{
//2.设置编码
setlocale(LC_ALL,"");
ros::init(argc,argv,"static_pub");
ros::NodeHandle nh;
//3.创建发布者对象
tf2_ros::StaticTransformBroadcaster pub;
//4.组织发布消息
geometry_msgs::TransformStamped tfs;
tfs.header.stamp=ros::Time::now();
tfs.header.frame_id="base_link";
tfs.child_frame_id="laser";
tfs.transform.translation.x=0.2;
tfs.transform.translation.y=0.0;
tfs.transform.translation.z=0.5;
//需要欧拉角转换
tf2::Quaternion qtn;//创建欧拉角对象
//向该对象设置欧拉角,这个对象可以将欧拉角转换成四元素
qtn.setRPY(0,0,0);//欧拉角的单位是弧度
tfs.transform.rotation.x = qtn.getX();
tfs.transform.rotation.y = qtn.getY();
tfs.transform.rotation.z = qtn.getZ();
tfs.transform.rotation.w = qtn.getW();
//5.发布数据
pub.sendTransform(tfs);
//6.spin
ros::spin();
return 0;
}