Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
galaxis::math Namespace Reference

Classes

struct  EulerAngles
 
struct  Quaternion
 

Functions

Quaternion euler_angles_to_quaternion (EulerAngles const &e)
 
EulerAngles quaternion_to_euler (Quaternion const &q)
 

Function Documentation

◆ euler_angles_to_quaternion()

Quaternion galaxis::math::euler_angles_to_quaternion ( EulerAngles const &  e)

Definition at line 23 of file math.hpp.

23  { // yaw (Z), pitch (Y), roll (X)
24 
25  // Abbreviations for the various angular functions
26  double cy = cos(e.yaw * 0.5);
27  double sy = sin(e.yaw * 0.5);
28  double cp = cos(e.pitch * 0.5);
29  double sp = sin(e.pitch * 0.5);
30  double cr = cos(e.roll * 0.5);
31  double sr = sin(e.roll * 0.5);
32 
33  Quaternion q;
34  q.w = cr * cp * cy + sr * sp * sy;
35  q.x = sr * cp * cy - cr * sp * sy;
36  q.y = cr * sp * cy + sr * cp * sy;
37  q.z = cr * cp * sy - sr * sp * cy;
38 
39  return q;
40  }

References galaxis::math::EulerAngles::pitch, galaxis::math::EulerAngles::roll, galaxis::math::Quaternion::w, galaxis::math::Quaternion::x, galaxis::math::Quaternion::y, galaxis::math::EulerAngles::yaw, and galaxis::math::Quaternion::z.

◆ quaternion_to_euler()

EulerAngles galaxis::math::quaternion_to_euler ( Quaternion const &  q)

Definition at line 42 of file math.hpp.

42  {
43  EulerAngles angles;
44 
45  // roll (x-axis rotation)
46  double sinr_cosp = 2 * (q.w * q.x + q.y * q.z);
47  double cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
48  angles.roll = std::atan2(sinr_cosp, cosr_cosp);
49 
50  // pitch (y-axis rotation)
51  double sinp = 2 * (q.w * q.y - q.z * q.x);
52  if (std::abs(sinp) >= 1)
53  angles.pitch = std::copysign(M_PI / 2, sinp); // use 90 degrees if out of range
54  else
55  angles.pitch = std::asin(sinp);
56 
57  // yaw (z-axis rotation)
58  double siny_cosp = 2 * (q.w * q.z + q.x * q.y);
59  double cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z);
60  angles.yaw = std::atan2(siny_cosp, cosy_cosp);
61 
62  return angles;
63  }

References galaxis::math::EulerAngles::pitch, galaxis::math::EulerAngles::roll, galaxis::math::Quaternion::w, galaxis::math::Quaternion::x, galaxis::math::Quaternion::y, galaxis::math::EulerAngles::yaw, and galaxis::math::Quaternion::z.