PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
Loading...
Searching...
No Matches
geometry.hpp
1/****************************************************************************
2 * Copyright (c) 2024 PX4 Development Team.
3 * SPDX-License-Identifier: BSD-3-Clause
4 ****************************************************************************/
5
25#pragma once
26
27#include <Eigen/Eigen>
28
29namespace px4_ros2 {
37template <typename Type>
38Type radToDeg(Type rad)
39{
40 return rad * static_cast<Type>(180.0 / M_PI);
41}
42
46template <typename Type>
47Type degToRad(Type deg)
48{
49 return deg * static_cast<Type>(M_PI / 180.0);
50}
51
52namespace literals {
53static inline constexpr float operator"" _deg(long double degrees)
54{
55 return static_cast<float>(degrees * M_PI / 180.0);
56}
57static inline constexpr float operator"" _rad(long double radians)
58{
59 return static_cast<float>(radians);
60}
61} // namespace literals
62
69template <typename Type>
70Type wrapPi(Type angle)
71{
72 while (angle >= M_PI) {
73 angle -= 2.0 * M_PI;
74 }
75 while (angle < -M_PI) {
76 angle += 2.0 * M_PI;
77 }
78 return angle;
79}
80
89template <typename Type>
90Eigen::Matrix<Type, 3, 1> quaternionToEulerRpy(const Eigen::Quaternion<Type>& q)
91{
92 Eigen::Matrix<Type, 3, 1> angles;
93 const Eigen::Matrix<Type, 3, 3> dcm = q.toRotationMatrix();
94
95 angles.y() = asin(-dcm.coeff(2, 0));
96
97 if ((std::fabs(angles.y() - static_cast<Type>(M_PI / 2))) < static_cast<Type>(1.0e-3)) {
98 angles.x() = 0;
99 angles.z() = atan2(dcm.coeff(1, 2), dcm.coeff(0, 2));
100 } else if ((std::fabs(angles.y() + static_cast<Type>(M_PI / 2))) < static_cast<Type>(1.0e-3)) {
101 angles.x() = 0;
102 angles.z() = atan2(-dcm.coeff(1, 2), -dcm.coeff(0, 2));
103 } else {
104 angles.x() = atan2(dcm.coeff(2, 1), dcm.coeff(2, 2));
105 angles.z() = atan2(dcm.coeff(1, 0), dcm.coeff(0, 0));
106 }
107
108 return angles;
109}
110
117template <typename Type>
118Eigen::Quaternion<Type> eulerRpyToQuaternion(const Eigen::Matrix<Type, 3, 1>& euler)
119{
120 return Eigen::Quaternion<Type>(
121 Eigen::AngleAxis<Type>(euler[2], Eigen::Matrix<Type, 3, 1>::UnitZ()) *
122 Eigen::AngleAxis<Type>(euler[1], Eigen::Matrix<Type, 3, 1>::UnitY()) *
123 Eigen::AngleAxis<Type>(euler[0], Eigen::Matrix<Type, 3, 1>::UnitX()));
124}
125
134template <typename Type>
135Eigen::Quaternion<Type> eulerRpyToQuaternion(const Type roll, const Type pitch, const Type yaw)
136{
137 return eulerRpyToQuaternion(Eigen::Matrix<Type, 3, 1>{roll, pitch, yaw});
138}
139
146template <typename Type>
147Type quaternionToRoll(const Eigen::Quaternion<Type>& q)
148{
149 return quaternionToEulerRpy(q)[0];
150}
151
158template <typename Type>
159Type quaternionToPitch(const Eigen::Quaternion<Type>& q)
160{
161 return quaternionToEulerRpy(q)[1];
162}
163
170template <typename Type>
171Type quaternionToYaw(const Eigen::Quaternion<Type>& q)
172{
173 return quaternionToEulerRpy(q)[2];
174}
175
177} // namespace px4_ros2
Eigen::Quaternion< Type > eulerRpyToQuaternion(const Eigen::Matrix< Type, 3, 1 > &euler)
Converts RPY extrinsic Tait-Bryan Euler angles (YPR intrinsic) to quaternion.
Definition geometry.hpp:118
Type quaternionToRoll(const Eigen::Quaternion< Type > &q)
Convert quaternion to roll angle in extrinsic RPY order (intrinsic YPR)
Definition geometry.hpp:147
Type degToRad(Type deg)
Converts degrees to radians.
Definition geometry.hpp:47
Type wrapPi(Type angle)
Wraps an angle to the range [-pi, pi).
Definition geometry.hpp:70
Eigen::Matrix< Type, 3, 1 > quaternionToEulerRpy(const Eigen::Quaternion< Type > &q)
Converts a quaternion to RPY extrinsic Tait-Bryan Euler angles (YPR intrinsic) XYZ axes correspond to...
Definition geometry.hpp:90
Type quaternionToPitch(const Eigen::Quaternion< Type > &q)
Convert quaternion to pitch angle in extrinsic RPY order (intrinsic YPR)
Definition geometry.hpp:159
Type radToDeg(Type rad)
Converts radians to degrees.
Definition geometry.hpp:38
Type quaternionToYaw(const Eigen::Quaternion< Type > &q)
Convert quaternion to yaw angle in extrinsic RPY order (intrinsic YPR)
Definition geometry.hpp:171