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
18{
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, 0.0}; // 180 deg along X, -90 deg along Z (intrinsic)
35 const Eigen::Quaternion<Type> q_yaw_minus_pi_2{kHalfSqrt2, 0.0, 0.0, -kHalfSqrt2}; // -90 deg along Z
36
37 return q_ned_to_enu * q_ned * q_yaw_minus_pi_2 * q_ned_to_enu.inverse();
38}
39
47template<typename Type>
48Eigen::Quaternion<Type> attitudeEnuToNed(const Eigen::Quaternion<Type> & q_enu)
49{
50 static constexpr Type kHalfSqrt2 = static_cast<Type>(0.7071067811865476);
51 const Eigen::Quaternion<Type> q_enu_to_ned {0.0, kHalfSqrt2, kHalfSqrt2, 0.0}; // 180 deg along X, -90 deg along Z (intrinsic)
52 const Eigen::Quaternion<Type> q_yaw_minus_pi_2{kHalfSqrt2, 0.0, 0.0, -kHalfSqrt2}; // -90 deg along Z
53
54 return q_enu_to_ned * q_enu * q_yaw_minus_pi_2 * q_enu_to_ned.inverse();
55}
56
64template<typename Type>
65Eigen::Matrix<Type, 3, 1> yawBodyToWorld(
66 Type yaw,
67 const Eigen::Matrix<Type, 3, 1> & point_body)
68{
69 const Eigen::Quaternion<Type> q_z(Eigen::AngleAxis<Type>(
70 yaw, Eigen::Matrix<Type, 3, 1>::UnitZ()));
71 return q_z * point_body;
72}
73
80template<typename T>
81static inline T yawNedToEnu(const T yaw_ned_rad)
82{
83 return wrapPi(static_cast<T>(M_PI / 2.0) - yaw_ned_rad);
84}
85
92template<typename T>
93static inline T yawEnuToNed(const T yaw_enu_rad)
94{
95 return wrapPi(static_cast<T>(M_PI / 2.0) - yaw_enu_rad);
96}
97
104template<typename T>
105static inline T yawRateNedToEnu(const T yaw_rate_ned) {return -yaw_rate_ned;}
106
113template<typename T>
114static inline T yawRateEnuToNed(const T yaw_rate_enu) {return -yaw_rate_enu;}
115
122template<typename T>
123static inline Eigen::Matrix<T, 3, 1> positionNedToEnu(const Eigen::Matrix<T, 3, 1> & ned)
124{
125 return {ned.y(), ned.x(), -ned.z()};
126}
127
134template<typename T>
135static inline Eigen::Matrix<T, 3, 1> positionEnuToNed(const Eigen::Matrix<T, 3, 1> & enu)
136{
137 return {enu.y(), enu.x(), -enu.z()};
138}
139
146template<typename T>
147static inline Eigen::Matrix<T, 3, 1> frdToFlu(const Eigen::Matrix<T, 3, 1> & frd)
148{
149 return {frd.x(), -frd.y(), -frd.z()};
150}
151
158template<typename T>
159static inline Eigen::Matrix<T, 3, 1> fluToFrd(const Eigen::Matrix<T, 3, 1> & flu)
160{
161 return {flu.x(), -flu.y(), -flu.z()};
162}
163
170template<typename T>
171static inline Eigen::Matrix<T, 3, 1> varianceNedToEnu(const Eigen::Matrix<T, 3, 1> & v_ned)
172{
173 return {v_ned.y(), v_ned.x(), v_ned.z()};
174}
175
182template<typename T>
183static inline Eigen::Matrix<T, 3, 1> varianceEnuToNed(const Eigen::Matrix<T, 3, 1> & v_enu)
184{
185 return {v_enu.y(), v_enu.x(), v_enu.z()};
186}
187
189} // namespace px4_ros2
static T yawNedToEnu(const T yaw_ned_rad)
Converts yaw from NED to ENU frame.
Definition frame_conversion.hpp:81
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:159
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:171
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:135
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:123
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:105
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:48
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:183
static T yawEnuToNed(const T yaw_enu_rad)
Converts yaw from ENU to NED frame.
Definition frame_conversion.hpp:93
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:147
static T yawRateEnuToNed(const T yaw_rate_enu)
Converts yaw rate from ENU to NED frame.
Definition frame_conversion.hpp:114
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:65
Type wrapPi(Type angle)
Wraps an angle to the range [-pi, pi).
Definition geometry.hpp:72