Skip to content

BUG:The operator function for quaternion division in the quaternion library seems to be incorrect #31007

@SuperLei917

Description

@SuperLei917

I am currently studying EKF3 and have discovered a function in the magnetometer fusion section controlMagYawReset()
https://github.com/ArduPilot/ardupilot/blob/aabbe500aa4b682305b3062116edebc3fd0528c6/libraries/AP_NavEKF3/AP_NavEKF3_MagFusion.cpp#L42
Assuming two quaternions are defined as follows:

Image

The product of the two is calculated as follows:

Image This is consistent with the multiplication operator function in the code, and there is no problem with it. But the subsequent division operator function seems incorrect. Before dividing quaternions, the code has already specified the normalization of quaternions. Combined with the inverse calculation of quaternions, the following can be obtained: Image In fact, it is the conjugate of y. So x/y is actually using the conjugate of y (y ^ -1) to replace y in the product result, and the calculation result is as follows: Image But this calculation result is not consistent with the operator function:

https://github.com/ArduPilot/ardupilot/blob/aabbe500aa4b682305b3062116edebc3fd0528c6/libraries/AP_Math/quaternion.cpp#L776C1-L799C2

ret.q1 = (rquat0*quat0 + rquat1*quat1 + rquat2*quat2 + rquat3*quat3);
ret.q2 = (rquat0*quat1 - rquat1*quat0 - rquat2*quat3 + rquat3*quat2);
ret.q3 = (rquat0*quat2 + rquat1*quat3 - rquat2*quat0 - rquat3*quat1);
ret.q4 = (rquat0*quat3 - rquat1*quat2 + rquat2*quat1 - `rquat3*quat0);

The quantities of rquat1... rquat3 in ret.q2, ret.q3, and ret.q4 do not seem to be correctly multiplied by a negative sign.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions