special_messages.cpp
Go to the documentation of this file.
1 #include "special_messages.h"
2 #include <cmath>
3 
5 {
6  RPY rpy;
7 
8  double quat_norm2 = (q.w * q.w) + (q.x * q.x) + (q.y * q.y) + (q.z * q.z);
9  if (std::abs(quat_norm2 - 1.0) > std::numeric_limits<double>::epsilon())
10  {
11  double mult = 1.0 / std::sqrt(quat_norm2);
12  q.x *= mult;
13  q.y *= mult;
14  q.z *= mult;
15  q.w *= mult;
16  }
17 
18  // roll (x-axis rotation)
19  double sinr_cosp = 2 * (q.w * q.x + q.y * q.z);
20  double cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
21  rpy.roll = std::atan2(sinr_cosp, cosr_cosp);
22 
23  // pitch (y-axis rotation)
24  double sinp = 2 * (q.w * q.y - q.z * q.x);
25  if (std::abs(sinp) >= 1)
26  {
27  rpy.pitch = std::copysign(M_PI_2, sinp); // use 90 degrees if out of range
28  }
29  else
30  {
31  rpy.pitch = std::asin(sinp);
32  }
33 
34  // yaw (z-axis rotation)
35  double siny_cosp = 2 * (q.w * q.z + q.x * q.y);
36  double cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z);
37  rpy.yaw = std::atan2(siny_cosp, cosy_cosp);
38 
39  return rpy;
40 }
PJ::Msg::Quaternion::w
double w
Definition: special_messages.h:96
PJ::Msg::RPY::roll
double roll
Definition: special_messages.h:106
M_PI_2
#define M_PI_2
Definition: qwt_math.h:60
PJ::Msg::RPY
Definition: special_messages.h:104
PJ::Msg::Quaternion::x
double x
Definition: special_messages.h:93
special_messages.h
PJ::Msg::RPY::pitch
double pitch
Definition: special_messages.h:107
PJ::Msg::RPY::yaw
double yaw
Definition: special_messages.h:108
PJ::Msg::QuaternionToRPY
RPY QuaternionToRPY(Quaternion q)
Definition: special_messages.cpp:4
PJ::Msg::Quaternion::z
double z
Definition: special_messages.h:95
PJ::Msg::Quaternion::y
double y
Definition: special_messages.h:94
PJ::Msg::Quaternion
Definition: special_messages.h:91


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:47