QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
APMFlightModesComponentController.cc
Go to the documentation of this file.
2#include "Fact.h"
3#include "ParameterManager.h"
4#include "Vehicle.h"
5
6#include <QtCore/QVariant>
7
8APMFlightModesComponentController::APMFlightModesComponentController(QObject *parent)
9 : FactPanelController(parent)
10 , _simpleModeFact(parameterExists(-1, _simpleParamName) ? getParameterFact(-1, _simpleParamName) : nullptr)
11 , _superSimpleModeFact(parameterExists(-1, _superSimpleParamName) ? getParameterFact(-1, _superSimpleParamName) : nullptr)
12 , _simpleModesSupported(_simpleModeFact && _superSimpleModeFact)
13{
14 const bool arduRoverFirmware = parameterExists(-1, QStringLiteral("MODE1"));
15 _modeParamPrefix = arduRoverFirmware ? QStringLiteral("MODE") : QStringLiteral("FLTMODE");
16 _modeChannelParam = arduRoverFirmware ? QStringLiteral("MODE_CH") : QStringLiteral("FLTMODE_CH");
17
18 for (int i = 0; i < _cFltModes; i++) {
19 _simpleModeEnabled.append(QVariant(false));
20 _superSimpleModeEnabled.append(QVariant(false));
21 }
22
23 if (_simpleModesSupported) {
24 _setupSimpleModeEnabled();
25
26 const uint8_t simpleModeValue = static_cast<uint8_t>(_simpleModeFact->rawValue().toUInt());
27 const uint8_t superSimpleModeValue = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toUInt());
28 if ((simpleModeValue == 0) && (superSimpleModeValue == 0)) {
29 _simpleMode = SimpleModeStandard;
30 } else if ((simpleModeValue == _allSimpleBits) && (superSimpleModeValue == 0)) {
31 _simpleMode = SimpleModeSimple;
32 } else if ((simpleModeValue == 0) && (superSimpleModeValue == _allSimpleBits)) {
33 _simpleMode = SimpleModeSuperSimple;
34 } else {
35 _simpleMode = SimpleModeCustom;
36 }
37
38 (void) connect(this, &APMFlightModesComponentController::simpleModeChanged, this, &APMFlightModesComponentController::_updateSimpleParamsFromSimpleMode);
39 }
40
41 QStringList usedParams;
42 for (int i = 1; i < 7; i++) {
43 usedParams << QStringLiteral("%1%2").arg(_modeParamPrefix).arg(i);
44 }
45 if (!_allParametersExists(ParameterManager::defaultComponentId, usedParams)) {
46 return;
47 }
48
49 for (int i = 0; i < _cChannelOptions; i++) {
50 _rgChannelOptionEnabled.append(QVariant(false));
51 }
52
53 (void) connect(_vehicle, &Vehicle::rcChannelsChanged, this, &APMFlightModesComponentController::channelValuesChanged);
54}
55
56void APMFlightModesComponentController::channelValuesChanged(QVector<int> channelValues)
57{
58 int flightModeChannel = 4;
59
60 if (parameterExists(ParameterManager::defaultComponentId, _modeChannelParam)) {
61 flightModeChannel = getParameterFact(ParameterManager::defaultComponentId, _modeChannelParam)->rawValue().toInt() - 1;
62 }
63
64 if (flightModeChannel >= static_cast<int>(channelValues.size())) {
65 return;
66 }
67
68 _activeFlightMode = 0;
69 int channelValue = channelValues[flightModeChannel];
70 if (channelValue != -1) {
71 bool found = false;
72 static constexpr const int rgThreshold[] = { 1230, 1360, 1490, 1620, 1749 };
73 for (size_t i = 0; i < std::size(rgThreshold); i++) {
74 if (channelValue <= rgThreshold[static_cast<size_t>(i)]) {
75 _activeFlightMode = static_cast<int>(i) + 1;
76 found = true;
77 break;
78 }
79 }
80 if (!found) {
81 _activeFlightMode = 6;
82 }
83 }
84 emit activeFlightModeChanged(_activeFlightMode);
85
86 for (int i = 0; i < _cChannelOptions; i++) {
87 _rgChannelOptionEnabled[i] = QVariant(false);
88 channelValue = channelValues[i + 5];
89 if (channelValue > 1800) {
90 _rgChannelOptionEnabled[i] = QVariant(true);
91 }
92 }
94}
95
96void APMFlightModesComponentController::_updateSimpleParamsFromSimpleMode()
97{
98 int newSimpleModeValue = 0;
99 int newSuperSimpleModeValue = 0;
100
101 if (_simpleMode == SimpleModeSimple) {
102 newSimpleModeValue = _allSimpleBits;
103 } else if (_simpleMode == SimpleModeSuperSimple) {
104 newSuperSimpleModeValue = _allSimpleBits;
105 }
106
107 for (int i = 0; i < _cFltModes; i++) {
108 _simpleModeEnabled[i] = false;
109 _superSimpleModeEnabled[i] = false;
110 }
113
114 _simpleModeFact->setRawValue(newSimpleModeValue);
115 _superSimpleModeFact->setRawValue(newSuperSimpleModeValue);
116}
117
118void APMFlightModesComponentController::setSimpleMode(int fltModeIndex, bool enabled)
119{
120 if (fltModeIndex < _cFltModes) {
121 uint8_t mode = static_cast<uint8_t>(_simpleModeFact->rawValue().toInt());
122 if (enabled) {
123 mode |= 1 << fltModeIndex;
124 } else {
125 mode &= ~(1 << fltModeIndex);
126 }
127 _simpleModeFact->setRawValue(mode);
128 }
129}
130
131void APMFlightModesComponentController::setSuperSimpleMode(int fltModeIndex, bool enabled)
132{
133 if (fltModeIndex < _cFltModes) {
134 uint8_t mode = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toInt());
135 if (enabled) {
136 mode |= 1 << fltModeIndex;
137 } else {
138 mode &= ~(1 << fltModeIndex);
139 }
140 _superSimpleModeFact->setRawValue(mode);
141 }
142}
143
144void APMFlightModesComponentController::_setupSimpleModeEnabled()
145{
146 const uint8_t simpleMode = static_cast<uint8_t>(_simpleModeFact->rawValue().toUInt());
147 const uint8_t superSimpleMode = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toUInt());
148
149 for (int i = 0; i < _cFltModes; i++) {
150 const uint8_t bitSet = static_cast<uint8_t>(1 << i);
151 _simpleModeEnabled[i] = !!(simpleMode & bitSet);
152 _superSimpleModeEnabled[i] = !!(superSimpleMode & bitSet);
153 }
154
157}
void simpleModeChanged(int simpleMode)
void activeFlightModeChanged(int activeFlightMode)
Used for handling missing Facts from C++ code.
static constexpr int defaultComponentId
void rcChannelsChanged(QVector< int > channelValues)