AP_RCProtocol_DSM.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. /*
  18. with thanks to PX4 dsm.c for DSM decoding approach
  19. */
  20. #include "AP_RCProtocol_DSM.h"
  21. extern const AP_HAL::HAL& hal;
  22. // #define DSM_DEBUG
  23. #ifdef DSM_DEBUG
  24. # define debug(fmt, args...) printf(fmt "\n", ##args)
  25. #else
  26. # define debug(fmt, args...) do {} while(0)
  27. #endif
  28. #define DSM_FRAME_SIZE 16 /**<DSM frame size in bytes*/
  29. #define DSM_FRAME_CHANNELS 7 /**<Max supported DSM channels*/
  30. void AP_RCProtocol_DSM::process_pulse(uint32_t width_s0, uint32_t width_s1)
  31. {
  32. uint8_t b;
  33. if (ss.process_pulse(width_s0, width_s1, b)) {
  34. _process_byte(ss.get_byte_timestamp_us()/1000U, b);
  35. }
  36. }
  37. /**
  38. * Attempt to decode a single channel raw channel datum
  39. *
  40. * The DSM* protocol doesn't provide any explicit framing,
  41. * so we detect dsm frame boundaries by the inter-dsm frame delay.
  42. *
  43. * The minimum dsm frame spacing is 11ms; with 16 bytes at 115200bps
  44. * dsm frame transmission time is ~1.4ms.
  45. *
  46. * We expect to only be called when bytes arrive for processing,
  47. * and if an interval of more than 5ms passes between calls,
  48. * the first byte we read will be the first byte of a dsm frame.
  49. *
  50. * In the case where byte(s) are dropped from a dsm frame, this also
  51. * provides a degree of protection. Of course, it would be better
  52. * if we didn't drop bytes...
  53. *
  54. * Upon receiving a full dsm frame we attempt to decode it
  55. *
  56. * @param[in] raw 16 bit raw channel value from dsm frame
  57. * @param[in] shift position of channel number in raw data
  58. * @param[out] channel pointer to returned channel number
  59. * @param[out] value pointer to returned channel value
  60. * @return true=raw value successfully decoded
  61. */
  62. bool AP_RCProtocol_DSM::dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value)
  63. {
  64. if (raw == 0xffff) {
  65. return false;
  66. }
  67. *channel = (raw >> shift) & 0xf;
  68. uint16_t data_mask = (1 << shift) - 1;
  69. *value = raw & data_mask;
  70. //debug("DSM: %d 0x%04x -> %d %d", shift, raw, *channel, *value);
  71. return true;
  72. }
  73. /**
  74. * Attempt to guess if receiving 10 or 11 bit channel values
  75. *
  76. * @param[in] reset true=reset the 10/11 bit state to unknown
  77. */
  78. void AP_RCProtocol_DSM::dsm_guess_format(bool reset, const uint8_t dsm_frame[16])
  79. {
  80. /* reset the 10/11 bit sniffed channel masks */
  81. if (reset) {
  82. cs10 = 0;
  83. cs11 = 0;
  84. samples = 0;
  85. channel_shift = 0;
  86. return;
  87. }
  88. /* scan the channels in the current dsm_frame in both 10- and 11-bit mode */
  89. for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
  90. const uint8_t *dp = &dsm_frame[2 + (2 * i)];
  91. uint16_t raw = (dp[0] << 8) | dp[1];
  92. unsigned channel, value;
  93. /* if the channel decodes, remember the assigned number */
  94. if (dsm_decode_channel(raw, 10, &channel, &value) && (channel < 31)) {
  95. cs10 |= (1 << channel);
  96. }
  97. if (dsm_decode_channel(raw, 11, &channel, &value) && (channel < 31)) {
  98. cs11 |= (1 << channel);
  99. }
  100. /* XXX if we cared, we could look for the phase bit here to decide 1 vs. 2-dsm_frame format */
  101. }
  102. /* wait until we have seen plenty of frames - 5 should normally be enough */
  103. if (samples++ < 5) {
  104. return;
  105. }
  106. /*
  107. * Iterate the set of sensible sniffed channel sets and see whether
  108. * decoding in 10 or 11-bit mode has yielded anything we recognize.
  109. *
  110. * XXX Note that due to what seem to be bugs in the DSM2 high-resolution
  111. * stream, we may want to sniff for longer in some cases when we think we
  112. * are talking to a DSM2 receiver in high-resolution mode (so that we can
  113. * reject it, ideally).
  114. * See e.g. http://git.openpilot.org/cru/OPReview-116 for a discussion
  115. * of this issue.
  116. */
  117. static const uint32_t masks[] = {
  118. 0x3f, /* 6 channels (DX6) */
  119. 0x7f, /* 7 channels (DX7) */
  120. 0xff, /* 8 channels (DX8) */
  121. 0x1ff, /* 9 channels (DX9, etc.) */
  122. 0x3ff, /* 10 channels (DX10) */
  123. 0x1fff, /* 13 channels (DX10t) */
  124. 0x3fff /* 18 channels (DX10) */
  125. };
  126. unsigned votes10 = 0;
  127. unsigned votes11 = 0;
  128. for (unsigned i = 0; i < sizeof(masks)/sizeof(masks[0]); i++) {
  129. if (cs10 == masks[i]) {
  130. votes10++;
  131. }
  132. if (cs11 == masks[i]) {
  133. votes11++;
  134. }
  135. }
  136. if ((votes11 == 1) && (votes10 == 0)) {
  137. channel_shift = 11;
  138. debug("DSM: 11-bit format");
  139. return;
  140. }
  141. if ((votes10 == 1) && (votes11 == 0)) {
  142. channel_shift = 10;
  143. debug("DSM: 10-bit format");
  144. return;
  145. }
  146. /* call ourselves to reset our state ... we have to try again */
  147. debug("DSM: format detect fail, 10: 0x%08x %u 11: 0x%08x %u", cs10, votes10, cs11, votes11);
  148. dsm_guess_format(true, dsm_frame);
  149. }
  150. /**
  151. * Decode the entire dsm frame (all contained channels)
  152. *
  153. */
  154. bool AP_RCProtocol_DSM::dsm_decode(uint32_t frame_time_ms, const uint8_t dsm_frame[16],
  155. uint16_t *values, uint16_t *num_values, uint16_t max_values)
  156. {
  157. /*
  158. * If we have lost signal for at least 200ms, reset the
  159. * format guessing heuristic.
  160. */
  161. if (((frame_time_ms - last_frame_time_ms) > 200U) && (channel_shift != 0)) {
  162. dsm_guess_format(true, dsm_frame);
  163. }
  164. /* we have received something we think is a dsm_frame */
  165. last_frame_time_ms = frame_time_ms;
  166. /* if we don't know the dsm_frame format, update the guessing state machine */
  167. if (channel_shift == 0) {
  168. dsm_guess_format(false, dsm_frame);
  169. return false;
  170. }
  171. /*
  172. * The encoding of the first two bytes is uncertain, so we're
  173. * going to ignore them for now.
  174. *
  175. * Each channel is a 16-bit unsigned value containing either a 10-
  176. * or 11-bit channel value and a 4-bit channel number, shifted
  177. * either 10 or 11 bits. The MSB may also be set to indicate the
  178. * second dsm_frame in variants of the protocol where more than
  179. * seven channels are being transmitted.
  180. */
  181. for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
  182. const uint8_t *dp = &dsm_frame[2 + (2 * i)];
  183. uint16_t raw = (dp[0] << 8) | dp[1];
  184. unsigned channel, value;
  185. if (!dsm_decode_channel(raw, channel_shift, &channel, &value)) {
  186. continue;
  187. }
  188. /* ignore channels out of range */
  189. if (channel >= max_values) {
  190. continue;
  191. }
  192. /* update the decoded channel count */
  193. if (channel >= *num_values) {
  194. *num_values = channel + 1;
  195. }
  196. /* convert 0-1024 / 0-2048 values to 1000-2000 ppm encoding. */
  197. if (channel_shift == 10) {
  198. value *= 2;
  199. }
  200. /*
  201. * Spektrum scaling is special. There are these basic considerations
  202. *
  203. * * Midpoint is 1520 us
  204. * * 100% travel channels are +- 400 us
  205. *
  206. * We obey the original Spektrum scaling (so a default setup will scale from
  207. * 1100 - 1900 us), but we do not obey the weird 1520 us center point
  208. * and instead (correctly) center the center around 1500 us. This is in order
  209. * to get something useful without requiring the user to calibrate on a digital
  210. * link for no reason.
  211. */
  212. /* scaled integer for decent accuracy while staying efficient */
  213. value = ((((int)value - 1024) * 1000) / 1700) + 1500;
  214. /*
  215. * Store the decoded channel into the R/C input buffer, taking into
  216. * account the different ideas about channel assignement that we have.
  217. *
  218. * Specifically, the first four channels in rc_channel_data are roll, pitch, thrust, yaw,
  219. * but the first four channels from the DSM receiver are thrust, roll, pitch, yaw.
  220. */
  221. switch (channel) {
  222. case 0:
  223. channel = 2;
  224. break;
  225. case 1:
  226. channel = 0;
  227. break;
  228. case 2:
  229. channel = 1;
  230. default:
  231. break;
  232. }
  233. values[channel] = value;
  234. }
  235. /*
  236. * Spektrum likes to send junk in higher channel numbers to fill
  237. * their packets. We don't know about a 13 channel model in their TX
  238. * lines, so if we get a channel count of 13, we'll return 12 (the last
  239. * data index that is stable).
  240. */
  241. if (*num_values == 13) {
  242. *num_values = 12;
  243. }
  244. #if 0
  245. if (channel_shift == 11) {
  246. /* Set the 11-bit data indicator */
  247. *num_values |= 0x8000;
  248. }
  249. #endif
  250. /*
  251. * XXX Note that we may be in failsafe here; we need to work out how to detect that.
  252. */
  253. return true;
  254. }
  255. /*
  256. start bind on DSM satellites
  257. */
  258. void AP_RCProtocol_DSM::start_bind(void)
  259. {
  260. bind_state = BIND_STATE1;
  261. }
  262. /*
  263. update function used for bind state machine
  264. */
  265. void AP_RCProtocol_DSM::update(void)
  266. {
  267. #if defined(HAL_GPIO_SPEKTRUM_PWR) && defined(HAL_GPIO_SPEKTRUM_RC)
  268. switch (bind_state) {
  269. case BIND_STATE_NONE:
  270. break;
  271. case BIND_STATE1:
  272. hal.gpio->write(HAL_GPIO_SPEKTRUM_PWR, !HAL_SPEKTRUM_PWR_ENABLED);
  273. hal.gpio->pinMode(HAL_GPIO_SPEKTRUM_RC, 1);
  274. hal.gpio->write(HAL_GPIO_SPEKTRUM_RC, 1);
  275. bind_last_ms = AP_HAL::millis();
  276. bind_state = BIND_STATE2;
  277. break;
  278. case BIND_STATE2: {
  279. uint32_t now = AP_HAL::millis();
  280. if (now - bind_last_ms > 500) {
  281. hal.gpio->write(HAL_GPIO_SPEKTRUM_PWR, HAL_SPEKTRUM_PWR_ENABLED);
  282. bind_last_ms = now;
  283. bind_state = BIND_STATE3;
  284. }
  285. break;
  286. }
  287. case BIND_STATE3: {
  288. uint32_t now = AP_HAL::millis();
  289. if (now - bind_last_ms > 72) {
  290. // 9 pulses works with all satellite receivers, and supports the highest
  291. // available protocol
  292. const uint8_t num_pulses = 9;
  293. for (uint8_t i=0; i<num_pulses; i++) {
  294. hal.scheduler->delay_microseconds(120);
  295. hal.gpio->write(HAL_GPIO_SPEKTRUM_RC, 0);
  296. hal.scheduler->delay_microseconds(120);
  297. hal.gpio->write(HAL_GPIO_SPEKTRUM_RC, 1);
  298. }
  299. bind_last_ms = now;
  300. bind_state = BIND_STATE4;
  301. }
  302. break;
  303. }
  304. case BIND_STATE4: {
  305. uint32_t now = AP_HAL::millis();
  306. if (now - bind_last_ms > 50) {
  307. hal.gpio->pinMode(HAL_GPIO_SPEKTRUM_RC, 0);
  308. bind_state = BIND_STATE_NONE;
  309. }
  310. break;
  311. }
  312. }
  313. #endif
  314. }
  315. /*
  316. parse one DSM byte, maintaining decoder state
  317. */
  318. bool AP_RCProtocol_DSM::dsm_parse_byte(uint32_t frame_time_ms, uint8_t b, uint16_t *values,
  319. uint16_t *num_values, uint16_t max_channels)
  320. {
  321. /* this is set by the decoding state machine and will default to false
  322. * once everything that was decodable has been decoded.
  323. */
  324. bool decode_ret = false;
  325. /* overflow check */
  326. if (byte_input.ofs == sizeof(byte_input.buf) / sizeof(byte_input.buf[0])) {
  327. byte_input.ofs = 0;
  328. dsm_decode_state = DSM_DECODE_STATE_DESYNC;
  329. debug("DSM: RESET (BUF LIM)\n");
  330. }
  331. if (byte_input.ofs == DSM_FRAME_SIZE) {
  332. byte_input.ofs = 0;
  333. dsm_decode_state = DSM_DECODE_STATE_DESYNC;
  334. debug("DSM: RESET (PACKET LIM)\n");
  335. }
  336. #ifdef DSM_DEBUG
  337. debug("dsm state: %s%s, count: %d, val: %02x\n",
  338. (dsm_decode_state == DSM_DECODE_STATE_DESYNC) ? "DSM_DECODE_STATE_DESYNC" : "",
  339. (dsm_decode_state == DSM_DECODE_STATE_SYNC) ? "DSM_DECODE_STATE_SYNC" : "",
  340. byte_input.ofs,
  341. (unsigned)b);
  342. #endif
  343. switch (dsm_decode_state) {
  344. case DSM_DECODE_STATE_DESYNC:
  345. /* we are de-synced and only interested in the frame marker */
  346. if ((frame_time_ms - last_rx_time_ms) >= 5) {
  347. dsm_decode_state = DSM_DECODE_STATE_SYNC;
  348. byte_input.ofs = 0;
  349. byte_input.buf[byte_input.ofs++] = b;
  350. }
  351. break;
  352. case DSM_DECODE_STATE_SYNC: {
  353. if ((frame_time_ms - last_rx_time_ms) >= 5 && byte_input.ofs > 0) {
  354. byte_input.ofs = 0;
  355. dsm_decode_state = DSM_DECODE_STATE_DESYNC;
  356. break;
  357. }
  358. byte_input.buf[byte_input.ofs++] = b;
  359. /* decode whatever we got and expect */
  360. if (byte_input.ofs < DSM_FRAME_SIZE) {
  361. break;
  362. }
  363. /*
  364. * Great, it looks like we might have a frame. Go ahead and
  365. * decode it.
  366. */
  367. decode_ret = dsm_decode(frame_time_ms, byte_input.buf, values, &chan_count, max_channels);
  368. /* we consumed the partial frame, reset */
  369. byte_input.ofs = 0;
  370. /* if decoding failed, set proto to desync */
  371. if (decode_ret == false) {
  372. dsm_decode_state = DSM_DECODE_STATE_DESYNC;
  373. }
  374. break;
  375. }
  376. default:
  377. debug("UNKNOWN PROTO STATE");
  378. decode_ret = false;
  379. }
  380. if (decode_ret) {
  381. *num_values = chan_count;
  382. }
  383. last_rx_time_ms = frame_time_ms;
  384. /* return false as default */
  385. return decode_ret;
  386. }
  387. // support byte input
  388. void AP_RCProtocol_DSM::_process_byte(uint32_t timestamp_ms, uint8_t b)
  389. {
  390. uint16_t v[AP_DSM_MAX_CHANNELS];
  391. uint16_t nchan;
  392. memcpy(v, last_values, sizeof(v));
  393. if (dsm_parse_byte(timestamp_ms, b, v, &nchan, AP_DSM_MAX_CHANNELS)) {
  394. memcpy(last_values, v, sizeof(v));
  395. if (nchan >= MIN_RCIN_CHANNELS) {
  396. add_input(nchan, last_values, false);
  397. }
  398. }
  399. }
  400. // support byte input
  401. void AP_RCProtocol_DSM::process_byte(uint8_t b, uint32_t baudrate)
  402. {
  403. if (baudrate != 115200) {
  404. return;
  405. }
  406. _process_byte(AP_HAL::millis(), b);
  407. }