2011-03-28 21:16:38 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file configccattitudewidget.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup ConfigPlugin Config Plugin
|
|
|
|
* @{
|
|
|
|
* @brief Configure Attitude module on CopterControl
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
#include "configccattitudewidget.h"
|
|
|
|
#include "ui_ccattitude.h"
|
2011-04-15 08:37:16 +02:00
|
|
|
#include "utils/coordinateconversions.h"
|
2011-06-24 02:19:11 +02:00
|
|
|
#include "attitudesettings.h"
|
2011-03-28 21:16:38 +02:00
|
|
|
#include <QMutexLocker>
|
2011-04-26 18:02:32 +02:00
|
|
|
#include <QMessageBox>
|
2011-03-28 21:16:38 +02:00
|
|
|
#include <QDebug>
|
2011-05-31 09:16:01 +02:00
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QUrl>
|
2012-06-05 02:13:55 +02:00
|
|
|
#include "accels.h"
|
|
|
|
#include "gyros.h"
|
2012-07-21 19:58:05 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
#include <coreplugin/generalsettings.h>
|
2011-03-28 21:16:38 +02:00
|
|
|
|
|
|
|
ConfigCCAttitudeWidget::ConfigCCAttitudeWidget(QWidget *parent) :
|
|
|
|
ConfigTaskWidget(parent),
|
|
|
|
ui(new Ui_ccattitude)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2012-08-01 15:42:21 +02:00
|
|
|
forceConnectedState(); //dynamic widgets don't recieve the connected signal
|
2011-03-28 21:16:38 +02:00
|
|
|
connect(ui->zeroBias,SIGNAL(clicked()),this,SLOT(startAccelCalibration()));
|
2011-05-05 23:02:42 +02:00
|
|
|
|
2012-07-21 19:58:05 +02:00
|
|
|
ExtensionSystem::PluginManager *pm=ExtensionSystem::PluginManager::instance();
|
|
|
|
Core::Internal::GeneralSettings * settings=pm->getObject<Core::Internal::GeneralSettings>();
|
|
|
|
if(!settings->useExpertMode())
|
|
|
|
ui->applyButton->setVisible(false);
|
|
|
|
|
2012-02-02 17:22:40 +01:00
|
|
|
addApplySaveButtons(ui->applyButton,ui->saveButton);
|
2011-08-02 18:06:17 +02:00
|
|
|
addUAVObject("AttitudeSettings");
|
|
|
|
|
2011-05-31 09:16:01 +02:00
|
|
|
// Connect the help button
|
|
|
|
connect(ui->ccAttitudeHelp, SIGNAL(clicked()), this, SLOT(openHelp()));
|
2012-11-16 21:22:13 +01:00
|
|
|
|
|
|
|
connect(ui->accelFiltering, SIGNAL(toggled(bool)), this, SLOT(setAccelFiltering(bool)));
|
2011-08-15 15:12:54 +02:00
|
|
|
addUAVObjectToWidgetRelation("AttitudeSettings","ZeroDuringArming",ui->zeroGyroBiasOnArming);
|
|
|
|
|
|
|
|
addUAVObjectToWidgetRelation("AttitudeSettings","BoardRotation",ui->rollBias,AttitudeSettings::BOARDROTATION_ROLL);
|
|
|
|
addUAVObjectToWidgetRelation("AttitudeSettings","BoardRotation",ui->pitchBias,AttitudeSettings::BOARDROTATION_PITCH);
|
|
|
|
addUAVObjectToWidgetRelation("AttitudeSettings","BoardRotation",ui->yawBias,AttitudeSettings::BOARDROTATION_YAW);
|
2011-08-02 18:06:17 +02:00
|
|
|
addWidget(ui->zeroBias);
|
2012-04-17 16:19:41 +02:00
|
|
|
refreshWidgetsValues();
|
2011-03-28 21:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigCCAttitudeWidget::~ConfigCCAttitudeWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2012-08-29 11:28:26 +02:00
|
|
|
void ConfigCCAttitudeWidget::sensorsUpdated(UAVObject * obj) {
|
2011-03-28 21:16:38 +02:00
|
|
|
|
2012-09-08 08:28:39 +02:00
|
|
|
if (!timer.isActive()) {
|
|
|
|
// ignore updates that come in after the timer has expired
|
|
|
|
return;
|
|
|
|
}
|
2011-03-28 21:16:38 +02:00
|
|
|
|
2012-08-29 11:28:26 +02:00
|
|
|
Accels * accels = Accels::GetInstance(getObjectManager());
|
|
|
|
Gyros * gyros = Gyros::GetInstance(getObjectManager());
|
|
|
|
|
2012-09-10 14:52:36 +02:00
|
|
|
// Accumulate samples until we have _at least_ NUM_SENSOR_UPDATES samples
|
|
|
|
// for both gyros and accels.
|
|
|
|
// Note that, at present, we stash the samples and then compute the bias
|
|
|
|
// at the end, even though the mean could be accumulated as we go.
|
|
|
|
// In future, a better algorithm could be used.
|
|
|
|
if(obj->getObjID() == Accels::OBJID) {
|
2012-08-29 11:28:26 +02:00
|
|
|
accelUpdates++;
|
2012-01-24 23:11:03 +01:00
|
|
|
Accels::DataFields accelsData = accels->getData();
|
|
|
|
x_accum.append(accelsData.x);
|
|
|
|
y_accum.append(accelsData.y);
|
|
|
|
z_accum.append(accelsData.z);
|
2012-09-10 14:52:36 +02:00
|
|
|
} else if (obj->getObjID() == Gyros::OBJID) {
|
2012-08-29 11:28:26 +02:00
|
|
|
gyroUpdates++;
|
2012-01-24 23:11:03 +01:00
|
|
|
Gyros::DataFields gyrosData = gyros->getData();
|
|
|
|
x_gyro_accum.append(gyrosData.x);
|
|
|
|
y_gyro_accum.append(gyrosData.y);
|
|
|
|
z_gyro_accum.append(gyrosData.z);
|
2012-09-10 14:52:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the progress indicator
|
|
|
|
ui->zeroBiasProgress->setValue((float) qMin(accelUpdates, gyroUpdates) / NUM_SENSOR_UPDATES * 100);
|
|
|
|
|
|
|
|
// If we have enough samples, then stop sampling and compute the biases
|
|
|
|
if (accelUpdates >= NUM_SENSOR_UPDATES && gyroUpdates >= NUM_SENSOR_UPDATES) {
|
2011-03-28 21:16:38 +02:00
|
|
|
timer.stop();
|
2012-08-29 11:28:26 +02:00
|
|
|
disconnect(obj,SIGNAL(objectUpdated(UAVObject*)),this,SLOT(sensorsUpdated(UAVObject*)));
|
2011-03-28 21:16:38 +02:00
|
|
|
disconnect(&timer,SIGNAL(timeout()),this,SLOT(timeout()));
|
|
|
|
|
|
|
|
float x_bias = listMean(x_accum) / ACCEL_SCALE;
|
|
|
|
float y_bias = listMean(y_accum) / ACCEL_SCALE;
|
|
|
|
float z_bias = (listMean(z_accum) + 9.81) / ACCEL_SCALE;
|
|
|
|
|
2011-06-24 17:51:07 +02:00
|
|
|
float x_gyro_bias = listMean(x_gyro_accum) * 100.0f;
|
|
|
|
float y_gyro_bias = listMean(y_gyro_accum) * 100.0f;
|
|
|
|
float z_gyro_bias = listMean(z_gyro_accum) * 100.0f;
|
2012-08-29 11:28:26 +02:00
|
|
|
accels->setMetadata(initialAccelsMdata);
|
|
|
|
gyros->setMetadata(initialGyrosMdata);
|
2011-03-28 21:16:38 +02:00
|
|
|
|
2011-06-24 02:19:11 +02:00
|
|
|
AttitudeSettings::DataFields attitudeSettingsData = AttitudeSettings::GetInstance(getObjectManager())->getData();
|
|
|
|
// We offset the gyro bias by current bias to help precision
|
|
|
|
attitudeSettingsData.AccelBias[0] += x_bias;
|
|
|
|
attitudeSettingsData.AccelBias[1] += y_bias;
|
|
|
|
attitudeSettingsData.AccelBias[2] += z_bias;
|
|
|
|
attitudeSettingsData.GyroBias[0] = -x_gyro_bias;
|
|
|
|
attitudeSettingsData.GyroBias[1] = -y_gyro_bias;
|
|
|
|
attitudeSettingsData.GyroBias[2] = -z_gyro_bias;
|
2011-10-08 21:15:48 +02:00
|
|
|
attitudeSettingsData.BiasCorrectGyro = AttitudeSettings::BIASCORRECTGYRO_TRUE;
|
2011-06-24 02:19:11 +02:00
|
|
|
AttitudeSettings::GetInstance(getObjectManager())->setData(attitudeSettingsData);
|
2012-08-01 15:42:21 +02:00
|
|
|
this->setDirty(true);
|
2012-09-10 14:52:36 +02:00
|
|
|
|
|
|
|
// reenable controls
|
|
|
|
enableControls(true);
|
2011-03-28 21:16:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigCCAttitudeWidget::timeout() {
|
2012-01-24 23:11:03 +01:00
|
|
|
UAVDataObject * obj = Accels::GetInstance(getObjectManager());
|
2012-08-29 11:28:26 +02:00
|
|
|
disconnect(obj,SIGNAL(objectUpdated(UAVObject*)),this,SLOT(sensorsUpdated(UAVObject*)));
|
2011-03-28 21:16:38 +02:00
|
|
|
disconnect(&timer,SIGNAL(timeout()),this,SLOT(timeout()));
|
|
|
|
|
2012-08-29 11:28:26 +02:00
|
|
|
Accels * accels = Accels::GetInstance(getObjectManager());
|
|
|
|
Gyros * gyros = Gyros::GetInstance(getObjectManager());
|
|
|
|
accels->setMetadata(initialAccelsMdata);
|
|
|
|
gyros->setMetadata(initialGyrosMdata);
|
|
|
|
|
2011-04-26 18:02:32 +02:00
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText(tr("Calibration timed out before receiving required updates."));
|
|
|
|
msgBox.setStandardButtons(QMessageBox::Ok);
|
|
|
|
msgBox.setDefaultButton(QMessageBox::Ok);
|
|
|
|
msgBox.exec();
|
|
|
|
|
2012-09-08 08:28:39 +02:00
|
|
|
// reset progress indicator
|
2012-09-10 14:52:36 +02:00
|
|
|
ui->zeroBiasProgress->setValue(0);
|
|
|
|
// reenable controls
|
|
|
|
enableControls(true);
|
2011-03-28 21:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigCCAttitudeWidget::startAccelCalibration() {
|
2012-09-10 14:52:36 +02:00
|
|
|
// disable controls during sampling
|
|
|
|
enableControls(false);
|
2011-03-28 21:16:38 +02:00
|
|
|
|
2012-08-29 11:28:26 +02:00
|
|
|
accelUpdates = 0;
|
|
|
|
gyroUpdates = 0;
|
2011-03-28 21:16:38 +02:00
|
|
|
x_accum.clear();
|
|
|
|
y_accum.clear();
|
|
|
|
z_accum.clear();
|
2011-06-24 02:03:35 +02:00
|
|
|
x_gyro_accum.clear();
|
|
|
|
y_gyro_accum.clear();
|
|
|
|
z_gyro_accum.clear();
|
2011-03-28 21:16:38 +02:00
|
|
|
|
2011-06-24 02:03:35 +02:00
|
|
|
// Disable gyro bias correction to see raw data
|
2011-06-24 02:19:11 +02:00
|
|
|
AttitudeSettings::DataFields attitudeSettingsData = AttitudeSettings::GetInstance(getObjectManager())->getData();
|
|
|
|
attitudeSettingsData.BiasCorrectGyro = AttitudeSettings::BIASCORRECTGYRO_FALSE;
|
|
|
|
AttitudeSettings::GetInstance(getObjectManager())->setData(attitudeSettingsData);
|
2011-06-24 02:03:35 +02:00
|
|
|
|
2011-03-28 21:16:38 +02:00
|
|
|
// Set up to receive updates
|
2012-08-29 11:28:26 +02:00
|
|
|
UAVDataObject * accels = Accels::GetInstance(getObjectManager());
|
|
|
|
UAVDataObject * gyros = Gyros::GetInstance(getObjectManager());
|
|
|
|
connect(accels,SIGNAL(objectUpdated(UAVObject*)),this,SLOT(sensorsUpdated(UAVObject*)));
|
|
|
|
connect(gyros,SIGNAL(objectUpdated(UAVObject*)),this,SLOT(sensorsUpdated(UAVObject*)));
|
2011-03-28 21:16:38 +02:00
|
|
|
|
|
|
|
// Speed up updates
|
2012-08-29 11:28:26 +02:00
|
|
|
initialAccelsMdata = accels->getMetadata();
|
|
|
|
UAVObject::Metadata accelsMdata = initialAccelsMdata;
|
|
|
|
UAVObject::SetFlightTelemetryUpdateMode(accelsMdata, UAVObject::UPDATEMODE_PERIODIC);
|
2012-09-10 14:52:36 +02:00
|
|
|
accelsMdata.flightTelemetryUpdatePeriod = 30; // ms
|
2012-08-29 11:28:26 +02:00
|
|
|
accels->setMetadata(accelsMdata);
|
|
|
|
|
|
|
|
initialGyrosMdata = gyros->getMetadata();
|
|
|
|
UAVObject::Metadata gyrosMdata = initialGyrosMdata;
|
|
|
|
UAVObject::SetFlightTelemetryUpdateMode(gyrosMdata, UAVObject::UPDATEMODE_PERIODIC);
|
2012-09-10 14:52:36 +02:00
|
|
|
gyrosMdata.flightTelemetryUpdatePeriod = 30; // ms
|
2012-08-29 11:28:26 +02:00
|
|
|
gyros->setMetadata(gyrosMdata);
|
2011-03-28 21:16:38 +02:00
|
|
|
|
2012-09-08 08:28:39 +02:00
|
|
|
// Set up timeout timer
|
|
|
|
timer.setSingleShot(true);
|
2012-09-10 14:52:36 +02:00
|
|
|
timer.start(5000 + (NUM_SENSOR_UPDATES * qMax(accelsMdata.flightTelemetryUpdatePeriod,
|
2012-09-08 08:28:39 +02:00
|
|
|
gyrosMdata.flightTelemetryUpdatePeriod)));
|
|
|
|
connect(&timer,SIGNAL(timeout()),this,SLOT(timeout()));
|
2011-03-28 21:16:38 +02:00
|
|
|
}
|
2011-03-28 23:31:44 +02:00
|
|
|
|
2011-05-31 09:16:01 +02:00
|
|
|
void ConfigCCAttitudeWidget::openHelp()
|
|
|
|
{
|
|
|
|
|
2012-07-30 00:29:39 +02:00
|
|
|
QDesktopServices::openUrl( QUrl("http://wiki.openpilot.org/x/44Cf", QUrl::StrictMode) );
|
2011-05-31 09:16:01 +02:00
|
|
|
}
|
|
|
|
|
2012-11-16 21:22:13 +01:00
|
|
|
void ConfigCCAttitudeWidget::setAccelFiltering(bool active)
|
|
|
|
{
|
|
|
|
AttitudeSettings* settings = AttitudeSettings::GetInstance(getObjectManager());
|
|
|
|
Q_ASSERT(settings);
|
|
|
|
AttitudeSettings::DataFields data = settings->getData();
|
|
|
|
|
|
|
|
data.AccelTau = active ? DEFAULT_ENABLED_ACCEL_TAU : 0.0f;
|
|
|
|
|
|
|
|
settings->setData(data);
|
|
|
|
setDirty(true);
|
|
|
|
}
|
|
|
|
|
2011-08-17 13:43:08 +02:00
|
|
|
void ConfigCCAttitudeWidget::enableControls(bool enable)
|
|
|
|
{
|
2012-11-16 21:22:13 +01:00
|
|
|
if(ui->zeroBias) {
|
2011-08-17 13:43:08 +02:00
|
|
|
ui->zeroBias->setEnabled(enable);
|
2012-11-16 21:22:13 +01:00
|
|
|
}
|
|
|
|
if(ui->accelFiltering) {
|
|
|
|
ui->accelFiltering->setEnabled(enable);
|
|
|
|
}
|
2011-08-17 13:43:08 +02:00
|
|
|
ConfigTaskWidget::enableControls(enable);
|
|
|
|
}
|
2012-06-05 00:38:57 +02:00
|
|
|
|
2012-11-16 21:22:13 +01:00
|
|
|
void ConfigCCAttitudeWidget::refreshWidgetsValues(UAVObject *obj)
|
|
|
|
{
|
|
|
|
AttitudeSettings* settings = AttitudeSettings::GetInstance(getObjectManager());
|
|
|
|
Q_ASSERT(settings);
|
|
|
|
AttitudeSettings::DataFields data = settings->getData();
|
|
|
|
ui->accelFiltering->setChecked(data.AccelTau > 0.0f);
|
|
|
|
|
|
|
|
ConfigTaskWidget::refreshWidgetsValues(obj);
|
|
|
|
}
|
|
|
|
|
2012-06-05 00:38:57 +02:00
|
|
|
void ConfigCCAttitudeWidget::updateObjectsFromWidgets()
|
|
|
|
{
|
|
|
|
ConfigTaskWidget::updateObjectsFromWidgets();
|
|
|
|
|
|
|
|
ui->zeroBiasProgress->setValue(0);
|
|
|
|
}
|