2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-12-30 19:00:04 -04:00
|
|
|
|
2014-07-06 23:03:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
2014-08-14 13:48:17 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdint.h>
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
#include "RCInput.h"
|
2014-10-06 23:24:23 -03:00
|
|
|
#include "sbus.h"
|
2014-10-07 22:49:42 -03:00
|
|
|
#include "dsm.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-06-27 07:29:14 -03:00
|
|
|
LinuxRCInput::LinuxRCInput() :
|
2014-10-08 19:19:35 -03:00
|
|
|
new_rc_input(false)
|
|
|
|
{
|
|
|
|
ppm_state._channel_counter = -1;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
void LinuxRCInput::init(void* machtnichts)
|
2014-08-14 13:48:17 -03:00
|
|
|
{
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2014-06-27 07:29:14 -03:00
|
|
|
bool LinuxRCInput::new_input()
|
|
|
|
{
|
|
|
|
return new_rc_input;
|
2014-03-25 00:39:41 -03:00
|
|
|
}
|
|
|
|
|
2014-06-27 07:29:14 -03:00
|
|
|
uint8_t LinuxRCInput::num_channels()
|
|
|
|
{
|
2014-08-14 13:48:17 -03:00
|
|
|
return _num_channels;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2014-06-27 07:29:14 -03:00
|
|
|
uint16_t LinuxRCInput::read(uint8_t ch)
|
|
|
|
{
|
|
|
|
new_rc_input = false;
|
|
|
|
if (_override[ch]) {
|
|
|
|
return _override[ch];
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-06-27 07:29:14 -03:00
|
|
|
uint8_t LinuxRCInput::read(uint16_t* periods, uint8_t len)
|
|
|
|
{
|
2014-08-14 13:48:17 -03:00
|
|
|
uint8_t i;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if((periods[i] = read(i))){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
break;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2014-08-14 13:48:17 -03:00
|
|
|
return (i+1);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2014-06-27 07:29:14 -03:00
|
|
|
bool LinuxRCInput::set_overrides(int16_t *overrides, uint8_t len)
|
|
|
|
{
|
|
|
|
bool res = false;
|
2014-08-14 13:48:17 -03:00
|
|
|
if(len > LINUX_RC_INPUT_NUM_CHANNELS){
|
|
|
|
len = LINUX_RC_INPUT_NUM_CHANNELS;
|
|
|
|
}
|
2014-06-27 07:29:14 -03:00
|
|
|
for (uint8_t i = 0; i < len; i++) {
|
|
|
|
res |= set_override(i, overrides[i]);
|
|
|
|
}
|
|
|
|
return res;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2014-06-27 07:29:14 -03:00
|
|
|
bool LinuxRCInput::set_override(uint8_t channel, int16_t override)
|
|
|
|
{
|
|
|
|
if (override < 0) return false; /* -1: no change. */
|
2014-08-14 13:48:17 -03:00
|
|
|
if (channel < LINUX_RC_INPUT_NUM_CHANNELS) {
|
2014-06-27 07:29:14 -03:00
|
|
|
_override[channel] = override;
|
|
|
|
if (override != 0) {
|
|
|
|
new_rc_input = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxRCInput::clear_overrides()
|
2014-06-27 07:29:14 -03:00
|
|
|
{
|
2014-08-14 13:48:17 -03:00
|
|
|
for (uint8_t i = 0; i < LINUX_RC_INPUT_NUM_CHANNELS; i++) {
|
|
|
|
_override[i] = 0;
|
2014-06-27 07:29:14 -03:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2014-08-19 00:37:10 -03:00
|
|
|
void LinuxRCInput::_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
|
2014-10-08 19:19:35 -03:00
|
|
|
if (ppm_state._channel_counter >= 5) {
|
|
|
|
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;
|
2014-08-17 23:32:22 -03:00
|
|
|
new_rc_input = true;
|
|
|
|
}
|
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;
|
2014-08-17 23:32:22 -03:00
|
|
|
new_rc_input = true;
|
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
|
|
|
|
*/
|
|
|
|
void LinuxRCInput::_process_sbus_pulse(uint16_t width_s0, uint16_t width_s1)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (sbus_decode(bytes, values, &num_values,
|
|
|
|
&sbus_failsafe, &sbus_frame_drop,
|
2014-10-08 19:19:35 -03:00
|
|
|
LINUX_RC_INPUT_NUM_CHANNELS) &&
|
|
|
|
num_values >= 5) {
|
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;
|
2014-10-08 19:19:35 -03:00
|
|
|
new_rc_input = true;
|
2014-10-06 23:24:23 -03:00
|
|
|
}
|
|
|
|
goto reset;
|
|
|
|
} else if (bits_s1 > 12) {
|
|
|
|
// break
|
|
|
|
goto reset;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
reset:
|
|
|
|
memset(&sbus_state, 0, sizeof(sbus_state));
|
|
|
|
}
|
|
|
|
|
2014-10-07 22:49:42 -03:00
|
|
|
void LinuxRCInput::_process_dsm_pulse(uint16_t width_s0, uint16_t width_s1)
|
|
|
|
{
|
|
|
|
// 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;
|
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];
|
|
|
|
|
|
|
|
// 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;
|
2014-10-08 19:19:35 -03:00
|
|
|
if (dsm_decode(hal.scheduler->micros64(), bytes, values, &num_values, 8) &&
|
|
|
|
num_values >= 5) {
|
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
|
|
|
}
|
|
|
|
_num_channels = num_values;
|
2014-10-08 19:19:35 -03:00
|
|
|
new_rc_input = true;
|
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:
|
|
|
|
memset(&dsm_state, 0, sizeof(dsm_state));
|
|
|
|
}
|
|
|
|
|
2014-10-06 23:24:23 -03:00
|
|
|
/*
|
|
|
|
process a RC input pulse of the given width
|
|
|
|
*/
|
|
|
|
void LinuxRCInput::_process_rc_pulse(uint16_t width_s0, uint16_t width_s1)
|
|
|
|
{
|
2014-10-07 22:49:42 -03:00
|
|
|
#if 0
|
|
|
|
// useful for debugging
|
|
|
|
static FILE *rclog;
|
|
|
|
if (rclog == NULL) {
|
|
|
|
rclog = fopen("/tmp/rcin.log", "w");
|
|
|
|
}
|
|
|
|
if (rclog) {
|
|
|
|
fprintf(rclog, "%u %u\n", (unsigned)width_s0, (unsigned)width_s1);
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-06 23:24:23 -03:00
|
|
|
// treat as PPM-sum
|
|
|
|
_process_ppmsum_pulse(width_s0 + width_s1);
|
|
|
|
|
|
|
|
// treat as SBUS
|
|
|
|
_process_sbus_pulse(width_s0, width_s1);
|
2014-10-07 22:49:42 -03:00
|
|
|
|
|
|
|
// treat as DSM
|
|
|
|
_process_dsm_pulse(width_s0, width_s1);
|
2014-10-06 23:24:23 -03:00
|
|
|
}
|
|
|
|
|
2013-12-30 19:00:04 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|