Skip to content

Commit

Permalink
libmavconn : enable low-latency mode on Linux
Browse files Browse the repository at this point in the history
Some common USB-UART convertors like the FTDI accumulates individual bytes from the serial link
in order to send them in a single USB packet (Nagling). This commit sets the ASYNC_LOW_LATENCY flag,
which the FTDI kernel driver interprets as a request to drop the Nagling timer to 1ms (i.e send all
accumulated bytes after 1ms.)

This reduces average link RTT to under 5ms at 921600 baud, and enables the use of mavros in
systems where low latency is required to get good performance for e.g estimation and controls.
mhkabir authored and vooon committed Mar 21, 2018
1 parent 95d44ab commit a77dcef
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions libmavconn/include/mavconn/serial.h
Original file line number Diff line number Diff line change
@@ -22,6 +22,10 @@
#include <mavconn/interface.h>
#include <mavconn/msgbuffer.h>

#if defined(__linux__)
#include <linux/serial.h>
#endif

namespace mavconn {
/**
* @brief Serial interface
11 changes: 10 additions & 1 deletion libmavconn/src/serial.cpp
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ MAVConnSerial::MAVConnSerial(uint8_t system_id, uint8_t component_id,

try {
serial_dev.open(device);
int fd = serial_dev.native_handle();

// Set baudrate and 8N1 mode
serial_dev.set_option(SPB::baud_rate(baudrate));
@@ -61,7 +62,6 @@ MAVConnSerial::MAVConnSerial(uint8_t system_id, uint8_t component_id,
// Workaround to set some options for the port manually. This is done in
// Boost.ASIO, but until v1.12.0 (Boost 1.66) there was a bug which doesn't enable relevant
// code. Fixed by commit: https://github.com/boostorg/asio/commit/619cea4356
int fd = serial_dev.native_handle();
termios tio;
tcgetattr(fd, &tio);

@@ -80,6 +80,15 @@ MAVConnSerial::MAVConnSerial(uint8_t system_id, uint8_t component_id,
// Commit settings
tcsetattr(fd, TCSANOW, &tio);
#endif

#if defined(__linux__)
// Enable low latency mode on Linux
struct serial_struct ser_info;
ioctl(fd, TIOCGSERIAL, &ser_info);
ser_info.flags |= ASYNC_LOW_LATENCY;
ioctl(fd, TIOCSSERIAL, &ser_info);
#endif

}
catch (boost::system::system_error &err) {
throw DeviceError("serial", err);

0 comments on commit a77dcef

Please sign in to comment.