AP_Airspeed_analog.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. /*
  14. * analog airspeed driver
  15. */
  16. #include <AP_HAL/AP_HAL.h>
  17. #include <AP_Common/AP_Common.h>
  18. #include "AP_Airspeed.h"
  19. #include "AP_Airspeed_analog.h"
  20. extern const AP_HAL::HAL &hal;
  21. // scaling for 3DR analog airspeed sensor
  22. #define VOLTS_TO_PASCAL 819
  23. AP_Airspeed_Analog::AP_Airspeed_Analog(AP_Airspeed &_frontend, uint8_t _instance) :
  24. AP_Airspeed_Backend(_frontend, _instance)
  25. {
  26. _source = hal.analogin->channel(get_pin());
  27. }
  28. bool AP_Airspeed_Analog::init()
  29. {
  30. return _source != nullptr;
  31. }
  32. // read the airspeed sensor
  33. bool AP_Airspeed_Analog::get_differential_pressure(float &pressure)
  34. {
  35. if (_source == nullptr) {
  36. return false;
  37. }
  38. // allow pin to change
  39. _source->set_pin(get_pin());
  40. pressure = _source->voltage_average_ratiometric() * VOLTS_TO_PASCAL / get_psi_range();
  41. return true;
  42. }