RC_Protocol: add detection of sumd protocol over sigread

This commit is contained in:
Siddharth Purohit 2018-07-12 20:19:32 +05:30 committed by Andrew Tridgell
parent bff985a9f4
commit f99bd32b51
6 changed files with 524 additions and 3 deletions

View File

@ -20,6 +20,8 @@
#include "AP_RCProtocol_DSM.h" #include "AP_RCProtocol_DSM.h"
#include "AP_RCProtocol_SBUS.h" #include "AP_RCProtocol_SBUS.h"
#include "AP_RCProtocol_SBUS_NI.h" #include "AP_RCProtocol_SBUS_NI.h"
#include "AP_RCProtocol_SUMD.h"
#include "AP_RCProtocol_SUMD_NI.h"
void AP_RCProtocol::init() void AP_RCProtocol::init()
{ {
@ -27,6 +29,7 @@ void AP_RCProtocol::init()
backend[AP_RCProtocol::SBUS] = new AP_RCProtocol_SBUS(*this); backend[AP_RCProtocol::SBUS] = new AP_RCProtocol_SBUS(*this);
backend[AP_RCProtocol::SBUS_NI] = new AP_RCProtocol_SBUS_NI(*this); backend[AP_RCProtocol::SBUS_NI] = new AP_RCProtocol_SBUS_NI(*this);
backend[AP_RCProtocol::DSM] = new AP_RCProtocol_DSM(*this); backend[AP_RCProtocol::DSM] = new AP_RCProtocol_DSM(*this);
backend[AP_RCProtocol::SUMD] = new AP_RCProtocol_SUMD(*this);
} }
void AP_RCProtocol::process_pulse(uint32_t width_s0, uint32_t width_s1) void AP_RCProtocol::process_pulse(uint32_t width_s0, uint32_t width_s1)
@ -55,6 +58,32 @@ void AP_RCProtocol::process_pulse(uint32_t width_s0, uint32_t width_s1)
} }
} }
void AP_RCProtocol::process_byte(uint8_t byte)
{
uint32_t now = AP_HAL::millis();
// first try current protocol
if (_detected_protocol != AP_RCProtocol::NONE && now - _last_input_ms < 200) {
backend[_detected_protocol]->process_byte(byte);
if (backend[_detected_protocol]->new_input()) {
_new_input = true;
_last_input_ms = AP_HAL::millis();
}
return;
}
// otherwise scan all protocols
for (uint8_t i = 0; i < AP_RCProtocol::NONE; i++) {
if (backend[i] != nullptr) {
backend[i]->process_byte(byte);
if (backend[i]->new_input()) {
_new_input = true;
_detected_protocol = (enum AP_RCProtocol::rcprotocol_t)i;
_last_input_ms = AP_HAL::millis();
}
}
}
}
bool AP_RCProtocol::new_input() bool AP_RCProtocol::new_input()
{ {
bool ret = _new_input; bool ret = _new_input;

View File

@ -18,7 +18,7 @@
#include <AP_HAL/AP_HAL.h> #include <AP_HAL/AP_HAL.h>
#include <AP_Common/AP_Common.h> #include <AP_Common/AP_Common.h>
#define MAX_RCIN_CHANNELS 16 #define MAX_RCIN_CHANNELS 32
#define MIN_RCIN_CHANNELS 5 #define MIN_RCIN_CHANNELS 5
class AP_RCProtocol_Backend; class AP_RCProtocol_Backend;
@ -29,10 +29,13 @@ public:
SBUS, SBUS,
SBUS_NI, SBUS_NI,
DSM, DSM,
SUMD,
NONE //last enum always is None NONE //last enum always is None
}; };
void init(); void init();
bool valid_serial_prot() { return _valid_serial_prot; }
void process_pulse(uint32_t width_s0, uint32_t width_s1); void process_pulse(uint32_t width_s0, uint32_t width_s1);
void process_byte(uint8_t byte);
enum rcprotocol_t protocol_detected() { return _detected_protocol; } enum rcprotocol_t protocol_detected() { return _detected_protocol; }
uint8_t num_channels(); uint8_t num_channels();
uint16_t read(uint8_t chan); uint16_t read(uint8_t chan);
@ -44,6 +47,7 @@ private:
AP_RCProtocol_Backend *backend[NONE]; AP_RCProtocol_Backend *backend[NONE];
bool _new_input = false; bool _new_input = false;
uint32_t _last_input_ms; uint32_t _last_input_ms;
bool _valid_serial_prot = false;
}; };
#include "AP_RCProtocol_Backend.h" #include "AP_RCProtocol_Backend.h"

