From 5690377e4c5452e6b164833c946755639dd696a9 Mon Sep 17 00:00:00 2001
From: Federico Mandelli <federico.mandelli@skywarder.eu>
Date: Mon, 11 Sep 2023 10:56:34 +0000
Subject: [PATCH] [Pitot] Velocity is now 0 if totalPressure is greater then
 staticPressure

---
 src/shared/sensors/analog/Pitot/Pitot.h | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/shared/sensors/analog/Pitot/Pitot.h b/src/shared/sensors/analog/Pitot/Pitot.h
index b598e3a93..4a445dcfa 100644
--- a/src/shared/sensors/analog/Pitot/Pitot.h
+++ b/src/shared/sensors/analog/Pitot/Pitot.h
@@ -59,15 +59,23 @@ public:
     {
         float totalPressure  = getTotalPressure();
         float staticPressure = getStaticPressure();
-
-        // clang-format off
-        float gamma = 1.4f;
-        float c     = sqrt(gamma * Constants::R * reference.refTemperature);
-        float M     = sqrt(((pow(totalPressure / staticPressure, (gamma - 1) / gamma)) - 1) * (2 / (gamma - 1)));
-        // clang-format on
-
         PitotData pitotSpeed;
-        pitotSpeed.airspeed  = M * c;
+        if (totalPressure > staticPressure)
+        {
+
+            float gamma = 1.4f;
+            float c     = sqrt(gamma * Constants::R * reference.refTemperature);
+            // clang-format off
+            float M             = sqrt(((pow(totalPressure / staticPressure, (gamma - 1) / gamma)) - 1) * (2 / (gamma - 1)));
+            // clang-format on
+            pitotSpeed.airspeed = M * c;
+            pitotSpeed.deltaP   = totalPressure - staticPressure;
+        }
+        else
+        {
+            pitotSpeed.airspeed = 0;
+            pitotSpeed.deltaP   = 0;
+        }
         pitotSpeed.timestamp = TimestampTimer::getTimestamp();
 
         return pitotSpeed;
-- 
GitLab