21 #include "tf2/convert.hpp"
22 #include "tf2/utils.hpp"
23 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
25 #include "nav2_util/robot_utils.hpp"
26 #include "rclcpp/logger.hpp"
32 geometry_msgs::msg::PoseStamped & global_pose,
33 tf2_ros::Buffer & tf_buffer,
const std::string global_frame,
34 const std::string robot_frame,
const double transform_timeout,
35 const rclcpp::Time stamp)
37 tf2::toMsg(tf2::Transform::getIdentity(), global_pose.pose);
38 global_pose.header.frame_id = robot_frame;
39 global_pose.header.stamp = stamp;
41 return transformPoseInTargetFrame(
42 global_pose, global_pose, tf_buffer, global_frame, transform_timeout);
45 bool transformPoseInTargetFrame(
46 const geometry_msgs::msg::PoseStamped & input_pose,
47 geometry_msgs::msg::PoseStamped & transformed_pose,
48 tf2_ros::Buffer & tf_buffer,
const std::string target_frame,
49 const double transform_timeout)
51 static rclcpp::Logger logger = rclcpp::get_logger(
"transformPoseInTargetFrame");
53 if(input_pose.header.frame_id == target_frame) {
54 transformed_pose = input_pose;
59 transformed_pose = tf_buffer.transform(
60 input_pose, target_frame,
61 tf2::durationFromSec(transform_timeout));
63 }
catch (tf2::LookupException & ex) {
66 "No Transform available Error looking up target frame: %s\n", ex.what());
67 }
catch (tf2::ConnectivityException & ex) {
70 "Connectivity Error looking up target frame: %s\n", ex.what());
71 }
catch (tf2::ExtrapolationException & ex) {
74 "Extrapolation Error looking up target frame: %s\n", ex.what());
75 }
catch (tf2::TimeoutException & ex) {
78 "Transform timeout with tolerance: %.4f", transform_timeout);
79 }
catch (tf2::TransformException & ex) {
81 logger,
"Failed to transform from %s to %s",
82 input_pose.header.frame_id.c_str(), target_frame.c_str());
89 const std::string & source_frame_id,
90 const std::string & target_frame_id,
91 const tf2::Duration & transform_tolerance,
92 const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
93 geometry_msgs::msg::TransformStamped & transform_msg)
95 if (source_frame_id == target_frame_id) {
102 transform_msg = tf_buffer->lookupTransform(
103 target_frame_id, source_frame_id,
104 tf2::TimePointZero, transform_tolerance);
105 }
catch (tf2::TransformException & e) {
107 rclcpp::get_logger(
"getTransform"),
108 "Failed to get \"%s\"->\"%s\" frame transform: %s",
109 source_frame_id.c_str(), target_frame_id.c_str(), e.what());
116 const std::string & source_frame_id,
117 const std::string & target_frame_id,
118 const tf2::Duration & transform_tolerance,
119 const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
120 tf2::Transform & tf2_transform)
122 tf2_transform.setIdentity();
123 geometry_msgs::msg::TransformStamped transform;
124 if (getTransform(source_frame_id, target_frame_id, transform_tolerance, tf_buffer, transform)) {
126 tf2::fromMsg(transform.transform, tf2_transform);
133 const std::string & source_frame_id,
134 const rclcpp::Time & source_time,
135 const std::string & target_frame_id,
136 const rclcpp::Time & target_time,
137 const std::string & fixed_frame_id,
138 const tf2::Duration & transform_tolerance,
139 const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
140 geometry_msgs::msg::TransformStamped & transform_msg)
145 transform_msg = tf_buffer->lookupTransform(
146 target_frame_id, target_time,
147 source_frame_id, source_time,
148 fixed_frame_id, transform_tolerance);
149 }
catch (tf2::TransformException & ex) {
151 rclcpp::get_logger(
"getTransform"),
152 "Failed to get \"%s\"->\"%s\" frame transform: %s",
153 source_frame_id.c_str(), target_frame_id.c_str(), ex.what());
161 const std::string & source_frame_id,
162 const rclcpp::Time & source_time,
163 const std::string & target_frame_id,
164 const rclcpp::Time & target_time,
165 const std::string & fixed_frame_id,
166 const tf2::Duration & transform_tolerance,
167 const std::shared_ptr<tf2_ros::Buffer> tf_buffer,
168 tf2::Transform & tf2_transform)
170 geometry_msgs::msg::TransformStamped transform;
171 tf2_transform.setIdentity();
173 source_frame_id, source_time, target_frame_id, target_time, fixed_frame_id,
174 transform_tolerance, tf_buffer, transform))
177 tf2::fromMsg(transform.transform, tf2_transform);
184 bool validateTwist(
const geometry_msgs::msg::Twist & msg)
186 if (std::isinf(msg.linear.x) || std::isnan(msg.linear.x)) {
190 if (std::isinf(msg.linear.y) || std::isnan(msg.linear.y)) {
194 if (std::isinf(msg.linear.z) || std::isnan(msg.linear.z)) {
198 if (std::isinf(msg.angular.x) || std::isnan(msg.angular.x)) {
202 if (std::isinf(msg.angular.y) || std::isnan(msg.angular.y)) {
206 if (std::isinf(msg.angular.z) || std::isnan(msg.angular.z)) {