View File

@ -25,7 +25,8 @@ class AP_RCProtocol_Backend
public: public:
AP_RCProtocol_Backend(AP_RCProtocol &_frontend); AP_RCProtocol_Backend(AP_RCProtocol &_frontend);
virtual void process_pulse(uint32_t width_s0, uint32_t width_s1) = 0; virtual void process_pulse(uint32_t width_s0, uint32_t width_s1) {}
virtual void process_byte(uint8_t byte) {}
uint16_t read(uint8_t chan); uint16_t read(uint8_t chan);
bool new_input(); bool new_input();
uint8_t num_channels(); uint8_t num_channels();
@ -35,7 +36,10 @@ public:
// allow for backends that need regular polling // allow for backends that need regular polling
virtual void update(void) {} virtual void update(void) {}
enum {
PARSE_TYPE_SIGREAD,
PARSE_TYPE_SERIAL
};
protected: protected:
void add_input(uint8_t num_channels, uint16_t *values, bool in_failsafe); void add_input(uint8_t num_channels, uint16_t *values, bool in_failsafe);

View File

@ -0,0 +1,384 @@
/*
SUMD decoder, based on PX4Firmware/src/rc/lib/rc/sumd.c from PX4Firmware
modified for use in AP_HAL_* by Andrew Tridgell
*/
/****************************************************************************
*
* Copyright (c) 2015 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/*
* @file sumd.h
*
* RC protocol definition for Graupner HoTT transmitter (SUMD/SUMH Protocol)
*
* @author Marco Bauer <marco@wtns.de>
*/
#include "AP_RCProtocol_SUMD.h"
#define SUMD_HEADER_LENGTH 3
#define SUMD_HEADER_ID 0xA8
#define SUMD_ID_SUMH 0x00
#define SUMD_ID_SUMD 0x01
#define SUMD_ID_FAILSAFE 0x81
/* define range mapping here, -+100% -> 1000..2000 */
#define SUMD_RANGE_MIN 0.0f
#define SUMD_RANGE_MAX 4096.0f
#define SUMD_TARGET_MIN 1000.0f
#define SUMD_TARGET_MAX 2000.0f
/* pre-calculate the floating point stuff as far as possible at compile time */
#define SUMD_SCALE_FACTOR ((SUMD_TARGET_MAX - SUMD_TARGET_MIN) / (SUMD_RANGE_MAX - SUMD_RANGE_MIN))
#define SUMD_SCALE_OFFSET (int)(SUMD_TARGET_MIN - (SUMD_SCALE_FACTOR * SUMD_RANGE_MIN + 0.5f))
// #define SUMD_DEBUG
extern const AP_HAL::HAL& hal;
uint16_t AP_RCProtocol_SUMD::sumd_crc16(uint16_t crc, uint8_t value)
{
int i;
crc ^= (uint16_t)value << 8;
for (i = 0; i < 8; i++) {
crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : (crc << 1);
}
return crc;
}
uint8_t AP_RCProtocol_SUMD::sumd_crc8(uint8_t crc, uint8_t value)
{
crc += value;
return crc;
}
void AP_RCProtocol_SUMD::process_pulse(uint32_t width_s0, uint32_t width_s1)
{
// convert to bit widths, allowing for up to about 4usec error, assuming 115200 bps
uint16_t bits_s0 = ((width_s0+4)*(uint32_t)115200) / 1000000;
uint16_t bits_s1 = ((width_s1+4)*(uint32_t)115200) / 1000000;
uint8_t bit_ofs, byte_ofs;
uint16_t nbits;
if (bits_s0 == 0 || bits_s1 == 0) {
// invalid data
goto reset;
}
byte_ofs = sumd_state.bit_ofs/10;
bit_ofs = sumd_state.bit_ofs%10;
// pull in the high bits
nbits = bits_s0;
if (nbits+bit_ofs > 10) {
nbits = 10 - bit_ofs;
}
sumd_state.bytes[byte_ofs] |= ((1U<<nbits)-1) << bit_ofs;
sumd_state.bit_ofs += nbits;
bit_ofs += nbits;
if (bits_s0 - nbits > 10) {
// we have a full frame
uint8_t byte;
uint8_t i;
for (i=0; i <= byte_ofs; i++) {
// get raw data
uint16_t v = sumd_state.bytes[i];
// check start bit
if ((v & 1) != 0) {
break;
}
// check stop bits
if ((v & 0x200) != 0x200) {
break;
}
byte = ((v>>1) & 0xFF);
process_byte(byte);
}
memset(&sumd_state, 0, sizeof(sumd_state));
}
byte_ofs = sumd_state.bit_ofs/10;
bit_ofs = sumd_state.bit_ofs%10;
if (bits_s1+bit_ofs > 10) {
// invalid data
goto reset;
}
// pull in the low bits
sumd_state.bit_ofs += bits_s1;
return;
reset:
memset(&sumd_state, 0, sizeof(sumd_state));
}
void AP_RCProtocol_SUMD::process_byte(uint8_t byte)
{
switch (_decode_state) {
case SUMD_DECODE_STATE_UNSYNCED:
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_UNSYNCED \n") ;
#endif
if (byte == SUMD_HEADER_ID) {
_rxpacket.header = byte;
_sumd = true;
_rxlen = 0;
_crc16 = 0x0000;
_crc8 = 0x00;
_crcOK = false;
_crc16 = sumd_crc16(_crc16, byte);
_crc8 = sumd_crc8(_crc8, byte);
_decode_state = SUMD_DECODE_STATE_GOT_HEADER;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_HEADER: %x \n", byte) ;
#endif
}
break;
case SUMD_DECODE_STATE_GOT_HEADER:
if (byte == SUMD_ID_SUMD || byte == SUMD_ID_SUMH) {
_rxpacket.status = byte;
if (byte == SUMD_ID_SUMH) {
_sumd = false;
}
if (_sumd) {
_crc16 = sumd_crc16(_crc16, byte);
} else {
_crc8 = sumd_crc8(_crc8, byte);
}
_decode_state = SUMD_DECODE_STATE_GOT_STATE;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_STATE: %x \n", byte) ;
#endif
} else {
_decode_state = SUMD_DECODE_STATE_UNSYNCED;
}
break;
case SUMD_DECODE_STATE_GOT_STATE:
if (byte >= 2 && byte <= SUMD_MAX_CHANNELS) {
_rxpacket.length = byte;
if (_sumd) {
_crc16 = sumd_crc16(_crc16, byte);
} else {
_crc8 = sumd_crc8(_crc8, byte);
}
_rxlen++;
_decode_state = SUMD_DECODE_STATE_GOT_LEN;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_LEN: %x (%d) \n", byte, byte) ;
#endif
} else {
_decode_state = SUMD_DECODE_STATE_UNSYNCED;
}
break;
case SUMD_DECODE_STATE_GOT_LEN:
_rxpacket.sumd_data[_rxlen] = byte;
if (_sumd) {
_crc16 = sumd_crc16(_crc16, byte);
} else {
_crc8 = sumd_crc8(_crc8, byte);
}
_rxlen++;
if (_rxlen <= ((_rxpacket.length * 2))) {
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_DATA[%d]: %x\n", _rxlen - 2, byte) ;
#endif
} else {
_decode_state = SUMD_DECODE_STATE_GOT_DATA;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_DATA -- finish --\n") ;
#endif
}
break;
case SUMD_DECODE_STATE_GOT_DATA:
_rxpacket.crc16_high = byte;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC16[1]: %x [%x]\n", byte, ((_crc16 >> 8) & 0xff)) ;
#endif
if (_sumd) {
_decode_state = SUMD_DECODE_STATE_GOT_CRC;
} else {
_decode_state = SUMD_DECODE_STATE_GOT_CRC16_BYTE_1;
}
break;
case SUMD_DECODE_STATE_GOT_CRC16_BYTE_1:
_rxpacket.crc16_low = byte;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC16[2]: %x [%x]\n", byte, (_crc16 & 0xff)) ;
#endif
_decode_state = SUMD_DECODE_STATE_GOT_CRC16_BYTE_2;
break;
case SUMD_DECODE_STATE_GOT_CRC16_BYTE_2:
_rxpacket.telemetry = byte;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_SUMH_TELEMETRY: %x\n", byte) ;
#endif
_decode_state = SUMD_DECODE_STATE_GOT_CRC;
break;
case SUMD_DECODE_STATE_GOT_CRC:
if (_sumd) {
_rxpacket.crc16_low = byte;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC[2]: %x [%x]\n\n", byte, (_crc16 & 0xff)) ;
#endif
if (_crc16 == (uint16_t)(_rxpacket.crc16_high << 8) + _rxpacket.crc16_low) {
_crcOK = true;
}
} else {
_rxpacket.crc8 = byte;
#ifdef SUMD_DEBUG
hal.console->printf(" SUMD_DECODE_STATE_GOT_CRC8_SUMH: %x [%x]\n\n", byte, _crc8) ;
#endif
if (_crc8 == _rxpacket.crc8) {
_crcOK = true;
}
}
if (_crcOK) {
#ifdef SUMD_DEBUG
hal.console->printf(" CRC - OK \n") ;
#endif
if (_sumd) {
#ifdef SUMD_DEBUG
hal.console->printf(" Got valid SUMD Packet\n") ;
#endif
} else {
#ifdef SUMD_DEBUG
hal.console->printf(" Got valid SUMH Packet\n") ;
#endif
}
#ifdef SUMD_DEBUG
hal.console->printf(" RXLEN: %d [Chans: %d] \n\n", _rxlen - 1, (_rxlen - 1) / 2) ;
#endif
unsigned i;
uint8_t num_values;
uint16_t values[SUMD_MAX_CHANNELS];
/* received Channels */
if ((uint16_t)_rxpacket.length > SUMD_MAX_CHANNELS) {
_rxpacket.length = (uint8_t) SUMD_MAX_CHANNELS;
}
num_values = (uint16_t)_rxpacket.length;
/* decode the actual packet */
/* reorder first 4 channels */
/* ch1 = roll -> sumd = ch2 */
values[0] = (uint16_t)((_rxpacket.sumd_data[1 * 2 + 1] << 8) | _rxpacket.sumd_data[1 * 2 + 2]) >> 3;
/* ch2 = pitch -> sumd = ch2 */
values[1] = (uint16_t)((_rxpacket.sumd_data[2 * 2 + 1] << 8) | _rxpacket.sumd_data[2 * 2 + 2]) >> 3;
/* ch3 = throttle -> sumd = ch2 */
values[2] = (uint16_t)((_rxpacket.sumd_data[0 * 2 + 1] << 8) | _rxpacket.sumd_data[0 * 2 + 2]) >> 3;
/* ch4 = yaw -> sumd = ch2 */
values[3] = (uint16_t)((_rxpacket.sumd_data[3 * 2 + 1] << 8) | _rxpacket.sumd_data[3 * 2 + 2]) >> 3;
/* we start at channel 5(index 4) */
unsigned chan_index = 4;
for (i = 4; i < _rxpacket.length; i++) {
#ifdef SUMD_DEBUG
hal.console->printf("ch[%d] : %x %x [ %x %d ]\n", i + 1, _rxpacket.sumd_data[i * 2 + 1], _rxpacket.sumd_data[i * 2 + 2],
((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3,
((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3);
#endif
values[chan_index] = (uint16_t)((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3;
/* convert values to 1000-2000 ppm encoding in a not too sloppy fashion */
//channels[chan_index] = (uint16_t)(channels[chan_index] * SUMD_SCALE_FACTOR + .5f) + SUMD_SCALE_OFFSET;
chan_index++;
}
if (_rxpacket.status == 0x01) {
add_input(num_values, values, false);
} else if (_rxpacket.status == 0x81) {
add_input(num_values, values, true);
}
} else {
#ifdef SUMD_DEBUG
hal.console->printf(" CRC - fail 0x%X 0x%X\n", _crc16, (uint16_t)(_rxpacket.crc16_high << 8) + _rxpacket.crc16_low);
#endif
}
_decode_state = SUMD_DECODE_STATE_UNSYNCED;
break;
}
}

View File

@ -0,0 +1,69 @@
/*
* This file is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Code by Andrew Tridgell and Siddharth Bharat Purohit
*/
#pragma once
#include "AP_RCProtocol.h"
#define SUMD_MAX_CHANNELS 32
class AP_RCProtocol_SUMD : public AP_RCProtocol_Backend {
public:
AP_RCProtocol_SUMD(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {}
void process_pulse(uint32_t width_s0, uint32_t width_s1) override;
void process_byte(uint8_t byte) override;
private:
static uint16_t sumd_crc16(uint16_t crc, uint8_t value);
static uint8_t sumd_crc8(uint8_t crc, uint8_t value);
#pragma pack(push, 1)
typedef struct {
uint8_t header; ///< 0xA8 for a valid packet
uint8_t status; ///< 0x01 valid and live SUMD data frame / 0x00 = SUMH / 0x81 = Failsafe
uint8_t length; ///< Channels
uint8_t sumd_data[SUMD_MAX_CHANNELS * 2]; ///< ChannelData (High Byte/ Low Byte)
uint8_t crc16_high; ///< High Byte of 16 Bit CRC
uint8_t crc16_low; ///< Low Byte of 16 Bit CRC
uint8_t telemetry; ///< Telemetry request
uint8_t crc8; ///< SUMH CRC8
} ReceiverFcPacketHoTT;
#pragma pack(pop)
enum SUMD_DECODE_STATE {
SUMD_DECODE_STATE_UNSYNCED = 0,
SUMD_DECODE_STATE_GOT_HEADER,
SUMD_DECODE_STATE_GOT_STATE,
SUMD_DECODE_STATE_GOT_LEN,
SUMD_DECODE_STATE_GOT_DATA,
SUMD_DECODE_STATE_GOT_CRC,
SUMD_DECODE_STATE_GOT_CRC16_BYTE_1,
SUMD_DECODE_STATE_GOT_CRC16_BYTE_2
};
enum SUMD_DECODE_STATE _decode_state = SUMD_DECODE_STATE_UNSYNCED;
uint8_t _rxlen;
ReceiverFcPacketHoTT _rxpacket;
uint8_t _crc8 = 0x00;
uint16_t _crc16 = 0x0000;
bool _sumd = true;
bool _crcOK = false;
struct {
uint16_t bytes[40];
uint8_t bit_ofs;
bool packet_parsed;
} sumd_state;
};

View File

@ -0,0 +1,31 @@
/*
* This file is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "AP_RCProtocol.h"
#include "AP_RCProtocol_SUMD.h"
class AP_RCProtocol_SUMD_NI : public AP_RCProtocol_SUMD {
public:
AP_RCProtocol_SUMD_NI(AP_RCProtocol &_frontend) : AP_RCProtocol_SUMD(_frontend), saved_width(0) {}
void process_pulse(uint32_t width_s0, uint32_t width_s1) override {
AP_RCProtocol_SUMD::process_pulse(saved_width, width_s0);
saved_width = width_s1;
}
private:
uint32_t saved_width;
};