tf2  master
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.
utils.h
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_H
16 #define TF2_IMPL_UTILS_H
17 
18 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
21 
22 namespace tf2 {
23 namespace impl {
24 
29 inline
31  return q;
32  }
33 
38 inline
39 tf2::Quaternion toQuaternion(const geometry_msgs::msg::Quaternion& q) {
40  tf2::Quaternion res;
41  fromMsg(q, res);
42  return res;
43  }
44 
49 inline
50 tf2::Quaternion toQuaternion(const geometry_msgs::msg::QuaternionStamped& q) {
51  tf2::Quaternion res;
52  fromMsg(q.quaternion, res);
53  return res;
54  }
55 
60 template<typename T>
62  geometry_msgs::msg::QuaternionStamped q = toMsg<tf2::Stamped<T>, geometry_msgs::msg::QuaternionStamped>(t);
63  return toQuaternion(q);
64  }
65 
71 template<typename T>
73  geometry_msgs::msg::Quaternion q = toMsg<T, geometry_msgs::msg::QuaternionStamped>(t);
74  return toQuaternion(q);
75  }
76 
86 inline
87 void getEulerYPR(const tf2::Quaternion& q, double &yaw, double &pitch, double &roll)
88 {
89  const double pi_2 = 1.57079632679489661923;
90  double sqw;
91  double sqx;
92  double sqy;
93  double sqz;
94 
95  sqx = q.x() * q.x();
96  sqy = q.y() * q.y();
97  sqz = q.z() * q.z();
98  sqw = q.w() * q.w();
99 
100  // Cases derived from https://orbitalstation.wordpress.com/tag/quaternion/
101  double sarg = -2 * (q.x()*q.z() - q.w()*q.y()) / (sqx + sqy + sqz + sqw); /* normalization added from urdfom_headers */
102  if (sarg <= -0.99999) {
103  pitch = -0.5*pi_2;
104  roll = 0;
105  yaw = -2 * atan2(q.y(), q.x());
106  } else if (sarg >= 0.99999) {
107  pitch = 0.5*pi_2;
108  roll = 0;
109  yaw = 2 * atan2(q.y(), q.x());
110  } else {
111  pitch = asin(sarg);
112  roll = atan2(2 * (q.y()*q.z() + q.w()*q.x()), sqw - sqx - sqy + sqz);
113  yaw = atan2(2 * (q.x()*q.y() + q.w()*q.z()), sqw + sqx - sqy - sqz);
114  }
115 }
116 
123 inline
124 double getYaw(const tf2::Quaternion& q)
125 {
126  double yaw;
127 
128  double sqw;
129  double sqx;
130  double sqy;
131  double sqz;
132 
133  sqx = q.x() * q.x();
134  sqy = q.y() * q.y();
135  sqz = q.z() * q.z();
136  sqw = q.w() * q.w();
137 
138  // Cases derived from https://orbitalstation.wordpress.com/tag/quaternion/
139  double sarg = -2 * (q.x()*q.z() - q.w()*q.y()) / (sqx + sqy + sqz + sqw); /* normalization added from urdfom_headers */
140 
141  if (sarg <= -0.99999) {
142  yaw = -2 * atan2(q.y(), q.x());
143  } else if (sarg >= 0.99999) {
144  yaw = 2 * atan2(q.y(), q.x());
145  } else {
146  yaw = atan2(2 * (q.x()*q.y() + q.w()*q.z()), sqw + sqx - sqy - sqz);
147  }
148  return yaw;
149 }
150 
151 }
152 }
153 
154 #endif //TF2_IMPL_UTILS_H
tf2::fromMsg
void fromMsg(const A &, B &b)
tf2::Stamped
The data type which will be cross compatable with geometry_msgs This is the tf2 datatype equivilant o...
Definition: transform_datatypes.h:45
tf2::impl::toQuaternion
tf2::Quaternion toQuaternion(const tf2::Quaternion &q)
Definition: utils.h:30
Quaternion.h
tf2::impl::getEulerYPR
void getEulerYPR(const tf2::Quaternion &q, double &yaw, double &pitch, double &roll)
Definition: utils.h:87
tf2::impl::getYaw
double getYaw(const tf2::Quaternion &q)
Definition: utils.h:124
tf2
Definition: buffer_core.h:55
tf2::Quaternion
The Quaternion implements quaternion to perform linear algebra rotations in combination with Matrix3x...
Definition: Quaternion.h:29
transform_datatypes.h