AP_WindVane_ModernDevice.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include "AP_WindVane_ModernDevice.h"
  14. // read wind speed from Modern Device rev p wind sensor
  15. // https://moderndevice.com/news/calibrating-rev-p-wind-sensor-new-regression/
  16. // constructor
  17. AP_WindVane_ModernDevice::AP_WindVane_ModernDevice(AP_WindVane &frontend) :
  18. AP_WindVane_Backend(frontend)
  19. {
  20. _speed_analog_source = hal.analogin->channel(ANALOG_INPUT_NONE);
  21. _temp_analog_source = hal.analogin->channel(ANALOG_INPUT_NONE);
  22. }
  23. void AP_WindVane_ModernDevice::update_speed()
  24. {
  25. float analog_voltage = 0.0f;
  26. // only read temp pin if defined, sensor will do OK assuming constant temp
  27. float temp_ambient = 28.0f; // equations were generated at this temp in above data sheet
  28. if (is_positive(_frontend._speed_sensor_temp_pin.get())) {
  29. _temp_analog_source->set_pin(_frontend._speed_sensor_temp_pin.get());
  30. analog_voltage = _temp_analog_source->voltage_average();
  31. temp_ambient = (analog_voltage - 0.4f) / 0.0195f; // deg C
  32. // constrain to reasonable range to avoid deviating from calibration too much and potential divide by zero
  33. temp_ambient = constrain_float(temp_ambient, 10.0f, 40.0f);
  34. }
  35. _speed_analog_source->set_pin(_frontend._speed_sensor_speed_pin.get());
  36. _current_analog_voltage = _speed_analog_source->voltage_average();
  37. // apply voltage offset and make sure not negative
  38. // by default the voltage offset is the number provide by the manufacturer
  39. analog_voltage = _current_analog_voltage - _frontend._speed_sensor_voltage_offset;
  40. if (is_negative(analog_voltage)) {
  41. analog_voltage = 0.0f;
  42. }
  43. // simplified equation from data sheet, converted from mph to m/s
  44. speed_update_frontend(24.254896f * powf((analog_voltage / powf(temp_ambient, 0.115157f)), 3.009364f));
  45. }
  46. void AP_WindVane_ModernDevice::calibrate()
  47. {
  48. gcs().send_text(MAV_SEVERITY_INFO, "WindVane: rev P. zero wind voltage offset set to %.1f",double(_current_analog_voltage));
  49. _frontend._speed_sensor_voltage_offset.set_and_save(_current_analog_voltage);
  50. _frontend._calibration.set_and_save(0);
  51. }