RCInput_SoloLink.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2017 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "RCInput_SoloLink.h"
  18. #include <errno.h>
  19. #include <inttypes.h>
  20. #include <stdio.h>
  21. #include <AP_HAL/AP_HAL.h>
  22. #define DEBUG 0
  23. #if DEBUG
  24. #define debug(fmt, args...) ::printf(fmt "\n", ##args)
  25. #else
  26. #define debug(fmt, args...)
  27. #endif
  28. extern const AP_HAL::HAL& hal;
  29. using namespace Linux;
  30. RCInput_SoloLink::RCInput_SoloLink()
  31. {
  32. memset(&_packet, 0, sizeof(_packet));
  33. }
  34. void RCInput_SoloLink::init()
  35. {
  36. if (!_socket.bind("0.0.0.0", PORT)) {
  37. AP_HAL::panic("failed to bind UDP socket");
  38. }
  39. // timeout is handled by poll() in SocketAPM
  40. _socket.set_blocking(true);
  41. return;
  42. }
  43. bool RCInput_SoloLink::_check_hdr(ssize_t len)
  44. {
  45. if (len < (ssize_t) sizeof(_packet)) {
  46. hal.console->printf("RCInput: Packet too small (%zd), doesn't contain full frame\n",
  47. len);
  48. return false;
  49. }
  50. uint64_t now_usec = AP_HAL::micros64();
  51. uint64_t delay = now_usec - _last_usec;
  52. if (_last_usec != 0 && delay > 40000) {
  53. debug("RCInput: no rc cmds received for %llu\n", (unsigned long long)delay);
  54. }
  55. _last_usec = now_usec;
  56. uint16_t seq = le16toh(_packet.seq);
  57. if (seq - _last_seq > 1) {
  58. debug("RCInput: gap in rc cmds : %u\n", seq - _last_seq);
  59. }
  60. _last_seq = seq;
  61. return true;
  62. }
  63. /* TODO: this should be a PollerThread or at least stop using a SchedThread */
  64. void RCInput_SoloLink::_timer_tick(void)
  65. {
  66. do {
  67. uint16_t channels[8];
  68. ssize_t r;
  69. r = _socket.recv(&_packet.buf, sizeof(_packet), 20);
  70. if (r < 0) {
  71. break;
  72. }
  73. if (!_check_hdr(r)) {
  74. break;
  75. }
  76. channels[0] = le16toh(_packet.channel[1]);
  77. channels[1] = le16toh(_packet.channel[2]);
  78. channels[2] = le16toh(_packet.channel[0]);
  79. for (unsigned int i = 3; i < 8; i++) {
  80. channels[i] = le16toh(_packet.channel[i]);
  81. }
  82. _update_periods(channels, 8);
  83. } while (true);
  84. }