Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef TF2_IMPL_UTILS_H
00016 #define TF2_IMPL_UTILS_H
00017
00018 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
00019 #include <tf2/transform_datatypes.h>
00020 #include <tf2/LinearMath/Quaternion.h>
00021
00022 namespace tf2 {
00023 namespace impl {
00024
00029 inline
00030 tf2::Quaternion toQuaternion(const tf2::Quaternion& q) {
00031 return q;
00032 }
00033
00038 inline
00039 tf2::Quaternion toQuaternion(const geometry_msgs::Quaternion& q) {
00040 tf2::Quaternion res;
00041 fromMsg(q, res);
00042 return res;
00043 }
00044
00049 inline
00050 tf2::Quaternion toQuaternion(const geometry_msgs::QuaternionStamped& q) {
00051 tf2::Quaternion res;
00052 fromMsg(q.quaternion, res);
00053 return res;
00054 }
00055
00060 template<typename T>
00061 tf2::Quaternion toQuaternion(const tf2::Stamped<T>& t) {
00062 geometry_msgs::QuaternionStamped q = toMsg(t);
00063 return toQuaternion(q);
00064 }
00065
00071 template<typename T>
00072 tf2::Quaternion toQuaternion(const T& t) {
00073 geometry_msgs::Quaternion q = toMsg(t);
00074 return toQuaternion(q);
00075 }
00076
00086 inline
00087 void getEulerYPR(const tf2::Quaternion& q, double &yaw, double &pitch, double &roll)
00088 {
00089 double sqw;
00090 double sqx;
00091 double sqy;
00092 double sqz;
00093
00094 sqx = q.x() * q.x();
00095 sqy = q.y() * q.y();
00096 sqz = q.z() * q.z();
00097 sqw = q.w() * q.w();
00098
00099
00100 double sarg = -2 * (q.x()*q.z() - q.w()*q.y()) / (sqx + sqy + sqz + sqw);
00101 if (sarg <= -0.99999) {
00102 pitch = -0.5*M_PI;
00103 roll = 0;
00104 yaw = -2 * atan2(q.y(), q.x());
00105 } else if (sarg >= 0.99999) {
00106 pitch = 0.5*M_PI;
00107 roll = 0;
00108 yaw = 2 * atan2(q.y(), q.x());
00109 } else {
00110 pitch = asin(sarg);
00111 roll = atan2(2 * (q.y()*q.z() + q.w()*q.x()), sqw - sqx - sqy + sqz);
00112 yaw = atan2(2 * (q.x()*q.y() + q.w()*q.z()), sqw + sqx - sqy - sqz);
00113 }
00114 };
00115
00122 inline
00123 double getYaw(const tf2::Quaternion& q)
00124 {
00125 double yaw;
00126
00127 double sqw;
00128 double sqx;
00129 double sqy;
00130 double sqz;
00131
00132 sqx = q.x() * q.x();
00133 sqy = q.y() * q.y();
00134 sqz = q.z() * q.z();
00135 sqw = q.w() * q.w();
00136
00137
00138 double sarg = -2 * (q.x()*q.z() - q.w()*q.y()) / (sqx + sqy + sqz + sqw);
00139
00140 if (sarg <= -0.99999) {
00141 yaw = -2 * atan2(q.y(), q.x());
00142 } else if (sarg >= 0.99999) {
00143 yaw = 2 * atan2(q.y(), q.x());
00144 } else {
00145 yaw = atan2(2 * (q.x()*q.y() + q.w()*q.z()), sqw + sqx - sqy - sqz);
00146 }
00147 return yaw;
00148 };
00149
00150 }
00151 }
00152
00153 #endif //TF2_IMPL_UTILS_H