aboutsummaryrefslogtreecommitdiff
path: root/src/modules/px4iofirmware/sbus.c
diff options
context:
space:
mode:
authorThomas Gubler <thomasgubler@gmail.com>2014-11-24 07:41:26 +0100
committerThomas Gubler <thomasgubler@gmail.com>2014-11-24 07:41:26 +0100
commitc37ff71e625310cdc777719a04c3702d9afa1f7f (patch)
tree714ecb7b510e7ff53080ce3f0caebe8b128a26f5 /src/modules/px4iofirmware/sbus.c
parentf36f54c621cb5b36d345c5a26f72e89fc948fa65 (diff)
parent512779907e06f059a15d54c88d71b73aad9aced0 (diff)
downloadpx4-firmware-c37ff71e625310cdc777719a04c3702d9afa1f7f.tar.gz
px4-firmware-c37ff71e625310cdc777719a04c3702d9afa1f7f.tar.bz2
px4-firmware-c37ff71e625310cdc777719a04c3702d9afa1f7f.zip
Merge remote-tracking branch 'upstream/master' into ros
Conflicts: makefiles/config_px4fmu-v2_test.mk
Diffstat (limited to 'src/modules/px4iofirmware/sbus.c')
-rw-r--r--src/modules/px4iofirmware/sbus.c40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/modules/px4iofirmware/sbus.c b/src/modules/px4iofirmware/sbus.c
index 6ead38d61..d76ec55f0 100644
--- a/src/modules/px4iofirmware/sbus.c
+++ b/src/modules/px4iofirmware/sbus.c
@@ -57,6 +57,7 @@
#define SBUS_FLAGS_BYTE 23
#define SBUS_FAILSAFE_BIT 3
#define SBUS_FRAMELOST_BIT 2
+#define SBUS1_FRAME_DELAY 14000
/*
Measured values with Futaba FX-30/R6108SB:
@@ -80,6 +81,7 @@ static int sbus_fd = -1;
static hrt_abstime last_rx_time;
static hrt_abstime last_frame_time;
+static hrt_abstime last_txframe_time = 0;
static uint8_t frame[SBUS_FRAME_SIZE];
@@ -122,10 +124,42 @@ sbus_init(const char *device)
void
sbus1_output(uint16_t *values, uint16_t num_values)
{
- char a = 'A';
- write(sbus_fd, &a, 1);
-}
+ uint8_t byteindex = 1; /*Data starts one byte into the sbus frame. */
+ uint8_t offset = 0;
+ uint16_t value;
+ hrt_abstime now;
+
+ now = hrt_absolute_time();
+
+ if ((now - last_txframe_time) > SBUS1_FRAME_DELAY) {
+ last_txframe_time = now;
+ uint8_t oframe[SBUS_FRAME_SIZE] = { 0x0f };
+
+ /* 16 is sbus number of servos/channels minus 2 single bit channels.
+ * currently ignoring single bit channels. */
+
+ for (unsigned i = 0; (i < num_values) && (i < 16); ++i) {
+ value = (uint16_t)(((values[i] - SBUS_SCALE_OFFSET) / SBUS_SCALE_FACTOR) + .5f);
+
+ /*protect from out of bounds values and limit to 11 bits*/
+ if (value > 0x07ff ) {
+ value = 0x07ff;
+ }
+
+ while (offset >= 8) {
+ ++byteindex;
+ offset -= 8;
+ }
+ oframe[byteindex] |= (value << (offset)) & 0xff;
+ oframe[byteindex + 1] |= (value >> (8 - offset)) & 0xff;
+ oframe[byteindex + 2] |= (value >> (16 - offset)) & 0xff;
+ offset += 11;
+ }
+
+ write(sbus_fd, oframe, SBUS_FRAME_SIZE);
+ }
+}
void
sbus2_output(uint16_t *values, uint16_t num_values)
{