38#include <rclcpp/version.h>
40#if RCLCPP_VERSION_GTE(29, 6, 0)
41#include <tf2_ros/buffer.hpp>
42#include <tf2_ros/transform_listener.hpp>
45#include <tf2_ros/buffer.h>
46#include <tf2_ros/transform_listener.h>
48#include <tf2_eigen/tf2_eigen.hpp>
52 tf_buffer_ = std::make_shared<tf2_ros::Buffer>();
53 tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);
54 psm_ = std::make_shared<planning_scene_monitor::PlanningSceneMonitor>(
"robot_description", tf_buffer_);
55 psm_->startStateMonitor();
66 thread_ = std::thread(&TransformProvider::run,
this);
78 throw std::runtime_error(
"Can not add handles if TransformProvider is running");
80 handle2context_[handle] = std::make_shared<TransformContext>(name);
85 if (frame_id_ != frame)
88 for (
auto& context_it : handle2context_)
91 context_it.second->mutex_.lock();
92 context_it.second->transformation_.matrix().setZero();
93 context_it.second->mutex_.unlock();
100 auto context_it = handle2context_.find(handle);
102 if (context_it == handle2context_.end())
104 ROS_ERROR(
"Unable to find mesh with handle %d", handle);
107 context_it->second->mutex_.lock();
108 transform = context_it->second->transformation_;
109 context_it->second->mutex_.unlock();
110 return !(transform.matrix().isZero(0));
113void TransformProvider::run()
115 if (handle2context_.empty())
116 throw std::runtime_error(
"TransformProvider is listening to empty list of frames!");
118 while (ros::ok() && !stop_)
121 update_rate_.sleep();
127 update_rate_ = ros::Rate(update_rate);
130void TransformProvider::updateTransforms()
133 if (frame_id_.empty())
135 ROS_DEBUG_THROTTLE(2.,
"Not updating transforms because frame_id_ is empty.");
139 static tf2::Stamped<Eigen::Isometry3d> input_transform, output_transform;
140 static moveit::core::RobotStatePtr robot_state;
141 robot_state = psm_->getStateMonitor()->getCurrentState();
144 geometry_msgs::TransformStamped common_tf =
145 tf_buffer_->lookupTransform(frame_id_, psm_->getPlanningScene()->getPlanningFrame(), ros::Time(0.0));
146 input_transform.stamp_ = common_tf.header.stamp;
148 catch (tf2::TransformException& ex)
150 ROS_ERROR(
"TF Problem: %s", ex.what());
153 input_transform.frame_id_ = psm_->getPlanningScene()->getPlanningFrame();
155 for (
auto& context_it : handle2context_)
160 input_transform.setData(robot_state->getGlobalLinkTransform(context_it.second->frame_id_));
161 tf_buffer_->transform(input_transform, output_transform, frame_id_);
163 catch (
const tf2::TransformException& ex)
165 handle2context_[context_it.first]->mutex_.lock();
166 handle2context_[context_it.first]->transformation_.matrix().setZero();
167 handle2context_[context_it.first]->mutex_.unlock();
170 catch (std::exception& ex)
172 ROS_ERROR(
"Caught %s while updating transforms", ex.what());
174 handle2context_[context_it.first]->mutex_.lock();
175 handle2context_[context_it.first]->transformation_ =
static_cast<Eigen::Isometry3d
>(output_transform);
176 handle2context_[context_it.first]->mutex_.unlock();