RCInput_Navio2.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <AP_HAL/AP_HAL.h>
  2. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2 || \
  3. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE
  4. #include <cstdio>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <cstdlib>
  8. #include <AP_Common/AP_Common.h>
  9. #include "RCInput_Navio2.h"
  10. using namespace Linux;
  11. extern const AP_HAL::HAL& hal;
  12. #define RCIN_SYSFS_PATH "/sys/kernel/rcio/rcin"
  13. void RCInput_Navio2::init()
  14. {
  15. for (size_t i = 0; i < ARRAY_SIZE(channels); i++) {
  16. channels[i] = open_channel(i);
  17. if (channels[i] < 0) {
  18. AP_HAL::panic("[RCInput_Navio2]: failed to open channels\n");
  19. }
  20. }
  21. }
  22. void RCInput_Navio2::_timer_tick(void)
  23. {
  24. if (AP_HAL::micros() - _last_timestamp < 10000) {
  25. return;
  26. }
  27. char buffer[12];
  28. for (size_t i = 0; i < ARRAY_SIZE(channels); i++) {
  29. if (::pread(channels[i], buffer, sizeof(buffer) - 1, 0) <= 0) {
  30. /* We ignore error in order not to spam the console */
  31. continue;
  32. }
  33. buffer[sizeof(buffer) - 1] = '\0';
  34. periods[i] = atoi(buffer);
  35. }
  36. _update_periods(periods, ARRAY_SIZE(periods));
  37. _last_timestamp = AP_HAL::micros();
  38. }
  39. RCInput_Navio2::RCInput_Navio2()
  40. {
  41. }
  42. RCInput_Navio2::~RCInput_Navio2()
  43. {
  44. }
  45. int RCInput_Navio2::open_channel(int channel)
  46. {
  47. char *channel_path;
  48. if (asprintf(&channel_path, "%s/ch%d", RCIN_SYSFS_PATH, channel) == -1) {
  49. AP_HAL::panic("[RCInput_Navio2]: not enough memory\n");
  50. }
  51. int fd = ::open(channel_path, O_RDONLY|O_CLOEXEC);
  52. free(channel_path);
  53. return fd;
  54. }
  55. #endif