# How to calculate roll, pitch and yaw?

**URL:** <https://discourse.julialang.org/t/how-to-calculate-roll-pitch-and-yaw/121183>\
**Category:** General Usage\
**Tags:** question, aerospace, rotations\
**Created:** [October 11, 2024, 8:24am UTC](https://discourse.julialang.org/t/how-to-calculate-roll-pitch-and-yaw/121183 "2024-10-11T08:24:18Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 21, 2024, 4:28pm UTC](https://discourse.julialang.org/t/how-to-calculate-roll-pitch-and-yaw/121183/19 "2024-10-21T16:28:52Z")

</div>

Just as a follow up:

The main problem was not yet mentioned in this discussion yet. The main problem was that the vectors of the kite reference frame are defined in in the ENU reference frame, but I need the orientation in the NED reference frame. The solution is simple: Convert the vectors of the kite reference frame to NED first. This results in the following code:

```julia
"""
    enu2ned(vec::AbstractVector)

Convert a vector from ENU (east, north, up) to NED (north, east, down) reference frame.
"""
function enu2ned(vec::AbstractVector)  
    R = @SMatrix[0 1 0; 1 0 0; 0 0 -1]
    R*vec
end
"""
    calc_orient_quat(x, y, z)

Convert the the kite reference frame, defined by the three ortho-normal vectors x, y and z in ENU reference frame 
to the quaternion defining the orientation in NED reference frame.
"""
function calc_orient_quat(x, y, z)
    x = enu2ned(x)
    y = enu2ned(y)
    z = enu2ned(z)
    # reference frame for the orientation: NED (north, east, down)
    ax = @SVector [1, 0, 0]
    ay = @SVector [0, 1, 0]
    az = @SVector [0, 0, 1]
    rotation = rot3d(ax, ay, az, x, y, z)
    q = QuatRotation(rotation)
    return Rotations.params(q)
end

```

For converting the quaternion to Euler angles I use now this function:

```julia
"""
    quat2euler(q::QuatRotation)
    quat2euler(q::AbstractVector)

Convert a quaternion to roll, pitch, and yaw angles in radian.
The quaternion can be a 4-element vector (w, i, j, k) or a QuatRotation object.
"""
quat2euler(q::AbstractVector) = quat2euler(QuatRotation(q))
function quat2euler(q::QuatRotation)  
    D = RFR.DCM(q)
    pitch = asin(−D[3,1])
    roll = atan(D[3,2], D[3,3])
    yaw = atan(D[2,1], D[1,1])
    return roll, pitch, yaw
end

```

All works fine now, and Daan van Wolffelaar contributed 40 unit tests and we used geogebra to verify the results.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-calculate-roll-pitch-and-yaw/121183)._
