From df7988fb490260cd5ac72aa2319b98c1a693b239 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 6 Feb 2019 20:48:18 +0000 Subject: [PATCH] Fix NaNs in the VSI when pressure reading is invalid Detect zero/very low pressure reading and avoid generating NaNs in the main update code. Thanks to Johnathan Redpath for the report. --- .../vertical_speed_indicator.cxx | 17 +++++++++++++++-- .../vertical_speed_indicator.hxx | 12 ++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Instrumentation/vertical_speed_indicator.cxx b/src/Instrumentation/vertical_speed_indicator.cxx index c364a700d..e4f3f908c 100644 --- a/src/Instrumentation/vertical_speed_indicator.cxx +++ b/src/Instrumentation/vertical_speed_indicator.cxx @@ -71,8 +71,21 @@ void VerticalSpeedIndicator::update (double dt) { if (_serviceable_node->getBoolValue()) { - double pressure_inHg = _pressure_node->getDoubleValue() ; - double pressure_Pa = pressure_inHg * SG_INHG_TO_PA; + const double pressure_inHg = _pressure_node->getDoubleValue() ; + const double pressure_Pa = pressure_inHg * SG_INHG_TO_PA; + + // this occurs if the static pressure source didn't report valid data + // yet. Don't continue processing here since we will generate NaNs + // when dividing by zero + if (_casing_pressure_Pa < 1e-3) { + if (pressure_Pa > 1e3) { + // once the pressure becomes valid, reinit + reinit(); + } else { + return; // no more processing + } + } + double Fsign = 0.; double orifice_mach = 0.0; diff --git a/src/Instrumentation/vertical_speed_indicator.hxx b/src/Instrumentation/vertical_speed_indicator.hxx index e552e536c..e290f8064 100644 --- a/src/Instrumentation/vertical_speed_indicator.hxx +++ b/src/Instrumentation/vertical_speed_indicator.hxx @@ -45,13 +45,13 @@ public: private: - double _casing_pressure_Pa; - double _casing_airmass_kg; - double _casing_density_kgpm3; - double _orifice_massflow_kgps; + double _casing_pressure_Pa = 0.0; + double _casing_airmass_kg = 0.0; + double _casing_density_kgpm3 = 0.0; + double _orifice_massflow_kgps = 0.0; - std::string _name; - int _num; + const std::string _name; + const int _num; std::string _static_pressure; std::string _static_temperature;