LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / plugins / config / configoplinkwidget.cpp
blob8fea81c2f974b7150777a9a6caf220180619c577
1 /**
2 ******************************************************************************
4 * @file configoplinkwidget.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015-2016.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup ConfigPlugin Config Plugin
10 * @{
11 * @brief The Configuration Gadget used to configure the OPLink and Revo modem
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "configoplinkwidget.h"
31 #include "ui_oplink.h"
33 #include <coreplugin/generalsettings.h>
34 #include <uavobjectmanager.h>
36 #include <oplinksettings.h>
37 #include <oplinkstatus.h>
39 #include <QMessageBox>
40 #include <QDateTime>
42 // Channel range and Frequency display
43 static const int MAX_CHANNEL_NUM = 250;
44 static const int MIN_CHANNEL_RANGE = 10;
45 static const float FIRST_FREQUENCY = 430.000;
46 static const float FREQUENCY_STEP = 0.040;
48 ConfigOPLinkWidget::ConfigOPLinkWidget(QWidget *parent) : ConfigTaskWidget(parent)
50 m_oplink = new Ui_OPLinkWidget();
51 m_oplink->setupUi(this);
53 // Connect to the OPLinkStatus object updates
54 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
55 UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
56 oplinkStatusObj = dynamic_cast<UAVDataObject *>(objManager->getObject("OPLinkStatus"));
57 Q_ASSERT(oplinkStatusObj);
58 connect(oplinkStatusObj, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(updateStatus(UAVObject *)));
60 // Connect to the OPLinkSettings object updates
61 oplinkSettingsObj = dynamic_cast<OPLinkSettings *>(objManager->getObject("OPLinkSettings"));
62 Q_ASSERT(oplinkSettingsObj);
63 connect(oplinkSettingsObj, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(updateSettings(UAVObject *)));
65 Core::Internal::GeneralSettings *settings = pm->getObject<Core::Internal::GeneralSettings>();
66 if (!settings->useExpertMode()) {
67 m_oplink->Apply->setVisible(false);
69 addApplySaveButtons(m_oplink->Apply, m_oplink->Save);
71 addWidgetBinding("OPLinkSettings", "MainPort", m_oplink->MainPort);
72 addWidgetBinding("OPLinkSettings", "FlexiPort", m_oplink->FlexiPort);
73 addWidgetBinding("OPLinkSettings", "VCPPort", m_oplink->VCPPort);
74 addWidgetBinding("OPLinkSettings", "MaxRFPower", m_oplink->MaxRFTxPower);
75 addWidgetBinding("OPLinkSettings", "MinChannel", m_oplink->MinimumChannel);
76 addWidgetBinding("OPLinkSettings", "MaxChannel", m_oplink->MaximumChannel);
77 addWidgetBinding("OPLinkSettings", "CoordID", m_oplink->CoordID);
78 addWidgetBinding("OPLinkSettings", "Coordinator", m_oplink->Coordinator);
79 addWidgetBinding("OPLinkSettings", "OneWay", m_oplink->OneWayLink);
80 addWidgetBinding("OPLinkSettings", "PPMOnly", m_oplink->PPMOnly);
81 addWidgetBinding("OPLinkSettings", "PPM", m_oplink->PPM);
82 addWidgetBinding("OPLinkSettings", "ComSpeed", m_oplink->ComSpeed);
83 addWidgetBinding("OPLinkSettings", "CustomDeviceID", m_oplink->CustomDeviceID);
85 addWidgetBinding("OPLinkStatus", "DeviceID", m_oplink->DeviceID);
86 addWidgetBinding("OPLinkStatus", "RxGood", m_oplink->Good);
87 addWidgetBinding("OPLinkStatus", "RxCorrected", m_oplink->Corrected);
88 addWidgetBinding("OPLinkStatus", "RxErrors", m_oplink->Errors);
89 addWidgetBinding("OPLinkStatus", "RxMissed", m_oplink->Missed);
90 addWidgetBinding("OPLinkStatus", "RxFailure", m_oplink->RxFailure);
91 addWidgetBinding("OPLinkStatus", "UAVTalkErrors", m_oplink->UAVTalkErrors);
92 addWidgetBinding("OPLinkStatus", "TxDropped", m_oplink->Dropped);
93 addWidgetBinding("OPLinkStatus", "TxFailure", m_oplink->TxFailure);
94 addWidgetBinding("OPLinkStatus", "Resets", m_oplink->Resets);
95 addWidgetBinding("OPLinkStatus", "Timeouts", m_oplink->Timeouts);
96 addWidgetBinding("OPLinkStatus", "RSSI", m_oplink->RSSI);
97 addWidgetBinding("OPLinkStatus", "HeapRemaining", m_oplink->FreeHeap);
98 addWidgetBinding("OPLinkStatus", "LinkQuality", m_oplink->LinkQuality);
99 addWidgetBinding("OPLinkStatus", "RXSeq", m_oplink->RXSeq);
100 addWidgetBinding("OPLinkStatus", "TXSeq", m_oplink->TXSeq);
101 addWidgetBinding("OPLinkStatus", "RXRate", m_oplink->RXRate);
102 addWidgetBinding("OPLinkStatus", "TXRate", m_oplink->TXRate);
103 addWidgetBinding("OPLinkStatus", "RXPacketRate", m_oplink->RXPacketRate);
104 addWidgetBinding("OPLinkStatus", "TXPacketRate", m_oplink->TXPacketRate);
106 // Connect the selection changed signals.
107 connect(m_oplink->PPMOnly, SIGNAL(toggled(bool)), this, SLOT(ppmOnlyChanged()));
108 connect(m_oplink->Coordinator, SIGNAL(toggled(bool)), this, SLOT(updateCoordID()));
109 connect(m_oplink->MinimumChannel, SIGNAL(valueChanged(int)), this, SLOT(minChannelChanged()));
110 connect(m_oplink->MaximumChannel, SIGNAL(valueChanged(int)), this, SLOT(maxChannelChanged()));
111 connect(m_oplink->CustomDeviceID, SIGNAL(editingFinished()), this, SLOT(updateCustomDeviceID()));
113 m_oplink->CustomDeviceID->setInputMask("HHHHHHHH");
114 m_oplink->CoordID->setInputMask("HHHHHHHH");
116 m_oplink->MinimumChannel->setKeyboardTracking(false);
117 m_oplink->MaximumChannel->setKeyboardTracking(false);
119 m_oplink->MaximumChannel->setMaximum(MAX_CHANNEL_NUM);
120 m_oplink->MinimumChannel->setMaximum(MAX_CHANNEL_NUM - MIN_CHANNEL_RANGE);
122 // Request and update of the setting object.
123 settingsUpdated = false;
124 setWikiURL("OPLink+Configuration");
125 autoLoadWidgets();
126 disableMouseWheelEvents();
127 updateEnableControls();
130 ConfigOPLinkWidget::~ConfigOPLinkWidget()
134 \brief Called by updates to @OPLinkStatus
136 void ConfigOPLinkWidget::updateStatus(UAVObject *object)
138 // Request and update of the setting object if we haven't received it yet.
139 if (!settingsUpdated) {
140 oplinkSettingsObj->requestUpdate();
143 // Update the link state
144 UAVObjectField *linkField = object->getField("LinkState");
145 m_oplink->LinkState->setText(linkField->getValue().toString());
146 bool linkConnected = (linkField->getValue() == linkField->getOptions().at(OPLinkStatus::LINKSTATE_CONNECTED));
148 m_oplink->PairSignalStrengthBar1->setValue(linkConnected ? m_oplink->RSSI->text().toInt() : -127);
149 m_oplink->PairSignalStrengthLabel1->setText(QString("%1dB").arg(m_oplink->PairSignalStrengthBar1->value()));
151 // Update the Description field
152 // TODO use UAVObjectUtilManager::descriptionToStructure()
153 UAVObjectField *descField = object->getField("Description");
154 if (descField->getValue(0) != QChar(255)) {
156 * This looks like a binary with a description at the end:
157 * 4 bytes: header: "OpFw".
158 * 4 bytes: GIT commit tag (short version of SHA1).
159 * 4 bytes: Unix timestamp of compile time.
160 * 2 bytes: target platform. Should follow same rule as BOARD_TYPE and BOARD_REVISION in board define files.
161 * 26 bytes: commit tag if it is there, otherwise branch name. '-dirty' may be added if needed. Zero-padded.
162 * 20 bytes: SHA1 sum of the firmware.
163 * 20 bytes: SHA1 sum of the uavo definitions.
164 * 20 bytes: free for now.
166 char buf[OPLinkStatus::DESCRIPTION_NUMELEM];
167 for (unsigned int i = 0; i < 26; ++i) {
168 buf[i] = descField->getValue(i + 14).toChar().toLatin1();
170 buf[26] = '\0';
171 QString descstr(buf);
172 quint32 gitDate = descField->getValue(11).toChar().toLatin1() & 0xFF;
173 for (int i = 1; i < 4; i++) {
174 gitDate = gitDate << 8;
175 gitDate += descField->getValue(11 - i).toChar().toLatin1() & 0xFF;
177 QString date = QDateTime::fromTime_t(gitDate).toUTC().toString("yyyy-MM-dd HH:mm");
178 m_oplink->FirmwareVersion->setText(descstr + " " + date);
179 } else {
180 m_oplink->FirmwareVersion->setText(tr("Unknown"));
183 // Update the serial number field
184 UAVObjectField *serialField = object->getField("CPUSerial");
185 char buf[OPLinkStatus::CPUSERIAL_NUMELEM * 2 + 1];
186 for (unsigned int i = 0; i < OPLinkStatus::CPUSERIAL_NUMELEM; ++i) {
187 unsigned char val = serialField->getValue(i).toUInt() >> 4;
188 buf[i * 2] = ((val < 10) ? '0' : '7') + val;
189 val = serialField->getValue(i).toUInt() & 0xf;
190 buf[i * 2 + 1] = ((val < 10) ? '0' : '7') + val;
192 buf[OPLinkStatus::CPUSERIAL_NUMELEM * 2] = '\0';
193 m_oplink->SerialNumber->setText(buf);
195 updateEnableControls();
199 \brief Called by updates to @OPLinkSettings
201 void ConfigOPLinkWidget::updateSettings(UAVObject *object)
203 Q_UNUSED(object);
205 if (!settingsUpdated) {
206 settingsUpdated = true;
207 // Enable components based on the board type connected.
208 UAVObjectField *board_type_field = oplinkStatusObj->getField("BoardType");
209 switch (board_type_field->getValue().toInt()) {
210 case 0x09: // Revolution
211 m_oplink->MainPort->setVisible(false);
212 m_oplink->MainPortLabel->setVisible(false);
213 m_oplink->FlexiPort->setVisible(false);
214 m_oplink->FlexiPortLabel->setVisible(false);
215 m_oplink->VCPPort->setVisible(false);
216 m_oplink->VCPPortLabel->setVisible(false);
217 m_oplink->FlexiIOPort->setVisible(false);
218 m_oplink->FlexiIOPortLabel->setVisible(false);
219 m_oplink->PPM->setEnabled(true);
220 break;
221 case 0x03: // OPLinkMini
222 m_oplink->MainPort->setVisible(true);
223 m_oplink->MainPortLabel->setVisible(true);
224 m_oplink->FlexiPort->setVisible(true);
225 m_oplink->FlexiPortLabel->setVisible(true);
226 m_oplink->VCPPort->setVisible(true);
227 m_oplink->VCPPortLabel->setVisible(true);
228 m_oplink->FlexiIOPort->setVisible(false);
229 m_oplink->FlexiIOPortLabel->setVisible(false);
230 m_oplink->PPM->setEnabled(false);
231 connect(m_oplink->MainPort, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePPMOptions()));
232 connect(m_oplink->FlexiPort, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePPMOptions()));
233 break;
234 case 0x0A: // OPLink?
235 m_oplink->MainPort->setVisible(true);
236 m_oplink->MainPortLabel->setVisible(true);
237 m_oplink->FlexiPort->setVisible(true);
238 m_oplink->FlexiPortLabel->setVisible(true);
239 m_oplink->VCPPort->setVisible(true);
240 m_oplink->VCPPortLabel->setVisible(true);
241 m_oplink->FlexiIOPort->setVisible(true);
242 m_oplink->FlexiIOPortLabel->setVisible(true);
243 m_oplink->PPM->setEnabled(false);
244 connect(m_oplink->MainPort, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePPMOptions()));
245 connect(m_oplink->FlexiPort, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePPMOptions()));
246 break;
247 default:
248 // This shouldn't happen.
249 break;
251 updateEnableControls();
255 void ConfigOPLinkWidget::updateEnableControls()
257 enableControls(true);
258 updatePPMOptions();
259 updateCustomDeviceID();
260 updateCoordID();
261 ppmOnlyChanged();
264 void ConfigOPLinkWidget::disconnected()
266 if (settingsUpdated) {
267 settingsUpdated = false;
271 void ConfigOPLinkWidget::updatePPMOptions()
273 bool is_oplm = m_oplink->MainPort->isVisible();
275 if (!is_oplm) {
276 return;
279 bool is_coordinator = m_oplink->Coordinator->isChecked();
280 bool is_ppm_active = ((isComboboxOptionSelected(m_oplink->MainPort, OPLinkSettings::MAINPORT_PPM)) ||
281 (isComboboxOptionSelected(m_oplink->FlexiPort, OPLinkSettings::FLEXIPORT_PPM)));
283 m_oplink->PPM->setEnabled(false);
284 m_oplink->PPM->setChecked(is_ppm_active);
285 m_oplink->PPMOnly->setEnabled(is_ppm_active);
287 if (!is_ppm_active) {
288 m_oplink->PPMOnly->setChecked(false);
289 QString selectPort = tr("Please select a port for PPM function.");
290 m_oplink->PPMOnly->setToolTip(selectPort);
291 m_oplink->PPM->setToolTip(selectPort);
292 } else {
293 if (is_coordinator) {
294 m_oplink->PPMOnly->setToolTip(tr("Only PPM packets will be transmitted and baudrate set to 9600 bauds by default."));
295 m_oplink->PPM->setToolTip(tr("PPM packets will be transmitted by this modem."));
296 } else {
297 m_oplink->PPMOnly->setToolTip(tr("Only PPM packets will be received and baudrate set to 9600 bauds by default."));
298 m_oplink->PPM->setToolTip(tr("PPM packets will be received by this modem."));
304 void ConfigOPLinkWidget::ppmOnlyChanged()
306 bool is_ppm_only = m_oplink->PPMOnly->isChecked();
307 bool is_oplm = m_oplink->MainPort->isVisible();
309 m_oplink->PPM->setEnabled(!is_ppm_only && !is_oplm);
310 m_oplink->OneWayLink->setEnabled(!is_ppm_only);
311 m_oplink->ComSpeed->setEnabled(!is_ppm_only);
314 void ConfigOPLinkWidget::minChannelChanged()
316 channelChanged(false);
319 void ConfigOPLinkWidget::maxChannelChanged()
321 channelChanged(true);
324 void ConfigOPLinkWidget::channelChanged(bool isMax)
326 int minChannel = m_oplink->MinimumChannel->value();
327 int maxChannel = m_oplink->MaximumChannel->value();
329 if ((maxChannel - minChannel) < MIN_CHANNEL_RANGE) {
330 if (isMax) {
331 minChannel = maxChannel - MIN_CHANNEL_RANGE;
332 } else {
333 maxChannel = minChannel + MIN_CHANNEL_RANGE;
336 if (maxChannel > MAX_CHANNEL_NUM) {
337 maxChannel = MAX_CHANNEL_NUM;
338 minChannel = MAX_CHANNEL_NUM - MIN_CHANNEL_RANGE;
341 if (minChannel < 0) {
342 minChannel = 0;
343 maxChannel = MIN_CHANNEL_RANGE;
347 m_oplink->MaximumChannel->setValue(maxChannel);
348 m_oplink->MinimumChannel->setValue(minChannel);
350 // Calculate and Display frequency in MHz
351 float minFrequency = FIRST_FREQUENCY + (minChannel * FREQUENCY_STEP);
352 float maxFrequency = FIRST_FREQUENCY + (maxChannel * FREQUENCY_STEP);
354 m_oplink->MinFreq->setText("(" + QString::number(minFrequency, 'f', 3) + " MHz)");
355 m_oplink->MaxFreq->setText("(" + QString::number(maxFrequency, 'f', 3) + " MHz)");
358 void ConfigOPLinkWidget::updateCoordID()
360 bool is_coordinator = m_oplink->Coordinator->isChecked();
361 bool coordinatorNotSet = (m_oplink->CoordID->text() == "0");
363 if (settingsUpdated && coordinatorNotSet) {
364 m_oplink->CoordID->clear();
366 m_oplink->CoordID->setEnabled(!is_coordinator);
369 void ConfigOPLinkWidget::updateCustomDeviceID()
371 bool customDeviceIDNotSet = (m_oplink->CustomDeviceID->text() == "0");
373 if (settingsUpdated && customDeviceIDNotSet) {
374 m_oplink->CustomDeviceID->clear();
375 m_oplink->CustomDeviceID->setPlaceholderText("AutoGen");