diff --git a/EKF/ekf_helper.cpp b/EKF/ekf_helper.cpp index 459a5e9d2f..3429c24465 100644 --- a/EKF/ekf_helper.cpp +++ b/EKF/ekf_helper.cpp @@ -506,17 +506,16 @@ bool Ekf::realignYawGPS() _flt_mag_align_start_time = _imu_sample_delayed.time_us; // calculate the amount that the quaternion has changed by - _state_reset_status.quat_change = quat_before_reset.inversed() * _state.quat_nominal; + _state_reset_status.quat_change = _state.quat_nominal * quat_before_reset.inversed(); // add the reset amount to the output observer buffered data - // Note q1 *= q2 is equivalent to q1 = q1 * q2 for (uint8_t i = 0; i < _output_buffer.get_length(); i++) { - _output_buffer[i].quat_nominal *= _state_reset_status.quat_change; + _output_buffer[i].quat_nominal = _state_reset_status.quat_change * _output_buffer[i].quat_nominal; } // apply the change in attitude quaternion to our newest quaternion estimate // which was already taken out from the output buffer - _output_new.quat_nominal = _output_new.quat_nominal * _state_reset_status.quat_change; + _output_new.quat_nominal = _state_reset_status.quat_change * _output_new.quat_nominal; // capture the reset event _state_reset_status.quat_counter++; @@ -718,7 +717,7 @@ bool Ekf::resetMagHeading(Vector3f &mag_init, bool increase_yaw_var, bool update _flt_mag_align_start_time = _imu_sample_delayed.time_us; // calculate the amount that the quaternion has changed by - Quatf q_error = quat_before_reset.inversed() * quat_after_reset; + Quatf q_error = quat_after_reset * quat_before_reset.inversed(); q_error.normalize(); // update quaternion states @@ -750,13 +749,12 @@ bool Ekf::resetMagHeading(Vector3f &mag_init, bool increase_yaw_var, bool update if (update_buffer) { // add the reset amount to the output observer buffered data for (uint8_t i = 0; i < _output_buffer.get_length(); i++) { - // Note q1 *= q2 is equivalent to q1 = q1 * q2 - _output_buffer[i].quat_nominal *= _state_reset_status.quat_change; + _output_buffer[i].quat_nominal = _state_reset_status.quat_change * _output_buffer[i].quat_nominal; } // apply the change in attitude quaternion to our newest quaternion estimate // which was already taken out from the output buffer - _output_new.quat_nominal = _output_new.quat_nominal * _state_reset_status.quat_change; + _output_new.quat_nominal = _state_reset_status.quat_change * _output_new.quat_nominal; } // capture the reset event