Skip to content

Commit

Permalink
Merge branch 'quaternion-fix' into 'main'
Browse files Browse the repository at this point in the history
Add Quaternion constructor for fields of mixed type

See merge request acubesat/adcs/adcs-simulation-julia!6
  • Loading branch information
xlxs4 committed Sep 19, 2023
2 parents aa8349a + 59ce76c commit 2709173
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function Base.show(io::IO, Q::Quaternion)
end

Quaternion(xs::Vector) = Quaternion(xs...)
function Quaternion(q1, q2, q3, q4)
promoted_type = promote_type(typeof(q1), typeof(q2), typeof(q3), typeof(q4))
return Quaternion{promoted_type}(promote(q1, q2, q3, q4)...)
end

function Base.convert(::Type{Quaternion{T}}, x::T) where {T}
return Quaternion(x, x, x, x)
end
Expand All @@ -39,12 +44,10 @@ function Base.getindex(Q::Quaternion, i::Int)
end

function Base.:*(Q1::Quaternion, Q2::Quaternion)
return Quaternion(
Q1.q1 * Q2.q1 - Q1.q2 * Q2.q2 - Q1.q3 * Q2.q3 - Q1.q4 * Q2.q4,
return Quaternion(Q1.q1 * Q2.q1 - Q1.q2 * Q2.q2 - Q1.q3 * Q2.q3 - Q1.q4 * Q2.q4,
Q1.q1 * Q2.q2 + Q1.q2 * Q2.q1 + Q1.q3 * Q2.q4 - Q1.q4 * Q2.q3,
Q1.q1 * Q2.q3 - Q1.q2 * Q2.q4 + Q1.q3 * Q2.q1 + Q1.q4 * Q2.q2,
Q1.q1 * Q2.q4 + Q1.q2 * Q2.q3 - Q1.q3 * Q2.q2 + Q1.q4 * Q2.q1
)
Q1.q1 * Q2.q4 + Q1.q2 * Q2.q3 - Q1.q3 * Q2.q2 + Q1.q4 * Q2.q1)
end

Base.:*(Q::Quaternion, n::Number) = Quaternion(Q.q1 * n, Q.q2 * n, Q.q3 * n, Q.q4 * n)
Expand Down Expand Up @@ -84,4 +87,4 @@ function quat_to_euler_deg(Q::Quaternion)
pitch = asin(2 * (Q.q1 * Q.q3 - Q.q4 * Q.q2))
yaw = atan(2 * (Q.q1 * Q.q4 + Q.q2 * Q.q3), 1 - 2 * (Q.q3^2 + Q.q4^2))
return [(roll * 180 / π), (pitch * 180 / π), (yaw * 180 / π)]
end
end

0 comments on commit 2709173

Please sign in to comment.