AP_WindVane_Analog.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_Analog.h"
  14. #define WINDVANE_CALIBRATION_VOLT_DIFF_MIN 1.0f // calibration routine's min voltage difference required for success
  15. // constructor
  16. AP_WindVane_Analog::AP_WindVane_Analog(AP_WindVane &frontend) :
  17. AP_WindVane_Backend(frontend)
  18. {
  19. _dir_analog_source = hal.analogin->channel(ANALOG_INPUT_NONE);
  20. }
  21. void AP_WindVane_Analog::update_direction()
  22. {
  23. _dir_analog_source->set_pin(_frontend._dir_analog_pin);
  24. _current_analog_voltage = _dir_analog_source->voltage_average_ratiometric();
  25. const float voltage_ratio = linear_interpolate(0.0f, 1.0f, _current_analog_voltage, _frontend._dir_analog_volt_min, _frontend._dir_analog_volt_max);
  26. const float direction = (voltage_ratio * radians(360 - _frontend._dir_analog_deadzone)) + radians(_frontend._dir_analog_bearing_offset);
  27. direction_update_frontend(wrap_PI(direction + AP::ahrs().yaw));
  28. }
  29. void AP_WindVane_Analog::calibrate()
  30. {
  31. // start calibration
  32. if (_cal_start_ms == 0) {
  33. _cal_start_ms = AP_HAL::millis();
  34. _cal_volt_max = _current_analog_voltage;
  35. _cal_volt_min = _current_analog_voltage;
  36. gcs().send_text(MAV_SEVERITY_INFO, "WindVane: Calibration started, rotate wind vane");
  37. }
  38. // record min and max voltage
  39. _cal_volt_max = MAX(_cal_volt_max, _current_analog_voltage);
  40. _cal_volt_min = MIN(_cal_volt_min, _current_analog_voltage);
  41. // calibrate for 30 seconds
  42. if ((AP_HAL::millis() - _cal_start_ms) > 30000) {
  43. // check for required voltage difference
  44. const float volt_diff = _cal_volt_max - _cal_volt_min;
  45. if (volt_diff >= WINDVANE_CALIBRATION_VOLT_DIFF_MIN) {
  46. // save min and max voltage
  47. _frontend._dir_analog_volt_max.set_and_save(_cal_volt_max);
  48. _frontend._dir_analog_volt_min.set_and_save(_cal_volt_min);
  49. gcs().send_text(MAV_SEVERITY_INFO, "WindVane: Calibration complete (volt min:%.1f max:%1.f)",
  50. (double)_cal_volt_min,
  51. (double)_cal_volt_max);
  52. } else {
  53. gcs().send_text(MAV_SEVERITY_INFO, "WindVane: Calibration failed (volt diff %.1f below %.1f)",
  54. (double)volt_diff,
  55. (double)WINDVANE_CALIBRATION_VOLT_DIFF_MIN);
  56. }
  57. _frontend._calibration.set_and_save(0);
  58. _cal_start_ms = 0;
  59. }
  60. }