Recently, I have been working on a visual recognition project, with the visual development being carried out on the NVIDIA JETSON TX2 board, although it can also be done on ODROID, depending on the situation.
The results of the visual recognition need to be transmitted to the flight controller, as the ROS system runs on the TX2. Therefore, the interaction between TX2 and PIXHAWK data utilizes MAVROS, where the flight controller receives expectations from MAVROS through the MAVLINK interface in OFFBOARD mode.
These expectations can include desired position, desired speed, and desired attitude, while the TX2 also retrieves necessary aircraft state information from MAVROS, generally including control mode, unlock status, attitude, speed, and position information.
The main information obtained by the TX2 comes from the MAVROS topic /mavros/local_position/pose, but all position and attitude information must be defined according to the coordinate system. Initially, I thought they were using NED and Aircraft coordinate systems, but many errors occurred during calculations. By echoing the values of this topic, it was easy to find that the position was using the EDU coordinate system.
However, since the attitude is represented by quaternions, it is difficult to clearly identify the transformation relationship between the two coordinate systems.
Therefore, I had to look into the source code of MAVROS.
First, download the MAVROS source code from GitHub:
git clone https://github.com/mavlink/mavros.git
Then, locate the local_position.cpp file in the plugins folder:

The advertise function returns a Publisher object. By calling the object’s publish() function, we can publish messages on this topic.

This is the process of MAVROS publishing messages.
Next, we see that the position information is in the Northeast Down (EDU) coordinate system, while the attitude information is the rotation relationship from the EDU coordinate system to the Baselink coordinate system.
This code can be found in the imu.cpp file:

The source code for the attitude information:

ned_aircraft_orientation is the quaternion attitude coordinates of the NED-Aircraft calculated by the flight controller,
enu_baselink_orientation is the quaternion attitude coordinates transformed into the ENU-Baselink coordinate system through two transformations.
The Aircraft coordinate system is defined as follows (X points forward along the aircraft’s heading in the reference plane, Y is perpendicular to the aircraft’s reference plane to the right, and Z is downward perpendicular to the XOY plane in the reference plane):

Baselink coordinate system is defined as follows (X points forward along the aircraft’s heading in the reference plane, Y is perpendicular to the aircraft’s reference plane to the left, and Z is upward perpendicular to the XOY plane in the reference plane):

All of the above coordinate transformations are accomplished using the rotation formulas from the Eigen library:

The above formula is actually the famous Rodrigues rotation formula, and its source code is as follows. Since it rotates counterclockwise around the axis, the result obtained by rotating around the coordinate axis is exactly the transpose of the rotation formula we derived earlier:

Rodrigues’ rotation formula is as follows:

Regarding the conversion relationships between Euler angles, quaternions, and rotation matrices in the Eigen library, it is a linear algebra library frequently used in ROS development.
We can write more about this later when we have time. The most important thing is to clarify their rotation directions, the coordinate systems in which the rotations are performed, and the order of the rotations.
This article is reproduced from Drone System Technology, authored by Wenlong, a PhD from Beihang University. The service provided includes free sharing of drone technology knowledge, development experience, practical problem analysis, and interpretation of the algorithms and code of the open-source flight controllers PX4 and ArduPilot. Follow the public account for timely access to knowledge and insights.
