Skip to content

Commit fe54465

Browse files
committed
Update the packet submission logic
Packet submission logic is updated to provide a lower channel update latency.
1 parent e636196 commit fe54465

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/src/rx-serial/SerialSatellite.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,52 @@ uint32_t SerialSatellite::sendRCFrame(bool frameAvailable, uint32_t *channelData
6565
}
6666
sendPackets = true;
6767

68-
uint16_t outgoingPacket[SATELLITE_CHANNEL_DATA_LENGTH];
69-
for (uint8_t ii = 0; ii < SATELLITE_CHANNEL_DATA_LENGTH; ++ii)
68+
uint16_t outgoingPacket[SATELLITE_CHANNELS_PER_PACKET];
69+
uint8_t outgoingPacketIndex = 0;
70+
const auto roundRobinIndexStartValue = roundRobinIndex;
71+
72+
// Our bandwidth is limited, so try to send updates only for the channels
73+
// with new values to reduce the channel update latency. Updates are
74+
// checked in round-robin fashion to make sure that the channels do not
75+
// starve.
76+
do
7077
{
71-
// These channels are sent in every packet
72-
if (ii < SATELLITE_FIRST_RR_CHANNEL)
78+
if (prevChannelData[roundRobinIndex] != channelData[roundRobinIndex])
7379
{
74-
outgoingPacket[ii] = crsfToSatellite(channelData[ii],
75-
CRSF_TO_SATELLITE_CH_MAP[ii],
76-
satelliteSystem);
80+
prevChannelData[roundRobinIndex] = channelData[roundRobinIndex];
81+
outgoingPacket[outgoingPacketIndex++] =
82+
crsfToSatellite(channelData[roundRobinIndex],
83+
CRSF_TO_SATELLITE_CH_MAP[roundRobinIndex],
84+
satelliteSystem);
85+
// Mark the channel as "sent", so we wouldn't submit it more than
86+
// once in a packet.
87+
channelData[roundRobinIndex] = UINT32_MAX;
7788
}
78-
// Round-robin channels
79-
else
89+
90+
++roundRobinIndex;
91+
if (roundRobinIndex >= SATELLITE_MAX_CHANNELS)
92+
{
93+
roundRobinIndex = 0;
94+
}
95+
if (outgoingPacketIndex >= SATELLITE_CHANNELS_PER_PACKET)
96+
{
97+
break;
98+
}
99+
} while (roundRobinIndex != roundRobinIndexStartValue);
100+
101+
// If less than 7 channel values have been changed since the last update,
102+
// fill the rest of the packet with data from arbitrary channels.
103+
for (uint8_t index = 0;
104+
(outgoingPacketIndex < SATELLITE_CHANNELS_PER_PACKET) && (index < SATELLITE_MAX_CHANNELS);
105+
++index)
106+
{
107+
// If the channel is not marked as "sent", add it to the packet.
108+
if (channelData[index] != UINT32_MAX)
80109
{
81-
outgoingPacket[ii] = crsfToSatellite(channelData[rr],
82-
CRSF_TO_SATELLITE_CH_MAP[rr],
83-
satelliteSystem);
84-
85-
// Update the round-robin index
86-
++rr;
87-
if (rr >= SATELLITE_NUM_CHANNELS)
88-
{
89-
rr = SATELLITE_FIRST_RR_CHANNEL;
90-
}
110+
outgoingPacket[outgoingPacketIndex++] =
111+
crsfToSatellite(channelData[index],
112+
CRSF_TO_SATELLITE_CH_MAP[index],
113+
satelliteSystem);
91114
}
92115
}
93116

src/src/rx-serial/SerialSatellite.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ class SerialSatellite : public SerialIO
44
{
55
public:
66
SerialSatellite(Stream &out, Stream &in)
7-
: SerialIO(&out, &in) {}
7+
: SerialIO(&out, &in) {
8+
memset(prevChannelData, UINT32_MAX, sizeof(prevChannelData));
9+
}
810

911
virtual ~SerialSatellite() = default;
1012

@@ -13,13 +15,14 @@ class SerialSatellite : public SerialIO
1315
uint32_t sendRCFrame(bool frameAvailable, uint32_t *channelData) override;
1416

1517
private:
16-
static constexpr uint16_t SATELLITE_NUM_CHANNELS = 12;
17-
static constexpr uint8_t SATELLITE_FIRST_RR_CHANNEL = 4;
18-
static constexpr uint8_t SATELLITE_CHANNEL_DATA_LENGTH = 7;
18+
static constexpr uint16_t SATELLITE_MAX_CHANNELS = 12;
19+
static constexpr uint8_t SATELLITE_CHANNELS_PER_PACKET = 7;
1920

2021
void processBytes(uint8_t *bytes, uint16_t size) override{};
2122

23+
uint32_t prevChannelData[SATELLITE_CHANNELS_PER_PACKET];
24+
2225
uint8_t fadeCount{0};
23-
uint8_t rr{SATELLITE_FIRST_RR_CHANNEL};
26+
uint8_t roundRobinIndex{0};
2427
bool sendPackets{false};
2528
};

0 commit comments

Comments
 (0)