ตรีโกณมิติ มุมออยเลอร์ และกรอบอ้างอิง
Trigonometric functions, roll/pitch/yaw, rotation order and an introduction to gimbal lock
Why it matters
An autopilot has to answer two questions all the time: where is the drone and which way is it pointing. Both answers depend on what we measure against, the reference frame, and describing the drone's orientation takes three angles called Euler angles. This page reviews the trigonometry you need, explains the frames used in aircraft and robotics, and then covers roll, pitch and yaw, rotation order and gimbal lock.
Trigonometry you will use
In a right triangle, sin θ = opposite/hypotenuse, cos θ = adjacent/hypotenuse and tan θ = opposite/adjacent. On the unit circle (radius 1), the point at angle θ from the horizontal axis has coordinates (cos θ, sin θ). This definition works for any angle, including angles above 90 degrees and negative angles.
Computers and autopilots work in radians (rad). One turn is 2π rad = 360 degrees, so 1 degree ≈ 0.01745 rad and 1 rad ≈ 57.3 degrees. Python's math.sin() always expects radians.
To get an angle from two components, use atan2(y, x) rather than atan(y/x). atan2 sees the sign of both inputs and returns the right angle in all four quadrants. atan(y/x) gives the same answer for (1, 1) and (−1, −1), which point in opposite directions.
Reference frames in aviation
A reference frame is a set of three perpendicular axes with an origin, used to state position and direction. Drone work uses two main families:
- Local NED (North-East-Down) earth frame. The origin is usually the take-off point; x points north, y east and z down. PX4 and ArduPilot use it as their main frame because heading, measured clockwise from north, falls out directly. Note that altitude is the negative of z: a drone 30 m up has z = −30 m.
- FRD (Forward-Right-Down) body frame. Fixed to the airframe: x to the nose, y to the right, z out of the belly. The IMU measures acceleration and rotation rate in this frame.
Robotics software built on ROS follows REP 103 instead: ENU (East-North-Up) for the world and FLU (Forward-Left-Up) for the body, with yaw measured counter-clockwise from east. Connecting an autopilot to ROS always involves a frame conversion. Bridges such as MAVROS do it automatically, but you still need to know which frame each topic is in.
Bearing from components
In NED, a target 300 m north and 400 m west (east = −400 m) is √(3002 + 4002) = 500 m away, at a bearing of atan2(E, N) = atan2(−400, 300) ≈ −53.1 degrees. Adding 360 gives 306.9 degrees, north-west. The argument order is (east, north) because bearing is measured from north, unlike the mathematical angle measured counter-clockwise from the x axis.
Euler angles: roll, pitch and yaw
Euler angles are three angles describing how to rotate the earth frame into the body frame. The usual aerospace convention rotates in the ZYX order:
- Yaw (ψ) about z first: turning the nose in the horizontal plane. Positive is to the right (clockwise seen from above).
- Pitch (θ) about the new y axis: nose up or down. Positive is nose up.
- Roll (φ) about the new x axis: banking. Positive is right wing down.
The order matters because 3-D rotations do not commute. Take two 90-degree rotations. Yaw 90 degrees first (nose east), then pitch 90 degrees: the nose points straight up. Pitch 90 degrees first (nose up), then rotate 90 degrees about the body z axis, which now points north horizontally: the nose ends up pointing east. The results are completely different. With small angles, say 5 degrees, the two orders differ very little, which is why small-angle approximations work for stability analysis near hover.
Tilt and multirotor motion
A multirotor moves horizontally by tilting so that its thrust gains a horizontal component. Tilting by θ while holding altitude requires thrust of 1/cos θ times the weight and produces a horizontal acceleration of g·tan θ.
- 15 degrees: acceleration 9.81 × tan 15° ≈ 2.63 m/s2; thrust 1/cos 15° ≈ 1.035 × weight.
- 30 degrees: acceleration ≈ 5.66 m/s2; thrust ≈ 1.155 × weight.
With 20 degrees of roll and 10 degrees of pitch at once, the total tilt of the body z axis from vertical follows cos(tilt) = cos φ × cos θ = cos 20° × cos 10°, giving about 22.3 degrees, not the 30 degrees you get by adding. Autopilots limit this total tilt so that vertical thrust stays sufficient.
Gimbal lock
Gimbal lock is the loss of one degree of freedom in Euler angles. It happens at pitch = ±90 degrees in the ZYX sequence. With the nose straight up, the body roll axis lines up with the earth yaw axis, so yaw and roll do the same thing. At pitch 90 degrees, (yaw 30°, roll 0°) and (yaw 0°, roll −30°) give exactly the same attitude. Only the difference between yaw and roll can be recovered, not each angle. Near this point, computing Euler angles from sensor data becomes numerically unstable.
Ordinary multirotors seldom reach 90 degrees of pitch, but acrobatic FPV drones and tail-sitter VTOL aircraft pass through it. Autopilot state estimators such as the EKF therefore store attitude as a quaternion, a set of four numbers without this singularity, and convert to Euler angles only for display.
Worked example: heading error
Assume the drone must turn to 10 degrees and is now at 350 degrees. Direct subtraction gives 10 − 350 = −340 degrees, and the controller would turn left almost a full circle when the shortest path is 20 degrees to the right. Wrap the error into −180 to +180 degrees: ((target − current + 180) mod 360) − 180 = ((10 − 350 + 180) mod 360) − 180 = (−160 mod 360) − 180 = 200 − 180 = +20 degrees, which is correct.
Common pitfalls
- Passing degrees to a function that expects radians. sin(30) in Python is the sine of 30 rad, about −0.988, not 0.5.
- Using atan instead of atan2, which gives a direction 180 degrees wrong in half the cases.
- Confusing bearing with the mathematical angle. Bearing runs clockwise from north; the mathematical angle runs counter-clockwise from x.
- Mixing NED and ENU data. x and y swap and z changes sign, so altitude turns into depth.
- Adding roll and pitch to get total tilt.
- Forgetting to wrap angles, so the drone turns the long way when crossing 0/360 degrees.
- Not stating the rotation order. The same three Euler angles give different attitudes in different orders. Always write the order in documents and code.
Continue in the lessons
Key references
- Stroud, K. A., & Booth, D. J. (2020). Engineering mathematics (8th ed.). Bloomsbury.
- Beard, R. W., & McLain, T. W. (2012). Small unmanned aircraft: Theory and practice. Princeton University Press. link
- Diebel, J. (2006). Representing attitude: Euler angles, unit quaternions, and rotation vectors. Stanford University. link
- Foote, T., & Purvis, M. (2010). REP 103: Standard units of measure and coordinate conventions. ROS Enhancement Proposals. link