WheelEncoder_Backend.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_Common/AP_Common.h>
  14. #include <AP_HAL/AP_HAL.h>
  15. #include "AP_WheelEncoder.h"
  16. #include "WheelEncoder_Backend.h"
  17. // base class constructor.
  18. AP_WheelEncoder_Backend::AP_WheelEncoder_Backend(AP_WheelEncoder &frontend, uint8_t instance, AP_WheelEncoder::WheelEncoder_State &state) :
  19. _frontend(frontend),
  20. _state(state)
  21. {
  22. state.instance = instance;
  23. }
  24. // return pin. returns -1 if pin is not defined for this instance
  25. int8_t AP_WheelEncoder_Backend::get_pin_a() const
  26. {
  27. if (_state.instance >= WHEELENCODER_MAX_INSTANCES) {
  28. return -1;
  29. }
  30. return _frontend._pina[_state.instance].get();
  31. }
  32. // return pin. returns -1 if pin is not defined for this instance
  33. int8_t AP_WheelEncoder_Backend::get_pin_b() const
  34. {
  35. if (_state.instance >= WHEELENCODER_MAX_INSTANCES) {
  36. return -1;
  37. }
  38. return _frontend._pinb[_state.instance].get();
  39. }
  40. // copy state to front end helper function
  41. void AP_WheelEncoder_Backend::copy_state_to_frontend(int32_t distance_count, uint32_t total_count, uint32_t error_count, uint32_t last_reading_ms)
  42. {
  43. // record distance and time change for calculating rate before previous state is overwritten
  44. _state.dt_ms = last_reading_ms - _state.last_reading_ms;
  45. _state.dist_count_change = distance_count - _state.distance_count;
  46. // copy distance and error count so it is accessible to front end
  47. _state.distance_count = distance_count;
  48. _state.total_count = total_count;
  49. _state.error_count = error_count;
  50. _state.last_reading_ms = last_reading_ms;
  51. }