AP_RCProtocol_ST24.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. SUMD decoder, based on PX4Firmware/src/rc/lib/rc/sumd.c from PX4Firmware
  3. modified for use in AP_HAL_* by Andrew Tridgell
  4. */
  5. /****************************************************************************
  6. *
  7. * Copyright (c) 2015 PX4 Development Team. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name PX4 nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /*
  38. * @file sumd.h
  39. *
  40. * RC protocol definition for Graupner HoTT transmitter (SUMD/SUMH Protocol)
  41. *
  42. * @author Marco Bauer <marco@wtns.de>
  43. */
  44. #include "AP_RCProtocol_ST24.h"
  45. // #define SUMD_DEBUG
  46. extern const AP_HAL::HAL& hal;
  47. uint8_t AP_RCProtocol_ST24::st24_crc8(uint8_t *ptr, uint8_t len)
  48. {
  49. uint8_t i, crc ;
  50. crc = 0;
  51. while (len--) {
  52. for (i = 0x80; i != 0; i >>= 1) {
  53. if ((crc & 0x80) != 0) {
  54. crc <<= 1;
  55. crc ^= 0x07;
  56. } else {
  57. crc <<= 1;
  58. }
  59. if ((*ptr & i) != 0) {
  60. crc ^= 0x07;
  61. }
  62. }
  63. ptr++;
  64. }
  65. return (crc);
  66. }
  67. void AP_RCProtocol_ST24::process_pulse(uint32_t width_s0, uint32_t width_s1)
  68. {
  69. uint8_t b;
  70. if (ss.process_pulse(width_s0, width_s1, b)) {
  71. _process_byte(b);
  72. }
  73. }
  74. void AP_RCProtocol_ST24::_process_byte(uint8_t byte)
  75. {
  76. switch (_decode_state) {
  77. case ST24_DECODE_STATE_UNSYNCED:
  78. if (byte == ST24_STX1) {
  79. _decode_state = ST24_DECODE_STATE_GOT_STX1;
  80. }
  81. break;
  82. case ST24_DECODE_STATE_GOT_STX1:
  83. if (byte == ST24_STX2) {
  84. _decode_state = ST24_DECODE_STATE_GOT_STX2;
  85. } else {
  86. _decode_state = ST24_DECODE_STATE_UNSYNCED;
  87. }
  88. break;
  89. case ST24_DECODE_STATE_GOT_STX2:
  90. /* ensure no data overflow failure or hack is possible */
  91. if ((unsigned)byte <= sizeof(_rxpacket.length) + sizeof(_rxpacket.type) + sizeof(_rxpacket.st24_data)) {
  92. _rxpacket.length = byte;
  93. _rxlen = 0;
  94. _decode_state = ST24_DECODE_STATE_GOT_LEN;
  95. } else {
  96. _decode_state = ST24_DECODE_STATE_UNSYNCED;
  97. }
  98. break;
  99. case ST24_DECODE_STATE_GOT_LEN:
  100. _rxpacket.type = byte;
  101. _rxlen++;
  102. _decode_state = ST24_DECODE_STATE_GOT_TYPE;
  103. break;
  104. case ST24_DECODE_STATE_GOT_TYPE:
  105. _rxpacket.st24_data[_rxlen - 1] = byte;
  106. _rxlen++;
  107. if (_rxlen == (_rxpacket.length - 1)) {
  108. _decode_state = ST24_DECODE_STATE_GOT_DATA;
  109. }
  110. break;
  111. case ST24_DECODE_STATE_GOT_DATA:
  112. _rxpacket.crc8 = byte;
  113. _rxlen++;
  114. if (st24_crc8((uint8_t *) & (_rxpacket.length), _rxlen) == _rxpacket.crc8) {
  115. /* decode the actual packet */
  116. switch (_rxpacket.type) {
  117. case ST24_PACKET_TYPE_CHANNELDATA12: {
  118. uint16_t values[12];
  119. uint8_t num_values;
  120. ChannelData12 *d = (ChannelData12 *)_rxpacket.st24_data;
  121. //TBD: add support for RSSI
  122. // *rssi = d->rssi;
  123. //*rx_count = d->packet_count;
  124. /* this can lead to rounding of the strides */
  125. num_values = (MAX_RCIN_CHANNELS < 12) ? MAX_RCIN_CHANNELS : 12;
  126. unsigned stride_count = (num_values * 3) / 2;
  127. unsigned chan_index = 0;
  128. for (unsigned i = 0; i < stride_count; i += 3) {
  129. values[chan_index] = ((uint16_t)d->channel[i] << 4);
  130. values[chan_index] |= ((uint16_t)(0xF0 & d->channel[i + 1]) >> 4);
  131. /* convert values to 1000-2000 ppm encoding in a not too sloppy fashion */
  132. values[chan_index] = (uint16_t)(values[chan_index] * ST24_SCALE_FACTOR + .5f) + ST24_SCALE_OFFSET;
  133. chan_index++;
  134. values[chan_index] = ((uint16_t)d->channel[i + 2]);
  135. values[chan_index] |= (((uint16_t)(0x0F & d->channel[i + 1])) << 8);
  136. /* convert values to 1000-2000 ppm encoding in a not too sloppy fashion */
  137. values[chan_index] = (uint16_t)(values[chan_index] * ST24_SCALE_FACTOR + .5f) + ST24_SCALE_OFFSET;
  138. chan_index++;
  139. }
  140. }
  141. break;
  142. case ST24_PACKET_TYPE_CHANNELDATA24: {
  143. uint16_t values[24];
  144. uint8_t num_values;
  145. ChannelData24 *d = (ChannelData24 *)&_rxpacket.st24_data;
  146. //*rssi = d->rssi;
  147. //*rx_count = d->packet_count;
  148. /* this can lead to rounding of the strides */
  149. num_values = (MAX_RCIN_CHANNELS < 24) ? MAX_RCIN_CHANNELS : 24;
  150. unsigned stride_count = (num_values * 3) / 2;
  151. unsigned chan_index = 0;
  152. for (unsigned i = 0; i < stride_count; i += 3) {
  153. values[chan_index] = ((uint16_t)d->channel[i] << 4);
  154. values[chan_index] |= ((uint16_t)(0xF0 & d->channel[i + 1]) >> 4);
  155. /* convert values to 1000-2000 ppm encoding in a not too sloppy fashion */
  156. values[chan_index] = (uint16_t)(values[chan_index] * ST24_SCALE_FACTOR + .5f) + ST24_SCALE_OFFSET;
  157. chan_index++;
  158. values[chan_index] = ((uint16_t)d->channel[i + 2]);
  159. values[chan_index] |= (((uint16_t)(0x0F & d->channel[i + 1])) << 8);
  160. /* convert values to 1000-2000 ppm encoding in a not too sloppy fashion */
  161. values[chan_index] = (uint16_t)(values[chan_index] * ST24_SCALE_FACTOR + .5f) + ST24_SCALE_OFFSET;
  162. chan_index++;
  163. }
  164. }
  165. break;
  166. case ST24_PACKET_TYPE_TRANSMITTERGPSDATA: {
  167. // ReceiverFcPacket* d = (ReceiverFcPacket*)&_rxpacket.st24_data;
  168. /* we silently ignore this data for now, as it is unused */
  169. }
  170. break;
  171. default:
  172. break;
  173. }
  174. } else {
  175. /* decoding failed */
  176. }
  177. _decode_state = ST24_DECODE_STATE_UNSYNCED;
  178. break;
  179. }
  180. }
  181. void AP_RCProtocol_ST24::process_byte(uint8_t byte, uint32_t baudrate)
  182. {
  183. if (baudrate != 115200) {
  184. return;
  185. }
  186. _process_byte(byte);
  187. }