AP_RCProtocol_PPMSum.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Code by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #include "AP_RCProtocol_PPMSum.h"
  18. /*
  19. process a PPM-sum pulse of the given width
  20. */
  21. void AP_RCProtocol_PPMSum::process_pulse(uint32_t width_s0, uint32_t width_s1)
  22. {
  23. if (width_s0 == 0 || width_s1 == 0) {
  24. //invalid data: reset frame
  25. ppm_state._channel_counter = -1;
  26. return;
  27. }
  28. uint32_t width_usec = width_s0 + width_s1;
  29. if (width_usec >= 2700) {
  30. // a long pulse indicates the end of a frame. Reset the
  31. // channel counter so next pulse is channel 0
  32. if (ppm_state._channel_counter >= MIN_RCIN_CHANNELS) {
  33. add_input(ppm_state._channel_counter, ppm_state._pulse_capt, false);
  34. }
  35. ppm_state._channel_counter = 0;
  36. return;
  37. }
  38. if (ppm_state._channel_counter == -1) {
  39. // we are not synchronised
  40. return;
  41. }
  42. /*
  43. we limit inputs to between 700usec and 2300usec. This allows us
  44. to decode SBUS on the same pin, as SBUS will have a maximum
  45. pulse width of 100usec
  46. */
  47. if (width_usec > 700 && width_usec < 2300) {
  48. // take a reading for the current channel
  49. // buffer these
  50. ppm_state._pulse_capt[ppm_state._channel_counter] = width_usec;
  51. // move to next channel
  52. ppm_state._channel_counter++;
  53. }
  54. // if we have reached the maximum supported channels then
  55. // mark as unsynchronised, so we wait for a wide pulse
  56. if (ppm_state._channel_counter >= MAX_RCIN_CHANNELS) {
  57. add_input(ppm_state._channel_counter, ppm_state._pulse_capt, false);
  58. ppm_state._channel_counter = -1;
  59. }
  60. }