Skip to content

Commit 70325fd

Browse files
Refactor WiFi MAVLink packet handling and counters
1 parent c39a340 commit 70325fd

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

lib/WIFI/devWIFI.cpp

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,15 @@ static constexpr uint16_t MAVLINK_PORT_LISTEN = 14555;
105105
// This is the port that we will send mavlink packets to
106106
static constexpr uint16_t MAVLINK_PORT_SEND = 14550;
107107
WiFiUDP mavlinkUDP;
108-
uint32_t mavlink_from_uart_counter = 0;
109-
uint32_t mavlink_to_uart_counter = 0;
108+
uint32_t mavlink_from_uart_packets = 0;
109+
uint32_t mavlink_to_uart_packets = 0;
110+
uint32_t mavlink_from_uart_drops = 0;
111+
uint32_t mavlink_to_uart_drops = 0;
112+
constexpr size_t mavlink_to_uart_buf_size = 16;
113+
constexpr size_t mavlink_to_uart_buf_threshold = mavlink_to_uart_buf_size / 4;
114+
constexpr size_t mavlink_to_uart_buf_timeout = 5; // 5ms
115+
mavlink_message_t mavlink_to_gcs_buf[mavlink_to_uart_buf_size];
116+
uint8_t mavlink_to_gcs_buf_count = 0;
110117
#endif
111118

112119
/** Is this an IP? */
@@ -469,8 +476,10 @@ static void WebMAVLinkHandler(AsyncWebServerRequest *request)
469476
{
470477
DynamicJsonDocument json(1024);
471478
json["enabled"] = wifiService == WIFI_SERVICE_MAVLINK_TX;
472-
json["counters"]["to_gcs"] = mavlink_from_uart_counter;
473-
json["counters"]["to_aircraft"] = mavlink_to_uart_counter;
479+
json["counters"]["packets_to_gcs"] = mavlink_from_uart_packets;
480+
json["counters"]["packets_to_aircraft"] = mavlink_to_uart_packets;
481+
json["counters"]["drops_to_gcs"] = mavlink_from_uart_drops;
482+
json["counters"]["drops_to_aircraft"] = mavlink_to_uart_drops;
474483
json["ports"]["listen"] = MAVLINK_PORT_LISTEN;
475484
json["ports"]["send"] = MAVLINK_PORT_SEND;
476485

@@ -731,16 +740,18 @@ static void HandleWebUpdate()
731740
mavlink_message_t msg;
732741
mavlink_status_t status;
733742
char val = Serial.read();
743+
static uint8_t seq = 0;
734744
if (mavlink_parse_char(MAVLINK_COMM_0, val, &msg, &status)) {
735-
mavlink_from_uart_counter++;
736-
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
737-
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
738-
739-
// Determine the broadcast address
740-
IPAddress broadcast = WiFi.getMode() == WIFI_STA ? WiFi.broadcastIP() : apBroadcast;
741-
mavlinkUDP.beginPacket(broadcast, MAVLINK_PORT_SEND);
742-
mavlinkUDP.write(buf, len);
743-
mavlinkUDP.endPacket();
745+
// Track gaps in the sequence number, add to a dropped counter
746+
if (seq != 0 && msg.seq != seq + 1) {
747+
mavlink_from_uart_drops += msg.seq - seq - 1;
748+
}
749+
seq = msg.seq;
750+
if (mavlink_to_gcs_buf_count < mavlink_to_uart_buf_size) {
751+
mavlink_to_gcs_buf[mavlink_to_gcs_buf_count++] = msg;
752+
} else {
753+
// TODO: log buffer overflow
754+
}
744755
}
745756
}
746757
#else
@@ -754,6 +765,38 @@ static void HandleWebUpdate()
754765
#endif
755766
}
756767

768+
#if defined(MAVLINK_ENABLED)
769+
// Dump the mavlink_to_gcs_buf to the GCS
770+
static unsigned long last_mavlink_to_gcs_dump = 0;
771+
if (
772+
mavlink_to_gcs_buf_count > mavlink_to_uart_buf_threshold ||
773+
(mavlink_to_gcs_buf_count > 0 && (millis() - last_mavlink_to_gcs_dump) > mavlink_to_uart_buf_timeout)
774+
)
775+
{
776+
IPAddress broadcast = WiFi.getMode() == WIFI_STA ? WiFi.broadcastIP() : apBroadcast;
777+
mavlinkUDP.beginPacket(broadcast, MAVLINK_PORT_SEND);
778+
for (uint8_t i = 0; i < mavlink_to_gcs_buf_count; i++)
779+
{
780+
mavlink_from_uart_packets++;
781+
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
782+
uint16_t len = mavlink_msg_to_send_buffer(buf, &mavlink_to_gcs_buf[i]);
783+
size_t sent = mavlinkUDP.write(buf, len);
784+
if (sent < len)
785+
{
786+
break;
787+
mavlinkUDP.endPacket();
788+
delay(2);
789+
mavlinkUDP.beginPacket(broadcast, MAVLINK_PORT_SEND);
790+
sent = mavlinkUDP.write(buf + sent, len - sent);
791+
mavlinkUDP.endPacket();
792+
}
793+
}
794+
mavlinkUDP.endPacket();
795+
mavlink_to_gcs_buf_count = 0;
796+
last_mavlink_to_gcs_dump = millis();
797+
}
798+
#endif
799+
757800
#if defined(MAVLINK_ENABLED)
758801
// Check if we have MAVLink UDP data to send over UART
759802
if (wifiService == WIFI_SERVICE_MAVLINK_TX) {
@@ -762,7 +805,7 @@ static void HandleWebUpdate()
762805
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
763806
mavlinkUDP.read(buf, packetSize);
764807
Serial.write(buf, packetSize);
765-
mavlink_to_uart_counter++;
808+
mavlink_to_uart_packets++;
766809
}
767810
}
768811
#endif

src/Tx_main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ void setup()
342342
Serial1.setDebugOutput(true);
343343
#endif
344344
Serial.begin(460800);
345+
Serial.setRxBufferSize(1024);
345346

346347
options_init();
347348

0 commit comments

Comments
 (0)