Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ groups:
default_value: OFF
- name: fw_tpa_time_constant
description: "TPA smoothing and delay time constant to reflect non-instant speed/throttle response of the plane. See **PID Attenuation and scaling** Wiki for full details."
default_value: 1500
default_value: 2000
field: throttle.fixedWingTauMs
min: 0
max: 5000
Expand Down
2 changes: 1 addition & 1 deletion src/main/flight/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ static float calculateFixedWingAirspeedTPAFactor(void){
const float airspeed = constrainf(getAirspeedEstimate(), 100.0f, 20000.0f); // cm/s, clamped to 3.6-720 km/h
const float referenceAirspeed = pidProfile()->fixedWingReferenceAirspeed; // in cm/s
float tpaFactor= powf(referenceAirspeed/airspeed, currentControlProfile->throttle.apa_pow/100.0f);
tpaFactor= constrainf(tpaFactor, 0.3f, 1.5f);
tpaFactor= constrainf(tpaFactor, 0.3f, 2.0f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a check to ensure tpaFactor is a finite number before the constrainf call to prevent potential propagation of NaN or inf values. [possible issue, importance: 7]

Suggested change
tpaFactor= constrainf(tpaFactor, 0.3f, 2.0f);
float factor = tpaFactor;
if (!isfinite(factor)) {
factor = 1.0f;
}
tpaFactor = constrainf(factor, 0.3f, 2.0f);

return tpaFactor;
}
Comment on lines 446 to 451
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a defensive fallback when fixedWingReferenceAirspeed (or the exponent inputs) are invalid/uninitialized to avoid returning extreme/NaN values from powf(). Default to a neutral factor (e.g., 1.0f) when configuration is non-positive or non-finite. [Learned best practice, importance: 6]

New proposed code:
 static float calculateFixedWingAirspeedTPAFactor(void){
     const float airspeed = constrainf(getAirspeedEstimate(), 100.0f, 20000.0f); // cm/s, clamped to 3.6-720 km/h
     const float referenceAirspeed = pidProfile()->fixedWingReferenceAirspeed; // in cm/s
-    float tpaFactor= powf(referenceAirspeed/airspeed, currentControlProfile->throttle.apa_pow/100.0f);
-    tpaFactor= constrainf(tpaFactor, 0.3f, 2.0f);
+    const float exponent = currentControlProfile->throttle.apa_pow / 100.0f;
+
+    if (!(referenceAirspeed > 0.0f) || !isfinite(referenceAirspeed) || !isfinite(exponent)) {
+        return 1.0f;
+    }
+
+    float tpaFactor = powf(referenceAirspeed / airspeed, exponent);
+    tpaFactor = constrainf(tpaFactor, 0.3f, 2.0f);
     return tpaFactor;
 }


Expand Down
Loading