2014-04-11 05:41:39 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file gyrobiascalibrationmodel.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
|
|
|
|
* @addtogroup board level calibration
|
|
|
|
* @{
|
|
|
|
* @addtogroup ConfigPlugin Config Plugin
|
|
|
|
* @{
|
|
|
|
* @brief Telemetry configuration panel
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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 "calibration/gyrobiascalibrationmodel.h"
|
|
|
|
#include "calibration/calibrationutils.h"
|
2014-04-27 12:58:59 +02:00
|
|
|
#include "calibration/calibrationuiutils.h"
|
2014-05-30 15:09:07 +02:00
|
|
|
#include "extensionsystem/pluginmanager.h"
|
2014-04-27 12:58:59 +02:00
|
|
|
|
2014-04-11 05:41:39 +02:00
|
|
|
static const int LEVEL_SAMPLES = 100;
|
2014-05-15 22:10:16 +02:00
|
|
|
|
2014-04-11 05:41:39 +02:00
|
|
|
namespace OpenPilot {
|
|
|
|
GyroBiasCalibrationModel::GyroBiasCalibrationModel(QObject *parent) :
|
2014-05-30 15:09:07 +02:00
|
|
|
QObject(parent), collectingData(false), m_dirty(false)
|
|
|
|
{
|
|
|
|
gyroState = GyroState::GetInstance(getObjectManager());
|
|
|
|
Q_ASSERT(gyroState);
|
|
|
|
|
|
|
|
gyroSensor = GyroSensor::GetInstance(getObjectManager());
|
|
|
|
Q_ASSERT(gyroSensor);
|
|
|
|
|
|
|
|
revoCalibration = RevoCalibration::GetInstance(getObjectManager());
|
|
|
|
Q_ASSERT(revoCalibration);
|
|
|
|
|
|
|
|
attitudeSettings = AttitudeSettings::GetInstance(getObjectManager());
|
|
|
|
Q_ASSERT(attitudeSettings);
|
|
|
|
|
|
|
|
accelGyroSettings = AccelGyroSettings::GetInstance(getObjectManager());
|
|
|
|
Q_ASSERT(accelGyroSettings);
|
|
|
|
}
|
2014-04-11 05:41:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
void GyroBiasCalibrationModel::start()
|
|
|
|
{
|
|
|
|
// Store and reset board rotation before calibration starts
|
|
|
|
storeAndClearBoardRotation();
|
|
|
|
|
|
|
|
RevoCalibration::DataFields revoCalibrationData = revoCalibration->getData();
|
2014-05-30 15:09:07 +02:00
|
|
|
memento.revoCalibrationData = revoCalibrationData;
|
2014-04-11 05:41:39 +02:00
|
|
|
revoCalibrationData.BiasCorrectedRaw = RevoCalibration::BIASCORRECTEDRAW_FALSE;
|
|
|
|
revoCalibration->setData(revoCalibrationData);
|
|
|
|
|
|
|
|
// Disable gyro bias correction while calibrating
|
|
|
|
AttitudeSettings::DataFields attitudeSettingsData = attitudeSettings->getData();
|
2014-05-30 15:09:07 +02:00
|
|
|
memento.attitudeSettingsData = attitudeSettingsData;
|
2014-04-11 05:41:39 +02:00
|
|
|
attitudeSettingsData.BiasCorrectGyro = AttitudeSettings::BIASCORRECTGYRO_FALSE;
|
|
|
|
attitudeSettings->setData(attitudeSettingsData);
|
2014-05-07 22:34:27 +02:00
|
|
|
|
2014-05-30 15:09:07 +02:00
|
|
|
UAVObject::Metadata gyroStateMetadata = gyroState->getMetadata();
|
|
|
|
memento.gyroStateMetadata = gyroStateMetadata;
|
|
|
|
UAVObject::SetFlightTelemetryUpdateMode(gyroStateMetadata, UAVObject::UPDATEMODE_PERIODIC);
|
|
|
|
gyroStateMetadata.flightTelemetryUpdatePeriod = 100;
|
|
|
|
gyroState->setMetadata(gyroStateMetadata);
|
|
|
|
|
|
|
|
UAVObject::Metadata gyroSensorMetadata = gyroSensor->getMetadata();
|
|
|
|
memento.gyroSensorMetadata = gyroSensorMetadata;
|
|
|
|
UAVObject::SetFlightTelemetryUpdateMode(gyroSensorMetadata, UAVObject::UPDATEMODE_PERIODIC);
|
|
|
|
gyroSensorMetadata.flightTelemetryUpdatePeriod = 100;
|
|
|
|
gyroSensor->setMetadata(gyroSensorMetadata);
|
2014-04-11 05:41:39 +02:00
|
|
|
|
|
|
|
gyro_accum_x.clear();
|
|
|
|
gyro_accum_y.clear();
|
|
|
|
gyro_accum_z.clear();
|
|
|
|
|
2014-05-07 22:34:27 +02:00
|
|
|
gyro_state_accum_x.clear();
|
|
|
|
gyro_state_accum_y.clear();
|
|
|
|
gyro_state_accum_z.clear();
|
|
|
|
|
2014-05-30 15:09:07 +02:00
|
|
|
// reset dirty state to forget previous unsaved runs
|
|
|
|
m_dirty = false;
|
2014-04-11 05:41:39 +02:00
|
|
|
|
2014-05-30 15:09:07 +02:00
|
|
|
started();
|
|
|
|
progressChanged(0);
|
|
|
|
displayVisualHelp(CALIBRATION_HELPER_PLANE_PREFIX + CALIBRATION_HELPER_IMAGE_NED);
|
|
|
|
displayInstructions(tr("Calibrating the gyroscopes. Keep the vehicle steady..."));
|
2014-04-11 05:41:39 +02:00
|
|
|
|
|
|
|
// Now connect to the accels and mag updates, gather for 100 samples
|
|
|
|
collectingData = true;
|
|
|
|
connect(gyroState, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(getSample(UAVObject *)));
|
2014-05-07 22:34:27 +02:00
|
|
|
connect(gyroSensor, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(getSample(UAVObject *)));
|
2014-04-11 05:41:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Updates the gyro bias raw values
|
|
|
|
*/
|
|
|
|
void GyroBiasCalibrationModel::getSample(UAVObject *obj)
|
|
|
|
{
|
|
|
|
QMutexLocker lock(&sensorsUpdateLock);
|
|
|
|
|
|
|
|
switch (obj->getObjID()) {
|
|
|
|
case GyroState::OBJID:
|
|
|
|
{
|
|
|
|
GyroState::DataFields gyroStateData = gyroState->getData();
|
2014-05-07 22:34:27 +02:00
|
|
|
gyro_state_accum_x.append(gyroStateData.x);
|
|
|
|
gyro_state_accum_y.append(gyroStateData.y);
|
|
|
|
gyro_state_accum_z.append(gyroStateData.z);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GyroSensor::OBJID:
|
|
|
|
{
|
|
|
|
GyroSensor::DataFields gyroSensorData = gyroSensor->getData();
|
|
|
|
gyro_accum_x.append(gyroSensorData.x);
|
|
|
|
gyro_accum_y.append(gyroSensorData.y);
|
|
|
|
gyro_accum_z.append(gyroSensorData.z);
|
2014-04-11 05:41:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Q_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work out the progress based on whichever has less
|
2014-05-07 22:34:27 +02:00
|
|
|
double p1 = (double)gyro_state_accum_x.size() / (double)LEVEL_SAMPLES;
|
2014-04-11 05:41:39 +02:00
|
|
|
double p2 = (double)gyro_accum_y.size() / (double)LEVEL_SAMPLES;
|
2014-05-07 22:34:27 +02:00
|
|
|
progressChanged(((p1 > p2) ? p1 : p2) * 100);
|
2014-04-11 05:41:39 +02:00
|
|
|
|
2014-05-07 22:34:27 +02:00
|
|
|
if ((gyro_accum_y.size() >= LEVEL_SAMPLES || (gyro_accum_y.size() == 0 && gyro_state_accum_y.size() >= LEVEL_SAMPLES)) &&
|
2014-04-11 05:41:39 +02:00
|
|
|
collectingData == true) {
|
|
|
|
collectingData = false;
|
2014-05-30 15:09:07 +02:00
|
|
|
m_dirty = true;
|
2014-04-11 05:41:39 +02:00
|
|
|
|
2014-05-07 22:34:27 +02:00
|
|
|
disconnect(gyroSensor, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(getSample(UAVObject *)));
|
2014-05-30 15:09:07 +02:00
|
|
|
disconnect(gyroState, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(getSample(UAVObject *)));
|
2014-04-11 05:41:39 +02:00
|
|
|
|
2014-05-30 15:09:07 +02:00
|
|
|
gyroState->setMetadata(memento.gyroStateMetadata);
|
|
|
|
gyroSensor->setMetadata(memento.gyroSensorMetadata);
|
|
|
|
revoCalibration->setData(memento.revoCalibrationData);
|
|
|
|
attitudeSettings->setData(memento.attitudeSettingsData);
|
2014-04-11 05:41:39 +02:00
|
|
|
|
2014-05-30 15:09:07 +02:00
|
|
|
// Recall saved board rotation
|
|
|
|
recallBoardRotation();
|
2014-04-11 05:41:39 +02:00
|
|
|
|
2014-05-30 15:09:07 +02:00
|
|
|
stopped();
|
2014-06-01 17:22:26 +02:00
|
|
|
displayInstructions(tr("Gyroscope calibration completed successfully."), WizardModel::Success);
|
2014-04-27 12:58:59 +02:00
|
|
|
displayVisualHelp(CALIBRATION_HELPER_IMAGE_EMPTY);
|
2014-04-11 05:41:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:09:07 +02:00
|
|
|
void GyroBiasCalibrationModel::save()
|
|
|
|
{
|
|
|
|
if (!m_dirty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Update biases based on collected data
|
|
|
|
AccelGyroSettings::DataFields accelGyroSettingsData = accelGyroSettings->getData();
|
|
|
|
|
|
|
|
// check whether the board does supports gyroSensor (updates were received)
|
|
|
|
if (gyro_accum_x.count() < LEVEL_SAMPLES / 10) {
|
|
|
|
accelGyroSettingsData.gyro_bias[AccelGyroSettings::GYRO_BIAS_X] += OpenPilot::CalibrationUtils::listMean(gyro_state_accum_x);
|
|
|
|
accelGyroSettingsData.gyro_bias[AccelGyroSettings::GYRO_BIAS_Y] += OpenPilot::CalibrationUtils::listMean(gyro_state_accum_y);
|
|
|
|
accelGyroSettingsData.gyro_bias[AccelGyroSettings::GYRO_BIAS_Z] += OpenPilot::CalibrationUtils::listMean(gyro_state_accum_z);
|
|
|
|
} else {
|
|
|
|
accelGyroSettingsData.gyro_bias[AccelGyroSettings::GYRO_BIAS_X] += OpenPilot::CalibrationUtils::listMean(gyro_accum_x);
|
|
|
|
accelGyroSettingsData.gyro_bias[AccelGyroSettings::GYRO_BIAS_Y] += OpenPilot::CalibrationUtils::listMean(gyro_accum_y);
|
|
|
|
accelGyroSettingsData.gyro_bias[AccelGyroSettings::GYRO_BIAS_Z] += OpenPilot::CalibrationUtils::listMean(gyro_accum_z);
|
|
|
|
}
|
|
|
|
|
|
|
|
accelGyroSettings->setData(accelGyroSettingsData);
|
|
|
|
|
|
|
|
m_dirty = false;
|
|
|
|
}
|
2014-04-11 05:41:39 +02:00
|
|
|
|
|
|
|
UAVObjectManager *GyroBiasCalibrationModel::getObjectManager()
|
|
|
|
{
|
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectManager *objMngr = pm->getObject<UAVObjectManager>();
|
|
|
|
|
|
|
|
Q_ASSERT(objMngr);
|
|
|
|
return objMngr;
|
|
|
|
}
|
|
|
|
}
|