QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4FlightModesController.cc
Go to the documentation of this file.
2
3#include <QtCore/QMap>
4
5#include "PX4FirmwarePlugin.h"
7#include "StandardModes.h"
8#include "Vehicle.h"
9
11 : FactPanelController(parent)
12{
13 // Long enough for a PX4 RC_CHANNELS stream as slow as 1 Hz
14 _rcTimeoutTimer.setInterval(2000);
15 _rcTimeoutTimer.setSingleShot(true);
16 (void) connect(&_rcTimeoutTimer, &QTimer::timeout, this, &PX4FlightModesController::_clearRcChannelValues);
17
18 if (!_vehicle) {
19 return;
20 }
21
22 (void) connect(_vehicle, &Vehicle::flightModesChanged, this, &PX4FlightModesController::_updateExternalModeNames);
23 _updateExternalModeNames();
24}
25
27{
28 if (monitorRc == _monitorRc) {
29 return;
30 }
31 _monitorRc = monitorRc;
32
33 if (_vehicle) {
34 // Raw rather than clamped: PX4 scales PWM against RC#_MIN/RC#_MAX, which can lie outside 1000-2000 us
35 if (_monitorRc) {
36 (void) connect(_vehicle, &Vehicle::rcChannelsRawChanged, this,
37 &PX4FlightModesController::_rcChannelsChanged);
38 } else {
39 (void) disconnect(_vehicle, &Vehicle::rcChannelsRawChanged, this,
40 &PX4FlightModesController::_rcChannelsChanged);
41 }
42 }
43 if (!_monitorRc) {
44 _rcTimeoutTimer.stop();
45 _clearRcChannelValues();
46 }
47 emit monitorRcChanged();
48}
49
50void PX4FlightModesController::_rcChannelsChanged(QVector<int> pwmValues)
51{
52 _rcChannelValues.clear();
53 _rcChannelValues.reserve(pwmValues.size());
54 for (const int pwm : pwmValues) {
55 _rcChannelValues.append(pwm);
56 }
57 _rcTimeoutTimer.start();
59}
60
61void PX4FlightModesController::_clearRcChannelValues()
62{
63 if (_rcChannelValues.isEmpty()) {
64 return;
65 }
66 _rcChannelValues.clear();
68}
69
70void PX4FlightModesController::_updateExternalModeNames()
71{
72 if (!_vehicle || !_vehicle->standardModes()) {
73 return;
74 }
75
76 const QStringList names = PX4FirmwarePlugin::externalFlightModeNames(_vehicle->standardModes()->modeList());
77 if (names != _externalModeNames) {
78 _externalModeNames = names;
80 }
81}
82
83QString PX4FlightModesController::selectionMethod(int modeChannel, int buttonMask) const
84{
85 switch (PX4FlightModeConfig::selectionMethod(modeChannel, buttonMask)) {
87 return QStringLiteral("switch");
89 return QStringLiteral("buttons");
91 break;
92 }
93
94 return QStringLiteral("none");
95}
96
97QVariantList PX4FlightModesController::buttonChannels(int buttonMask) const
98{
99 return _toVariantList(PX4FlightModeConfig::buttonChannels(buttonMask));
100}
101
102QVariantList PX4FlightModesController::ignoredButtonChannels(int buttonMask) const
103{
104 return _toVariantList(PX4FlightModeConfig::ignoredButtonChannels(buttonMask));
105}
106
107QVariantList PX4FlightModesController::modeChoices(const QString& paramName, const QStringList& enumStrings,
108 const QVariantList& enumValues, const QStringList& externalNames,
109 int currentValue) const
110{
111 QVariantList choices;
112 for (const PX4FlightModeConfig::Choice& choice :
113 PX4FlightModeConfig::modeChoices(paramName, enumStrings, enumValues, externalNames, currentValue)) {
114 choices.append(QVariantMap{{QStringLiteral("label"), choice.label}, {QStringLiteral("value"), choice.value}});
115 }
116
117 return choices;
118}
119
120QString PX4FlightModesController::modeName(const QString& paramName, const QStringList& enumStrings,
121 const QVariantList& enumValues, const QStringList& externalNames,
122 int value) const
123{
124 return PX4FlightModeConfig::modeName(paramName, enumStrings, enumValues, externalNames, value);
125}
126
127QVariantList PX4FlightModesController::_toVariantList(const QList<int>& values)
128{
129 QVariantList list;
130 for (const int value : values) {
131 list.append(value);
132 }
133
134 return list;
135}
FactPanelController(QObject *parent=nullptr)
static QStringList externalFlightModeNames(const FlightModeList &modeList)
externalFlightModeName of each of External Mode 1-8, in order
Q_INVOKABLE QVariantList buttonChannels(int buttonMask) const
RC channels (1-based) of the mode buttons in RC_MAP_FLTM_BTN, lowest first. Button N selects slot N.
PX4FlightModesController(QObject *parent=nullptr)
true: rcChannelValues follows the vehicle's RC input. Off by default, for users that only need the mo...
Q_INVOKABLE QVariantList modeChoices(const QString &paramName, const QStringList &enumStrings, const QVariantList &enumValues, const QStringList &externalNames, int currentValue) const
Q_INVOKABLE QString modeName(const QString &paramName, const QStringList &enumStrings, const QVariantList &enumValues, const QStringList &externalNames, int value) const
Label of the mode a parameter value selects, as modeChoices shows it. Empty for a value with no choic...
Q_INVOKABLE QVariantList ignoredButtonChannels(int buttonMask) const
Channels set in RC_MAP_FLTM_BTN that PX4 ignores, because only the lowest six get a slot.
Q_INVOKABLE QString selectionMethod(int modeChannel, int buttonMask) const
"switch", "buttons" or "none" from RC_MAP_FLTMODE and RC_MAP_FLTM_BTN
void rcChannelsRawChanged(QVector< int > channelValues)
void flightModesChanged()
QList< int > buttonChannels(int buttonMask)
QString modeName(const QString &paramName, const QStringList &enumStrings, const QVariantList &enumValues, const QStringList &externalNames, int value)
Label of the mode a parameter value selects, as modeChoices shows it. Empty for a value with no choic...
SelectionMethod selectionMethod(int modeChannel, int buttonMask)
PX4 ignores the mode buttons while a mode channel is set.
QList< Choice > modeChoices(const QString &paramName, const QStringList &enumStrings, const QVariantList &enumValues, const QStringList &externalNames, int currentValue)
QList< int > ignoredButtonChannels(int buttonMask)
Channels set in RC_MAP_FLTM_BTN that PX4 ignores, because only the lowest kModeSlotCount get a slot.