aboutsummaryrefslogtreecommitdiff
path: root/apps/px4io/dsm.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/px4io/dsm.c')
-rw-r--r--apps/px4io/dsm.c70
1 files changed, 42 insertions, 28 deletions
diff --git a/apps/px4io/dsm.c b/apps/px4io/dsm.c
index 744dac3d6..2611f3a03 100644
--- a/apps/px4io/dsm.c
+++ b/apps/px4io/dsm.c
@@ -104,7 +104,7 @@ dsm_init(const char *device)
return dsm_fd;
}
-void
+bool
dsm_input(void)
{
ssize_t ret;
@@ -141,7 +141,7 @@ dsm_input(void)
/* if the read failed for any reason, just give up here */
if (ret < 1)
- return;
+ goto out;
last_rx_time = now;
/*
@@ -153,7 +153,7 @@ dsm_input(void)
* If we don't have a full frame, return
*/
if (partial_frame_count < DSM_FRAME_SIZE)
- return;
+ goto out;
/*
* Great, it looks like we might have a frame. Go ahead and
@@ -161,6 +161,12 @@ dsm_input(void)
*/
dsm_decode(now);
partial_frame_count = 0;
+
+out:
+ /*
+ * If we have seen a frame in the last 200ms, we consider ourselves 'locked'
+ */
+ return (now - last_frame_time) < 200000;
}
static bool
@@ -275,10 +281,13 @@ dsm_decode(hrt_abstime frame_time)
*/
if (((frame_time - last_frame_time) > 1000000) && (channel_shift != 0))
dsm_guess_format(true);
+
+ /* we have received something we think is a frame */
last_frame_time = frame_time;
+
+ /* if we don't know the frame format, update the guessing state machine */
if (channel_shift == 0) {
dsm_guess_format(false);
- system_state.dsm_input_ok = false;
return;
}
@@ -293,10 +302,6 @@ dsm_decode(hrt_abstime frame_time)
* seven channels are being transmitted.
*/
- const unsigned dsm_chancount = (DSM_FRAME_CHANNELS < PX4IO_INPUT_CHANNELS) ? DSM_FRAME_CHANNELS : PX4IO_INPUT_CHANNELS;
-
- uint16_t dsm_channels[dsm_chancount];
-
for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
uint8_t *dp = &frame[2 + (2 * i)];
@@ -311,31 +316,40 @@ dsm_decode(hrt_abstime frame_time)
continue;
/* update the decoded channel count */
- if (channel > ppm_decoded_channels)
- ppm_decoded_channels = channel;
+ if (channel >= system_state.rc_channels)
+ system_state.rc_channels = channel + 1;
/* convert 0-1024 / 0-2048 values to 1000-2000 ppm encoding in a very sloppy fashion */
if (channel_shift == 11)
value /= 2;
-
- /* stuff the decoded channel into the PPM input buffer */
- dsm_channels[channel] = 988 + value;
+ value += 998;
+
+ /*
+ * 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;
+ case 1:
+ channel = 0;
+ break;
+ case 2:
+ channel = 1;
+ default:
+ break;
+ }
+ system_state.rc_channel_data[channel] = value;
}
- /* DSM input is valid */
- system_state.dsm_input_ok = true;
-
- /* check if no S.BUS data is available */
- if (!system_state.sbus_input_ok) {
+ /* and note that we have received data from the R/C controller */
+ /* XXX failsafe will cause problems here - need a strategy for detecting it */
+ system_state.rc_channels_timestamp = frame_time;
- for (unsigned i = 0; i < dsm_chancount; i++) {
- system_state.rc_channel_data[i] = dsm_channels[i];
- }
-
- /* and note that we have received data from the R/C controller */
- /* XXX failsafe will cause problems here - need a strategy for detecting it */
- system_state.rc_channels_timestamp = frame_time;
- system_state.rc_channels = dsm_chancount;
- system_state.fmu_report_due = true;
- }
+ /* trigger an immediate report to the FMU */
+ system_state.fmu_report_due = true;
}