2012-11-07 06:47:01 -04:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 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 dsm.c
|
|
|
|
*
|
|
|
|
* Serial protocol decoder for the Spektrum DSM* family of protocols.
|
|
|
|
*
|
|
|
|
* Decodes into the global PPM buffer and updates accordingly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2012-11-30 04:02:47 -04:00
|
|
|
#include <termios.h>
|
2012-11-07 06:47:01 -04:00
|
|
|
|
|
|
|
#include <drivers/drv_hrt.h>
|
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
#define DEBUG
|
|
|
|
|
2012-11-07 06:47:01 -04:00
|
|
|
#include "px4io.h"
|
|
|
|
|
|
|
|
#define DSM_FRAME_SIZE 16
|
|
|
|
#define DSM_FRAME_CHANNELS 7
|
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
static int dsm_fd = -1;
|
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
static hrt_abstime last_rx_time;
|
2012-11-07 06:47:01 -04:00
|
|
|
static hrt_abstime last_frame_time;
|
|
|
|
|
|
|
|
static uint8_t frame[DSM_FRAME_SIZE];
|
|
|
|
|
|
|
|
static unsigned partial_frame_count;
|
|
|
|
static unsigned channel_shift;
|
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
unsigned dsm_frame_drops;
|
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
static bool dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value);
|
|
|
|
static void dsm_guess_format(bool reset);
|
2013-01-13 22:57:27 -04:00
|
|
|
static bool dsm_decode(hrt_abstime now, uint16_t *values, uint16_t *num_values);
|
2012-11-07 06:47:01 -04:00
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
int
|
|
|
|
dsm_init(const char *device)
|
2012-11-07 06:47:01 -04:00
|
|
|
{
|
2012-11-30 04:02:47 -04:00
|
|
|
if (dsm_fd < 0)
|
2013-01-13 22:57:27 -04:00
|
|
|
dsm_fd = open(device, O_RDONLY | O_NONBLOCK);
|
2012-11-07 06:47:01 -04:00
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
if (dsm_fd >= 0) {
|
|
|
|
struct termios t;
|
|
|
|
|
|
|
|
/* 115200bps, no parity, one stop bit */
|
|
|
|
tcgetattr(dsm_fd, &t);
|
|
|
|
cfsetspeed(&t, 115200);
|
|
|
|
t.c_cflag &= ~(CSTOPB | PARENB);
|
|
|
|
tcsetattr(dsm_fd, TCSANOW, &t);
|
|
|
|
|
|
|
|
/* initialise the decoder */
|
|
|
|
partial_frame_count = 0;
|
|
|
|
last_rx_time = hrt_absolute_time();
|
|
|
|
|
|
|
|
/* reset the format detector */
|
|
|
|
dsm_guess_format(true);
|
2012-11-07 06:47:01 -04:00
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
debug("DSM: ready");
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
} else {
|
|
|
|
debug("DSM: open failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return dsm_fd;
|
2012-11-07 06:47:01 -04:00
|
|
|
}
|
|
|
|
|
2012-12-04 13:52:16 -04:00
|
|
|
bool
|
2013-01-13 22:57:27 -04:00
|
|
|
dsm_input(uint16_t *values, uint16_t *num_values)
|
2012-11-07 06:47:01 -04:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
hrt_abstime now;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The DSM* protocol doesn't provide any explicit framing,
|
|
|
|
* so we detect frame boundaries by the inter-frame delay.
|
|
|
|
*
|
|
|
|
* The minimum frame spacing is 11ms; with 16 bytes at 115200bps
|
|
|
|
* frame transmission time is ~1.4ms.
|
|
|
|
*
|
|
|
|
* We expect to only be called when bytes arrive for processing,
|
2012-12-29 20:01:24 -04:00
|
|
|
* and if an interval of more than 5ms passes between calls,
|
2012-11-07 06:47:01 -04:00
|
|
|
* the first byte we read will be the first byte of a frame.
|
2012-11-29 04:35:21 -04:00
|
|
|
*
|
|
|
|
* In the case where byte(s) are dropped from a frame, this also
|
|
|
|
* provides a degree of protection. Of course, it would be better
|
|
|
|
* if we didn't drop bytes...
|
2012-11-07 06:47:01 -04:00
|
|
|
*/
|
|
|
|
now = hrt_absolute_time();
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
if ((now - last_rx_time) > 5000) {
|
2012-11-30 04:02:47 -04:00
|
|
|
if (partial_frame_count > 0) {
|
|
|
|
dsm_frame_drops++;
|
|
|
|
partial_frame_count = 0;
|
|
|
|
}
|
2012-11-29 04:35:21 -04:00
|
|
|
}
|
2012-11-07 06:47:01 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch bytes, but no more than we would need to complete
|
|
|
|
* the current frame.
|
|
|
|
*/
|
2012-11-30 04:02:47 -04:00
|
|
|
ret = read(dsm_fd, &frame[partial_frame_count], DSM_FRAME_SIZE - partial_frame_count);
|
2012-11-07 06:47:01 -04:00
|
|
|
|
|
|
|
/* if the read failed for any reason, just give up here */
|
|
|
|
if (ret < 1)
|
2013-01-13 22:57:27 -04:00
|
|
|
return false;
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
last_rx_time = now;
|
2012-11-07 06:47:01 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add bytes to the current frame
|
|
|
|
*/
|
|
|
|
partial_frame_count += ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we don't have a full frame, return
|
|
|
|
*/
|
|
|
|
if (partial_frame_count < DSM_FRAME_SIZE)
|
2013-01-13 22:57:27 -04:00
|
|
|
return false;
|
2012-11-07 06:47:01 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Great, it looks like we might have a frame. Go ahead and
|
|
|
|
* decode it.
|
|
|
|
*/
|
|
|
|
partial_frame_count = 0;
|
2013-01-13 22:57:27 -04:00
|
|
|
return dsm_decode(now, values, num_values);
|
2012-11-07 06:47:01 -04:00
|
|
|
}
|
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
static bool
|
|
|
|
dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (raw == 0xffff)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*channel = (raw >> shift) & 0xf;
|
|
|
|
|
|
|
|
uint16_t data_mask = (1 << shift) - 1;
|
|
|
|
*value = raw & data_mask;
|
|
|
|
|
|
|
|
//debug("DSM: %d 0x%04x -> %d %d", shift, raw, *channel, *value);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-07 06:47:01 -04:00
|
|
|
static void
|
2012-11-29 04:35:21 -04:00
|
|
|
dsm_guess_format(bool reset)
|
2012-11-07 06:47:01 -04:00
|
|
|
{
|
2012-11-29 04:35:21 -04:00
|
|
|
static uint32_t cs10;
|
|
|
|
static uint32_t cs11;
|
|
|
|
static unsigned samples;
|
|
|
|
|
|
|
|
/* reset the 10/11 bit sniffed channel masks */
|
|
|
|
if (reset) {
|
|
|
|
cs10 = 0;
|
|
|
|
cs11 = 0;
|
|
|
|
samples = 0;
|
|
|
|
channel_shift = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scan the channels in the current frame in both 10- and 11-bit mode */
|
|
|
|
for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
|
|
|
|
|
|
|
|
uint8_t *dp = &frame[2 + (2 * i)];
|
|
|
|
uint16_t raw = (dp[0] << 8) | dp[1];
|
|
|
|
unsigned channel, value;
|
|
|
|
|
|
|
|
/* if the channel decodes, remember the assigned number */
|
|
|
|
if (dsm_decode_channel(raw, 10, &channel, &value) && (channel < 31))
|
|
|
|
cs10 |= (1 << channel);
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
if (dsm_decode_channel(raw, 11, &channel, &value) && (channel < 31))
|
|
|
|
cs11 |= (1 << channel);
|
|
|
|
|
|
|
|
/* XXX if we cared, we could look for the phase bit here to decide 1 vs. 2-frame format */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait until we have seen plenty of frames - 2 should normally be enough */
|
|
|
|
if (samples++ < 5)
|
|
|
|
return;
|
|
|
|
|
2012-12-29 20:01:24 -04:00
|
|
|
/*
|
2012-11-29 04:35:21 -04:00
|
|
|
* Iterate the set of sensible sniffed channel sets and see whether
|
|
|
|
* decoding in 10 or 11-bit mode has yielded anything we recognise.
|
2012-11-29 14:18:21 -04:00
|
|
|
*
|
|
|
|
* XXX Note that due to what seem to be bugs in the DSM2 high-resolution
|
|
|
|
* stream, we may want to sniff for longer in some cases when we think we
|
|
|
|
* are talking to a DSM2 receiver in high-resolution mode (so that we can
|
|
|
|
* reject it, ideally).
|
|
|
|
* See e.g. http://git.openpilot.org/cru/OPReview-116 for a discussion
|
|
|
|
* of this issue.
|
2012-11-29 04:35:21 -04:00
|
|
|
*/
|
2012-12-29 20:01:24 -04:00
|
|
|
static uint32_t masks[] = {
|
2012-11-29 04:35:21 -04:00
|
|
|
0x3f, /* 6 channels (DX6) */
|
|
|
|
0x7f, /* 7 channels (DX7) */
|
|
|
|
0xff, /* 8 channels (DX8) */
|
|
|
|
0x3ff, /* 10 channels (DX10) */
|
|
|
|
0x3fff /* 18 channels (DX10) */
|
|
|
|
};
|
|
|
|
unsigned votes10 = 0;
|
|
|
|
unsigned votes11 = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < (sizeof(masks) / sizeof(masks[0])); i++) {
|
|
|
|
|
|
|
|
if (cs10 == masks[i])
|
|
|
|
votes10++;
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
if (cs11 == masks[i])
|
|
|
|
votes11++;
|
|
|
|
}
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
if ((votes11 == 1) && (votes10 == 0)) {
|
|
|
|
channel_shift = 11;
|
|
|
|
debug("DSM: detected 11-bit format");
|
|
|
|
return;
|
|
|
|
}
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
if ((votes10 == 1) && (votes11 == 0)) {
|
|
|
|
channel_shift = 10;
|
|
|
|
debug("DSM: detected 10-bit format");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call ourselves to reset our state ... we have to try again */
|
|
|
|
debug("DSM: format detector failed, 10: 0x%08x %d 11: 0x%08x %d", cs10, votes10, cs11, votes11);
|
|
|
|
dsm_guess_format(true);
|
|
|
|
}
|
|
|
|
|
2013-01-13 22:57:27 -04:00
|
|
|
static bool
|
|
|
|
dsm_decode(hrt_abstime frame_time, uint16_t *values, uint16_t *num_values)
|
2012-11-29 04:35:21 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
2012-12-29 20:01:24 -04:00
|
|
|
debug("DSM frame %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x",
|
|
|
|
frame[0], frame[1], frame[2], frame[3], frame[4], frame[5], frame[6], frame[7],
|
|
|
|
frame[8], frame[9], frame[10], frame[11], frame[12], frame[13], frame[14], frame[15]);
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* If we have lost signal for at least a second, reset the
|
2012-11-29 04:35:21 -04:00
|
|
|
* format guessing heuristic.
|
|
|
|
*/
|
|
|
|
if (((frame_time - last_frame_time) > 1000000) && (channel_shift != 0))
|
|
|
|
dsm_guess_format(true);
|
2012-12-04 03:20:28 -04:00
|
|
|
|
|
|
|
/* we have received something we think is a frame */
|
2012-11-29 04:35:21 -04:00
|
|
|
last_frame_time = frame_time;
|
2012-12-04 03:20:28 -04:00
|
|
|
|
|
|
|
/* if we don't know the frame format, update the guessing state machine */
|
2012-11-29 04:35:21 -04:00
|
|
|
if (channel_shift == 0) {
|
|
|
|
dsm_guess_format(false);
|
2013-01-13 22:57:27 -04:00
|
|
|
return false;
|
2012-11-29 04:35:21 -04:00
|
|
|
}
|
2012-11-07 06:47:01 -04:00
|
|
|
|
|
|
|
/*
|
2012-12-29 20:01:24 -04:00
|
|
|
* The encoding of the first two bytes is uncertain, so we're
|
2012-11-29 14:18:21 -04:00
|
|
|
* going to ignore them for now.
|
2012-11-07 06:47:01 -04:00
|
|
|
*
|
|
|
|
* Each channel is a 16-bit unsigned value containing either a 10-
|
|
|
|
* or 11-bit channel value and a 4-bit channel number, shifted
|
|
|
|
* either 10 or 11 bits. The MSB may also be set to indicate the
|
|
|
|
* second frame in variants of the protocol where more than
|
|
|
|
* seven channels are being transmitted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
|
|
|
|
|
|
|
|
uint8_t *dp = &frame[2 + (2 * i)];
|
|
|
|
uint16_t raw = (dp[0] << 8) | dp[1];
|
2012-11-29 04:35:21 -04:00
|
|
|
unsigned channel, value;
|
2012-11-07 06:47:01 -04:00
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
if (!dsm_decode_channel(raw, channel_shift, &channel, &value))
|
2012-11-07 06:47:01 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore channels out of range */
|
|
|
|
if (channel >= PX4IO_INPUT_CHANNELS)
|
|
|
|
continue;
|
|
|
|
|
2012-11-29 04:35:21 -04:00
|
|
|
/* update the decoded channel count */
|
2013-01-13 22:57:27 -04:00
|
|
|
if (channel >= *num_values)
|
|
|
|
*num_values = channel + 1;
|
2012-11-07 06:47:01 -04:00
|
|
|
|
|
|
|
/* convert 0-1024 / 0-2048 values to 1000-2000 ppm encoding in a very sloppy fashion */
|
|
|
|
if (channel_shift == 11)
|
2012-11-29 04:35:21 -04:00
|
|
|
value /= 2;
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-12-05 02:00:24 -04:00
|
|
|
value += 998;
|
|
|
|
|
2012-12-29 20:01:24 -04:00
|
|
|
/*
|
2012-12-05 02:00:24 -04:00
|
|
|
* Store the decoded channel into the R/C input buffer, taking into
|
|
|
|
* account the different ideas about channel assignement that we have.
|
|
|
|
*
|
|
|
|
* Specifically, the first four channels in rc_channel_data are roll, pitch, thrust, yaw,
|
|
|
|
* but the first four channels from the DSM receiver are thrust, roll, pitch, yaw.
|
|
|
|
*/
|
|
|
|
switch (channel) {
|
|
|
|
case 0:
|
|
|
|
channel = 2;
|
|
|
|
break;
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-12-05 02:00:24 -04:00
|
|
|
case 1:
|
|
|
|
channel = 0;
|
|
|
|
break;
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-12-05 02:00:24 -04:00
|
|
|
case 2:
|
|
|
|
channel = 1;
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2012-12-05 02:00:24 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-12-29 20:01:24 -04:00
|
|
|
|
2013-01-13 22:57:27 -04:00
|
|
|
values[channel] = value;
|
2012-11-07 06:47:01 -04:00
|
|
|
}
|
2012-11-29 04:35:21 -04:00
|
|
|
|
2013-01-13 22:57:27 -04:00
|
|
|
/*
|
|
|
|
* XXX Note that we may be in failsafe here; we need to work out how to detect that.
|
|
|
|
*/
|
|
|
|
return true;
|
2012-11-07 06:47:01 -04:00
|
|
|
}
|