21 #include "tf2/convert.hpp"
22 #include "tf2/utils.hpp"
23 #include "nav2_ros_common/tf2_factories.hpp"
24 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
26 #include "nav2_util/robot_utils.hpp"
27 #include "rclcpp/logger.hpp"
33 geometry_msgs::msg::PoseStamped & global_pose,
34 nav2::TransformBuffer & tf_buffer,
const std::string global_frame,
35 const std::string robot_frame,
const double transform_timeout,
36 const rclcpp::Time stamp)
38 tf2::toMsg(tf2::Transform::getIdentity(), global_pose.pose);
39 global_pose.header.frame_id = robot_frame;
40 global_pose.header.stamp = stamp;
42 return transformPoseInTargetFrame(
43 global_pose, global_pose, tf_buffer, global_frame, transform_timeout);
46 bool transformPoseInTargetFrame(
47 const geometry_msgs::msg::PoseStamped & input_pose,
48 geometry_msgs::msg::PoseStamped & transformed_pose,
49 nav2::TransformBuffer & tf_buffer,
const std::string target_frame,
50 const double transform_timeout)
52 static rclcpp::Logger logger = rclcpp::get_logger(
"transformPoseInTargetFrame");
54 if (input_pose.header.frame_id == target_frame) {
55 transformed_pose = input_pose;
60 transformed_pose = tf_buffer.transform(
61 input_pose, target_frame,
62 tf2::durationFromSec(transform_timeout));
64 }
catch (tf2::LookupException & ex) {
67 "No Transform available Error looking up target frame: %s\n", ex.what());
68 }
catch (tf2::ConnectivityException & ex) {
71 "Connectivity Error looking up target frame: %s\n", ex.what());
72 }
catch (tf2::ExtrapolationException & ex) {
75 "Extrapolation Error looking up target frame: %s\n", ex.what());
76 }
catch (tf2::TimeoutException & ex) {
79 "Transform timeout with tolerance: %.4f", transform_timeout);
80 }
catch (tf2::TransformException & ex) {
82 logger,
"Failed to transform from %s to %s",
83 input_pose.header.frame_id.c_str(), target_frame.c_str());
90 const std::string & source_frame_id,
91 const std::string & target_frame_id,
92 const tf2::Duration & transform_tolerance,
93 const nav2::TransformBuffer::SharedPtr tf_buffer,
94 geometry_msgs::msg::TransformStamped & transform_msg)
96 if (source_frame_id == target_frame_id) {
103 transform_msg = tf_buffer->lookupTransform(
104 target_frame_id, source_frame_id,
105 tf2::TimePointZero, transform_tolerance);
106 }
catch (tf2::TransformException & e) {
108 rclcpp::get_logger(
"getTransform"),
109 "Failed to get \"%s\"->\"%s\" frame transform: %s",
110 source_frame_id.c_str(), target_frame_id.c_str(), e.what());
117 const std::string & source_frame_id,
118 const std::string & target_frame_id,
119 const tf2::Duration & transform_tolerance,
120 const nav2::TransformBuffer::SharedPtr tf_buffer,
121 tf2::Transform & tf2_transform)
123 tf2_transform.setIdentity();
124 geometry_msgs::msg::TransformStamped transform;
125 if (getTransform(source_frame_id, target_frame_id, transform_tolerance, tf_buffer, transform)) {
127 tf2::fromMsg(transform.transform, tf2_transform);
134 const std::string & source_frame_id,
135 const rclcpp::Time & source_time,
136 const std::string & target_frame_id,
137 const rclcpp::Time & target_time,
138 const std::string & fixed_frame_id,
139 const tf2::Duration & transform_tolerance,
140 const nav2::TransformBuffer::SharedPtr tf_buffer,
141 geometry_msgs::msg::TransformStamped & transform_msg)
146 transform_msg = tf_buffer->lookupTransform(
147 target_frame_id, target_time,
148 source_frame_id, source_time,
149 fixed_frame_id, transform_tolerance);
150 }
catch (tf2::TransformException & ex) {
152 rclcpp::get_logger(
"getTransform"),
153 "Failed to get \"%s\"->\"%s\" frame transform: %s",
154 source_frame_id.c_str(), target_frame_id.c_str(), ex.what());
162 const std::string & source_frame_id,
163 const rclcpp::Time & source_time,
164 const std::string & target_frame_id,
165 const rclcpp::Time & target_time,
166 const std::string & fixed_frame_id,
167 const tf2::Duration & transform_tolerance,
168 const nav2::TransformBuffer::SharedPtr tf_buffer,
169 tf2::Transform & tf2_transform)
171 geometry_msgs::msg::TransformStamped transform;
172 tf2_transform.setIdentity();
174 source_frame_id, source_time, target_frame_id, target_time, fixed_frame_id,
175 transform_tolerance, tf_buffer, transform))
178 tf2::fromMsg(transform.transform, tf2_transform);
185 bool validateTwist(
const geometry_msgs::msg::Twist & msg)
187 if (std::isinf(msg.linear.x) || std::isnan(msg.linear.x)) {
191 if (std::isinf(msg.linear.y) || std::isnan(msg.linear.y)) {
195 if (std::isinf(msg.linear.z) || std::isnan(msg.linear.z)) {
199 if (std::isinf(msg.angular.x) || std::isnan(msg.angular.x)) {
203 if (std::isinf(msg.angular.y) || std::isnan(msg.angular.y)) {
207 if (std::isinf(msg.angular.z) || std::isnan(msg.angular.z)) {