QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4FlightModeConfig.cc
Go to the documentation of this file.
2
3#include <QtCore/QCoreApplication>
4
5#include <algorithm>
6
7#include "PX4FirmwarePlugin.h"
8
10
11namespace {
12
13// First external mode in COM_FLTMODE_BOOT, which uses PX4's navigation state numbers
14constexpr int kFirstExternalBootValue = 23;
15
16// First external mode in the mode slots
17constexpr int kFirstExternalSlotValue = 100;
18
19QList<int> allButtonChannels(int buttonMask)
20{
21 QList<int> channels;
22 for (int bit = 0; bit < kMaxButtonChannels; bit++) {
23 if (buttonMask & (1 << bit)) {
24 channels.append(bit + 1);
25 }
26 }
27
28 return channels;
29}
30
31} // namespace
32
33SelectionMethod selectionMethod(int modeChannel, int buttonMask)
34{
35 if (modeChannel > 0) {
37 }
38
39 return buttonMask > 0 ? SelectionMethod::Buttons : SelectionMethod::None;
40}
41
42QList<int> buttonChannels(int buttonMask)
43{
44 const QList<int> channels = allButtonChannels(buttonMask);
45
46 return channels.first(std::min<qsizetype>(kModeSlotCount, channels.size()));
47}
48
49QList<int> ignoredButtonChannels(int buttonMask)
50{
51 const QList<int> channels = allButtonChannels(buttonMask);
52
53 return channels.size() > kModeSlotCount ? channels.mid(kModeSlotCount) : QList<int>();
54}
55
56int externalModeIndex(const QString& paramName, int value)
57{
58 const int firstExternalValue =
59 paramName == QLatin1String("COM_FLTMODE_BOOT") ? kFirstExternalBootValue : kFirstExternalSlotValue;
60 const int index = value - firstExternalValue + 1;
61
62 return (index >= 1 && index <= PX4FirmwarePlugin::kExternalFlightModeCount) ? index : 0;
63}
64
65QList<Choice> modeChoices(const QString& paramName, const QStringList& enumStrings, const QVariantList& enumValues,
66 const QStringList& externalNames, int currentValue)
67{
68 QList<Choice> choices;
69 const qsizetype count = std::min(enumStrings.size(), enumValues.size());
70 for (qsizetype i = 0; i < count; i++) {
71 const int value = enumValues.at(i).toInt();
72 const int externalIndex = externalModeIndex(paramName, value);
73 if (externalIndex == 0) {
74 choices.append({enumStrings.at(i), value});
75 continue;
76 }
77
78 const QString externalName = externalNames.value(externalIndex - 1);
79 if (!externalName.isEmpty()) {
80 choices.append({externalName, value});
81 } else if (value == currentValue) {
82 choices.append({QCoreApplication::translate("PX4FlightModeConfig", "%1 (not running)").arg(enumStrings.at(i)),
83 value});
84 }
85 }
86
87 return choices;
88}
89
90QString modeName(const QString& paramName, const QStringList& enumStrings, const QVariantList& enumValues,
91 const QStringList& externalNames, int value)
92{
93 const QList<Choice> choices = modeChoices(paramName, enumStrings, enumValues, externalNames, value);
94 const auto choice =
95 std::find_if(choices.cbegin(), choices.cend(), [value](const Choice& entry) { return entry.value == value; });
96
97 return choice == choices.cend() ? QString() : choice->label;
98}
99
100} // namespace PX4FlightModeConfig
static constexpr int kExternalFlightModeCount
Number of PX4 External Mode slots. PX4FirmwarePlugin.cc checks it against px4_custom_mode....
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...
int externalModeIndex(const QString &paramName, int value)
constexpr int kMaxButtonChannels
Number of RC channels RC_MAP_FLTM_BTN has a bit for.
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.
constexpr int kModeSlotCount
Number of mode slots, COM_FLTMODE1 to COM_FLTMODE6.