@@ -105,8 +105,15 @@ static constexpr uint16_t MAVLINK_PORT_LISTEN = 14555;
105
105
// This is the port that we will send mavlink packets to
106
106
static constexpr uint16_t MAVLINK_PORT_SEND = 14550 ;
107
107
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 ;
110
117
#endif
111
118
112
119
/* * Is this an IP? */
@@ -469,8 +476,10 @@ static void WebMAVLinkHandler(AsyncWebServerRequest *request)
469
476
{
470
477
DynamicJsonDocument json (1024 );
471
478
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;
474
483
json[" ports" ][" listen" ] = MAVLINK_PORT_LISTEN;
475
484
json[" ports" ][" send" ] = MAVLINK_PORT_SEND;
476
485
@@ -731,16 +740,18 @@ static void HandleWebUpdate()
731
740
mavlink_message_t msg;
732
741
mavlink_status_t status;
733
742
char val = Serial.read ();
743
+ static uint8_t seq = 0 ;
734
744
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
+ }
744
755
}
745
756
}
746
757
#else
@@ -754,6 +765,38 @@ static void HandleWebUpdate()
754
765
#endif
755
766
}
756
767
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
+
757
800
#if defined(MAVLINK_ENABLED)
758
801
// Check if we have MAVLink UDP data to send over UART
759
802
if (wifiService == WIFI_SERVICE_MAVLINK_TX) {
@@ -762,7 +805,7 @@ static void HandleWebUpdate()
762
805
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
763
806
mavlinkUDP.read (buf, packetSize);
764
807
Serial.write (buf, packetSize);
765
- mavlink_to_uart_counter ++;
808
+ mavlink_to_uart_packets ++;
766
809
}
767
810
}
768
811
#endif
0 commit comments