SMACC2
Loading...
Searching...
No Matches
level_frame.hpp
Go to the documentation of this file.
1// Copyright 2026 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <tf2/LinearMath/Matrix3x3.h>
18#include <tf2/LinearMath/Quaternion.h>
19
20#include <array>
21#include <cmath>
22
23namespace cl_px4_mr
24{
25
26// Roll/pitch-only rotation from a PX4 attitude quaternion (order w, x, y, z,
27// FRD body -> NED earth): maps a body-FLU vector (x forward, y left, z up -
28// the frame of a level-mounted sensor) into a LEVEL frame with the same
29// heading: x = horizontal forward, y = horizontal left, z = up. Row-major 3x3.
30inline void levelRotationFromPx4Quaternion(const std::array<float, 4> & q, std::array<float, 9> & r)
31{
32 // px4_ros_com frame_transforms constants (tf2 order x, y, z, w)
33 const tf2::Quaternion kNedEnuQ(0.70710678118654752, 0.70710678118654752, 0.0, 0.0);
34 const tf2::Quaternion kFrdFluQ(1.0, 0.0, 0.0, 0.0);
35 const tf2::Quaternion qNedFrd(q[1], q[2], q[3], q[0]);
36 tf2::Quaternion qEnuFlu = kNedEnuQ * qNedFrd * kFrdFluQ;
37 qEnuFlu.normalize();
38 const tf2::Matrix3x3 m(qEnuFlu);
39 const double yaw = std::atan2(m[1][0], m[0][0]);
40 tf2::Matrix3x3 unyaw;
41 unyaw.setRPY(0.0, 0.0, -yaw);
42 const tf2::Matrix3x3 level = unyaw * m;
43 for (int i = 0; i < 3; ++i)
44 {
45 for (int j = 0; j < 3; ++j)
46 {
47 r[i * 3 + j] = static_cast<float>(level[i][j]);
48 }
49 }
50}
51
52} // namespace cl_px4_mr
void levelRotationFromPx4Quaternion(const std::array< float, 4 > &q, std::array< float, 9 > &r)