2 ******************************************************************************
4 * @file configccattitudewidget.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
7 * @addtogroup GCSPlugins GCS Plugins
9 * @addtogroup ConfigPlugin Config Plugin
11 * @brief Configure Attitude module on CopterControl
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
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
28 #include "configccattitudewidget.h"
30 #include "ui_ccattitude.h"
32 #include "utils/coordinateconversions.h"
33 #include <extensionsystem/pluginmanager.h>
34 #include <coreplugin/generalsettings.h>
35 #include <calibration/calibrationutils.h>
37 #include "attitudesettings.h"
38 #include "accelstate.h"
39 #include "accelgyrosettings.h"
40 #include "gyrostate.h"
42 #include <QMutexLocker>
43 #include <QMessageBox>
45 #include <QDesktopServices>
48 ConfigCCAttitudeWidget::ConfigCCAttitudeWidget(QWidget
*parent
) :
49 ConfigTaskWidget(parent
),
53 connect(ui
->zeroBias
, SIGNAL(clicked()), this, SLOT(startAccelCalibration()));
55 ExtensionSystem::PluginManager
*pm
= ExtensionSystem::PluginManager::instance();
56 Core::Internal::GeneralSettings
*settings
= pm
->getObject
<Core::Internal::GeneralSettings
>();
57 if (!settings
->useExpertMode()) {
58 ui
->applyButton
->setVisible(false);
61 addApplySaveButtons(ui
->applyButton
, ui
->saveButton
);
62 addUAVObject("AttitudeSettings");
63 addUAVObject("AccelGyroSettings");
65 // Connect the help button
66 connect(ui
->ccAttitudeHelp
, SIGNAL(clicked()), this, SLOT(openHelp()));
68 addWidgetBinding("AttitudeSettings", "ZeroDuringArming", ui
->zeroGyroBiasOnArming
);
69 addWidgetBinding("AttitudeSettings", "AccelTau", ui
->accelTauSpinbox
);
71 addWidgetBinding("AttitudeSettings", "BoardRotation", ui
->rollBias
, AttitudeSettings::BOARDROTATION_ROLL
);
72 addWidgetBinding("AttitudeSettings", "BoardRotation", ui
->pitchBias
, AttitudeSettings::BOARDROTATION_PITCH
);
73 addWidgetBinding("AttitudeSettings", "BoardRotation", ui
->yawBias
, AttitudeSettings::BOARDROTATION_YAW
);
74 addWidget(ui
->zeroBias
);
76 refreshWidgetsValues();
77 forceConnectedState();
80 ConfigCCAttitudeWidget::~ConfigCCAttitudeWidget()
85 void ConfigCCAttitudeWidget::sensorsUpdated(UAVObject
*obj
)
87 if (!timer
.isActive()) {
88 // ignore updates that come in after the timer has expired
92 AccelState
*accelState
= AccelState::GetInstance(getObjectManager());
93 GyroState
*gyroState
= GyroState::GetInstance(getObjectManager());
95 // Accumulate samples until we have _at least_ NUM_SENSOR_UPDATES samples
96 // for both gyros and accels.
97 // Note that, at present, we stash the samples and then compute the bias
98 // at the end, even though the mean could be accumulated as we go.
99 // In future, a better algorithm could be used.
100 if (obj
->getObjID() == AccelState::OBJID
) {
102 AccelState::DataFields accelStateData
= accelState
->getData();
103 x_accum
.append(accelStateData
.x
);
104 y_accum
.append(accelStateData
.y
);
105 z_accum
.append(accelStateData
.z
);
106 } else if (obj
->getObjID() == GyroState::OBJID
) {
108 GyroState::DataFields gyroStateData
= gyroState
->getData();
109 x_gyro_accum
.append(gyroStateData
.x
);
110 y_gyro_accum
.append(gyroStateData
.y
);
111 z_gyro_accum
.append(gyroStateData
.z
);
114 // update the progress indicator
115 ui
->zeroBiasProgress
->setValue((float)qMin(accelUpdates
, gyroUpdates
) / NUM_SENSOR_UPDATES
* 100);
117 // If we have enough samples, then stop sampling and compute the biases
118 if (accelUpdates
>= NUM_SENSOR_UPDATES
&& gyroUpdates
>= NUM_SENSOR_UPDATES
) {
120 disconnect(obj
, SIGNAL(objectUpdated(UAVObject
*)), this, SLOT(sensorsUpdated(UAVObject
*)));
121 disconnect(&timer
, SIGNAL(timeout()), this, SLOT(timeout()));
123 float x_bias
= OpenPilot::CalibrationUtils::listMean(x_accum
);
124 float y_bias
= OpenPilot::CalibrationUtils::listMean(y_accum
);
125 float z_bias
= OpenPilot::CalibrationUtils::listMean(z_accum
) + 9.81;
127 float x_gyro_bias
= OpenPilot::CalibrationUtils::listMean(x_gyro_accum
);
128 float y_gyro_bias
= OpenPilot::CalibrationUtils::listMean(y_gyro_accum
);
129 float z_gyro_bias
= OpenPilot::CalibrationUtils::listMean(z_gyro_accum
);
130 accelState
->setMetadata(initialAccelStateMdata
);
131 gyroState
->setMetadata(initialGyroStateMdata
);
133 AccelGyroSettings::DataFields accelGyroSettingsData
= AccelGyroSettings::GetInstance(getObjectManager())->getData();
134 AttitudeSettings::DataFields attitudeSettingsData
= AttitudeSettings::GetInstance(getObjectManager())->getData();
135 // We offset the gyro bias by current bias to help precision
136 accelGyroSettingsData
.accel_bias
[0] += x_bias
;
137 accelGyroSettingsData
.accel_bias
[1] += y_bias
;
138 accelGyroSettingsData
.accel_bias
[2] += z_bias
;
139 accelGyroSettingsData
.gyro_bias
[0] = -x_gyro_bias
;
140 accelGyroSettingsData
.gyro_bias
[1] = -y_gyro_bias
;
141 accelGyroSettingsData
.gyro_bias
[2] = -z_gyro_bias
;
142 attitudeSettingsData
.BiasCorrectGyro
= AttitudeSettings::BIASCORRECTGYRO_TRUE
;
143 AttitudeSettings::GetInstance(getObjectManager())->setData(attitudeSettingsData
);
144 AccelGyroSettings::GetInstance(getObjectManager())->setData(accelGyroSettingsData
);
145 this->setDirty(true);
148 enableControls(true);
152 void ConfigCCAttitudeWidget::timeout()
154 UAVDataObject
*obj
= AccelState::GetInstance(getObjectManager());
156 disconnect(obj
, SIGNAL(objectUpdated(UAVObject
*)), this, SLOT(sensorsUpdated(UAVObject
*)));
157 disconnect(&timer
, SIGNAL(timeout()), this, SLOT(timeout()));
159 AccelState
*accelState
= AccelState::GetInstance(getObjectManager());
160 GyroState
*gyroState
= GyroState::GetInstance(getObjectManager());
161 accelState
->setMetadata(initialAccelStateMdata
);
162 gyroState
->setMetadata(initialGyroStateMdata
);
165 msgBox
.setText(tr("Calibration timed out before receiving required updates."));
166 msgBox
.setStandardButtons(QMessageBox::Ok
);
167 msgBox
.setDefaultButton(QMessageBox::Ok
);
170 // reset progress indicator
171 ui
->zeroBiasProgress
->setValue(0);
173 enableControls(true);
176 void ConfigCCAttitudeWidget::startAccelCalibration()
178 // disable controls during sampling
179 enableControls(false);
186 x_gyro_accum
.clear();
187 y_gyro_accum
.clear();
188 z_gyro_accum
.clear();
190 // Disable gyro bias correction to see raw data
191 AttitudeSettings::DataFields attitudeSettingsData
= AttitudeSettings::GetInstance(getObjectManager())->getData();
192 attitudeSettingsData
.BiasCorrectGyro
= AttitudeSettings::BIASCORRECTGYRO_FALSE
;
193 AttitudeSettings::GetInstance(getObjectManager())->setData(attitudeSettingsData
);
195 // Set up to receive updates
196 UAVDataObject
*accelState
= AccelState::GetInstance(getObjectManager());
197 UAVDataObject
*gyroState
= GyroState::GetInstance(getObjectManager());
198 connect(accelState
, SIGNAL(objectUpdated(UAVObject
*)), this, SLOT(sensorsUpdated(UAVObject
*)));
199 connect(gyroState
, SIGNAL(objectUpdated(UAVObject
*)), this, SLOT(sensorsUpdated(UAVObject
*)));
202 initialAccelStateMdata
= accelState
->getMetadata();
203 UAVObject::Metadata accelStateMdata
= initialAccelStateMdata
;
204 UAVObject::SetFlightTelemetryUpdateMode(accelStateMdata
, UAVObject::UPDATEMODE_PERIODIC
);
205 accelStateMdata
.flightTelemetryUpdatePeriod
= 30; // ms
206 accelState
->setMetadata(accelStateMdata
);
208 initialGyroStateMdata
= gyroState
->getMetadata();
209 UAVObject::Metadata gyroStateMdata
= initialGyroStateMdata
;
210 UAVObject::SetFlightTelemetryUpdateMode(gyroStateMdata
, UAVObject::UPDATEMODE_PERIODIC
);
211 gyroStateMdata
.flightTelemetryUpdatePeriod
= 30; // ms
212 gyroState
->setMetadata(gyroStateMdata
);
214 // Set up timeout timer
215 timer
.setSingleShot(true);
216 timer
.start(5000 + (NUM_SENSOR_UPDATES
* qMax(accelStateMdata
.flightTelemetryUpdatePeriod
,
217 gyroStateMdata
.flightTelemetryUpdatePeriod
)));
218 connect(&timer
, SIGNAL(timeout()), this, SLOT(timeout()));
221 void ConfigCCAttitudeWidget::openHelp()
223 QDesktopServices::openUrl(QUrl(QString(WIKI_URL_ROOT
) + QString("CC+Attitude+Configuration"),
227 void ConfigCCAttitudeWidget::setAccelFiltering(bool active
)
233 void ConfigCCAttitudeWidget::enableControls(bool enable
)
235 ui
->zeroBias
->setEnabled(enable
);
236 ui
->zeroGyroBiasOnArming
->setEnabled(enable
);
237 ui
->accelTauSpinbox
->setEnabled(enable
);
238 ConfigTaskWidget::enableControls(enable
);
241 void ConfigCCAttitudeWidget::updateObjectsFromWidgets()
243 ConfigTaskWidget::updateObjectsFromWidgets();
245 ui
->zeroBiasProgress
->setValue(0);