Skip to content

Commit 20c1d70

Browse files
committed
AP_RCProtocol: add support for MAVLink receiver, handle RADIO_RC_CHANNELS message
1 parent a8e0f3c commit 20c1d70

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

libraries/AP_RCProtocol/AP_RCProtocol.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "AP_RCProtocol_FPort.h"
3434
#include "AP_RCProtocol_FPort2.h"
3535
#include "AP_RCProtocol_DroneCAN.h"
36+
#include "AP_RCProtocol_MAVLinkRadio.h"
3637
#include <AP_Math/AP_Math.h>
3738
#include <RC_Channel/RC_Channel.h>
3839

@@ -82,6 +83,9 @@ void AP_RCProtocol::init()
8283
#if AP_RCPROTOCOL_DRONECAN_ENABLED
8384
backend[AP_RCProtocol::DRONECAN] = new AP_RCProtocol_DroneCAN(*this);
8485
#endif
86+
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
87+
backend[AP_RCProtocol::MAVLINK_RADIO] = new AP_RCProtocol_MAVLinkRadio(*this);
88+
#endif
8589
}
8690

8791
AP_RCProtocol::~AP_RCProtocol()
@@ -504,6 +508,10 @@ const char *AP_RCProtocol::protocol_name_from_protocol(rcprotocol_t protocol)
504508
#if AP_RCPROTOCOL_DRONECAN_ENABLED
505509
case DRONECAN:
506510
return "DroneCAN";
511+
#endif
512+
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
513+
case MAVLINK_RADIO:
514+
return "MAVRadio";
507515
#endif
508516
case NONE:
509517
break;
@@ -539,6 +547,34 @@ bool AP_RCProtocol::protocol_enabled(rcprotocol_t protocol) const
539547
return ((1U<<(uint8_t(protocol)+1)) & rc_protocols_mask) != 0;
540548
}
541549

550+
void AP_RCProtocol::handle_radio_rc_channels(const mavlink_radio_rc_channels_t* packet)
551+
{
552+
// receiving this message is also used to check if the receiver is present
553+
// so let's first do the receiver detection
554+
if (_detected_protocol == AP_RCProtocol::NONE) { // still searching
555+
#if AP_RC_CHANNEL_ENABLED
556+
rc_protocols_mask = rc().enabled_protocols();
557+
#endif
558+
if (!protocol_enabled(MAVLINK_RADIO)) return; // not our turn
559+
_detected_protocol = AP_RCProtocol::MAVLINK_RADIO;
560+
}
561+
562+
// here now comes the message handling itself
563+
564+
if (_detected_protocol != AP_RCProtocol::MAVLINK_RADIO) {
565+
return;
566+
}
567+
568+
// now update the backend
569+
backend[_detected_protocol]->update_radio_rc_channels(packet);
570+
571+
// now we can ask the backend if it got a new input
572+
if (backend[_detected_protocol]->new_input()) {
573+
_new_input = true;
574+
_last_input_ms = AP_HAL::millis();
575+
}
576+
};
577+
542578
namespace AP {
543579
AP_RCProtocol &RC()
544580
{

libraries/AP_RCProtocol/AP_RCProtocol.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <AP_HAL/AP_HAL.h>
2222
#include <AP_Common/AP_Common.h>
23+
#include <GCS_MAVLink/GCS_MAVLink.h>
2324

2425
#define MAX_RCIN_CHANNELS 18
2526
#define MIN_RCIN_CHANNELS 5
@@ -69,6 +70,9 @@ class AP_RCProtocol {
6970
#endif
7071
#if AP_RCPROTOCOL_DRONECAN_ENABLED
7172
DRONECAN = 13,
73+
#endif
74+
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
75+
MAVLINK_RADIO = 14,
7276
#endif
7377
NONE //last enum always is None
7478
};
@@ -148,6 +152,9 @@ class AP_RCProtocol {
148152
#endif
149153
#if AP_RCPROTOCOL_DRONECAN_ENABLED
150154
case DRONECAN:
155+
#endif
156+
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
157+
case MAVLINK_RADIO:
151158
#endif
152159
case NONE:
153160
return false;
@@ -195,6 +202,9 @@ class AP_RCProtocol {
195202
return _detected_with_bytes;
196203
}
197204

205+
// handle mavlink radio
206+
void handle_radio_rc_channels(const mavlink_radio_rc_channels_t* packet);
207+
198208
private:
199209
void check_added_uart(void);
200210

libraries/AP_RCProtocol/AP_RCProtocol_Backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class AP_RCProtocol_Backend {
4848
PARSE_TYPE_SERIAL
4949
};
5050

51+
// update from mavlink messages
52+
virtual void update_radio_rc_channels(const mavlink_radio_rc_channels_t* packet) {}
53+
5154
// get number of frames, ignoring failsafe
5255
uint32_t get_rc_frame_count(void) const {
5356
return rc_frame_count;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#include "AP_RCProtocol_config.h"
3+
4+
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
5+
6+
#include "AP_RCProtocol_MAVLinkRadio.h"
7+
8+
// constructor
9+
AP_RCProtocol_MAVLinkRadio::AP_RCProtocol_MAVLinkRadio(AP_RCProtocol &_frontend) :
10+
AP_RCProtocol_Backend(_frontend)
11+
{}
12+
13+
void AP_RCProtocol_MAVLinkRadio::update_radio_rc_channels(const mavlink_radio_rc_channels_t* packet)
14+
{
15+
uint8_t count = packet->count;
16+
if (count >= MAX_RCIN_CHANNELS) count = MAX_RCIN_CHANNELS;
17+
18+
uint16_t rc_chan[MAX_RCIN_CHANNELS];
19+
for (uint8_t i = 0; i < count; i++) {
20+
// The channel values are in centered 13 bit format. Range is [-4096,4096], center is 0.
21+
// According to specification, the conversion to PWM is x * 5/32 + 1500.
22+
rc_chan[i] = ((int32_t)packet->channels[i] * 5) / 32 + 1500;
23+
}
24+
25+
bool failsafe = (packet->flags & RADIO_RC_CHANNELS_FLAGS_FAILSAFE);
26+
27+
add_input(count, rc_chan, failsafe);
28+
}
29+
30+
#endif // AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
31+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
#pragma once
3+
4+
#include "AP_RCProtocol_config.h"
5+
6+
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
7+
8+
#include "AP_RCProtocol.h"
9+
10+
11+
class AP_RCProtocol_MAVLinkRadio : public AP_RCProtocol_Backend {
12+
public:
13+
14+
AP_RCProtocol_MAVLinkRadio(AP_RCProtocol &_frontend);
15+
16+
// update from mavlink messages
17+
void update_radio_rc_channels(const mavlink_radio_rc_channels_t* packet) override;
18+
};
19+
20+
#endif // AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
21+

libraries/AP_RCProtocol/AP_RCProtocol_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@
6161
#ifndef AP_RCPROTOCOL_FASTSBUS_ENABLED
6262
#define AP_RCPROTOCOL_FASTSBUS_ENABLED AP_RCPROTOCOL_BACKEND_DEFAULT_ENABLED && AP_RCPROTOCOL_SBUS_ENABLED
6363
#endif
64+
65+
#ifndef AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
66+
#define AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED AP_RCPROTOCOL_BACKEND_DEFAULT_ENABLED && BOARD_FLASH_SIZE > 1024
67+
#endif

0 commit comments

Comments
 (0)