Skip to content

Commit 476d7c5

Browse files
authored
Re-architect mavlink UDP forwarder (#154)
* Re-architect mavlink-UDP code to support future telem forwarding options
1 parent 77447c3 commit 476d7c5

File tree

10 files changed

+227
-271
lines changed

10 files changed

+227
-271
lines changed

html/log.html

Lines changed: 0 additions & 17 deletions
This file was deleted.

html/log.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/MAVLink/MAVLink.cpp

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,62 @@
33
#include "MAVLink.h"
44
#include <config.h>
55

6-
// Returns whether the message is a control message, i.e. a message where we don't want to
7-
// pass the message to the flight controller, but rather handle it ourselves. If the message
8-
// is a control message, the function handles it internally.
9-
bool MAVLink::handleControlMessage(mavlink_message_t *msg)
6+
void
7+
MAVLink::ProcessMAVLinkFromTX(uint8_t c)
108
{
11-
bool shouldForward = true;
12-
// Check for messages addressed to the Backpack
13-
switch (msg->msgid)
9+
mavlink_status_t status;
10+
mavlink_message_t msg;
11+
12+
if (mavlink_frame_char(MAVLINK_COMM_0, c, &msg, &status) != MAVLINK_FRAMING_INCOMPLETE)
1413
{
15-
case MAVLINK_MSG_ID_COMMAND_INT:
16-
mavlink_command_int_t commandMsg;
17-
mavlink_msg_command_int_decode(msg, &commandMsg);
18-
if (commandMsg.target_component == MAV_COMP_ID_UDP_BRIDGE)
14+
if (mavlink_to_gcs_buf_count >= MAVLINK_BUF_SIZE)
15+
{
16+
// Cant fit any more msgs in the queue,
17+
// drop the oldest msg and start overwriting
18+
mavlink_stats.overflows_downlink++;
19+
mavlink_to_gcs_buf_count = 0;
20+
}
21+
22+
// Track gaps in the sequence number, add to a dropped counter
23+
uint8_t seq = msg.seq;
24+
if (expectedSeqSet && seq != expectedSeq)
1925
{
20-
shouldForward = false;
21-
constexpr uint8_t ELRS_MODE_CHANGE = 0x8;
22-
switch (commandMsg.command)
26+
// account for rollovers
27+
if (seq < expectedSeq)
2328
{
24-
case MAV_CMD_USER_1:
25-
switch ((int)commandMsg.param1)
26-
{
27-
case ELRS_MODE_CHANGE:
28-
switch ((int)commandMsg.param2)
29-
{
30-
case 0: // TX_NORMAL_MODE
31-
config.SetStartWiFiOnBoot(false);
32-
ESP.restart();
33-
break;
34-
case 1: // TX_MAVLINK_MODE
35-
if (config.GetWiFiService() != WIFI_SERVICE_MAVLINK_TX)
36-
{
37-
config.SetWiFiService(WIFI_SERVICE_MAVLINK_TX);
38-
config.SetStartWiFiOnBoot(true);
39-
config.Commit();
40-
ESP.restart();
41-
}
42-
break;
43-
}
44-
}
29+
mavlink_stats.drops_downlink += (UINT8_MAX - expectedSeq) + seq;
4530
}
46-
break;
31+
else
32+
{
33+
mavlink_stats.drops_downlink += seq - expectedSeq;
34+
}
35+
}
36+
expectedSeq = seq + 1;
37+
expectedSeqSet = true;
38+
39+
// Queue the msgs, to forward to peers
40+
mavlink_to_gcs_buf[mavlink_to_gcs_buf_count] = msg;
41+
mavlink_to_gcs_buf_count++;
42+
mavlink_stats.packets_downlink++;
43+
}
44+
}
45+
46+
void
47+
MAVLink::ProcessMAVLinkFromGCS(uint8_t *data, uint16_t len)
48+
{
49+
mavlink_status_t status;
50+
mavlink_message_t msg;
51+
52+
for (uint16_t i = 0; i < len; i++)
53+
{
54+
if (mavlink_frame_char(MAVLINK_COMM_1, data[i], &msg, &status) != MAVLINK_FRAMING_INCOMPLETE)
55+
{
56+
// Send the message to the tx uart
57+
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
58+
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
59+
Serial.write(buf, len);
60+
mavlink_stats.packets_uplink++;
4761
}
4862
}
49-
return shouldForward;
5063
}
51-
#endif
64+
#endif

lib/MAVLink/MAVLink.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
#if defined(MAVLINK_ENABLED)
22
#include "common/mavlink.h"
33

4+
// This is the port that we will listen for mavlink packets on
5+
constexpr uint16_t MAVLINK_PORT_LISTEN = 14555;
6+
// This is the port that we will send mavlink packets to
7+
constexpr uint16_t MAVLINK_PORT_SEND = 14550;
8+
// Size of the buffer that we will use to store mavlink packets in units of mavlink_message_t
9+
constexpr size_t MAVLINK_BUF_SIZE = 16;
10+
// Threshold at which we will flush the buffer
11+
constexpr size_t MAVLINK_BUF_THRESHOLD = MAVLINK_BUF_SIZE / 2;
12+
// Timeout for flushing the buffer in ms
13+
constexpr size_t MAVLINK_BUF_TIMEOUT = 500;
14+
15+
typedef struct {
16+
uint32_t packets_downlink; // packets from the aircraft
17+
uint32_t packets_uplink; // packets to the aircraft
18+
uint32_t drops_downlink; // dropped packets from the aircraft
19+
//uint32_t drops_uplink; // framing in the uplink direction cost too much time
20+
uint32_t overflows_downlink; // buffer overflows from the aircraft
21+
} mavlink_stats_t;
22+
423
class MAVLink
524
{
625
public:
7-
static bool handleControlMessage(mavlink_message_t* msg);
26+
MAVLink() : mavlink_to_gcs_buf_count(0), expectedSeq(0), expectedSeqSet(false) {}
27+
void ProcessMAVLinkFromTX(uint8_t c);
28+
void ProcessMAVLinkFromGCS(uint8_t *data, uint16_t len);
29+
30+
// Getters
31+
mavlink_stats_t* GetMavlinkStats() { return &mavlink_stats; }
32+
mavlink_message_t* GetQueuedMsgs() { return mavlink_to_gcs_buf; }
33+
uint8_t GetQueuedMsgCount() { return mavlink_to_gcs_buf_count; }
34+
35+
// Setters
36+
void ResetQueuedMsgCount() { mavlink_to_gcs_buf_count = 0; }
37+
38+
private:
39+
mavlink_stats_t mavlink_stats;
40+
mavlink_message_t mavlink_to_gcs_buf[MAVLINK_BUF_SIZE]; // Buffer for storing mavlink packets from the aircraft so that we can send them to the GCS efficiently
41+
uint8_t mavlink_to_gcs_buf_count; // Count of the number of messages in the buffer
42+
uint8_t expectedSeq;
43+
bool expectedSeqSet;
844
};
9-
#endif
45+
#endif

lib/MSP/msptypes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#define MSP_ELRS_SET_SEND_UID 0x00B5
2424
#define MSP_ELRS_SET_OSD 0x00B6
2525

26+
// Config opcodes
27+
#define MSP_ELRS_BACKPACK_CONFIG 0x30
28+
#define MSP_ELRS_BACKPACK_CONFIG_TLM_MODE 0x31
29+
2630
// CRSF encapsulated msp defines
2731
#define ENCAPSULATED_MSP_PAYLOAD_SIZE 4
2832
#define ENCAPSULATED_MSP_FRAME_LEN 8

0 commit comments

Comments
 (0)