PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
Loading...
Searching...
No Matches
frame_conversion.hpp
1/****************************************************************************
2 * Copyright (c) 2024 PX4 Development Team.
3 * SPDX-License-Identifier: BSD-3-Clause
4 ****************************************************************************/
5
12#pragma once
13
14#include <Eigen/Eigen>
15#include <px4_ros2/utils/geometry.hpp>
16
17namespace px4_ros2 {
30template <typename Type>
31Eigen::Quaternion<Type> attitudeNedToEnu(const Eigen::Quaternion<Type>& q_ned)
32{
33 static constexpr Type kHalfSqrt2 = static_cast<Type>(0.7071067811865476);
34 const Eigen::Quaternion<Type> q_ned_to_enu{0.0, kHalfSqrt2, kHalfSqrt2,
35 0.0}; // 180 deg along X, -90 deg along Z (intrinsic)
36 const Eigen::Quaternion<Type> q_flu_to_frd{0.0, 1.0, 0.0, 0.0}; // 180 deg along X
37
38 return q_ned_to_enu * q_ned * q_flu_to_frd;
39}
40
49template <typename Type>
50Eigen::Quaternion<Type> attitudeEnuToNed(const Eigen::Quaternion<Type>& q_enu)
51{
52 static constexpr Type kHalfSqrt2 = static_cast<Type>(0.7071067811865476);
53 const Eigen::Quaternion<Type> q_enu_to_ned{0.0, kHalfSqrt2, kHalfSqrt2,
54 0.0}; // 180 deg along X, -90 deg along Z (intrinsic)
55 const Eigen::Quaternion<Type> q_frd_to_flu{0.0, 1.0, 0.0, 0.0}; // 180 deg along X
56
57 return q_enu_to_ned * q_enu * q_frd_to_flu;
58}
59
67template <typename Type>
68Eigen::Matrix<Type, 3, 1> yawBodyToWorld(Type yaw, const Eigen::Matrix<Type, 3, 1>& point_body)
69{
70 const Eigen::Quaternion<Type> q_z(
71 Eigen::AngleAxis<Type>(yaw, Eigen::Matrix<Type, 3, 1>::UnitZ()));
72 return q_z * point_body;
73}
74
81template <typename T>
82static inline T yawNedToEnu(const T yaw_ned_rad)
83{
84 return wrapPi(static_cast<T>(M_PI / 2.0) - yaw_ned_rad);
85}
86
93template <typename T>
94static inline T yawEnuToNed(const T yaw_enu_rad)
95{
96 return wrapPi(static_cast<T>(M_PI / 2.0) - yaw_enu_rad);
97}
98
105template <typename T>
106static inline T yawRateNedToEnu(const T yaw_rate_ned)
107{
108 return -yaw_rate_ned;
109}
110
117template <typename T>
118static inline T yawRateEnuToNed(const T yaw_rate_enu)
119{
120 return -yaw_rate_enu;
121}
122
129template <typename T>
130static inline Eigen::Matrix<T, 3, 1> positionNedToEnu(const Eigen::Matrix<T, 3, 1>& ned)
131{
132 return {ned.y(), ned.x(), -ned.z()};
133}
134
141template <typename T>
142static inline Eigen::Matrix<T, 3, 1> positionEnuToNed(const Eigen::Matrix<T, 3, 1>& enu)
143{
144 return {enu.y(), enu.x(), -enu.z()};
145}
146
153template <typename T>
154static inline Eigen::Matrix<T, 3, 1> frdToFlu(const Eigen::Matrix<T, 3, 1>& frd)
155{
156 return {frd.x(), -frd.y(), -frd.z()};
157}
158
165template <typename T>
166static inline Eigen::Matrix<T, 3, 1> fluToFrd(const Eigen::Matrix<T, 3, 1>& flu)
167{
168 return {flu.x(), -flu.y(), -flu.z()};
169}
170
177template <typename T>
178static inline Eigen::Matrix<T, 3, 1> varianceNedToEnu(const Eigen::Matrix<T, 3, 1>& v_ned)
179{
180 return {v_ned.y(), v_ned.x(), v_ned.z()};
181}
182
189template <typename T>
190static inline Eigen::Matrix<T, 3, 1> varianceEnuToNed(const Eigen::Matrix<T, 3, 1>& v_enu)
191{
192 return {v_enu.y(), v_enu.x(), v_enu.z()};
193}
194
196} // namespace px4_ros2
static T yawNedToEnu(const T yaw_ned_rad)
Converts yaw from NED to ENU frame.
Definition frame_conversion.hpp:82
static Eigen::Matrix< T, 3, 1 > fluToFrd(const Eigen::Matrix< T, 3, 1 > &flu)
Converts coordinates from FLU to FRD frame.
Definition frame_conversion.hpp:166
static Eigen::Matrix< T, 3, 1 > varianceNedToEnu(const Eigen::Matrix< T, 3, 1 > &v_ned)
Converts variance from NED to ENU frame.
Definition frame_conversion.hpp:178
static Eigen::Matrix< T, 3, 1 > positionEnuToNed(const Eigen::Matrix< T, 3, 1 > &enu)
Converts coordinates from ENU to NED frame.
Definition frame_conversion.hpp:142
static Eigen::Matrix< T, 3, 1 > positionNedToEnu(const Eigen::Matrix< T, 3, 1 > &ned)
Converts coordinates from NED to ENU frame.
Definition frame_conversion.hpp:130
Eigen::Quaternion< Type > attitudeNedToEnu(const Eigen::Quaternion< Type > &q_ned)
Converts attitude from NED to ENU frame. Performs reference frame change and quaternion rotation s....
Definition frame_conversion.hpp:31
static T yawRateNedToEnu(const T yaw_rate_ned)
Converts yaw rate from NED to ENU frame.
Definition frame_conversion.hpp:106
Eigen::Quaternion< Type > attitudeEnuToNed(const Eigen::Quaternion< Type > &q_enu)
Converts attitude from ENU to NED frame. Performs reference frame change and quaternion rotation s....
Definition frame_conversion.hpp:50
static Eigen::Matrix< T, 3, 1 > varianceEnuToNed(const Eigen::Matrix< T, 3, 1 > &v_enu)
Converts variance from ENU to NED frame.
Definition frame_conversion.hpp:190
static T yawEnuToNed(const T yaw_enu_rad)
Converts yaw from ENU to NED frame.
Definition frame_conversion.hpp:94
static Eigen::Matrix< T, 3, 1 > frdToFlu(const Eigen::Matrix< T, 3, 1 > &frd)
Converts coordinates from FRD to FLU frame.
Definition frame_conversion.hpp:154
static T yawRateEnuToNed(const T yaw_rate_enu)
Converts yaw rate from ENU to NED frame.
Definition frame_conversion.hpp:118
Eigen::Matrix< Type, 3, 1 > yawBodyToWorld(Type yaw, const Eigen::Matrix< Type, 3, 1 > &point_body)
Transforms a point from body frame to world frame given the yaw of the body's attitude.
Definition frame_conversion.hpp:68
Type wrapPi(Type angle)
Wraps an angle to the range [-pi, pi).
Definition geometry.hpp:70