You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
qodo-code-reviewBot
changed the title
Better yaw estimation for mulitirotor(magless operation)
Better yaw estimation for mulitirotor(magless operation)
Nov 13, 2025
Objective: To create a detailed and reliable record of critical system actions for security analysis and compliance.
Status: No audit logs: The new logic adjusting GPS/ACC weighting and configuration changes introduce critical flight-behavior decisions without corresponding audit/event logging of key outcomes or thresholds used.
Referred Code
staticfloatimuCalculateMcCogAccWeight(void)
{
fpVector3_taccBFNorm;
vectorScale(&accBFNorm, &imuMeasuredAccelBFFiltered, 1.0f / GRAVITY_CMSS);
floatwCoGAcc=constrainf((accBFNorm.z-1.0f)*2, 0.0f, 1.0f); //z direction is verified via SITLwCoGAcc=wCoGAcc*imuCalculateAccelerometerWeightRateIgnore(4.0f); //reduce weight when fast angular rate, gps acc is considered laggingreturnwCoGAcc;
Generic: Meaningful Naming and Self-Documenting Code
Objective: Ensure all identifiers clearly express their purpose and intent, making code self-documenting
Status: Ambiguous naming: The parameter and variable name acc_ignore_slope_multipiler appears misspelled and unclear, which can hinder readability and intent comprehension.
Referred Code
imuCalculateFilters(dT);
// centrifugal force compensationstaticfpVector3_tvEstcentrifugalAccelBF_velned;
staticfpVector3_tvEstcentrifugalAccelBF_turnrate;
floatacc_ignore_slope_multipiler=1.0f; // when using gps centrifugal_force_compensation, AccelerometerWeightRateIgnore slope will be multiplied by this valueif (isGPSTrustworthy())
{
imuCalculateGPSacceleration(&vCOGAcc, &vEstcentrifugalAccelBF_velned, &acc_ignore_slope_multipiler);
useCOGAcc= true; //currently only for multicopter
}
if (STATE(AIRPLANE))
{
imuCalculateTurnRateacceleration(&vEstcentrifugalAccelBF_turnrate, dT, &acc_ignore_slope_multipiler);
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful degradation
Status: Missing edge checks: Newly added weighting paths depend on GPS trust and vectors but add no explicit handling for cases like null/invalid vectors or extreme rates beyond expected bounds in the new rate-ignored weighting.
Referred Code
staticfloatimuCalculateMcCogAccWeight(void)
{
fpVector3_taccBFNorm;
vectorScale(&accBFNorm, &imuMeasuredAccelBFFiltered, 1.0f / GRAVITY_CMSS);
floatwCoGAcc=constrainf((accBFNorm.z-1.0f)*2, 0.0f, 1.0f); //z direction is verified via SITLwCoGAcc=wCoGAcc*imuCalculateAccelerometerWeightRateIgnore(4.0f); //reduce weight when fast angular rate, gps acc is considered laggingreturnwCoGAcc;
Objective: To create a detailed and reliable record of critical system actions for security analysis and compliance.
Status: No audit logs: New logic changes IMU weighting and GPS usage without adding any critical action logging, but it's unclear whether this subsystem is expected to produce audit trails.
Referred Code
staticfloatimuCalculateMcCogWeight(void)
{
//used when flying stright. 1G accelerationfloatwCoG=imuCalculateAccelerometerWeightNearness(&imuMeasuredAccelBFFiltered);
floatrotRateMagnitude=fast_fsqrtf(vectorNormSquared(&imuMeasuredRotationBFFiltered));
constfloatrateSlopeMax=DEGREES_TO_RADIANS((imuConfig()->acc_ignore_rate)) *4.0f;
wCoG *= scaleRangef(constrainf(rotRateMagnitude, 0.0f, rateSlopeMax), 0.0f, rateSlopeMax, 1.0f, 0.0f);
returnwCoG;
}
staticfloatimuCalculateMcCogAccWeight(void)
{
fpVector3_taccBFNorm;
vectorScale(&accBFNorm, &imuMeasuredAccelBFFiltered, 1.0f / GRAVITY_CMSS);
floatwCoGAcc=constrainf((accBFNorm.z-1.0f)*2, 0.0f, 1.0f); //z direction is verified via SITLwCoGAcc=wCoGAcc*imuCalculateAccelerometerWeightRateIgnore(4.0f); //reduce weight when fast angular rate, gps acc is considered laggingreturnwCoGAcc;
}
staticvoidimuMahonyAHRSupdate(floatdt, constfpVector3_t*gyroBF, constfpVector3_t*accBF, constfpVector3_t*magBF, constfpVector3_t*vCOG, constfpVector3_t*vCOGAcc, floataccWScaler, floatmagWScaler)
{
STATIC_FASTRAMfpVector3_tvGyroDriftEstimate= { 0 };
... (clipped96lines)
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful degradation
Status: No error paths: The new computations (e.g., weighting and vector operations) add no checks for invalid inputs (null vectors, NaNs) or edge cases, though this may be handled elsewhere in the flight stack.
Referred Code
staticfloatimuCalculateMcCogAccWeight(void)
{
fpVector3_taccBFNorm;
vectorScale(&accBFNorm, &imuMeasuredAccelBFFiltered, 1.0f / GRAVITY_CMSS);
floatwCoGAcc=constrainf((accBFNorm.z-1.0f)*2, 0.0f, 1.0f); //z direction is verified via SITLwCoGAcc=wCoGAcc*imuCalculateAccelerometerWeightRateIgnore(4.0f); //reduce weight when fast angular rate, gps acc is considered laggingreturnwCoGAcc;
}
staticvoidimuMahonyAHRSupdate(floatdt, constfpVector3_t*gyroBF, constfpVector3_t*accBF, constfpVector3_t*magBF, constfpVector3_t*vCOG, constfpVector3_t*vCOGAcc, floataccWScaler, floatmagWScaler)
{
STATIC_FASTRAMfpVector3_tvGyroDriftEstimate= { 0 };
fpQuaternion_tprevOrientation=orientation;
fpVector3_tvRotation=*gyroBF;
/* Calculate general spin rate (rad/s) */constfloatspin_rate_sq=vectorNormSquared(&vRotation);
/* Step 1: Yaw correction */// Use measured magnetic field vectorif (magBF||vCOG||vCOGAcc) {
... (clipped90lines)
Generic: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent vulnerabilities
Status: Input validation: New use of GPS-derived vectors and rates lacks explicit validation in the diff (e.g., bounds, NaN checks), but these may be guaranteed upstream in sensor fusion code.
Referred Code
imuCalculateFilters(dT);
// centrifugal force compensationstaticfpVector3_tvEstcentrifugalAccelBF_velned;
staticfpVector3_tvEstcentrifugalAccelBF_turnrate;
floatacc_ignore_slope_multipiler=1.0f; // when using gps centrifugal_force_compensation, AccelerometerWeightRateIgnore slope will be multiplied by this valueif (isGPSTrustworthy())
{
imuCalculateGPSacceleration(&vCOGAcc, &vEstcentrifugalAccelBF_velned, &acc_ignore_slope_multipiler);
useCOGAcc= true; //currently only for multicopter
}
if (STATE(AIRPLANE))
{
imuCalculateTurnRateacceleration(&vEstcentrifugalAccelBF_turnrate, dT, &acc_ignore_slope_multipiler);
-//used when flying stright. 1G acceleration+// Used when flying straight: 1 g acceleration
...
-wCoGAcc = wCoGAcc * imuCalculateAccelerometerWeightRateIgnore(4.0f); //reduce weight when fast angular rate, gps acc is considered lagging+wCoGAcc = wCoGAcc * imuCalculateAccelerometerWeightRateIgnore(4.0f); // Reduce weight at high angular rate; GPS acceleration may lag
[To ensure code accuracy, apply this suggestion manually]
Suggestion importance[1-10]: 5
__
Why:
Relevant best practice - Use clear, correctly spelled comments to improve maintainability and avoid ambiguity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Follow-up PR for #11046
cleaned up the code.
some twicks
realflight testes, shows its ablity to recover from toliet blow
https://youtu.be/46TANQQ4KBc
PR Type
Enhancement
Description
Add new function to calculate accelerometer weight based on angular rate
Integrate rate-based weight reduction in GPS acceleration heading estimation
Remove debug logging statements for cleaner production code
Lower default
acc_ignore_ratethreshold from 20 to 15 deg/sDiagram Walkthrough
File Walkthrough
imu.c
Enhance yaw estimation with angular rate weightingsrc/main/flight/imu.c
imuCalculateAccelerometerWeightRateIgnore()to calculate weightreduction based on angular rate
imuCalculateMcCogAccWeight()to reduce GPS acceleration vector weightduring high angular rates
imuMahonyAHRSupdate()andimuCalculateEstimatedAttitude()functionsflight scenarios
Settings.md
Lower accelerometer ignore rate threshold defaultdocs/Settings.md
acc_ignore_rateparameter from 20 to 15deg/s
settings.yaml
Update default accelerometer ignore rate settingsrc/main/fc/settings.yaml
ahrs_acc_ignore_ratedefault value from 20 to 15 deg/s