-
Notifications
You must be signed in to change notification settings - Fork 166
/
outputchannelform.cpp
392 lines (340 loc) · 12.7 KB
/
outputchannelform.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
******************************************************************************
*
* @file outputchannelform.cpp
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
* @author Tau Labs, http://taulabs.org, Copyright (C) 2014
* @addtogroup GCSPlugins GCS Plugins
* @{
* @addtogroup ConfigPlugin Config Plugin
* @{
* @brief Servo output configuration form for the config output gadget
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>
*/
#include <cmath>
#include <limits>
#include "outputchannelform.h"
#include "configoutputwidget.h"
#include "actuatorsettings.h"
OutputChannelForm::OutputChannelForm(const int index, QWidget *parent, const bool showLegend)
: ConfigTaskWidget(parent)
, ui()
, m_index(index)
, m_inChannelTest(false)
, limitMin(1000)
, limitMax(2000)
, minMaxFixed(false)
{
ui.setupUi(this);
ActuatorSettings *actuatorSettings = ActuatorSettings::GetInstance(getObjectManager());
Q_ASSERT(actuatorSettings);
if (!showLegend) {
// Remove legend
QGridLayout *grid_layout = dynamic_cast<QGridLayout *>(layout());
Q_ASSERT(grid_layout);
for (int col = 0; col < grid_layout->columnCount();
col++) { // remove every item in first row
QLayoutItem *item = grid_layout->itemAtPosition(0, col);
if (!item)
continue;
// get widget from layout item
QWidget *legend_widget = item->widget();
if (!legend_widget)
continue;
// delete widget
grid_layout->removeWidget(legend_widget);
delete legend_widget;
}
}
// The convention is Channel 1 to Channel 10.
ui.actuatorNumber->setText(QString("%1:").arg(m_index + 1));
// Register for ActuatorSettings changes:
connect(ui.actuatorMin, &QAbstractSpinBox::editingFinished, this,
&OutputChannelForm::setChannelRange);
connect(ui.actuatorMax, &QAbstractSpinBox::editingFinished, this,
&OutputChannelForm::setChannelRange);
connect(ui.pb_reverseActuator, &QAbstractButton::clicked, this,
&OutputChannelForm::reverseChannel);
// Connect the channel out sliders to our signal in order to send updates in test mode
connect(ui.actuatorNeutral, &QAbstractSlider::valueChanged, this,
&OutputChannelForm::sendChannelTest);
// Connect UI elements to dirty/clean (i.e. changed/unchanged) signal/slot
connect(ui.actuatorMin, QOverload<int>::of(&QSpinBox::valueChanged), this,
&OutputChannelForm::notifyFormChanged);
connect(ui.actuatorMax, QOverload<int>::of(&QSpinBox::valueChanged), this,
&OutputChannelForm::notifyFormChanged);
connect(ui.actuatorNeutral, &QAbstractSlider::sliderReleased, this,
&OutputChannelForm::notifyFormChanged);
ui.actuatorLink->setChecked(false);
connect(ui.actuatorLink, &QAbstractButton::toggled, this, &OutputChannelForm::linkToggled);
disableMouseWheelEvents();
}
OutputChannelForm::~OutputChannelForm()
{
// Do nothing
}
/**
* Restrict UI to protect users from accidental misuse.
*/
void OutputChannelForm::enableChannelTest(bool state)
{
if (m_inChannelTest == state)
return;
m_inChannelTest = state;
if (m_inChannelTest) {
// Prevent users from changing the minimum & maximum ranges while
// moving the sliders. Thanks Ivan for the tip :)
ui.actuatorMin->setEnabled(false);
ui.actuatorMax->setEnabled(false);
ui.pb_reverseActuator->setEnabled(false);
} else {
ui.actuatorMin->setEnabled(!minMaxFixed);
ui.actuatorMax->setEnabled(!minMaxFixed);
ui.pb_reverseActuator->setEnabled(!minMaxFixed);
}
}
/**
* Toggles the channel linked state for use in testing mode
*/
void OutputChannelForm::linkToggled(bool state)
{
Q_UNUSED(state)
if (!m_inChannelTest)
return; // we are not in Test Output mode
// find the minimum slider value for the linked ones
if (!parent())
return;
int min = 10000;
int linked_count = 0;
QList<OutputChannelForm *> outputChannelForms = parent()->findChildren<OutputChannelForm *>();
// set the linked channels of the parent widget to the same value
foreach (OutputChannelForm *outputChannelForm, outputChannelForms) {
if (!outputChannelForm->ui.actuatorLink->checkState())
continue;
if (this == outputChannelForm)
continue;
int value = outputChannelForm->ui.actuatorNeutral->value();
if (min > value)
min = value;
linked_count++;
}
if (linked_count <= 0)
return; // no linked channels
// set the linked channels to the same value
foreach (OutputChannelForm *outputChannelForm, outputChannelForms) {
if (!outputChannelForm->ui.actuatorLink->checkState())
continue;
outputChannelForm->ui.actuatorNeutral->setValue(min);
}
}
/**
* Set maximal channel value.
*/
void OutputChannelForm::setMax(int maximum)
{
setMinmax(ui.actuatorMin->value(), maximum);
}
/**
* Set minimal channel value.
*/
void OutputChannelForm::setMin(int minimum)
{
setMinmax(minimum, ui.actuatorMax->value());
}
/**
* Set minimal and maximal channel value.
*/
void OutputChannelForm::setMinmax(int minimum, int maximum)
{
inhibitRangeChanges = true;
if (minMaxFixed) {
ui.actuatorMin->setValue(limitMin);
ui.actuatorMax->setValue(limitMax);
} else {
ui.actuatorMin->setValue(minimum);
ui.actuatorMax->setValue(maximum);
}
inhibitRangeChanges = false;
setChannelRange();
}
/**
* Set neutral of channel.
*/
void OutputChannelForm::setNeutral(int value)
{
ui.actuatorNeutral->setValue(value);
}
void OutputChannelForm::alignFields()
{
int actuatorWidth = 0;
foreach (OutputChannelForm *form, parent()->findChildren<OutputChannelForm *>()) {
actuatorWidth = fmax(actuatorWidth, form->ui.actuatorMin->minimumSize().width());
actuatorWidth = fmax(actuatorWidth, form->ui.actuatorMin->sizeHint().width());
actuatorWidth = fmax(actuatorWidth, form->ui.actuatorMax->minimumSize().width());
actuatorWidth = fmax(actuatorWidth, form->ui.actuatorMax->sizeHint().width());
}
foreach (OutputChannelForm *form, parent()->findChildren<OutputChannelForm *>()) {
form->ui.actuatorMin->setMinimumSize(actuatorWidth, 0);
form->ui.actuatorMax->setMinimumSize(actuatorWidth, 0);
}
}
/**
* Set the channel assignment label.
*/
void OutputChannelForm::setAssignment(const QString &assignment)
{
ui.actuatorName->setText(assignment);
QFontMetrics metrics(ui.actuatorName->font());
int width = metrics.width(assignment) + 1;
foreach (OutputChannelForm *form, parent()->findChildren<OutputChannelForm *>()) {
if (form == this)
continue;
if (form->ui.actuatorName->minimumSize().width() < width)
form->ui.actuatorName->setMinimumSize(width, 0);
else
width = form->ui.actuatorName->minimumSize().width();
}
ui.actuatorName->setMinimumSize(width, 0);
}
/**
* Sets the minimum/maximum value of the channel output sliders.
* Have to do it here because setMinimum is not a slot.
*
* One added trick: if the slider is at its min when the value
* is changed, then keep it on the min.
*/
void OutputChannelForm::setChannelRange()
{
if (inhibitRangeChanges) {
return;
}
if (minMaxFixed) { // special case Dshot
ui.actuatorNeutral->setRange(limitMin, limitMax);
ui.actuatorNeutral->setEnabled(true);
} else if (ui.actuatorMin->value() < ui.actuatorMax->value()) {
ui.actuatorNeutral->setRange(ui.actuatorMin->value(), ui.actuatorMax->value());
ui.actuatorNeutral->setEnabled(true);
} else if (ui.actuatorMin->value() > ui.actuatorMax->value()) {
ui.actuatorNeutral->setRange(ui.actuatorMax->value(), ui.actuatorMin->value());
ui.actuatorNeutral->setEnabled(true);
} else {
// when the min and max is equal, disable this slider to prevent crash
// from Qt bug: https://bugreports.qt.io/browse/QTBUG-43398
ui.actuatorNeutral->setRange(ui.actuatorMin->value() - 1, ui.actuatorMin->value() + 1);
ui.actuatorNeutral->setEnabled(false);
setNeutral(ui.actuatorMin->value());
}
// Force a full slider update
updateSlider();
}
/**
* Reverses the channel when the checkbox is clicked
*/
void OutputChannelForm::reverseChannel()
{
// can't reverse Dshot at this time
if (!ui.actuatorMin->isEnabled() || !ui.actuatorMax->isEnabled())
return;
// Swap the min & max values
setMinmax(ui.actuatorMax->value(), ui.actuatorMin->value());
setChannelRange();
}
/**
* Inverts the slider when the output channel is reversed
*/
/**
* @brief OutputChannelForm::updateSlider
*/
void OutputChannelForm::updateSlider()
{
// Invert the slider
if (ui.actuatorMin->value() > ui.actuatorMax->value()) {
ui.actuatorNeutral->setInvertedAppearance(true);
// Set the QSlider groove colors so that the fill is on the side of the minimum value
ui.actuatorNeutral->setProperty("state", "inverted");
} else {
ui.actuatorNeutral->setInvertedAppearance(false);
// Set the QSlider groove colors so that the fill is on the side of the minimum value
ui.actuatorNeutral->setProperty("state", "normal");
}
// Force refresh of style sheet.
ui.actuatorNeutral->setStyle(QApplication::style());
}
/**
* Emits the channel value which will be sent to the UAV to move the servo.
* Returns immediately if we are not in testing mode.
*/
void OutputChannelForm::sendChannelTest(int value)
{
int in_value = value;
QSlider *ob = dynamic_cast<QSlider *>(QObject::sender());
if (!ob)
return;
if (ui.actuatorLink->checkState() && parent()) { // the channel is linked to other channels
QList<OutputChannelForm *> outputChannelForms =
parent()->findChildren<OutputChannelForm *>();
// set the linked channels of the parent widget to the same value
foreach (OutputChannelForm *outputChannelForm, outputChannelForms) {
if (this == outputChannelForm)
continue;
if (!outputChannelForm->ui.actuatorLink->checkState())
continue;
int val = in_value;
if (val < outputChannelForm->ui.actuatorNeutral->minimum())
val = outputChannelForm->ui.actuatorNeutral->minimum();
if (val > outputChannelForm->ui.actuatorNeutral->maximum())
val = outputChannelForm->ui.actuatorNeutral->maximum();
if (outputChannelForm->ui.actuatorNeutral->value() == val)
continue;
outputChannelForm->ui.actuatorNeutral->setValue(val);
}
}
if (!m_inChannelTest)
return; // we are not in Test Output mode
emit channelChanged(index(), value);
}
/**
* @brief OutputChannelForm::setUpdated Slot that receives signals indicating the UI is updated
*/
void OutputChannelForm::notifyFormChanged()
{
// If we are not in Test Output mode, set form as dirty
if (!m_inChannelTest) {
emit formChanged();
}
setChannelRange();
}
void OutputChannelForm::updateChannelLimits(int minPulse, int maxPulse, bool digitalProtocol)
{
limitMin = minPulse;
limitMax = maxPulse;
minMaxFixed = digitalProtocol;
if (digitalProtocol) {
// digital protocol like Dshot, fix min to 0, max to maxPulse, and neutral range goes
// between minPulse and maxPulse
ui.actuatorMin->setRange(0, 0);
ui.actuatorMax->setRange(limitMax, limitMax);
} else {
ui.actuatorMin->setRange(limitMin, limitMax);
ui.actuatorMax->setRange(limitMin, limitMax);
}
// these are fixed with digital protocols like Dshot
ui.actuatorMin->setEnabled(!minMaxFixed && !m_inChannelTest);
ui.actuatorMax->setEnabled(!minMaxFixed && !m_inChannelTest);
ui.pb_reverseActuator->setEnabled(!minMaxFixed && !m_inChannelTest);
setChannelRange();
alignFields();
}