@@ -625,6 +625,13 @@ void processContinuousServoAutotrim(const float dT)
625625 if (ARMING_FLAG (ARMED )) {
626626 trimState = AUTOTRIM_COLLECTING ;
627627 if ((millis () - lastUpdateTimeMs ) > 500 ) {
628+ static float itermRateOfChange [2 ];
629+ for (int axis = FD_ROLL ; axis <= FD_PITCH ; axis ++ ) {
630+ const float currentIterm = getAxisIterm (axis );
631+ itermRateOfChange [axis ] = fabsf (currentIterm - prevAxisIterm [axis ]) / 0.5f ;
632+ prevAxisIterm [axis ] = currentIterm ;
633+ }
634+
628635 const bool planeIsFlyingStraight = rotRateMagnitudeFiltered <= DEGREES_TO_RADIANS (servoConfig ()-> servo_autotrim_rotation_limit );
629636 const bool noRotationCommanded = targetRateMagnitudeFiltered <= servoConfig ()-> servo_autotrim_rotation_limit ;
630637 const bool sticksAreCentered = !areSticksDeflected ();
@@ -642,13 +649,7 @@ void processContinuousServoAutotrim(const float dT)
642649 for (int axis = FD_ROLL ; axis <= FD_PITCH ; axis ++ ) {
643650 // For each stabilized axis, add 5 units of I-term to all associated servo midpoints
644651 const float axisIterm = getAxisIterm (axis );
645-
646- // Calculate I-term rate of change (per second) to detect stability
647- const float itermRateOfChange = fabsf (axisIterm - prevAxisIterm [axis ]) / 0.5f ;
648- prevAxisIterm [axis ] = axisIterm ;
649-
650- // Only apply trim if I-term is stable (not changing rapidly due to recent maneuver)
651- const bool itermIsStable = itermRateOfChange < servoConfig ()-> servo_autotrim_iterm_rate_limit ;
652+ const bool itermIsStable = itermRateOfChange [axis ] < servoConfig ()-> servo_autotrim_iterm_rate_limit ;
652653
653654 if (fabsf (axisIterm ) > SERVO_AUTOTRIM_UPDATE_SIZE && itermIsStable ) {
654655 const int8_t ItermUpdate = axisIterm > 0.0f ? SERVO_AUTOTRIM_UPDATE_SIZE : - SERVO_AUTOTRIM_UPDATE_SIZE ;
0 commit comments