Skip to content

Commit 4ae6da9

Browse files
committed
Address Copilot review comments
- Move I-term tracking outside stable flight check for accurate rate calculation - Move servo_autotrim_iterm_rate_limit to end of struct for EEPROM compatibility
1 parent 3878e4d commit 4ae6da9

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/main/flight/servos.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/main/flight/servos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ typedef struct servoConfig_s {
170170
uint8_t servo_protocol; // See servoProtocolType_e
171171
uint8_t tri_unarmed_servo; // send tail servo correction pulses even when unarmed
172172
uint8_t servo_autotrim_rotation_limit; // Max rotation for servo midpoints to be updated
173-
uint8_t servo_autotrim_iterm_rate_limit; // Max I-term rate of change (units/sec) to apply autotrim
174173
uint8_t servo_autotrim_iterm_threshold; // How much of the Iterm is carried over to the servo midpoints on each update
174+
uint8_t servo_autotrim_iterm_rate_limit; // Max I-term rate of change (units/sec) to apply autotrim
175175
} servoConfig_t;
176176

177177
PG_DECLARE(servoConfig_t, servoConfig);

0 commit comments

Comments
 (0)