From bae275898b0cb560dae45eaafc9a8682eb80e235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 18 Aug 2022 16:29:25 +0200 Subject: [PATCH] rc/sbus: restart parser after sucessful decoding & increase time limit Instead of directly passing the next packet to the parser after successful parsing, switch the state to SBUS2_DECODE_STATE_SBUS_START and search for the start byte again. The timeout is increased as the IO main loop also takes a bit of time (max ~0.7ms). Tested on v5x with Futaba R7008SB (60Hz update rate) and FrSky X8R (111Hz update rate). Background: When using the Futaba R7008SB, I noticed there's additional bytes added in between packets. Often it's a null byte, but sometimes more. There's some consistency but I did not find any documentation for it. Sample data: a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 c0 8b 00 0f 04 ec 1f a8 fb 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 04 ec 1f a8 fb 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 05 ec 1f 30 60 bf 1c bd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 07 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 03 c0 31 00 0f 05 fc 1f a8 fb 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 05 fc 1f a8 fb 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 0f 04 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 04 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 03 c4 00 00 0f 04 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 04 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 0f 04 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 03 c0 31 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 05 fc 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 0f 05 fc 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 03 c0 31 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 05 f4 1f a8 fb 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 03 c4 00 00 b0 60 7f 1c bd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 05 ec 1f a8 fb 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 03 c0 31 00 b0 60 bf 1c bd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 04 f4 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 30 70 7f 1c bd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 34 00 0f 05 f4 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 04 00 03 c4 00 00 0f 05 fc 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 05 ec 1f b0 60 bf 1c bd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 14 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 01 08 40 00 02 10 80 00 24 00 0f 05 ec 1f a8 fd 07 16 5b 81 05 d4 a0 06 20 00 This was causing the parser to skip entire packets resulting in an update rate of ~31Hz on the FMU side. With this patch the update rate increases to 42-48Hz. The investigation was triggered by an RC glitch with a packet containing random channel data. It's likely, although not completely verified that the frequent desync randomly happend to pass the CRC check with garbage data. --- src/lib/rc/sbus.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/lib/rc/sbus.cpp b/src/lib/rc/sbus.cpp index 3a1f992ecf..04fd38d8fc 100644 --- a/src/lib/rc/sbus.cpp +++ b/src/lib/rc/sbus.cpp @@ -299,7 +299,7 @@ sbus_input(int sbus_fd, uint16_t *values, uint16_t *num_values, bool *sbus_fails * provides a degree of protection. Of course, it would be better * if we didn't drop bytes... */ - if (now - last_rx_time > 3_ms) { + if (now - last_rx_time > 4_ms) { if (partial_frame_count > 0) { partial_frame_count = 0; sbus_decode_state = SBUS2_DECODE_STATE_DESYNC; @@ -309,13 +309,6 @@ sbus_input(int sbus_fd, uint16_t *values, uint16_t *num_values, bool *sbus_fails } } - if (partial_frame_count == 0 && buf[0] != SBUS_START_SYMBOL) { - /* don't bother going through the buffer if we don't get the - * expected start symbol as a first byte */ - sbus_decode_state = SBUS2_DECODE_STATE_DESYNC; - return false; - } - #endif /* __PX4_NUTTX */ /* @@ -372,17 +365,18 @@ sbus_parse(uint64_t now, uint8_t *frame, unsigned len, uint16_t *values, switch (sbus_decode_state) { case SBUS2_DECODE_STATE_DESYNC: + /* fall through */ + case SBUS2_DECODE_STATE_SBUS_START: + /* we are de-synced and only interested in the frame marker */ if (frame[d] == SBUS_START_SYMBOL) { - sbus_decode_state = SBUS2_DECODE_STATE_SBUS_START; + sbus_decode_state = SBUS2_DECODE_STATE_SBUS2_SYNC; partial_frame_count = 0; sbus_frame[partial_frame_count++] = frame[d]; } break; - /* fall through */ - case SBUS2_DECODE_STATE_SBUS_START: case SBUS2_DECODE_STATE_SBUS1_SYNC: /* fall through */ @@ -438,6 +432,10 @@ sbus_parse(uint64_t now, uint8_t *frame, unsigned len, uint16_t *values, partial_frame_count = 0; } + if (decode_ret) { + sbus_decode_state = SBUS2_DECODE_STATE_SBUS_START; + } + } break;