AP_RCProtocol_SUMD.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_SUMD.h"
  45. #include <AP_Math/crc.h>
  46. #define SUMD_HEADER_LENGTH 3
  47. #define SUMD_HEADER_ID 0xA8
  48. #define SUMD_ID_SUMH 0x00
  49. #define SUMD_ID_SUMD 0x01
  50. #define SUMD_ID_FAILSAFE 0x81
  51. /* define range mapping here, -+100% -> 1000..2000 */
  52. #define SUMD_RANGE_MIN 0.0f
  53. #define SUMD_RANGE_MAX 4096.0f
  54. #define SUMD_TARGET_MIN 1000.0f
  55. #define SUMD_TARGET_MAX 2000.0f
  56. /* pre-calculate the floating point stuff as far as possible at compile time */
  57. #define SUMD_SCALE_FACTOR ((SUMD_TARGET_MAX - SUMD_TARGET_MIN) / (SUMD_RANGE_MAX - SUMD_RANGE_MIN))
  58. #define SUMD_SCALE_OFFSET (int)(SUMD_TARGET_MIN - (SUMD_SCALE_FACTOR * SUMD_RANGE_MIN + 0.5f))
  59. // #define SUMD_DEBUG
  60. extern const AP_HAL::HAL& hal;
  61. uint8_t AP_RCProtocol_SUMD::sumd_crc8(uint8_t crc, uint8_t value)
  62. {
  63. crc += value;
  64. return crc;
  65. }
  66. void AP_RCProtocol_SUMD::process_pulse(uint32_t width_s0, uint32_t width_s1)
  67. {
  68. uint8_t b;
  69. if (ss.process_pulse(width_s0, width_s1, b)) {
  70. _process_byte(ss.get_byte_timestamp_us(), b);
  71. }
  72. }
  73. void AP_RCProtocol_SUMD::_process_byte(uint32_t timestamp_us, uint8_t byte)
  74. {
  75. if (timestamp_us - last_packet_us > 5000U) {
  76. _decode_state = SUMD_DECODE_STATE_UNSYNCED;
  77. }
  78. switch (_decode_state) {
  79. case SUMD_DECODE_STATE_UNSYNCED:
  80. #ifdef SUMD_DEBUG
  81. hal.console->printf(" SUMD_DECODE_STATE_UNSYNCED \n") ;
  82. #endif
  83. if (byte == SUMD_HEADER_ID) {
  84. _rxpacket.header = byte;
  85. _sumd = true;
  86. _rxlen = 0;
  87. _crc16 = 0x0000;
  88. _crc8 = 0x00;
  89. _crcOK = false;
  90. _crc16 = crc_xmodem_update(_crc16, byte);
  91. _crc8 = sumd_crc8(_crc8, byte);
  92. _decode_state = SUMD_DECODE_STATE_GOT_HEADER;
  93. #ifdef SUMD_DEBUG
  94. hal.console->printf(" SUMD_DECODE_STATE_GOT_HEADER: %x \n", byte) ;
  95. #endif
  96. last_packet_us = timestamp_us;
  97. }
  98. break;
  99. case SUMD_DECODE_STATE_GOT_HEADER:
  100. if (byte == SUMD_ID_SUMD || byte == SUMD_ID_SUMH) {
  101. _rxpacket.status = byte;
  102. if (byte == SUMD_ID_SUMH) {
  103. _sumd = false;
  104. }
  105. if (_sumd) {
  106. _crc16 = crc_xmodem_update(_crc16, byte);
  107. } else {
  108. _crc8 = sumd_crc8(_crc8, byte);
  109. }
  110. _decode_state = SUMD_DECODE_STATE_GOT_STATE;
  111. #ifdef SUMD_DEBUG
  112. hal.console->printf(" SUMD_DECODE_STATE_GOT_STATE: %x \n", byte) ;
  113. #endif
  114. } else {
  115. _decode_state = SUMD_DECODE_STATE_UNSYNCED;
  116. }
  117. break;
  118. case SUMD_DECODE_STATE_GOT_STATE:
  119. if (byte >= 2 && byte <= SUMD_MAX_CHANNELS) {
  120. _rxpacket.length = byte;
  121. if (_sumd) {
  122. _crc16 = crc_xmodem_update(_crc16, byte);
  123. } else {
  124. _crc8 = sumd_crc8(_crc8, byte);
  125. }
  126. _rxlen++;
  127. _decode_state = SUMD_DECODE_STATE_GOT_LEN;
  128. #ifdef SUMD_DEBUG
  129. hal.console->printf(" SUMD_DECODE_STATE_GOT_LEN: %x (%d) \n", byte, byte) ;
  130. #endif
  131. } else {
  132. _decode_state = SUMD_DECODE_STATE_UNSYNCED;
  133. }
  134. break;
  135. case SUMD_DECODE_STATE_GOT_LEN:
  136. _rxpacket.sumd_data[_rxlen] = byte;
  137. if (_sumd) {
  138. _crc16 = crc_xmodem_update(_crc16, byte);
  139. } else {
  140. _crc8 = sumd_crc8(_crc8, byte);
  141. }
  142. _rxlen++;
  143. if (_rxlen <= ((_rxpacket.length * 2))) {
  144. #ifdef SUMD_DEBUG
  145. hal.console->printf(" SUMD_DECODE_STATE_GOT_DATA[%d]: %x\n", _rxlen - 2, byte) ;
  146. #endif
  147. } else {
  148. _decode_state = SUMD_DECODE_STATE_GOT_DATA;
  149. #ifdef SUMD_DEBUG
  150. hal.console->printf(" SUMD_DECODE_STATE_GOT_DATA -- finish --\n") ;
  151. #endif
  152. }
  153. break;
  154. case SUMD_DECODE_STATE_GOT_DATA:
  155. _rxpacket.crc16_high = byte;
  156. #ifdef SUMD_DEBUG
  157. hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC16[1]: %x [%x]\n", byte, ((_crc16 >> 8) & 0xff)) ;
  158. #endif
  159. if (_sumd) {
  160. _decode_state = SUMD_DECODE_STATE_GOT_CRC;
  161. } else {
  162. _decode_state = SUMD_DECODE_STATE_GOT_CRC16_BYTE_1;
  163. }
  164. break;
  165. case SUMD_DECODE_STATE_GOT_CRC16_BYTE_1:
  166. _rxpacket.crc16_low = byte;
  167. #ifdef SUMD_DEBUG
  168. hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC16[2]: %x [%x]\n", byte, (_crc16 & 0xff)) ;
  169. #endif
  170. _decode_state = SUMD_DECODE_STATE_GOT_CRC16_BYTE_2;
  171. break;
  172. case SUMD_DECODE_STATE_GOT_CRC16_BYTE_2:
  173. _rxpacket.telemetry = byte;
  174. #ifdef SUMD_DEBUG
  175. hal.console->printf(" SUMD_DECODE_STATE_GOT_SUMH_TELEMETRY: %x\n", byte) ;
  176. #endif
  177. _decode_state = SUMD_DECODE_STATE_GOT_CRC;
  178. break;
  179. case SUMD_DECODE_STATE_GOT_CRC:
  180. if (_sumd) {
  181. _rxpacket.crc16_low = byte;
  182. #ifdef SUMD_DEBUG
  183. hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC[2]: %x [%x]\n\n", byte, (_crc16 & 0xff)) ;
  184. #endif
  185. if (_crc16 == (uint16_t)(_rxpacket.crc16_high << 8) + _rxpacket.crc16_low) {
  186. _crcOK = true;
  187. }
  188. } else {
  189. _rxpacket.crc8 = byte;
  190. #ifdef SUMD_DEBUG
  191. hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC8_SUMH: %x [%x]\n\n", byte, _crc8) ;
  192. #endif
  193. if (_crc8 == _rxpacket.crc8) {
  194. _crcOK = true;
  195. }
  196. }
  197. if (_crcOK) {
  198. #ifdef SUMD_DEBUG
  199. hal.console->printf(" CRC - OK \n") ;
  200. #endif
  201. if (_sumd) {
  202. #ifdef SUMD_DEBUG
  203. hal.console->printf(" Got valid SUMD Packet\n") ;
  204. #endif
  205. } else {
  206. #ifdef SUMD_DEBUG
  207. hal.console->printf(" Got valid SUMH Packet\n") ;
  208. #endif
  209. }
  210. #ifdef SUMD_DEBUG
  211. hal.console->printf(" RXLEN: %d [Chans: %d] \n\n", _rxlen - 1, (_rxlen - 1) / 2) ;
  212. #endif
  213. unsigned i;
  214. uint8_t num_values;
  215. uint16_t values[SUMD_MAX_CHANNELS];
  216. /* received Channels */
  217. if ((uint16_t)_rxpacket.length > SUMD_MAX_CHANNELS) {
  218. _rxpacket.length = (uint8_t) SUMD_MAX_CHANNELS;
  219. }
  220. num_values = (uint16_t)_rxpacket.length;
  221. /* decode the actual packet */
  222. /* reorder first 4 channels */
  223. /* ch1 = roll -> sumd = ch2 */
  224. values[0] = (uint16_t)((_rxpacket.sumd_data[1 * 2 + 1] << 8) | _rxpacket.sumd_data[1 * 2 + 2]) >> 3;
  225. /* ch2 = pitch -> sumd = ch2 */
  226. values[1] = (uint16_t)((_rxpacket.sumd_data[2 * 2 + 1] << 8) | _rxpacket.sumd_data[2 * 2 + 2]) >> 3;
  227. /* ch3 = throttle -> sumd = ch2 */
  228. values[2] = (uint16_t)((_rxpacket.sumd_data[0 * 2 + 1] << 8) | _rxpacket.sumd_data[0 * 2 + 2]) >> 3;
  229. /* ch4 = yaw -> sumd = ch2 */
  230. values[3] = (uint16_t)((_rxpacket.sumd_data[3 * 2 + 1] << 8) | _rxpacket.sumd_data[3 * 2 + 2]) >> 3;
  231. /* we start at channel 5(index 4) */
  232. unsigned chan_index = 4;
  233. for (i = 4; i < _rxpacket.length; i++) {
  234. #ifdef SUMD_DEBUG
  235. hal.console->printf("ch[%u] : %x %x [ %x %d ]\n", i + 1, _rxpacket.sumd_data[i * 2 + 1], _rxpacket.sumd_data[i * 2 + 2],
  236. ((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3,
  237. ((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3);
  238. #endif
  239. values[chan_index] = (uint16_t)((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3;
  240. /* convert values to 1000-2000 ppm encoding in a not too sloppy fashion */
  241. //channels[chan_index] = (uint16_t)(channels[chan_index] * SUMD_SCALE_FACTOR + .5f) + SUMD_SCALE_OFFSET;
  242. chan_index++;
  243. }
  244. if (_rxpacket.status == 0x01) {
  245. add_input(num_values, values, false);
  246. } else if (_rxpacket.status == 0x81) {
  247. add_input(num_values, values, true);
  248. }
  249. } else {
  250. #ifdef SUMD_DEBUG
  251. hal.console->printf(" CRC - fail 0x%X 0x%X\n", _crc16, (uint16_t)(_rxpacket.crc16_high << 8) + _rxpacket.crc16_low);
  252. #endif
  253. }
  254. _decode_state = SUMD_DECODE_STATE_UNSYNCED;
  255. break;
  256. }
  257. }
  258. void AP_RCProtocol_SUMD::process_byte(uint8_t byte, uint32_t baudrate)
  259. {
  260. if (baudrate != 115200) {
  261. return;
  262. }
  263. _process_byte(AP_HAL::micros(), byte);
  264. }