From bd354f11897d6b6d6fc45c190fbcfb73e716385c Mon Sep 17 00:00:00 2001 From: Shashank Agarwal <48916902+Witty-Wizard@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:42:39 +0530 Subject: [PATCH] [FIXED]: IBus decoding Bug Created 3 new struct for decoding different packtes of IBus, SBus, CRSF --- examples/atmel/ibus_basic/ibus_basic.ino | 2 +- examples/espresiff/ibus_basic/ibus_basic.ino | 2 +- examples/espresiff/sbus_basic/sbus_basic.ino | 2 +- src/SerialIO.h | 11 -------- src/crsf.h | 3 +- src/ibus.cpp | 6 ++-- src/ibus.h | 22 ++++++++++++++- src/sbus.cpp | 4 +-- src/sbus.h | 29 +++++++++++++++++++- 9 files changed, 60 insertions(+), 21 deletions(-) diff --git a/examples/atmel/ibus_basic/ibus_basic.ino b/examples/atmel/ibus_basic/ibus_basic.ino index b8051c0..8e57969 100644 --- a/examples/atmel/ibus_basic/ibus_basic.ino +++ b/examples/atmel/ibus_basic/ibus_basic.ino @@ -3,7 +3,7 @@ */ #include -crsf_channels_t channelData; +ibus_channels_t channelData; ibus receiver(&Serial); diff --git a/examples/espresiff/ibus_basic/ibus_basic.ino b/examples/espresiff/ibus_basic/ibus_basic.ino index b6bfdc3..e80a03a 100644 --- a/examples/espresiff/ibus_basic/ibus_basic.ino +++ b/examples/espresiff/ibus_basic/ibus_basic.ino @@ -4,7 +4,7 @@ #include #define RX_PIN 16 -crsf_channels_t channelData; +ibus_channels_t channelData; ibus receiver(&Serial1,RX_PIN); diff --git a/examples/espresiff/sbus_basic/sbus_basic.ino b/examples/espresiff/sbus_basic/sbus_basic.ino index 973d3c0..df87be3 100644 --- a/examples/espresiff/sbus_basic/sbus_basic.ino +++ b/examples/espresiff/sbus_basic/sbus_basic.ino @@ -4,7 +4,7 @@ #include #define RX_PIN 16 -crsf_channels_t channelData; +sbus_channels_t channelData; sbus receiver(&Serial1,RX_PIN); diff --git a/src/SerialIO.h b/src/SerialIO.h index 109a806..257de3c 100644 --- a/src/SerialIO.h +++ b/src/SerialIO.h @@ -43,19 +43,8 @@ class SerialIO { */ /**************************************************************************/ virtual void processIncoming() = 0; - /**************************************************************************/ - /*! - @brief Gives the decoded rc channels - @param channelData - A pointer to a crsf_channels_t struct where the decoded channel - data will be stored. - - */ - /**************************************************************************/ - virtual void getChannel(crsf_channels_t *channelData) = 0; protected: -crsf_channels_t channelData; Stream *_rxPort; // Pointer to the hardware serial port used for communication. bool _headerDetected; // Flag indicating whether a header has been detected diff --git a/src/crsf.h b/src/crsf.h index 989c09b..c6c7d52 100644 --- a/src/crsf.h +++ b/src/crsf.h @@ -16,6 +16,7 @@ */ class crsf : public SerialIO { private: + crsf_channels_t channelData; uint8_t _rxData[CRSF_MAX_PACKET_SIZE]; public: @@ -45,7 +46,7 @@ class crsf : public SerialIO { * @param channelData Pointer to a crsf_channels_t struct where the decoded * channel data will be stored. */ - void getChannel(crsf_channels_t *channelData) override; + void getChannel(crsf_channels_t *channelData); uint8_t crc8(uint8_t *data, uint8_t len); }; diff --git a/src/ibus.cpp b/src/ibus.cpp index 52f54f9..f5b9202 100644 --- a/src/ibus.cpp +++ b/src/ibus.cpp @@ -43,7 +43,8 @@ void ibus::processIncoming() { } } - if (ibus::checkSum()) { + if (checkSum()) { + channelData.header = (_rxData[2] << 8) | _rxData[0]; channelData.channel1 = (_rxData[3] << 8) | _rxData[2]; channelData.channel2 = (_rxData[5] << 8) | _rxData[4]; channelData.channel3 = (_rxData[7] << 8) | _rxData[6]; @@ -58,10 +59,11 @@ void ibus::processIncoming() { channelData.channel12 = (_rxData[25] << 8) | _rxData[24]; channelData.channel13 = (_rxData[27] << 8) | _rxData[26]; channelData.channel14 = (_rxData[29] << 8) | _rxData[28]; + channelData.checksum = (_rxData[31] << 8) | _rxData[30]; } } -void ibus::getChannel(crsf_channels_t *channelData) { +void ibus::getChannel(ibus_channels_t *channelData) { *channelData = this->channelData; } diff --git a/src/ibus.h b/src/ibus.h index 4e289cb..5fefc15 100644 --- a/src/ibus.h +++ b/src/ibus.h @@ -14,11 +14,31 @@ #define IBUS_HEADER1 0x20 #define IBUS_HEADER2 0x40 +typedef struct ibus_channels_s { + unsigned header : 16; + unsigned channel1 : 16; + unsigned channel2 : 16; + unsigned channel3 : 16; + unsigned channel4 : 16; + unsigned channel5 : 16; + unsigned channel6 : 16; + unsigned channel7 : 16; + unsigned channel8 : 16; + unsigned channel9 : 16; + unsigned channel10 : 16; + unsigned channel11 : 16; + unsigned channel12 : 16; + unsigned channel13 : 16; + unsigned channel14 : 16; + unsigned checksum : 16; +} PACKED ibus_channels_t; + /** * @brief A class for handling IBUS protocol communication. */ class ibus : public SerialIO { private: + ibus_channels_t channelData; uint8_t _rxData[IBUS_MAX_PACKET_SIZE]; ///< Buffer to store received IBUS data bool checkSum(); @@ -49,7 +69,7 @@ class ibus : public SerialIO { * @param channelData Pointer to a crsf_channels_t struct where the decoded * channel data will be stored. */ - void getChannel(crsf_channels_t *channelData) override; + void getChannel(ibus_channels_t *channelData); }; #endif // IBUS_H diff --git a/src/sbus.cpp b/src/sbus.cpp index 06f5844..f181f5d 100644 --- a/src/sbus.cpp +++ b/src/sbus.cpp @@ -43,9 +43,9 @@ void sbus::processIncoming() { _headerDetected = false; } } - memcpy(&channelData, _rxData + 1, sizeof(channelData)); + memcpy(&channelData, _rxData, sizeof(channelData)); } -void sbus::getChannel(crsf_channels_t *channelData) { +void sbus::getChannel(sbus_channels_t *channelData) { *channelData = this->channelData; } \ No newline at end of file diff --git a/src/sbus.h b/src/sbus.h index 5d5a954..f540a99 100644 --- a/src/sbus.h +++ b/src/sbus.h @@ -14,11 +14,38 @@ #define SBUS_BAUDRATE 100000 ///< SBUS baudrate #define SBUS_MAX_PACKET_SIZE 25 ///< SBUS packet length +typedef struct sbus_channels_s { + unsigned header : 8; + unsigned channel1 : 11; + unsigned channel2 : 11; + unsigned channel3 : 11; + unsigned channel4 : 11; + unsigned channel5 : 11; + unsigned channel6 : 11; + unsigned channel7 : 11; + unsigned channel8 : 11; + unsigned channel9 : 11; + unsigned channel10 : 11; + unsigned channel11 : 11; + unsigned channel12 : 11; + unsigned channel13 : 11; + unsigned channel14 : 11; + unsigned channel15 : 11; + unsigned channel16 : 11; + unsigned dummy : 4; + unsigned failsafe : 1; + unsigned framelost : 1; + unsigned channel17 : 1; + unsigned channel18 : 1; + unsigned footer : 8; +} PACKED sbus_channels_t; + /** * @brief A class for handling SBUS protocol communication. */ class sbus : public SerialIO { private: + sbus_channels_t channelData; uint8_t _rxData[SBUS_MAX_PACKET_SIZE]; public: @@ -48,7 +75,7 @@ class sbus : public SerialIO { * @param channelData Pointer to a crsf_channels_t struct where the decoded * channel data will be stored. */ - void getChannel(crsf_channels_t *channelData) override; + void getChannel(sbus_channels_t *channelData); }; #endif // SBUS_H