LP-106 Setup Wizard refresh : Add dual servo setup (dual aileron or
[librepilot.git] / ground / gcs / src / plugins / setupwizard / pages / esccalibrationpage.cpp
blob72010315104205fa6bdb3d40df6cbc85834cb605
1 /**
2 ******************************************************************************
4 * @file EscCalibrationPage.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
6 * @addtogroup
7 * @{
8 * @addtogroup EscCalibrationPage
9 * @{
10 * @brief
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "esccalibrationpage.h"
30 #include "ui_esccalibrationpage.h"
31 #include "setupwizard.h"
32 #include "mixersettings.h"
33 #include "extensionsystem/pluginmanager.h"
34 #include "uavobjectmanager.h"
35 #include "vehicleconfigurationhelper.h"
36 #include "actuatorsettings.h"
38 #include <QThread>
40 EscCalibrationPage::EscCalibrationPage(SetupWizard *wizard, QWidget *parent) :
41 AbstractWizardPage(wizard, parent),
42 ui(new Ui::EscCalibrationPage), m_isCalibrating(false)
44 ui->setupUi(this);
46 ui->outputHigh->setEnabled(false);
47 ui->outputLow->setEnabled(true);
48 ui->outputLevel->setEnabled(true);
49 ui->outputLevel->setText(QString(tr("%1 µs")).arg(OFF_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS));
51 connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startButtonClicked()));
52 connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stopButtonClicked()));
54 connect(ui->securityCheckBox1, SIGNAL(toggled(bool)), this, SLOT(securityCheckBoxesToggled()));
55 connect(ui->securityCheckBox2, SIGNAL(toggled(bool)), this, SLOT(securityCheckBoxesToggled()));
56 connect(ui->securityCheckBox3, SIGNAL(toggled(bool)), this, SLOT(securityCheckBoxesToggled()));
59 EscCalibrationPage::~EscCalibrationPage()
61 delete ui;
64 bool EscCalibrationPage::validatePage()
66 return true;
69 void EscCalibrationPage::enableButtons(bool enable)
71 getWizard()->button(QWizard::NextButton)->setEnabled(enable);
72 getWizard()->button(QWizard::CancelButton)->setEnabled(enable);
73 getWizard()->button(QWizard::BackButton)->setEnabled(enable);
74 getWizard()->button(QWizard::CustomButton1)->setEnabled(enable);
75 ui->securityCheckBox1->setEnabled(enable);
76 ui->securityCheckBox2->setEnabled(enable);
77 ui->securityCheckBox3->setEnabled(enable);
78 QApplication::processEvents();
81 void EscCalibrationPage::resetAllSecurityCheckboxes()
83 ui->securityCheckBox1->setChecked(false);
84 ui->securityCheckBox2->setChecked(false);
85 ui->securityCheckBox3->setChecked(false);
88 int EscCalibrationPage::getHighOutputRate()
90 if (getWizard()->getEscType() == SetupWizard::ESC_ONESHOT) {
91 return HIGH_ONESHOT125_OUTPUT_PULSE_LENGTH_MICROSECONDS;
92 } else {
93 return HIGH_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS;
97 void EscCalibrationPage::startButtonClicked()
99 if (!m_isCalibrating) {
100 m_isCalibrating = true;
101 ui->startButton->setEnabled(false);
102 enableButtons(false);
103 ui->outputHigh->setEnabled(true);
104 ui->outputLow->setEnabled(false);
105 ui->nonconnectedLabel->setEnabled(false);
106 ui->connectedLabel->setEnabled(true);
107 ui->outputLevel->setText(QString(tr("%1 µs")).arg(getHighOutputRate()));
108 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
109 UAVObjectManager *uavoManager = pm->getObject<UAVObjectManager>();
110 Q_ASSERT(uavoManager);
111 MixerSettings *mSettings = MixerSettings::GetInstance(uavoManager);
112 Q_ASSERT(mSettings);
113 QString mixerTypePattern = "Mixer%1Type";
115 OutputCalibrationUtil::startOutputCalibration();
116 // First check if any servo and set his value to 1500 (like Tricopter)
117 for (quint32 i = 0; i < ActuatorSettings::CHANNELADDR_NUMELEM; i++) {
118 UAVObjectField *field = mSettings->getField(mixerTypePattern.arg(i + 1));
119 Q_ASSERT(field);
120 if (field->getValue().toString() == field->getOptions().at(VehicleConfigurationHelper::MIXER_TYPE_SERVO)) {
121 m_outputUtil.startChannelOutput(i, 1500);
122 m_outputUtil.stopChannelOutput();
125 // Find motors and start Esc procedure
126 for (quint32 i = 0; i < ActuatorSettings::CHANNELADDR_NUMELEM; i++) {
127 UAVObjectField *field = mSettings->getField(mixerTypePattern.arg(i + 1));
128 Q_ASSERT(field);
129 if (field->getValue().toString() == field->getOptions().at(VehicleConfigurationHelper::MIXER_TYPE_MOTOR)) {
130 m_outputChannels << i;
133 m_outputUtil.startChannelOutput(m_outputChannels, OFF_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS);
134 QThread::msleep(100);
135 m_outputUtil.setChannelOutputValue(getHighOutputRate());
137 ui->stopButton->setEnabled(true);
141 void EscCalibrationPage::stopButtonClicked()
143 if (m_isCalibrating) {
144 ui->stopButton->setEnabled(false);
145 ui->outputHigh->setEnabled(false);
147 // Set to low pwm out
148 m_outputUtil.setChannelOutputValue(LOW_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS);
149 ui->outputLevel->setText(QString(tr("%1 µs")).arg(LOW_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS));
150 QApplication::processEvents();
151 QThread::msleep(2000);
153 // Ramp down to off pwm out
154 for (int i = LOW_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS; i >= OFF_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS; i -= 10) {
155 m_outputUtil.setChannelOutputValue(i);
156 ui->outputLevel->setText(QString(tr("%1 µs")).arg(i));
157 QApplication::processEvents();
158 QThread::msleep(200);
161 // Stop output
162 m_outputUtil.stopChannelOutput();
163 OutputCalibrationUtil::stopOutputCalibration();
165 ui->outputLevel->setText(QString(tr("%1 µs")).arg(OFF_PWM_OUTPUT_PULSE_LENGTH_MICROSECONDS));
166 ui->outputHigh->setEnabled(false);
167 ui->outputLow->setEnabled(true);
168 ui->nonconnectedLabel->setEnabled(true);
169 ui->connectedLabel->setEnabled(false);
170 m_outputChannels.clear();
171 m_isCalibrating = false;
172 resetAllSecurityCheckboxes();
173 enableButtons(true);
177 void EscCalibrationPage::securityCheckBoxesToggled()
179 ui->startButton->setEnabled(ui->securityCheckBox1->isChecked() &&
180 ui->securityCheckBox2->isChecked() &&
181 ui->securityCheckBox3->isChecked());
185 void EscCalibrationPage::initializePage()
187 resetAllSecurityCheckboxes();