@@ -186,7 +186,7 @@ bool aoaControlEnable(int8_t input_rc_channel)
186186 return rcValue > 1666 ;
187187}
188188
189- void aoaControlUpdate (float pidPitchOutput , float rateError , float newPTerm , float newDTerm , float newFFTerm , float errorGyroIf , float limit )
189+ void aoaControlUpdate (float * pidPitchOutput , float rateError , float newPTerm , float newDTerm , float newFFTerm , float errorGyroIf , float limit )
190190{
191191 isAoaControlEnabled = aoaControlEnable (aoaControlConfig ()-> fw_aoa_control_channel );
192192
@@ -202,22 +202,37 @@ void aoaControlUpdate(float pidPitchOutput, float rateError, float newPTerm, flo
202202 float aoaServoOffset = 0.0f ;
203203
204204 if (aoaControlConfig ()-> fw_aoa_aircraft_type == AIRCRAFT_CANARD ) {
205- // Canard layout: always intervene with servo offset
205+
206+ const float thresholdRatio = aoaControlConfig ()-> fw_aoa_intervention_threshold * 0.01f ;
207+ const int16_t upperThreshold = upperLimitAngle * thresholdRatio ;
208+ const int16_t lowerThreshold = lowerLimitAngle * thresholdRatio ;
209+ const int16_t aoaDeg = DECIDEGREES_TO_DEGREES (filteredAoa );
210+
206211 const int16_t lowerLimit = lowerLimitAngle * deg2pwm ;
207212 const int16_t upperLimit = upperLimitAngle * deg2pwm ;
213+ float constrainedPidOutput = constrainf (* pidPitchOutput , - upperLimit , - lowerLimit );
208214
209- float pidOutput = pidPitchOutput * kp ;
210- float constrainedPidOutput = constrainf (pidOutput , - upperLimit , - lowerLimit );
215+ float aoaError = 0.0f ;
216+ if (aoaDeg > upperThreshold ) {
217+ aoaError = aoaDeg - upperThreshold ;
218+ } else if (aoaDeg < lowerThreshold ) {
219+ aoaError = aoaDeg - lowerThreshold ;
220+ }
211221
222+ float interventionOffset = aoaError * kp * deg2pwm ;
212223 aoaServoOffset = (DECIDEGREES_TO_DEGREES (filteredAoa ) - aoaControlConfig ()-> fw_aoa_trim_angle ) * deg2pwm ;
213-
214- aoaPidOutput = isAoaControlEnabled ? constrainedPidOutput + aoaServoOffset : constrainedPidOutput ;
224+ aoaPidOutput = isAoaControlEnabled ? constrainedPidOutput + aoaServoOffset : constrainedPidOutput ;
215225 aoaPidOutput = constrainf (aoaPidOutput , - limit , + limit );
216226
217- DEBUG_SET (DEBUG_AOA , 1 , pidOutput );
218- DEBUG_SET (DEBUG_AOA , 2 , pidPitchOutput );
227+ if (isAoaControlEnabled && aoaError != 0.0f ) {
228+ * pidPitchOutput += interventionOffset ;
229+ * pidPitchOutput = constrainf (* pidPitchOutput , - limit , + limit );
230+ }
231+
232+ DEBUG_SET (DEBUG_AOA , 1 , aoaError );
233+ DEBUG_SET (DEBUG_AOA , 2 , * pidPitchOutput );
219234 DEBUG_SET (DEBUG_AOA , 3 , aoaControlConfig ()-> fw_aoa_kp );
220- DEBUG_SET (DEBUG_AOA , 4 , constrainedPidOutput );
235+ DEBUG_SET (DEBUG_AOA , 4 , interventionOffset );
221236 DEBUG_SET (DEBUG_AOA , 5 , aoaServoOffset );
222237 DEBUG_SET (DEBUG_AOA , 6 , aoaPidOutput );
223238 DEBUG_SET (DEBUG_AOA , 7 , rateError );
@@ -237,11 +252,11 @@ void aoaControlUpdate(float pidPitchOutput, float rateError, float newPTerm, flo
237252
238253 aoaServoOffset = aoaError * kp * deg2pwm ;
239254
240- aoaPidOutput = isAoaControlEnabled ? pidPitchOutput + aoaServoOffset : pidPitchOutput ;
255+ aoaPidOutput = isAoaControlEnabled ? * pidPitchOutput + aoaServoOffset : * pidPitchOutput ;
241256 aoaPidOutput = constrainf (aoaPidOutput , - limit , + limit );
242257
243258 DEBUG_SET (DEBUG_AOA , 1 , aoaError );
244- DEBUG_SET (DEBUG_AOA , 2 , pidPitchOutput );
259+ DEBUG_SET (DEBUG_AOA , 2 , * pidPitchOutput );
245260 DEBUG_SET (DEBUG_AOA , 3 , aoaControlConfig ()-> fw_aoa_kp );
246261 DEBUG_SET (DEBUG_AOA , 4 , aoaServoOffset );
247262 DEBUG_SET (DEBUG_AOA , 5 , aoaServoOffset );
0 commit comments