tf2_ros::TransformBroadcaster
#include "tf2_ros/transform_broadcaster.h"
auto node = rclcpp::Node::make_shared("test");
std::shared_ptr<tf2_ros::TransformBroadcaster> odom_broadcaster; //定义发布器
odom_broadcaster = std::make_unique<tf2_ros::TransformBroadcaster>(node);
geometry_msgs::msg::TransformStamped odom_trans;
odom_broadcaster->sendTransform(odom_trans);
tf2_ros::TransformListener
#include "tf2_ros/transform_listener.h"
auto node = rclcpp::Node::make_shared("test");
tf2_ros::Buffer *tf_buffer;
tf_buffer = new tf2_ros::Buffer(node->get_clock());
tf2_ros::TransformListener *tf_listener = new tf2_ros::TransformListener(*tf_buffer,node);
geometry_msgs::msg::TransformStamped lis_trans;
rclcpp::WallRate loop(1);
while (rclcpp::ok())
{
try
{
lis_trans = tf_buffer->lookupTransform(map_frame_,base_frame_,tf2::TimePointZero );
RCLCPP_INFO(node->get_logger(),"%d %d %d",lis_trans.transform.translation.x,lis_trans.transform.translation.y,lis_trans.transform.translation.z);
}
catch(tf2::TransformException &ex)
{
RCLCPP_WARN(node->get_logger(),"%s",ex.what());
}
loop.sleep();
}