123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #include "AP_RCProtocol_IBUS.h"
- AP_RCProtocol_IBUS::AP_RCProtocol_IBUS(AP_RCProtocol &_frontend) :
- AP_RCProtocol_Backend(_frontend)
- {}
- bool AP_RCProtocol_IBUS::ibus_decode(const uint8_t frame[IBUS_FRAME_SIZE], uint16_t *values, bool *ibus_failsafe)
- {
- uint32_t chksum = 96;
-
- if ((frame[0] != 0x20) || (frame[1] != 0x40)) {
- return false;
- }
-
- for (uint8_t channel = 0, pick=2; channel < IBUS_INPUT_CHANNELS; channel++, pick+=2) {
- values[channel]=frame[pick]|(frame[pick+1] & 0x0F)<<8;
- chksum+=frame[pick]+frame[pick+1];
- }
- chksum += frame[IBUS_FRAME_SIZE-2]|frame[IBUS_FRAME_SIZE-1]<<8;
- if (chksum!=0xFFFF) {
- return false;
- }
- if ((frame[3]&0xF0) || (frame[9]&0xF0)) {
- *ibus_failsafe = true;
- } else {
- *ibus_failsafe = false;
- }
- return true;
- }
- void AP_RCProtocol_IBUS::process_pulse(uint32_t w0, uint32_t w1)
- {
- uint8_t b;
- if (ss.process_pulse(w0, w1, b)) {
- _process_byte(ss.get_byte_timestamp_us(), b);
- }
- }
- void AP_RCProtocol_IBUS::_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 != 0x20 && 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[IBUS_INPUT_CHANNELS];
- bool ibus_failsafe = false;
- if (ibus_decode(byte_input.buf, values, &ibus_failsafe)) {
- add_input(IBUS_INPUT_CHANNELS, values, ibus_failsafe);
- }
- byte_input.ofs = 0;
- }
- }
- void AP_RCProtocol_IBUS::process_byte(uint8_t b, uint32_t baudrate)
- {
- if (baudrate != 115200) {
- return;
- }
- _process_byte(AP_HAL::micros(), b);
- }
|