2014-08-14 13:48:17 -03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-08-14 13:48:17 -03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2016-05-17 22:44:30 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-12-13 23:19:54 -04:00
|
|
|
#include <AP_HAL/utility/dsm.h>
|
2016-09-16 05:20:22 -03:00
|
|
|
#include <AP_HAL/utility/sumd.h>
|
|
|
|
#include <AP_HAL/utility/st24.h>
|
2016-10-14 02:14:07 -03:00
|
|
|
#include <AP_HAL/utility/srxl.h>
|
2014-10-07 22:49:42 -03:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include "RCInput.h"
|
|
|
|
#include "sbus.h"
|
|
|
|
|
2016-07-09 03:23:57 -03:00
|
|
|
#define MIN_NUM_CHANNELS 5
|
|
|
|
|
2014-10-07 22:49:42 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
using namespace Linux;
|
2014-08-19 00:37:10 -03:00
|
|
|
|
2017-01-07 21:37:07 -04:00
|
|
|
RCInput::RCInput()
|
2014-10-08 19:19:35 -03:00
|
|
|
{
|
|
|
|
ppm_state._channel_counter = -1;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2015-12-02 11:14:20 -04:00
|
|
|
void RCInput::init()
|
2014-08-14 13:48:17 -03:00
|
|
|
{
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
bool RCInput::new_input()
|
2014-06-27 07:29:14 -03:00
|
|
|
{
|
2017-01-07 21:37:07 -04:00
|
|
|
bool ret = rc_input_count != last_rc_input_count;
|
|
|
|
if (ret) {
|
2017-01-10 02:08:42 -04:00
|
|
|
last_rc_input_count.store(rc_input_count);
|
2017-01-07 21:37:07 -04:00
|
|
|
}
|
|
|
|
return ret;
|
2014-03-25 00:39:41 -03:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
uint8_t RCInput::num_channels()
|
2014-06-27 07:29:14 -03:00
|
|
|
{
|
2014-08-14 13:48:17 -03:00
|
|
|
return _num_channels;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
void RCInput::set_num_channels(uint8_t num)
|
|
|
|
{
|
|
|
|
_num_channels = num;
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
uint16_t RCInput::read(uint8_t ch)
|
2014-06-27 07:29:14 -03:00
|
|
|
{
|
2014-10-08 19:19:35 -03:00
|
|
|
if (ch >= _num_channels) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return _pwm_values[ch];
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
uint8_t RCInput::read(uint16_t* periods, uint8_t len)
|
2014-06-27 07:29:14 -03:00
|
|
|
{
|
2014-08-14 13:48:17 -03:00
|
|
|
uint8_t i;
|
|
|
|
for (i=0; i<len; i++) {
|
2016-04-26 20:48:35 -03:00
|
|
|
periods[i] = read(i);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2016-04-26 20:48:35 -03:00
|
|
|
return len;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2014-08-17 23:32:22 -03:00
|
|
|
/*
|
2014-10-06 23:24:23 -03:00
|
|
|
process a PPM-sum pulse of the given width
|
2014-08-17 23:32:22 -03:00
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCInput::_process_ppmsum_pulse(uint16_t width_usec)
|
2014-08-14 13:48:17 -03:00
|
|
|
{
|
2015-02-08 02:09:42 -04:00
|
|
|
if (width_usec >= 2700) {
|
2014-08-17 23:32:22 -03:00
|
|
|
// a long pulse indicates the end of a frame. Reset the
|
|
|
|
// channel counter so next pulse is channel 0
|
2016-07-09 03:23:57 -03:00
|
|
|
if (ppm_state._channel_counter >= MIN_NUM_CHANNELS) {
|
2014-10-08 19:19:35 -03:00
|
|
|
for (uint8_t i=0; i<ppm_state._channel_counter; i++) {
|
|
|
|
_pwm_values[i] = ppm_state._pulse_capt[i];
|
|
|
|
}
|
|
|
|
_num_channels = ppm_state._channel_counter;
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2014-08-17 23:32:22 -03:00
|
|
|
}
|
2014-10-08 19:19:35 -03:00
|
|
|
ppm_state._channel_counter = 0;
|
2014-08-17 23:32:22 -03:00
|
|
|
return;
|
|
|
|
}
|
2014-10-08 19:19:35 -03:00
|
|
|
if (ppm_state._channel_counter == -1) {
|
2014-08-17 23:32:22 -03:00
|
|
|
// we are not synchronised
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-06 23:24:23 -03:00
|
|
|
/*
|
|
|
|
we limit inputs to between 700usec and 2300usec. This allows us
|
|
|
|
to decode SBUS on the same pin, as SBUS will have a maximum
|
|
|
|
pulse width of 100usec
|
|
|
|
*/
|
|
|
|
if (width_usec > 700 && width_usec < 2300) {
|
|
|
|
// take a reading for the current channel
|
2014-10-08 19:19:35 -03:00
|
|
|
// buffer these
|
|
|
|
ppm_state._pulse_capt[ppm_state._channel_counter] = width_usec;
|
2014-10-06 23:24:23 -03:00
|
|
|
|
|
|
|
// move to next channel
|
2014-10-08 19:19:35 -03:00
|
|
|
ppm_state._channel_counter++;
|
2014-10-06 23:24:23 -03:00
|
|
|
}
|
2014-08-17 23:32:22 -03:00
|
|
|
|
|
|
|
// if we have reached the maximum supported channels then
|
|
|
|
// mark as unsynchronised, so we wait for a wide pulse
|
2014-10-08 19:19:35 -03:00
|
|
|
if (ppm_state._channel_counter >= LINUX_RC_INPUT_NUM_CHANNELS) {
|
|
|
|
for (uint8_t i=0; i<ppm_state._channel_counter; i++) {
|
|
|
|
_pwm_values[i] = ppm_state._pulse_capt[i];
|
|
|
|
}
|
|
|
|
_num_channels = ppm_state._channel_counter;
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2014-10-08 19:19:35 -03:00
|
|
|
ppm_state._channel_counter = -1;
|
2014-08-14 13:48:17 -03:00
|
|
|
}
|
2014-08-17 23:32:22 -03:00
|
|
|
}
|
|
|
|
|
2014-10-06 23:24:23 -03:00
|
|
|
/*
|
|
|
|
process a SBUS input pulse of the given width
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCInput::_process_sbus_pulse(uint16_t width_s0, uint16_t width_s1)
|
2014-10-06 23:24:23 -03:00
|
|
|
{
|
|
|
|
// convert to bit widths, allowing for up to 1usec error, assuming 100000 bps
|
|
|
|
uint16_t bits_s0 = (width_s0+1) / 10;
|
|
|
|
uint16_t bits_s1 = (width_s1+1) / 10;
|
|
|
|
uint16_t nlow;
|
|
|
|
|
|
|
|
uint8_t byte_ofs = sbus_state.bit_ofs/12;
|
|
|
|
uint8_t bit_ofs = sbus_state.bit_ofs%12;
|
|
|
|
|
|
|
|
if (bits_s0 == 0 || bits_s1 == 0) {
|
|
|
|
// invalid data
|
|
|
|
goto reset;
|
|
|
|
}
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2014-10-06 23:24:23 -03:00
|
|
|
if (bits_s0+bit_ofs > 10) {
|
|
|
|
// invalid data as last two bits must be stop bits
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pull in the high bits
|
|
|
|
sbus_state.bytes[byte_ofs] |= ((1U<<bits_s0)-1) << bit_ofs;
|
|
|
|
sbus_state.bit_ofs += bits_s0;
|
|
|
|
bit_ofs += bits_s0;
|
|
|
|
|
|
|
|
// pull in the low bits
|
|
|
|
nlow = bits_s1;
|
|
|
|
if (nlow + bit_ofs > 12) {
|
|
|
|
nlow = 12 - bit_ofs;
|
|
|
|
}
|
|
|
|
bits_s1 -= nlow;
|
|
|
|
sbus_state.bit_ofs += nlow;
|
|
|
|
|
|
|
|
if (sbus_state.bit_ofs == 25*12 && bits_s1 > 12) {
|
|
|
|
// we have a full frame
|
|
|
|
uint8_t bytes[25];
|
|
|
|
uint8_t i;
|
|
|
|
for (i=0; i<25; i++) {
|
|
|
|
// get inverted data
|
|
|
|
uint16_t v = ~sbus_state.bytes[i];
|
|
|
|
// check start bit
|
|
|
|
if ((v & 1) != 0) {
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
// check stop bits
|
|
|
|
if ((v & 0xC00) != 0xC00) {
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
// check parity
|
|
|
|
uint8_t parity = 0, j;
|
|
|
|
for (j=1; j<=8; j++) {
|
|
|
|
parity ^= (v & (1U<<j))?1:0;
|
|
|
|
}
|
|
|
|
if (parity != (v&0x200)>>9) {
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
bytes[i] = ((v>>1) & 0xFF);
|
|
|
|
}
|
|
|
|
uint16_t values[LINUX_RC_INPUT_NUM_CHANNELS];
|
|
|
|
uint16_t num_values=0;
|
|
|
|
bool sbus_failsafe=false, sbus_frame_drop=false;
|
2016-05-17 23:26:57 -03:00
|
|
|
if (sbus_decode(bytes, values, &num_values,
|
|
|
|
&sbus_failsafe, &sbus_frame_drop,
|
|
|
|
LINUX_RC_INPUT_NUM_CHANNELS) &&
|
2016-07-09 03:23:57 -03:00
|
|
|
num_values >= MIN_NUM_CHANNELS) {
|
2014-10-06 23:24:23 -03:00
|
|
|
for (i=0; i<num_values; i++) {
|
2014-10-08 19:19:35 -03:00
|
|
|
_pwm_values[i] = values[i];
|
2014-10-06 23:24:23 -03:00
|
|
|
}
|
|
|
|
_num_channels = num_values;
|
2016-10-16 16:53:33 -03:00
|
|
|
if (!sbus_failsafe) {
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2016-10-16 16:53:33 -03:00
|
|
|
}
|
2014-10-06 23:24:23 -03:00
|
|
|
}
|
|
|
|
goto reset;
|
|
|
|
} else if (bits_s1 > 12) {
|
|
|
|
// break
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
reset:
|
2016-05-17 23:26:57 -03:00
|
|
|
memset(&sbus_state, 0, sizeof(sbus_state));
|
2014-10-06 23:24:23 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCInput::_process_dsm_pulse(uint16_t width_s0, uint16_t width_s1)
|
2014-10-07 22:49:42 -03:00
|
|
|
{
|
|
|
|
// convert to bit widths, allowing for up to 1usec 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 = dsm_state.bit_ofs/10;
|
|
|
|
bit_ofs = dsm_state.bit_ofs%10;
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2014-10-27 17:17:17 -03:00
|
|
|
if(byte_ofs > 15) {
|
|
|
|
// invalid data
|
|
|
|
goto reset;
|
|
|
|
}
|
2014-10-07 22:49:42 -03:00
|
|
|
|
|
|
|
// pull in the high bits
|
|
|
|
nbits = bits_s0;
|
|
|
|
if (nbits+bit_ofs > 10) {
|
|
|
|
nbits = 10 - bit_ofs;
|
|
|
|
}
|
|
|
|
dsm_state.bytes[byte_ofs] |= ((1U<<nbits)-1) << bit_ofs;
|
|
|
|
dsm_state.bit_ofs += nbits;
|
|
|
|
bit_ofs += nbits;
|
|
|
|
|
|
|
|
if (bits_s0 - nbits > 10) {
|
|
|
|
if (dsm_state.bit_ofs == 16*10) {
|
|
|
|
// we have a full frame
|
|
|
|
uint8_t bytes[16];
|
|
|
|
uint8_t i;
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
|
// get raw data
|
|
|
|
uint16_t v = dsm_state.bytes[i];
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2014-10-07 22:49:42 -03:00
|
|
|
// check start bit
|
|
|
|
if ((v & 1) != 0) {
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
// check stop bits
|
|
|
|
if ((v & 0x200) != 0x200) {
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
bytes[i] = ((v>>1) & 0xFF);
|
|
|
|
}
|
|
|
|
uint16_t values[8];
|
|
|
|
uint16_t num_values=0;
|
2016-05-17 23:26:57 -03:00
|
|
|
if (dsm_decode(AP_HAL::micros64(), bytes, values, &num_values, 8) &&
|
2016-07-09 03:23:57 -03:00
|
|
|
num_values >= MIN_NUM_CHANNELS) {
|
2014-10-07 22:49:42 -03:00
|
|
|
for (i=0; i<num_values; i++) {
|
2014-10-08 19:19:35 -03:00
|
|
|
_pwm_values[i] = values[i];
|
2014-10-07 22:49:42 -03:00
|
|
|
}
|
2016-05-17 23:26:57 -03:00
|
|
|
_num_channels = num_values;
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2014-10-07 22:49:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(&dsm_state, 0, sizeof(dsm_state));
|
|
|
|
}
|
|
|
|
|
|
|
|
byte_ofs = dsm_state.bit_ofs/10;
|
|
|
|
bit_ofs = dsm_state.bit_ofs%10;
|
|
|
|
|
|
|
|
if (bits_s1+bit_ofs > 10) {
|
|
|
|
// invalid data
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pull in the low bits
|
|
|
|
dsm_state.bit_ofs += bits_s1;
|
|
|
|
return;
|
|
|
|
reset:
|
2016-05-17 23:26:57 -03:00
|
|
|
memset(&dsm_state, 0, sizeof(dsm_state));
|
2014-10-07 22:49:42 -03:00
|
|
|
}
|
|
|
|
|
2017-08-10 00:08:28 -03:00
|
|
|
void RCInput::_process_pwm_pulse(uint16_t channel, uint16_t width_s0, uint16_t width_s1)
|
|
|
|
{
|
|
|
|
if (channel < _num_channels) {
|
|
|
|
_pwm_values[channel] = width_s1; // range: 700usec ~ 2300usec
|
|
|
|
rc_input_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 23:24:23 -03:00
|
|
|
/*
|
|
|
|
process a RC input pulse of the given width
|
|
|
|
*/
|
2016-01-18 18:04:14 -04:00
|
|
|
void RCInput::_process_rc_pulse(uint16_t width_s0, uint16_t width_s1)
|
2014-10-06 23:24:23 -03:00
|
|
|
{
|
2014-10-07 22:49:42 -03:00
|
|
|
#if 0
|
|
|
|
// useful for debugging
|
|
|
|
static FILE *rclog;
|
2016-10-30 02:24:21 -03:00
|
|
|
if (rclog == nullptr) {
|
2014-10-07 22:49:42 -03:00
|
|
|
rclog = fopen("/tmp/rcin.log", "w");
|
|
|
|
}
|
|
|
|
if (rclog) {
|
|
|
|
fprintf(rclog, "%u %u\n", (unsigned)width_s0, (unsigned)width_s1);
|
|
|
|
}
|
|
|
|
#endif
|
2016-01-18 18:04:14 -04:00
|
|
|
// treat as PPM-sum
|
|
|
|
_process_ppmsum_pulse(width_s0 + width_s1);
|
2014-10-06 23:24:23 -03:00
|
|
|
|
2016-01-18 18:04:14 -04:00
|
|
|
// treat as SBUS
|
|
|
|
_process_sbus_pulse(width_s0, width_s1);
|
2014-10-07 22:49:42 -03:00
|
|
|
|
2016-01-18 18:04:14 -04:00
|
|
|
// treat as DSM
|
|
|
|
_process_dsm_pulse(width_s0, width_s1);
|
2014-10-06 23:24:23 -03:00
|
|
|
}
|
|
|
|
|
2015-09-04 13:37:09 -03:00
|
|
|
/*
|
|
|
|
* Update channel values directly
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void RCInput::_update_periods(uint16_t *periods, uint8_t len)
|
2015-09-04 13:37:09 -03:00
|
|
|
{
|
|
|
|
if (len > LINUX_RC_INPUT_NUM_CHANNELS) {
|
|
|
|
len = LINUX_RC_INPUT_NUM_CHANNELS;
|
|
|
|
}
|
|
|
|
for (unsigned int i=0; i < len; i++) {
|
|
|
|
_pwm_values[i] = periods[i];
|
|
|
|
}
|
|
|
|
_num_channels = len;
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2015-09-04 13:37:09 -03:00
|
|
|
}
|
|
|
|
|
2015-12-13 23:19:54 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
add some bytes of input in DSM serial stream format, coping with partial packets
|
|
|
|
*/
|
2016-10-14 02:22:47 -03:00
|
|
|
bool RCInput::add_dsm_input(const uint8_t *bytes, size_t nbytes)
|
2015-12-13 23:19:54 -04:00
|
|
|
{
|
|
|
|
if (nbytes == 0) {
|
2016-10-14 02:22:47 -03:00
|
|
|
return false;
|
2015-12-13 23:19:54 -04:00
|
|
|
}
|
|
|
|
const uint8_t dsm_frame_size = sizeof(dsm.frame);
|
2016-10-14 02:22:47 -03:00
|
|
|
bool ret = false;
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2015-12-13 23:19:54 -04:00
|
|
|
if (now - dsm.last_input_ms > 5) {
|
|
|
|
// resync based on time
|
|
|
|
dsm.partial_frame_count = 0;
|
|
|
|
}
|
|
|
|
dsm.last_input_ms = now;
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2015-12-13 23:19:54 -04:00
|
|
|
while (nbytes > 0) {
|
|
|
|
size_t n = nbytes;
|
|
|
|
if (dsm.partial_frame_count + n > dsm_frame_size) {
|
|
|
|
n = dsm_frame_size - dsm.partial_frame_count;
|
|
|
|
}
|
|
|
|
if (n > 0) {
|
|
|
|
memcpy(&dsm.frame[dsm.partial_frame_count], bytes, n);
|
|
|
|
dsm.partial_frame_count += n;
|
|
|
|
nbytes -= n;
|
|
|
|
bytes += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dsm.partial_frame_count == dsm_frame_size) {
|
|
|
|
dsm.partial_frame_count = 0;
|
|
|
|
uint16_t values[16] {};
|
|
|
|
uint16_t num_values=0;
|
2016-10-16 07:31:22 -03:00
|
|
|
/*
|
|
|
|
we only accept input when nbytes==0 as dsm is highly
|
|
|
|
sensitive to framing, and extra bytes may be an
|
|
|
|
indication this is really SRXL
|
|
|
|
*/
|
2015-12-13 23:19:54 -04:00
|
|
|
if (dsm_decode(AP_HAL::micros64(), dsm.frame, values, &num_values, 16) &&
|
2016-10-16 07:31:22 -03:00
|
|
|
num_values >= MIN_NUM_CHANNELS &&
|
|
|
|
nbytes == 0) {
|
2015-12-13 23:19:54 -04:00
|
|
|
for (uint8_t i=0; i<num_values; i++) {
|
|
|
|
if (values[i] != 0) {
|
|
|
|
_pwm_values[i] = values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
the apparent number of channels can change on DSM,
|
|
|
|
as they are spread across multiple frames. We just
|
|
|
|
use the max num_values we get
|
|
|
|
*/
|
|
|
|
if (num_values > _num_channels) {
|
|
|
|
_num_channels = num_values;
|
|
|
|
}
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2015-12-13 23:19:54 -04:00
|
|
|
#if 0
|
|
|
|
printf("Decoded DSM %u channels %u %u %u %u %u %u %u %u\n",
|
|
|
|
(unsigned)num_values,
|
|
|
|
values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7]);
|
|
|
|
#endif
|
2016-10-14 02:22:47 -03:00
|
|
|
ret = true;
|
2015-12-13 23:19:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 02:22:47 -03:00
|
|
|
return ret;
|
2015-12-13 23:19:54 -04:00
|
|
|
}
|
2016-06-07 02:06:35 -03:00
|
|
|
|
|
|
|
|
2016-09-16 05:20:22 -03:00
|
|
|
/*
|
|
|
|
add some bytes of input in SUMD serial stream format, coping with partial packets
|
|
|
|
*/
|
2016-10-14 02:22:47 -03:00
|
|
|
bool RCInput::add_sumd_input(const uint8_t *bytes, size_t nbytes)
|
2016-09-16 05:20:22 -03:00
|
|
|
{
|
|
|
|
uint16_t values[LINUX_RC_INPUT_NUM_CHANNELS];
|
|
|
|
uint8_t rssi;
|
|
|
|
uint8_t rx_count;
|
|
|
|
uint16_t channel_count;
|
2016-10-14 02:22:47 -03:00
|
|
|
bool ret = false;
|
|
|
|
|
2016-09-16 05:20:22 -03:00
|
|
|
while (nbytes > 0) {
|
|
|
|
if (sumd_decode(*bytes++, &rssi, &rx_count, &channel_count, values, LINUX_RC_INPUT_NUM_CHANNELS) == 0) {
|
|
|
|
if (channel_count > LINUX_RC_INPUT_NUM_CHANNELS) {
|
2016-10-14 02:22:47 -03:00
|
|
|
continue;
|
2016-09-16 05:20:22 -03:00
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<channel_count; i++) {
|
|
|
|
if (values[i] != 0) {
|
|
|
|
_pwm_values[i] = values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_num_channels = channel_count;
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2016-10-14 02:22:47 -03:00
|
|
|
ret = true;
|
2017-06-20 00:12:40 -03:00
|
|
|
_rssi = rssi;
|
2016-09-16 05:20:22 -03:00
|
|
|
}
|
|
|
|
nbytes--;
|
|
|
|
}
|
2016-10-14 02:22:47 -03:00
|
|
|
return ret;
|
2016-09-16 05:20:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
add some bytes of input in ST24 serial stream format, coping with partial packets
|
|
|
|
*/
|
2016-10-14 02:22:47 -03:00
|
|
|
bool RCInput::add_st24_input(const uint8_t *bytes, size_t nbytes)
|
2016-09-16 05:20:22 -03:00
|
|
|
{
|
|
|
|
uint16_t values[LINUX_RC_INPUT_NUM_CHANNELS];
|
|
|
|
uint8_t rssi;
|
|
|
|
uint8_t rx_count;
|
|
|
|
uint16_t channel_count;
|
2016-10-14 02:22:47 -03:00
|
|
|
bool ret = false;
|
|
|
|
|
2016-09-16 05:20:22 -03:00
|
|
|
while (nbytes > 0) {
|
|
|
|
if (st24_decode(*bytes++, &rssi, &rx_count, &channel_count, values, LINUX_RC_INPUT_NUM_CHANNELS) == 0) {
|
|
|
|
if (channel_count > LINUX_RC_INPUT_NUM_CHANNELS) {
|
2016-10-14 02:22:47 -03:00
|
|
|
continue;
|
2016-09-16 05:20:22 -03:00
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<channel_count; i++) {
|
|
|
|
if (values[i] != 0) {
|
|
|
|
_pwm_values[i] = values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_num_channels = channel_count;
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2016-10-14 02:22:47 -03:00
|
|
|
ret = true;
|
2017-06-20 00:12:40 -03:00
|
|
|
_rssi = rssi;
|
2016-09-16 05:20:22 -03:00
|
|
|
}
|
|
|
|
nbytes--;
|
|
|
|
}
|
2016-10-14 02:22:47 -03:00
|
|
|
return ret;
|
2016-09-16 05:20:22 -03:00
|
|
|
}
|
|
|
|
|
2016-10-14 02:14:07 -03:00
|
|
|
/*
|
|
|
|
add some bytes of input in SRXL serial stream format, coping with partial packets
|
|
|
|
*/
|
2016-10-14 02:22:47 -03:00
|
|
|
bool RCInput::add_srxl_input(const uint8_t *bytes, size_t nbytes)
|
2016-10-14 02:14:07 -03:00
|
|
|
{
|
|
|
|
uint16_t values[LINUX_RC_INPUT_NUM_CHANNELS];
|
|
|
|
uint8_t channel_count;
|
|
|
|
uint64_t now = AP_HAL::micros64();
|
2016-10-14 02:22:47 -03:00
|
|
|
bool ret = false;
|
2016-10-15 02:26:29 -03:00
|
|
|
bool failsafe_state;
|
2016-10-14 02:14:07 -03:00
|
|
|
|
|
|
|
while (nbytes > 0) {
|
2016-10-16 07:31:22 -03:00
|
|
|
if (srxl_decode(now, *bytes++, &channel_count, values, LINUX_RC_INPUT_NUM_CHANNELS, &failsafe_state) == 0) {
|
2016-10-14 02:14:07 -03:00
|
|
|
if (channel_count > LINUX_RC_INPUT_NUM_CHANNELS) {
|
2016-10-14 02:22:47 -03:00
|
|
|
continue;
|
2016-10-14 02:14:07 -03:00
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<channel_count; i++) {
|
|
|
|
_pwm_values[i] = values[i];
|
|
|
|
}
|
|
|
|
_num_channels = channel_count;
|
2016-10-16 07:31:22 -03:00
|
|
|
if (failsafe_state == false) {
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2016-10-16 07:31:22 -03:00
|
|
|
}
|
2016-10-14 02:22:47 -03:00
|
|
|
ret = true;
|
2016-10-14 02:14:07 -03:00
|
|
|
}
|
|
|
|
nbytes--;
|
|
|
|
}
|
2016-10-14 02:22:47 -03:00
|
|
|
return ret;
|
2016-10-14 02:14:07 -03:00
|
|
|
}
|
|
|
|
|
2016-09-16 05:20:22 -03:00
|
|
|
|
2016-06-07 02:06:35 -03:00
|
|
|
/*
|
|
|
|
add some bytes of input in SBUS serial stream format, coping with partial packets
|
|
|
|
*/
|
|
|
|
void RCInput::add_sbus_input(const uint8_t *bytes, size_t nbytes)
|
|
|
|
{
|
|
|
|
if (nbytes == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const uint8_t sbus_frame_size = sizeof(sbus.frame);
|
|
|
|
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - sbus.last_input_ms > 5) {
|
|
|
|
// resync based on time
|
|
|
|
sbus.partial_frame_count = 0;
|
|
|
|
}
|
|
|
|
sbus.last_input_ms = now;
|
|
|
|
|
|
|
|
while (nbytes > 0) {
|
|
|
|
size_t n = nbytes;
|
|
|
|
if (sbus.partial_frame_count + n > sbus_frame_size) {
|
|
|
|
n = sbus_frame_size - sbus.partial_frame_count;
|
|
|
|
}
|
|
|
|
if (n > 0) {
|
|
|
|
memcpy(&sbus.frame[sbus.partial_frame_count], bytes, n);
|
|
|
|
sbus.partial_frame_count += n;
|
|
|
|
nbytes -= n;
|
|
|
|
bytes += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sbus.partial_frame_count == sbus_frame_size) {
|
|
|
|
sbus.partial_frame_count = 0;
|
|
|
|
uint16_t values[16] {};
|
|
|
|
uint16_t num_values=0;
|
|
|
|
bool sbus_failsafe;
|
|
|
|
bool sbus_frame_drop;
|
|
|
|
if (sbus_decode(sbus.frame, values, &num_values, &sbus_failsafe, &sbus_frame_drop, 16) &&
|
2016-07-09 03:23:57 -03:00
|
|
|
num_values >= MIN_NUM_CHANNELS) {
|
2016-06-07 02:06:35 -03:00
|
|
|
for (uint8_t i=0; i<num_values; i++) {
|
|
|
|
if (values[i] != 0) {
|
|
|
|
_pwm_values[i] = values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
the apparent number of channels can change on SBUS,
|
|
|
|
as they are spread across multiple frames. We just
|
|
|
|
use the max num_values we get
|
|
|
|
*/
|
|
|
|
if (num_values > _num_channels) {
|
|
|
|
_num_channels = num_values;
|
|
|
|
}
|
2016-10-16 16:53:33 -03:00
|
|
|
if (!sbus_failsafe) {
|
2017-01-07 21:37:07 -04:00
|
|
|
rc_input_count++;
|
2016-10-16 16:53:33 -03:00
|
|
|
}
|
2016-06-07 02:06:35 -03:00
|
|
|
#if 0
|
2016-10-16 16:53:33 -03:00
|
|
|
printf("Decoded SBUS %u channels %u %u %u %u %u %u %u %u %s\n",
|
2016-06-07 02:06:35 -03:00
|
|
|
(unsigned)num_values,
|
2016-10-16 16:53:33 -03:00
|
|
|
values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7],
|
|
|
|
sbus_failsafe?"FAIL":"OK");
|
2016-06-07 02:06:35 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|