AP_WindVane_Backend.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.h"
  14. #include "AP_WindVane_Backend.h"
  15. // base class constructor.
  16. AP_WindVane_Backend::AP_WindVane_Backend(AP_WindVane &frontend) :
  17. _frontend(frontend)
  18. {
  19. }
  20. // update speed to frontend
  21. void AP_WindVane_Backend::speed_update_frontend(float apparent_speed_in)
  22. {
  23. // apply low pass filter if enabled
  24. if (is_positive(_frontend._speed_filt_hz)) {
  25. _speed_filt.set_cutoff_frequency(_frontend._speed_filt_hz);
  26. _frontend._speed_apparent = _speed_filt.apply(apparent_speed_in, 0.02f);
  27. } else {
  28. _frontend._speed_apparent = apparent_speed_in;
  29. }
  30. }
  31. // update direction to frontend
  32. void AP_WindVane_Backend::direction_update_frontend(float apparent_angle_ef)
  33. {
  34. // apply low pass filter if enabled
  35. if (is_positive(_frontend._dir_filt_hz)) {
  36. _dir_sin_filt.set_cutoff_frequency(_frontend._dir_filt_hz);
  37. _dir_cos_filt.set_cutoff_frequency(_frontend._dir_filt_hz);
  38. // https://en.wikipedia.org/wiki/Mean_of_circular_quantities
  39. const float filtered_sin = _dir_sin_filt.apply(sinf(apparent_angle_ef), 0.05f);
  40. const float filtered_cos = _dir_cos_filt.apply(cosf(apparent_angle_ef), 0.05f);
  41. _frontend._direction_apparent_ef = atan2f(filtered_sin, filtered_cos);
  42. } else {
  43. _frontend._direction_apparent_ef = apparent_angle_ef;
  44. }
  45. // make sure between -pi and pi
  46. _frontend._direction_apparent_ef = wrap_PI(_frontend._direction_apparent_ef);
  47. }
  48. // calibrate WindVane
  49. void AP_WindVane_Backend::calibrate()
  50. {
  51. gcs().send_text(MAV_SEVERITY_INFO, "WindVane: No cal required");
  52. _frontend._calibration.set_and_save(0);
  53. return;
  54. }