123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #include "AP_RCProtocol_SBUS.h"
- #define SBUS_FRAME_SIZE 25
- #define SBUS_INPUT_CHANNELS 16
- #define SBUS_FLAGS_BYTE 23
- #define SBUS_FAILSAFE_BIT 3
- #define SBUS_FRAMELOST_BIT 2
- #define SBUS_RANGE_MIN 200.0f
- #define SBUS_RANGE_MAX 1800.0f
- #define SBUS_TARGET_MIN 1000.0f
- #define SBUS_TARGET_MAX 2000.0f
- #define SBUS_SCALE_FACTOR ((SBUS_TARGET_MAX - SBUS_TARGET_MIN) / (SBUS_RANGE_MAX - SBUS_RANGE_MIN))
- #define SBUS_SCALE_OFFSET (int)(SBUS_TARGET_MIN - (SBUS_SCALE_FACTOR * SBUS_RANGE_MIN + 0.5f))
- struct sbus_bit_pick {
- uint8_t byte;
- uint8_t rshift;
- uint8_t mask;
- uint8_t lshift;
- };
- static const struct sbus_bit_pick sbus_decoder[SBUS_INPUT_CHANNELS][3] = {
- { { 0, 0, 0xff, 0}, { 1, 0, 0x07, 8}, { 0, 0, 0x00, 0} },
- { { 1, 3, 0x1f, 0}, { 2, 0, 0x3f, 5}, { 0, 0, 0x00, 0} },
- { { 2, 6, 0x03, 0}, { 3, 0, 0xff, 2}, { 4, 0, 0x01, 10} },
- { { 4, 1, 0x7f, 0}, { 5, 0, 0x0f, 7}, { 0, 0, 0x00, 0} },
- { { 5, 4, 0x0f, 0}, { 6, 0, 0x7f, 4}, { 0, 0, 0x00, 0} },
- { { 6, 7, 0x01, 0}, { 7, 0, 0xff, 1}, { 8, 0, 0x03, 9} },
- { { 8, 2, 0x3f, 0}, { 9, 0, 0x1f, 6}, { 0, 0, 0x00, 0} },
- { { 9, 5, 0x07, 0}, {10, 0, 0xff, 3}, { 0, 0, 0x00, 0} },
- { {11, 0, 0xff, 0}, {12, 0, 0x07, 8}, { 0, 0, 0x00, 0} },
- { {12, 3, 0x1f, 0}, {13, 0, 0x3f, 5}, { 0, 0, 0x00, 0} },
- { {13, 6, 0x03, 0}, {14, 0, 0xff, 2}, {15, 0, 0x01, 10} },
- { {15, 1, 0x7f, 0}, {16, 0, 0x0f, 7}, { 0, 0, 0x00, 0} },
- { {16, 4, 0x0f, 0}, {17, 0, 0x7f, 4}, { 0, 0, 0x00, 0} },
- { {17, 7, 0x01, 0}, {18, 0, 0xff, 1}, {19, 0, 0x03, 9} },
- { {19, 2, 0x3f, 0}, {20, 0, 0x1f, 6}, { 0, 0, 0x00, 0} },
- { {20, 5, 0x07, 0}, {21, 0, 0xff, 3}, { 0, 0, 0x00, 0} }
- };
- AP_RCProtocol_SBUS::AP_RCProtocol_SBUS(AP_RCProtocol &_frontend, bool _inverted) :
- AP_RCProtocol_Backend(_frontend),
- inverted(_inverted)
- {}
- bool AP_RCProtocol_SBUS::sbus_decode(const uint8_t frame[25], uint16_t *values, uint16_t *num_values,
- bool *sbus_failsafe, bool *sbus_frame_drop, uint16_t max_values)
- {
-
- if ((frame[0] != 0x0f)) {
- return false;
- }
- switch (frame[24]) {
- case 0x00:
-
- break;
- case 0x03:
-
- break;
- case 0x83:
-
- break;
- case 0x43:
- case 0xC3:
- case 0x23:
- case 0xA3:
- case 0x63:
- case 0xE3:
- break;
- default:
-
- break;
- }
- unsigned chancount = (max_values > SBUS_INPUT_CHANNELS) ?
- SBUS_INPUT_CHANNELS : max_values;
-
- for (unsigned channel = 0; channel < chancount; channel++) {
- unsigned value = 0;
- for (unsigned pick = 0; pick < 3; pick++) {
- const struct sbus_bit_pick *decode = &sbus_decoder[channel][pick];
- if (decode->mask != 0) {
- unsigned piece = frame[1 + decode->byte];
- piece >>= decode->rshift;
- piece &= decode->mask;
- piece <<= decode->lshift;
- value |= piece;
- }
- }
-
- values[channel] = (uint16_t)(value * SBUS_SCALE_FACTOR +.5f) + SBUS_SCALE_OFFSET;
- }
-
- if (max_values > 17 && chancount > 15) {
- chancount = 18;
-
- values[16] = (frame[SBUS_FLAGS_BYTE] & (1 << 0))?1998:998;
-
- values[17] = (frame[SBUS_FLAGS_BYTE] & (1 << 1))?1998:998;
- }
-
- *num_values = chancount;
-
- if (frame[SBUS_FLAGS_BYTE] & (1 << SBUS_FAILSAFE_BIT)) {
-
- *sbus_failsafe = true;
- *sbus_frame_drop = true;
- } else if (frame[SBUS_FLAGS_BYTE] & (1 << SBUS_FRAMELOST_BIT)) {
-
- *sbus_failsafe = false;
- *sbus_frame_drop = true;
- } else {
- *sbus_failsafe = false;
- *sbus_frame_drop = false;
- }
- return true;
- }
- void AP_RCProtocol_SBUS::process_pulse(uint32_t width_s0, uint32_t width_s1)
- {
- uint32_t w0 = width_s0;
- uint32_t w1 = width_s1;
- if (inverted) {
- w0 = saved_width;
- w1 = width_s0;
- saved_width = width_s1;
- }
- uint8_t b;
- if (ss.process_pulse(w0, w1, b)) {
- _process_byte(ss.get_byte_timestamp_us(), b);
- }
- }
- void AP_RCProtocol_SBUS::_process_byte(uint32_t timestamp_us, uint8_t b)
- {
- const bool have_frame_gap = (timestamp_us - byte_input.last_byte_us >= 2000U);
- byte_input.last_byte_us = timestamp_us;
- if (have_frame_gap) {
-
-
- byte_input.ofs = 0;
- }
- if (b != 0x0F && byte_input.ofs == 0) {
-
- return;
- }
- if (byte_input.ofs == 0 && !have_frame_gap) {
-
- return;
- }
- byte_input.buf[byte_input.ofs++] = b;
- if (byte_input.ofs == sizeof(byte_input.buf)) {
- uint16_t values[SBUS_INPUT_CHANNELS];
- uint16_t num_values=0;
- bool sbus_failsafe = false;
- bool sbus_frame_drop = false;
- if (sbus_decode(byte_input.buf, values, &num_values,
- &sbus_failsafe, &sbus_frame_drop, SBUS_INPUT_CHANNELS) &&
- num_values >= MIN_RCIN_CHANNELS) {
- add_input(num_values, values, sbus_failsafe);
- }
- byte_input.ofs = 0;
- }
- }
- void AP_RCProtocol_SBUS::process_byte(uint8_t b, uint32_t baudrate)
- {
- if (baudrate != 100000) {
- return;
- }
- _process_byte(AP_HAL::micros(), b);
- }
|