From f50326c154a1417ba064ac2480ab0f71c3bedc9d Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Wed, 22 Sep 2021 20:51:03 +0100 Subject: [PATCH] AP_WindVane: check for valid analog pins --- libraries/AP_WindVane/AP_WindVane_Analog.cpp | 5 ++++- libraries/AP_WindVane/AP_WindVane_ModernDevice.cpp | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libraries/AP_WindVane/AP_WindVane_Analog.cpp b/libraries/AP_WindVane/AP_WindVane_Analog.cpp index 004767ab56..ecf38c5dad 100644 --- a/libraries/AP_WindVane/AP_WindVane_Analog.cpp +++ b/libraries/AP_WindVane/AP_WindVane_Analog.cpp @@ -26,7 +26,10 @@ AP_WindVane_Analog::AP_WindVane_Analog(AP_WindVane &frontend) : void AP_WindVane_Analog::update_direction() { - _dir_analog_source->set_pin(_frontend._dir_analog_pin); + if (!_dir_analog_source->set_pin(_frontend._dir_analog_pin)) { + // pin invalid, don't have health monitoring to report yet + return; + } _current_analog_voltage = _dir_analog_source->voltage_latest(); const float voltage_ratio = linear_interpolate(0.0f, 1.0f, _current_analog_voltage, _frontend._dir_analog_volt_min, _frontend._dir_analog_volt_max); diff --git a/libraries/AP_WindVane/AP_WindVane_ModernDevice.cpp b/libraries/AP_WindVane/AP_WindVane_ModernDevice.cpp index 30714106df..402f3b9f07 100644 --- a/libraries/AP_WindVane/AP_WindVane_ModernDevice.cpp +++ b/libraries/AP_WindVane/AP_WindVane_ModernDevice.cpp @@ -32,14 +32,20 @@ void AP_WindVane_ModernDevice::update_speed() // only read temp pin if defined, sensor will do OK assuming constant temp float temp_ambient = 28.0f; // equations were generated at this temp in above data sheet if (is_positive(_frontend._speed_sensor_temp_pin.get())) { - _temp_analog_source->set_pin(_frontend._speed_sensor_temp_pin.get()); + if (!_temp_analog_source->set_pin(_frontend._speed_sensor_temp_pin.get())) { + // pin invalid, don't have health monitoring to report yet + return; + } analog_voltage = _temp_analog_source->voltage_average(); temp_ambient = (analog_voltage - 0.4f) / 0.0195f; // deg C // constrain to reasonable range to avoid deviating from calibration too much and potential divide by zero temp_ambient = constrain_float(temp_ambient, 10.0f, 40.0f); } - _speed_analog_source->set_pin(_frontend._speed_sensor_speed_pin.get()); + if (!_speed_analog_source->set_pin(_frontend._speed_sensor_speed_pin.get())) { + // pin invalid, don't have health monitoring to report yet + return; + } _current_analog_voltage = _speed_analog_source->voltage_average(); // apply voltage offset and make sure not negative