1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/****************************************************************************
*
* (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/
#include "APMFlightModesComponentController.h"
#include "QGCMAVLink.h"
#include <QVariant>
#include <QQmlProperty>
bool APMFlightModesComponentController::_typeRegistered = false;
const char* APMFlightModesComponentController::_simpleParamName = "SIMPLE";
const char* APMFlightModesComponentController::_superSimpleParamName = "SUPER_SIMPLE";
APMFlightModesComponentController::APMFlightModesComponentController(void)
: _activeFlightMode (0)
, _channelCount (Vehicle::cMaxRcChannels)
, _simpleMode (SimpleModeStandard)
, _simpleModeFact (parameterExists(-1, _simpleParamName) ? getParameterFact(-1, _simpleParamName) : nullptr)
, _superSimpleModeFact (parameterExists(-1, _superSimpleParamName) ? getParameterFact(-1, _superSimpleParamName) : nullptr)
, _simpleModesSupported (_simpleModeFact && _superSimpleModeFact)
{
if (!_typeRegistered) {
qmlRegisterUncreatableType<APMFlightModesComponentController>("QGroundControl.Controllers", 1, 0, "APMFlightModesComponentController", "Reference only");
}
bool arduRoverFirmware = parameterExists(-1, QStringLiteral("MODE1"));
_modeParamPrefix = arduRoverFirmware ? QStringLiteral("MODE") : QStringLiteral("FLTMODE");
_modeChannelParam = arduRoverFirmware ? QStringLiteral("MODE_CH") : QStringLiteral("FLTMODE_CH");
_simpleModeNames << tr("Off") << tr("Simple") << tr("Super-Simple") << tr("Custom");
for (int i=0; i<_cFltModes; i++) {
_simpleModeEnabled.append(QVariant(false));
_superSimpleModeEnabled.append(QVariant(false));
}
if (_simpleModesSupported) {
_setupSimpleModeEnabled();
uint8_t simpleModeValue = static_cast<uint8_t>(_simpleModeFact->rawValue().toUInt());
uint8_t superSimpleModeValue = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toUInt());
if (simpleModeValue == 0 && superSimpleModeValue == 0) {
_simpleMode = SimpleModeStandard;
} else if (simpleModeValue == _allSimpleBits && superSimpleModeValue == 0) {
_simpleMode = SimpleModeSimple;
} else if (simpleModeValue == 0 && superSimpleModeValue == _allSimpleBits) {
_simpleMode = SimpleModeSuperSimple;
} else {
_simpleMode = SimpleModeCustom;
}
connect(this, &APMFlightModesComponentController::simpleModeChanged, this, &APMFlightModesComponentController::_updateSimpleParamsFromSimpleMode);
}
QStringList usedParams;
for (int i=1; i<7; i++) {
usedParams << QStringLiteral("%1%2").arg(_modeParamPrefix).arg(i);
}
if (!_allParametersExists(FactSystem::defaultComponentId, usedParams)) {
return;
}
for (int i=0; i<_cChannelOptions; i++) {
_rgChannelOptionEnabled.append(QVariant(false));
}
connect(_vehicle, &Vehicle::rcChannelsChanged, this, &APMFlightModesComponentController::_rcChannelsChanged);
}
/// Connected to Vehicle::rcChannelsChanged signal
void APMFlightModesComponentController::_rcChannelsChanged(int channelCount, int pwmValues[Vehicle::cMaxRcChannels])
{
int flightModeChannel = 4;
if (parameterExists(FactSystem::defaultComponentId, _modeChannelParam)) {
flightModeChannel = getParameterFact(FactSystem::defaultComponentId, _modeChannelParam)->rawValue().toInt() - 1;
}
if (flightModeChannel >= channelCount) {
return;
}
_activeFlightMode = 0;
int channelValue = pwmValues[flightModeChannel];
if (channelValue != -1) {
bool found = false;
int rgThreshold[] = { 1230, 1360, 1490, 1620, 1749 };
for (int i=0; i<5; i++) {
if (channelValue <= rgThreshold[i]) {
_activeFlightMode = i + 1;
found = true;
break;
}
}
if (!found) {
_activeFlightMode = 6;
}
}
emit activeFlightModeChanged(_activeFlightMode);
for (int i=0; i<_cChannelOptions; i++) {
_rgChannelOptionEnabled[i] = QVariant(false);
channelValue = pwmValues[i+6];
if (channelValue > 1800) {
_rgChannelOptionEnabled[i] = QVariant(true);
}
}
emit channelOptionEnabledChanged();
}
void APMFlightModesComponentController::_updateSimpleParamsFromSimpleMode(void)
{
int newSimpleModeValue = 0;
int newSuperSimpleModeValue = 0;
if (_simpleMode == SimpleModeSimple) {
newSimpleModeValue = _allSimpleBits;
} else if (_simpleMode == SimpleModeSuperSimple) {
newSuperSimpleModeValue = _allSimpleBits;
}
for (int i=0; i<_cFltModes; i++) {
_simpleModeEnabled[i] = false;
_superSimpleModeEnabled[i] = false;
}
emit simpleModeEnabledChanged();
emit superSimpleModeEnabledChanged();
_simpleModeFact->setRawValue(newSimpleModeValue);
_superSimpleModeFact->setRawValue(newSuperSimpleModeValue);
}
void APMFlightModesComponentController::setSimpleMode(int fltModeIndex, bool enabled)
{
if (fltModeIndex < _cFltModes) {
uint8_t mode = static_cast<uint8_t>(_simpleModeFact->rawValue().toInt());
if (enabled) {
mode |= 1 << fltModeIndex;
} else {
mode &= ~(1 << fltModeIndex);
}
_simpleModeFact->setRawValue(mode);
}
}
void APMFlightModesComponentController::setSuperSimpleMode(int fltModeIndex, bool enabled)
{
if (fltModeIndex < _cFltModes) {
uint8_t mode = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toInt());
if (enabled) {
mode |= 1 << fltModeIndex;
} else {
mode &= ~(1 << fltModeIndex);
}
_superSimpleModeFact->setRawValue(mode);
}
}
void APMFlightModesComponentController::_setupSimpleModeEnabled(void)
{
uint8_t simpleMode = static_cast<uint8_t>(_simpleModeFact->rawValue().toUInt());
uint8_t superSimpleMode = static_cast<uint8_t>(_superSimpleModeFact->rawValue().toUInt());
for (int i=0; i<_cFltModes; i++) {
uint8_t bitSet = static_cast<uint8_t>(1 << i);
_simpleModeEnabled[i] = !!(simpleMode & bitSet);
_superSimpleModeEnabled[i] = !!(superSimpleMode & bitSet);
}
emit simpleModeEnabledChanged();
emit superSimpleModeEnabledChanged();
}