dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
configstabilizationwidget.cpp
Go to the documentation of this file.
1 
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with this program; if not, see <http://www.gnu.org/licenses/>
27  *
28  * Additional note on redistribution: The copyright and license notices above
29  * must be maintained in each individual source file that is a derivative work
30  * of this source file; otherwise redistribution is prohibited.
31  */
32 
34 #include "manualcontrolsettings.h"
35 #include "systemident.h"
36 
37 #include <coreplugin/iboardtype.h>
39 
40 #include <QDebug>
41 #include <QStringList>
42 #include <QWidget>
43 #include <QTextEdit>
44 #include <QVBoxLayout>
45 #include <QPushButton>
46 #include <QDesktopServices>
47 #include <QUrl>
48 #include <QList>
49 
51  : ConfigTaskWidget(parent)
52  , manualControlSettings(nullptr)
53 {
54  m_stabilization = new Ui_StabilizationWidget();
55  m_stabilization->setupUi(this);
56 
57  m_stabilization->tabWidget->setCurrentIndex(0);
58 
59  updateInProgress = false;
60 
61  // display switch arming not selected warning when hangtime enabled
62  connect(m_stabilization->sbHangtimeDuration,
63  QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
64  &ConfigStabilizationWidget::hangtimeDurationChanged);
65  manualControlSettings = getObjectManager()->getObject(ManualControlSettings::NAME);
66  if (manualControlSettings)
67  connect(manualControlSettings, &UAVObject::objectUpdated, this,
68  &ConfigStabilizationWidget::hangtimeDurationChanged);
69  connect(m_stabilization->gbHangtime, &QGroupBox::toggled, this,
70  &ConfigStabilizationWidget::hangtimeToggle);
71 
73 
74  connect(m_stabilization->checkBox_7, &QCheckBox::stateChanged, this,
75  &ConfigStabilizationWidget::linkCheckBoxes);
76  connect(m_stabilization->checkBox_2, &QCheckBox::stateChanged, this,
77  &ConfigStabilizationWidget::linkCheckBoxes);
78  connect(m_stabilization->checkBox_8, &QCheckBox::stateChanged, this,
79  &ConfigStabilizationWidget::linkCheckBoxes);
80  connect(m_stabilization->checkBox_3, &QCheckBox::stateChanged, this,
81  &ConfigStabilizationWidget::linkCheckBoxes);
82 
83  connect(m_stabilization->cbLinkRateRollYaw, &QCheckBox::stateChanged, this,
84  &ConfigStabilizationWidget::ratesLink);
85  connect(m_stabilization->cbLinkRateRollPitch, &QCheckBox::stateChanged, this,
86  &ConfigStabilizationWidget::ratesLink);
87 
88  connect(m_stabilization->sliderLTRoll, &QAbstractSlider::valueChanged,
89  m_stabilization->rateRollLT, &QSpinBox::setValue);
90  connect(m_stabilization->rateRollLT, QOverload<int>::of(&QSpinBox::valueChanged),
91  m_stabilization->sliderLTRoll, &QSlider::setValue);
92  connect(m_stabilization->sliderLTPitch, &QAbstractSlider::valueChanged,
93  m_stabilization->ratePitchLT, &QSpinBox::setValue);
94  connect(m_stabilization->ratePitchLT, QOverload<int>::of(&QSpinBox::valueChanged),
95  m_stabilization->sliderLTPitch, &QSlider::setValue);
96  connect(m_stabilization->sliderLTYaw, &QAbstractSlider::valueChanged,
97  m_stabilization->rateYawLT, &QSpinBox::setValue);
98  connect(m_stabilization->rateYawLT, QOverload<int>::of(&QSpinBox::valueChanged),
99  m_stabilization->sliderLTYaw, &QSlider::setValue);
100 
101  connect(m_stabilization->fullStickRateRoll,
102  QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
103  &ConfigStabilizationWidget::setMaximums);
104  connect(m_stabilization->fullStickRatePitch,
105  QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
106  &ConfigStabilizationWidget::setMaximums);
107  connect(m_stabilization->fullStickRateYaw, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
108  this, &ConfigStabilizationWidget::setMaximums);
109  connect(m_stabilization->centerStickRateRoll, QOverload<int>::of(&QSpinBox::valueChanged), this,
110  &ConfigStabilizationWidget::derivedValuesChanged);
111  connect(m_stabilization->centerStickRatePitch, QOverload<int>::of(&QSpinBox::valueChanged),
112  this, &ConfigStabilizationWidget::derivedValuesChanged);
113  connect(m_stabilization->centerStickRateYaw, QOverload<int>::of(&QSpinBox::valueChanged), this,
114  &ConfigStabilizationWidget::derivedValuesChanged);
115  connect(m_stabilization->rateRollLT, QOverload<int>::of(&QSpinBox::valueChanged), this,
116  &ConfigStabilizationWidget::derivedValuesChanged);
117  connect(m_stabilization->ratePitchLT, QOverload<int>::of(&QSpinBox::valueChanged), this,
118  &ConfigStabilizationWidget::derivedValuesChanged);
119  connect(m_stabilization->rateYawLT, QOverload<int>::of(&QSpinBox::valueChanged), this,
120  &ConfigStabilizationWidget::derivedValuesChanged);
121 
122  connect(m_stabilization->sliderCRateRoll, &QAbstractSlider::valueChanged,
123  m_stabilization->centerStickRateRoll, &QSpinBox::setValue);
124  connect(m_stabilization->centerStickRateRoll, QOverload<int>::of(&QSpinBox::valueChanged),
125  m_stabilization->sliderCRateRoll, &QSlider::setValue);
126  connect(m_stabilization->sliderCRatePitch, &QAbstractSlider::valueChanged,
127  m_stabilization->centerStickRatePitch, &QSpinBox::setValue);
128  connect(m_stabilization->centerStickRatePitch, QOverload<int>::of(&QSpinBox::valueChanged),
129  m_stabilization->sliderCRatePitch, &QSlider::setValue);
130  connect(m_stabilization->sliderCRateYaw, &QAbstractSlider::valueChanged,
131  m_stabilization->centerStickRateYaw, &QSpinBox::setValue);
132  connect(m_stabilization->centerStickRateYaw, QOverload<int>::of(&QSpinBox::valueChanged),
133  m_stabilization->sliderCRateYaw, &QSlider::setValue);
134 
135  connect(m_stabilization->rateRollExpo, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
136  this, &ConfigStabilizationWidget::sourceValuesChanged);
137  connect(m_stabilization->ratePitchExpo, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
138  this, &ConfigStabilizationWidget::sourceValuesChanged);
139  connect(m_stabilization->rateYawExpo, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
140  this, &ConfigStabilizationWidget::sourceValuesChanged);
141 
142  connect(m_stabilization->rateRollExponent, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
143  this, &ConfigStabilizationWidget::sourceValuesChanged);
144  connect(m_stabilization->ratePitchExponent,
145  QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
146  &ConfigStabilizationWidget::sourceValuesChanged);
147  connect(m_stabilization->rateYawExponent, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
148  this, &ConfigStabilizationWidget::sourceValuesChanged);
149 
150  connect(this, &ConfigTaskWidget::widgetContentsChanged, this,
151  &ConfigStabilizationWidget::processLinkedWidgets);
152 
154 
155  connect(this, &ConfigTaskWidget::autoPilotConnected, this,
156  &ConfigStabilizationWidget::applyRateLimits);
157 
158  connect(this, &ConfigTaskWidget::autoPilotConnected, this,
159  &ConfigStabilizationWidget::enableDerivedControls);
160  connect(this, &ConfigTaskWidget::autoPilotDisconnected, this,
161  &ConfigStabilizationWidget::disableDerivedControls);
162 
163  disableDerivedControls();
164 }
165 
166 void ConfigStabilizationWidget::setDerivedControlsEnabled(bool enable)
167 {
168  m_stabilization->sliderLTRoll->setEnabled(enable);
169  m_stabilization->rateRollLT->setEnabled(enable);
170  m_stabilization->sliderLTPitch->setEnabled(enable);
171  m_stabilization->ratePitchLT->setEnabled(enable);
172  m_stabilization->sliderLTYaw->setEnabled(enable);
173  m_stabilization->rateYawLT->setEnabled(enable);
174  m_stabilization->sliderCRateRoll->setEnabled(enable);
175  m_stabilization->centerStickRateRoll->setEnabled(enable);
176  m_stabilization->sliderCRatePitch->setEnabled(enable);
177  m_stabilization->centerStickRatePitch->setEnabled(enable);
178  m_stabilization->sliderCRateYaw->setEnabled(enable);
179  m_stabilization->centerStickRateYaw->setEnabled(enable);
180 }
181 
182 void ConfigStabilizationWidget::enableDerivedControls()
183 {
184  setDerivedControlsEnabled(true);
185 }
186 
187 void ConfigStabilizationWidget::disableDerivedControls()
188 {
189  setDerivedControlsEnabled(false);
190 }
191 
193 {
194  // Do nothing
195 }
196 
197 void ConfigStabilizationWidget::ratesLink(int value)
198 {
199  Q_UNUSED(value);
200 
201  bool hideYaw = m_stabilization->cbLinkRateRollYaw->isChecked();
202  bool hidePitch = m_stabilization->cbLinkRateRollPitch->isChecked();
203 
204  m_stabilization->lblYawRate->setHidden(hideYaw);
205  m_stabilization->sliderFullStickRateYaw->setHidden(hideYaw);
206  m_stabilization->sliderLTYaw->setHidden(hideYaw);
207  m_stabilization->sliderCRateYaw->setHidden(hideYaw);
208  m_stabilization->fullStickRateYaw->setHidden(hideYaw);
209  m_stabilization->rateYawLT->setHidden(hideYaw);
210  m_stabilization->centerStickRateYaw->setHidden(hideYaw);
211  m_stabilization->sliderRateYawExpo->setHidden(hideYaw);
212  m_stabilization->rateYawExpo->setHidden(hideYaw);
213  m_stabilization->sliderExponentYaw->setHidden(hideYaw);
214  m_stabilization->rateYawExponent->setHidden(hideYaw);
215 
216  m_stabilization->lblPitchRate->setHidden(hidePitch);
217  m_stabilization->sliderFullStickRatePitch->setHidden(hidePitch);
218  m_stabilization->sliderLTPitch->setHidden(hidePitch);
219  m_stabilization->sliderCRatePitch->setHidden(hidePitch);
220  m_stabilization->fullStickRatePitch->setHidden(hidePitch);
221  m_stabilization->ratePitchLT->setHidden(hidePitch);
222  m_stabilization->centerStickRatePitch->setHidden(hidePitch);
223  m_stabilization->sliderRatePitchExpo->setHidden(hidePitch);
224  m_stabilization->ratePitchExpo->setHidden(hidePitch);
225  m_stabilization->sliderExponentPitch->setHidden(hidePitch);
226  m_stabilization->ratePitchExponent->setHidden(hidePitch);
227 
228  QString rollLbl = QString(tr("Roll"));
229 
230  if (hidePitch) {
231  rollLbl += tr(" & Pitch");
232  }
233 
234  if (hideYaw) {
235  rollLbl += tr(" & Yaw");
236  }
237 
238  m_stabilization->lblRollRate->setText(rollLbl);
239 
240  sourceValuesChanged();
241 }
242 
243 void ConfigStabilizationWidget::linkCheckBoxes(int value)
244 {
245  if (sender() == m_stabilization->checkBox_7)
246  m_stabilization->checkBox_3->setCheckState((Qt::CheckState)value);
247  else if (sender() == m_stabilization->checkBox_3)
248  m_stabilization->checkBox_7->setCheckState((Qt::CheckState)value);
249  else if (sender() == m_stabilization->checkBox_8)
250  m_stabilization->checkBox_2->setCheckState((Qt::CheckState)value);
251  else if (sender() == m_stabilization->checkBox_2)
252  m_stabilization->checkBox_8->setCheckState((Qt::CheckState)value);
253 }
254 
255 void ConfigStabilizationWidget::setMaximums()
256 {
257  m_stabilization->centerStickRateRoll->setMaximum(m_stabilization->fullStickRateRoll->value());
258  m_stabilization->centerStickRatePitch->setMaximum(m_stabilization->fullStickRatePitch->value());
259  m_stabilization->centerStickRateYaw->setMaximum(m_stabilization->fullStickRateYaw->value());
260 
261  m_stabilization->sliderCRateRoll->setMaximum(m_stabilization->fullStickRateRoll->value());
262  m_stabilization->sliderCRatePitch->setMaximum(m_stabilization->fullStickRatePitch->value());
263  m_stabilization->sliderCRateYaw->setMaximum(m_stabilization->fullStickRateYaw->value());
264 
265  derivedValuesChanged();
266  updateGraphs();
267 }
268 
269 void ConfigStabilizationWidget::sourceValuesChanged()
270 {
271  if (updateInProgress) {
272  return;
273  }
274 
275  updateInProgress = true;
276 
277  bool hideYaw = m_stabilization->cbLinkRateRollYaw->isChecked();
278  bool hidePitch = m_stabilization->cbLinkRateRollPitch->isChecked();
279 
280  if (hideYaw) {
281  m_stabilization->fullStickRateYaw->setValue(m_stabilization->fullStickRateRoll->value());
282  m_stabilization->rateYawExpo->setValue(m_stabilization->rateRollExpo->value());
283  m_stabilization->rateYawExponent->setValue(m_stabilization->rateRollExponent->value());
284  }
285 
286  if (hidePitch) {
287  m_stabilization->fullStickRatePitch->setValue(m_stabilization->fullStickRateRoll->value());
288  m_stabilization->ratePitchExpo->setValue(m_stabilization->rateRollExpo->value());
289  m_stabilization->ratePitchExponent->setValue(m_stabilization->rateRollExponent->value());
290  }
291 
292  /* invert 'derived' math / operations */
293  m_stabilization->centerStickRateRoll->setValue(m_stabilization->fullStickRateRoll->value()
294  * (100 - m_stabilization->rateRollExpo->value())
295  / 100);
296  m_stabilization->centerStickRatePitch->setValue(
297  m_stabilization->fullStickRatePitch->value()
298  * (100 - m_stabilization->ratePitchExpo->value()) / 100);
299  m_stabilization->centerStickRateYaw->setValue(m_stabilization->fullStickRateYaw->value()
300  * (100 - m_stabilization->rateYawExpo->value())
301  / 100);
302 
303  m_stabilization->rateRollLT->setValue(
304  100 * exp(log(0.01) / m_stabilization->rateRollExponent->value()));
305  m_stabilization->ratePitchLT->setValue(
306  100 * exp(log(0.01) / m_stabilization->ratePitchExponent->value()));
307  m_stabilization->rateYawLT->setValue(
308  100 * exp(log(0.01) / m_stabilization->rateYawExponent->value()));
309 
310  updateInProgress = false;
311 
312  updateGraphs();
313 }
314 
315 void ConfigStabilizationWidget::derivedValuesChanged()
316 {
317  if (updateInProgress) {
318  return;
319  }
320 
321  updateInProgress = true;
322 
323  bool hideYaw = m_stabilization->cbLinkRateRollYaw->isChecked();
324  bool hidePitch = m_stabilization->cbLinkRateRollPitch->isChecked();
325 
326  if (hideYaw) {
327  m_stabilization->fullStickRateYaw->setValue(m_stabilization->fullStickRateRoll->value());
328  m_stabilization->centerStickRateYaw->setValue(
329  m_stabilization->centerStickRateRoll->value());
330  m_stabilization->rateYawLT->setValue(m_stabilization->rateRollLT->value());
331  }
332 
333  if (hidePitch) {
334  m_stabilization->fullStickRatePitch->setValue(m_stabilization->fullStickRateRoll->value());
335  m_stabilization->centerStickRatePitch->setValue(
336  m_stabilization->centerStickRateRoll->value());
337  m_stabilization->ratePitchLT->setValue(m_stabilization->rateRollLT->value());
338  }
339 
340  m_stabilization->rateRollExpo->setValue(100
341  - m_stabilization->centerStickRateRoll->value() * 100
342  / m_stabilization->fullStickRateRoll->value());
343  m_stabilization->ratePitchExpo->setValue(100
344  - m_stabilization->centerStickRatePitch->value() * 100
345  / m_stabilization->fullStickRatePitch->value());
346  m_stabilization->rateYawExpo->setValue(100
347  - m_stabilization->centerStickRateYaw->value() * 100
348  / m_stabilization->fullStickRateYaw->value());
349 
350  m_stabilization->rateRollExponent->setValue(
351  log(0.01) / log((m_stabilization->rateRollLT->value() / 100.0f)));
352  m_stabilization->ratePitchExponent->setValue(
353  log(0.01) / log((m_stabilization->ratePitchLT->value() / 100.0f)));
354  m_stabilization->rateYawExponent->setValue(
355  log(0.01) / log((m_stabilization->rateYawLT->value() / 100.0f)));
356 
357  updateInProgress = false;
358 
359  updateGraphs();
360 }
361 
362 void ConfigStabilizationWidget::updateGraphs()
363 {
364  m_stabilization->expoPlot->plotDataRoll(m_stabilization->rateRollExpo->value(),
365  m_stabilization->fullStickRateRoll->value(),
366  m_stabilization->rateRollExponent->value() * 10.0);
367  m_stabilization->expoPlot->plotDataPitch(m_stabilization->ratePitchExpo->value(),
368  m_stabilization->fullStickRatePitch->value(),
369  m_stabilization->ratePitchExponent->value() * 10.0);
370  m_stabilization->expoPlot->plotDataYaw(m_stabilization->rateYawExpo->value(),
371  m_stabilization->fullStickRateYaw->value(),
372  m_stabilization->rateYawExponent->value() * 10.0);
373 }
374 
375 void ConfigStabilizationWidget::processLinkedWidgets(QWidget *widget)
376 {
377  if (m_stabilization->checkBox_7->checkState() == Qt::Checked) {
378  if (widget == m_stabilization->RateRollKp) {
379  m_stabilization->RatePitchKp->setValue(m_stabilization->RateRollKp->value());
380  } else if (widget == m_stabilization->RateRollKi) {
381  m_stabilization->RatePitchKi->setValue(m_stabilization->RateRollKi->value());
382  } else if (widget == m_stabilization->RateRollILimit) {
383  m_stabilization->RatePitchILimit->setValue(m_stabilization->RateRollILimit->value());
384  } else if (widget == m_stabilization->RatePitchKp) {
385  m_stabilization->RateRollKp->setValue(m_stabilization->RatePitchKp->value());
386  } else if (widget == m_stabilization->RatePitchKi) {
387  m_stabilization->RateRollKi->setValue(m_stabilization->RatePitchKi->value());
388  } else if (widget == m_stabilization->RatePitchILimit) {
389  m_stabilization->RateRollILimit->setValue(m_stabilization->RatePitchILimit->value());
390  } else if (widget == m_stabilization->RollRateKd) {
391  m_stabilization->PitchRateKd->setValue(m_stabilization->RollRateKd->value());
392  } else if (widget == m_stabilization->PitchRateKd) {
393  m_stabilization->RollRateKd->setValue(m_stabilization->PitchRateKd->value());
394  }
395  }
396  if (m_stabilization->checkBox_8->checkState() == Qt::Checked) {
397  if (widget == m_stabilization->AttitudeRollKp) {
398  m_stabilization->AttitudePitchKp->setValue(m_stabilization->AttitudeRollKp->value());
399  } else if (widget == m_stabilization->AttitudeRollKi) {
400  m_stabilization->AttitudePitchKi->setValue(m_stabilization->AttitudeRollKi->value());
401  } else if (widget == m_stabilization->AttitudeRollILimit) {
402  m_stabilization->AttitudePitchILimit->setValue(
403  m_stabilization->AttitudeRollILimit->value());
404  } else if (widget == m_stabilization->AttitudePitchKp) {
405  m_stabilization->AttitudeRollKp->setValue(m_stabilization->AttitudePitchKp->value());
406  } else if (widget == m_stabilization->AttitudePitchKi) {
407  m_stabilization->AttitudeRollKi->setValue(m_stabilization->AttitudePitchKi->value());
408  } else if (widget == m_stabilization->AttitudePitchILimit) {
409  m_stabilization->AttitudeRollILimit->setValue(
410  m_stabilization->AttitudePitchILimit->value());
411  }
412  }
413 }
414 
415 void ConfigStabilizationWidget::applyRateLimits()
416 {
418 
419  double maxRate = 500; // Default to slowest across boards
420  if (board)
421  maxRate = board->queryMaxGyroRate() * 0.85;
422 
423  m_stabilization->fullStickRateRoll->setMaximum(maxRate);
424  m_stabilization->fullStickRatePitch->setMaximum(maxRate);
425  m_stabilization->fullStickRateYaw->setMaximum(maxRate);
426 }
427 
428 void ConfigStabilizationWidget::hangtimeDurationChanged()
429 {
430  bool warn = m_stabilization->sbHangtimeDuration->value() > 0.0;
431 
432  if (warn && !m_stabilization->gbHangtime->isChecked())
433  m_stabilization->gbHangtime->setChecked(true);
434  else if (!warn && m_stabilization->gbHangtime->isChecked())
435  m_stabilization->gbHangtime->setChecked(false);
436 
437  if (manualControlSettings) {
438  UAVObjectField *field = manualControlSettings->getField("Arming");
439  if (field) {
440  const QString option = field->getValue().toString();
441  warn &= !option.startsWith("Switch") && option != "Always Disarmed";
442  }
443  }
444  m_stabilization->lblSwitchArmingWarning->setVisible(warn);
445 }
446 
447 void ConfigStabilizationWidget::hangtimeToggle(bool enabled)
448 {
449  if (!enabled) {
450  m_stabilization->sbHangtimeDuration->setValue(0.0); // 0.0 is disabled
451  } else if (m_stabilization->sbHangtimeDuration->value() == 0.0) {
452  m_stabilization->sbHangtimeDuration->setValue(2.5); // default duration in s
453 
454  UAVObject *systemIdent =
456 
457  if (systemIdent) {
458  UAVObjectField *field = systemIdent->getField("HoverThrottle");
459 
460  if (field) {
461  float hoverThrottle = field->getValue().toFloat();
462  if (hoverThrottle > 0.01) {
463  float powerAdd = hoverThrottle * 85;
464 
465  if (powerAdd > 20) {
466  powerAdd = 20;
467  }
468 
469  m_stabilization->sbHangtimePower->setValue(powerAdd);
470  }
471  }
472  }
473 
474  }
475 }
void autoPilotConnected()
QVariant getValue(int index=0) const
void objectUpdated(UAVObject *obj)
Signal sent whenever any field of the object is updated.
virtual int queryMaxGyroRate()
Query the board for the currently set max rate of the gyro.
Definition: iboardtype.h:216
UAVObjectField * getField(const QString &name)
Definition: uavobject.cpp:236
void autoPilotDisconnected()
() NAME()
ConfigStabilizationWidget(QWidget *parent=nullptr)
void widgetContentsChanged(QWidget *widget)
UAVObject * getObject(const QString &name, quint32 instId=0)
UAVObjectUtilManager * getObjectUtilManager()
ConfigTaskWidget::getObjectUtilManager Utility function to get a pointer to the object manager utilit...
Core::IBoardType * getBoardType()
Get the IBoardType corresponding to the connected board.
UAVObjectManager * getObjectManager()
ConfigTaskWidget::getObjectManager Utility function to get a pointer to the object manager...