Skip to content

Commit 13ae3e7

Browse files
demvladledvinap
andauthored
Added quaternions checking to prevent negative sqrt argument in attitudes calculation algorithm (#813)
* Added quaternions checking to prevent negative sqrt argument * Quaternion normalize in case of its module more than 1 Co-authored-by: Petr Ledvina <[email protected]> * Quaternions checking code improvement * The math issue is resolved --------- Co-authored-by: Petr Ledvina <[email protected]>
1 parent 6ff3484 commit 13ae3e7

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/flightlog.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,19 @@ export function FlightLog(logData) {
737737
z: srcFrame[imuQuaternion[2]] / scaleFromFixedInt16,
738738
w: 1.0,
739739
};
740-
q.w = Math.sqrt(1.0 - (q.x ** 2 + q.y ** 2 + q.z ** 2));
740+
741+
let m = q.x ** 2 + q.y ** 2 + q.z ** 2;
742+
if (m < 1.0) {
743+
// reconstruct .w of unit quaternion
744+
q.w = Math.sqrt(1.0 - m);
745+
} else {
746+
// normalize [0,x,y,z]
747+
m = Math.sqrt(m);
748+
q.x /= m;
749+
q.y /= m;
750+
q.z /= m;
751+
q.w = 0;
752+
}
741753
const xx = q.x ** 2,
742754
xy = q.x * q.y,
743755
xz = q.x * q.z,

0 commit comments

Comments
 (0)