tf2 rolling
tf2 maintains the relationship between coordinate frames in a tree structure buffered in time, and lets the user transform points, vectors, etc between any two coordinate frames at any desired point in time.
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1// Copyright 2014 Open Source Robotics Foundation, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef TF2__IMPL__UTILS_HPP_
16#define TF2__IMPL__UTILS_HPP_
17
18#include <tf2/convert.hpp>
21#include <geometry_msgs/msg/quaternion.hpp>
22#include <geometry_msgs/msg/quaternion_stamped.hpp>
23
24
25namespace tf2
26{
27
28// Forward declare functions needed in this header
29void fromMsg(const geometry_msgs::msg::Quaternion & in, tf2::Quaternion & out);
30
31namespace impl
32{
33
38inline
40{
41 return q;
42}
43
48inline
49tf2::Quaternion toQuaternion(const geometry_msgs::msg::Quaternion & q)
50{
52 fromMsg(q, res);
53 return res;
54}
55
60inline
61tf2::Quaternion toQuaternion(const geometry_msgs::msg::QuaternionStamped & q)
62{
64 fromMsg(q.quaternion, res);
65 return res;
66}
67
72template<typename T>
74{
75 geometry_msgs::msg::QuaternionStamped q = toMsg<tf2::Stamped<T>,
76 geometry_msgs::msg::QuaternionStamped>(t);
77 return toQuaternion(q);
78}
79
85template<typename T>
87{
88 geometry_msgs::msg::Quaternion q = toMsg<T, geometry_msgs::msg::QuaternionStamped>(t);
89 return toQuaternion(q);
90}
91
101inline
102void getEulerYPR(const tf2::Quaternion & q, double & yaw, double & pitch, double & roll)
103{
104 const double pi_2 = 1.57079632679489661923;
105 double sqw;
106 double sqx;
107 double sqy;
108 double sqz;
109
110 sqx = q.x() * q.x();
111 sqy = q.y() * q.y();
112 sqz = q.z() * q.z();
113 sqw = q.w() * q.w();
114
115 // Cases derived from https://orbitalstation.wordpress.com/tag/quaternion/
116 // normalization added from urdfom_headers
117 double sarg = -2 * (q.x() * q.z() - q.w() * q.y()) / (sqx + sqy + sqz + sqw);
118 if (sarg <= -0.99999) {
119 pitch = -0.5 * pi_2;
120 roll = 0;
121 yaw = -2 * atan2(q.y(), q.x());
122 } else if (sarg >= 0.99999) {
123 pitch = 0.5 * pi_2;
124 roll = 0;
125 yaw = 2 * atan2(q.y(), q.x());
126 } else {
127 pitch = asin(sarg);
128 roll = atan2(2 * (q.y() * q.z() + q.w() * q.x()), sqw - sqx - sqy + sqz);
129 yaw = atan2(2 * (q.x() * q.y() + q.w() * q.z()), sqw + sqx - sqy - sqz);
130 }
131}
132
139inline
140double getYaw(const tf2::Quaternion & q)
141{
142 double yaw;
143
144 double sqw;
145 double sqx;
146 double sqy;
147 double sqz;
148
149 sqx = q.x() * q.x();
150 sqy = q.y() * q.y();
151 sqz = q.z() * q.z();
152 sqw = q.w() * q.w();
153
154 // Cases derived from https://orbitalstation.wordpress.com/tag/quaternion/
155 // normalization added from urdfom_headers
156 double sarg = -2 * (q.x() * q.z() - q.w() * q.y()) / (sqx + sqy + sqz + sqw);
157
158 if (sarg <= -0.99999) {
159 yaw = -2 * atan2(q.y(), q.x());
160 } else if (sarg >= 0.99999) {
161 yaw = 2 * atan2(q.y(), q.x());
162 } else {
163 yaw = atan2(2 * (q.x() * q.y() + q.w() * q.z()), sqw + sqx - sqy - sqz);
164 }
165 return yaw;
166}
167
168} // namespace impl
169} // namespace tf2
170#endif // TF2__IMPL__UTILS_HPP_
The Quaternion implements quaternion to perform linear algebra rotations in combination with Matrix3x...
Definition Quaternion.hpp:30
The data type which will be cross compatable with geometry_msgs This is the tf2 datatype equivilant o...
Definition transform_datatypes.hpp:49
Author: Tully Foote.
void getEulerYPR(const tf2::Quaternion &q, double &yaw, double &pitch, double &roll)
Definition utils.hpp:102
double getYaw(const tf2::Quaternion &q)
Definition utils.hpp:140
tf2::Quaternion toQuaternion(const tf2::Quaternion &q)
Definition utils.hpp:39
Definition buffer_core.hpp:58
B toMsg(const A &a)
Function that converts from one type to a ROS message type. It has to be implemented by each data typ...
void fromMsg(const A &a, B &b)
Function that converts from a ROS message type to another type. It has to be implemented by each data...
Author: Tully Foote.