diff --git a/ground/openpilotgcs/src/plugins/config/calibration/thermal/settinghandlingtransitions.h b/ground/openpilotgcs/src/plugins/config/calibration/thermal/settinghandlingtransitions.h new file mode 100644 index 000000000..9e0a08664 --- /dev/null +++ b/ground/openpilotgcs/src/plugins/config/calibration/thermal/settinghandlingtransitions.h @@ -0,0 +1,115 @@ +/** + ****************************************************************************** + * + * @file settinghandlingtransitions.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014. + * + * @brief + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * 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 + */ + +#ifndef SETTINGSHANDLINGTRANSITIONS_H +#define SETTINGSHANDLINGTRANSITIONS_H +#include +#include + +#include "thermalcalibrationhelper.h" + +class BoardStatusSaveTransition : public QSignalTransition +{ + Q_OBJECT +public: + BoardStatusSaveTransition(ThermalCalibrationHelper *helper, QState *currentState, QState *targetState) + : QSignalTransition(helper, SIGNAL(statusSaveCompleted(bool))), + m_helper(helper) + { + QObject::connect(currentState, SIGNAL(entered()), this, SLOT(enterState())); + setTargetState(targetState); + } + + virtual bool eventTest(QEvent *e) + { + qDebug() << "BoardStatusSaveTransition::eventTest"; + if (!QSignalTransition::eventTest(e)) + return false; + QStateMachine::SignalEvent *se = static_cast(e); + + // check wether status stave was successful and retry if not + qDebug() << "BoardStatusSavedTransition::eventTest - " << se->arguments().at(0).toBool(); + if(se->arguments().at(0).toBool()){ + return true; + } else { + m_helper->statusSave(); + } + return false; + } + + virtual void onTransition(QEvent *e) + { + QStateMachine::SignalEvent *se = static_cast(e); + qDebug() << "BoardStatusSaveTransition :" << se->arguments().at(0).toBool(); + } +public slots: + void enterState(){ + m_helper->statusSave(); + } +private: + ThermalCalibrationHelper *m_helper; +}; + + +class BoardStatusRestoreTransition : public QSignalTransition +{ + Q_OBJECT +public: + BoardStatusRestoreTransition(ThermalCalibrationHelper *helper, QState *currentState, QState *targetState) + : QSignalTransition(helper, SIGNAL(statusRestoreCompleted(bool))), + m_helper(helper) + { + QObject::connect(currentState, SIGNAL(entered()), this, SLOT(enterState())); + setTargetState(targetState); + } + + virtual bool eventTest(QEvent *e) + { + if (!QSignalTransition::eventTest(e)) + return false; + QStateMachine::SignalEvent *se = static_cast(e); + + // check wether status stave was successful and retry if not + if(se->arguments().at(0).toBool()){ + return true; + } else { + qDebug() << "BoardStatusRestoreTransition::eventTest - statusRestore() "; + m_helper->statusRestore(); + } + return false; + } +public slots: + void enterState(){ + m_helper->statusRestore(); + } +private: + ThermalCalibrationHelper *m_helper; +}; + + +#endif // SETTINGSHANDLINGTRANSITIONS_H diff --git a/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationhelper.cpp b/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationhelper.cpp new file mode 100644 index 000000000..8a6c9213c --- /dev/null +++ b/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationhelper.cpp @@ -0,0 +1,198 @@ +/** + ****************************************************************************** + * + * @file thermalcalibrationhelper.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013. + * + * @brief Utilities for thermal calibration + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * 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 "thermalcalibrationhelper.h" + +ThermalCalibrationHelper::ThermalCalibrationHelper(QObject *parent) : + QObject(parent) +{ + m_boardInitialSettings = thermalCalibrationBoardSettings(); +} + +/** + * @brief Change board settings to prepare it for calibration + * @return + */ + +bool ThermalCalibrationHelper::setupBoardForCalibration(){ + qDebug() << "setupBoardForCalibration"; + + UAVObjectManager *objManager = getObjectManager(); + Q_ASSERT(objManager); + + // accelSensor Meta + AccelSensor *accelSensor = AccelSensor::GetInstance(objManager); + Q_ASSERT(accelSensor); + setMetadataForCalibration(accelSensor); + + // gyroSensor Meta + GyroSensor *gyroSensor = GyroSensor::GetInstance(objManager); + Q_ASSERT(gyroSensor); + setMetadataForCalibration(gyroSensor); + + // baroSensor Meta + BaroSensor *baroSensor = BaroSensor::GetInstance(objManager); + Q_ASSERT(baroSensor); + setMetadataForCalibration(baroSensor); + + // clean any correction before calibrating + RevoSettings *revoSettings = RevoSettings::GetInstance(objManager); + Q_ASSERT(revoSettings); + RevoSettings::DataFields revoSettingsData = revoSettings->getData(); + for (int i = 0; i < RevoSettings::BAROTEMPCORRECTIONPOLYNOMIAL_NUMELEM; i++){ + revoSettingsData.BaroTempCorrectionPolynomial[i] = 0.0f; + } + revoSettings->setData(revoSettingsData); + + return true; +} + + + +/** + * @brief Save board status to be later restored using restoreBoardStatus + * @return + */ +bool ThermalCalibrationHelper::saveBoardInitialSettings(){ + // Store current board status: + qDebug() << "Save initial settings"; + + UAVObjectManager *objManager = getObjectManager(); + Q_ASSERT(objManager); + // accelSensor Meta + AccelSensor *accelSensor = AccelSensor::GetInstance(objManager); + Q_ASSERT(accelSensor); + m_boardInitialSettings.accelSensorMeta = accelSensor->getMetadata(); + // gyroSensor Meta + GyroSensor *gyroSensor = GyroSensor::GetInstance(objManager); + Q_ASSERT(gyroSensor); + m_boardInitialSettings.gyroSensorMeta = gyroSensor->getMetadata(); + + // baroSensor Meta + BaroSensor *baroSensor = BaroSensor::GetInstance(objManager); + Q_ASSERT(baroSensor); + m_boardInitialSettings.baroensorMeta = baroSensor->getMetadata(); + + // revoSettings data + RevoSettings *revoSettings = RevoSettings::GetInstance(objManager); + Q_ASSERT(revoSettings); + m_boardInitialSettings.revoSettings = revoSettings->getData(); + + // accelGyroSettings data + /* + * Note: for revolution it is not neede but in case of CC we would prevent having + * a new set of xxxSensor UAVOs beside actual xxxState so it may be needed to reset the following + AccelGyroSettings *accelGyroSettings = AccelGyroSettings::GetInstance(objManager); + Q_ASSERT(accelGyroSettings); + m_boardInitialSettings.accelGyroSettings = accelGyroSettings->getData(); + */ + m_boardInitialSettings.statusSaved = true; + return true; +} + +void ThermalCalibrationHelper::setupBoard(){ + if(setupBoardForCalibration()){ + emit setupBoardCompleted(true); + } else { + emit setupBoardCompleted(false); + } +} + +void ThermalCalibrationHelper::statusRestore(){ + if(isBoardInitialSettingsSaved() && restoreInitialSettings()){ + clearBoardInitialSettingsSaved(); + emit statusRestoreCompleted(true); + } else { + emit statusRestoreCompleted(false); + } +} + +void ThermalCalibrationHelper::statusSave(){ + //prevent saving multiple times + if(!isBoardInitialSettingsSaved() && saveBoardInitialSettings()){ + emit statusSaveCompleted(true); + } else { + emit statusSaveCompleted(false); + } +} + +/** + * @brief restore board settings from status saved calling saveBoardStatus + * @return true if success + */ +bool ThermalCalibrationHelper::restoreInitialSettings(){ + if(!m_boardInitialSettings.statusSaved) { + return false; + } + // restore initial board status + UAVObjectManager *objManager = getObjectManager(); + Q_ASSERT(objManager); + + // accelSensor Meta + AccelSensor *accelSensor = AccelSensor::GetInstance(objManager); + Q_ASSERT(accelSensor); + accelSensor->setMetadata(m_boardInitialSettings.accelSensorMeta); + + // gyroSensor Meta + GyroSensor *gyroSensor = GyroSensor::GetInstance(objManager); + Q_ASSERT(gyroSensor); + gyroSensor->setMetadata(m_boardInitialSettings.gyroSensorMeta); + + // baroSensor Meta + BaroSensor *baroSensor = BaroSensor::GetInstance(objManager); + Q_ASSERT(baroSensor); + baroSensor->setMetadata(m_boardInitialSettings.baroensorMeta); + + // revoSettings data + RevoSettings *revoSettings = RevoSettings::GetInstance(objManager); + Q_ASSERT(revoSettings); + revoSettings->setData(m_boardInitialSettings.revoSettings); + + m_boardInitialSettings.statusSaved = false; + return true; +} + +void ThermalCalibrationHelper::setMetadataForCalibration(UAVDataObject *uavo) +{ + Q_ASSERT(uavo); + UAVObject::Metadata meta = uavo->getMetadata(); + UAVObject::SetFlightTelemetryUpdateMode(meta, UAVObject::UPDATEMODE_PERIODIC); + meta.flightTelemetryUpdatePeriod = 100; + uavo->setMetadata(meta); +} + +/** + * Util function to get a pointer to the object manager + * @return pointer to the UAVObjectManager + */ +UAVObjectManager *ThermalCalibrationHelper::getObjectManager(){ + ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); + UAVObjectManager *objMngr = pm->getObject(); + + Q_ASSERT(objMngr); + return objMngr; +} diff --git a/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationhelper.h b/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationhelper.h new file mode 100644 index 000000000..3eed185bb --- /dev/null +++ b/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationhelper.h @@ -0,0 +1,102 @@ +/** + ****************************************************************************** + * + * @file thermalcalibrationhelper.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014. + * + * @brief Thermal calibration helper functions + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * 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 + */ +#ifndef THERMALCALIBRATIONHELPER_H +#define THERMALCALIBRATIONHELPER_H + +#include +#include + +#include "uavobjectmanager.h" +#include +#include + +#include "extensionsystem/pluginmanager.h" + +// UAVOs +#include +#include +#include +#include "accelgyrosettings.h" + + +// Calibration data +#include +#include +#include + + + +typedef struct { + // this is not needed for revo, but should for CC/CC3D + // AccelGyroSettings::DataFields accelGyroSettings; + RevoSettings::DataFields revoSettings; + UAVObject::Metadata gyroSensorMeta; + UAVObject::Metadata accelSensorMeta; + UAVObject::Metadata baroensorMeta; + bool statusSaved = false; +} thermalCalibrationBoardSettings; + +class ThermalCalibrationHelper : public QObject +{ + Q_OBJECT +public: + explicit ThermalCalibrationHelper(QObject *parent = 0); + + /* board settings save/restore */ + bool saveBoardInitialSettings(); + bool restoreInitialSettings(); + bool isBoardInitialSettingsSaved(){ + return m_boardInitialSettings.statusSaved; + } +private: + void setMetadataForCalibration(UAVDataObject *uavo); + void clearBoardInitialSettingsSaved(){ + m_boardInitialSettings.statusSaved = false; + } +signals: + void statusRestoreCompleted(bool succesful); + void statusSaveCompleted(bool succesful); +public slots: + void statusSave(); + void statusRestore(); + + /* board configuration setup for calibration */ +public: + bool setupBoardForCalibration(); +signals: + void setupBoardCompleted(bool succesful); + +public slots: + void setupBoard(); + +private: + thermalCalibrationBoardSettings m_boardInitialSettings; + UAVObjectManager *getObjectManager(); +}; + +#endif // THERMALCALIBRATIONHELPER_H diff --git a/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationtransitions.h b/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationtransitions.h new file mode 100644 index 000000000..62c7a45f8 --- /dev/null +++ b/ground/openpilotgcs/src/plugins/config/calibration/thermal/thermalcalibrationtransitions.h @@ -0,0 +1,81 @@ +/** + ****************************************************************************** + * + * @file thermalcalibrationtransitions.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014. + * + * @brief State transitions used to implement thermal calibration + * @see The GNU Public License (GPL) Version 3 + * @defgroup + * @{ + * + *****************************************************************************/ +/* + * 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 + */ + +#ifndef THERMALCALIBRATIONTRANSITIONS_H +#define THERMALCALIBRATIONTRANSITIONS_H + +#include +#include + +#include "thermalcalibrationhelper.h" + +class BoardSetupTransition : public QSignalTransition +{ + Q_OBJECT +public: + BoardSetupTransition (ThermalCalibrationHelper *helper, QState *currentState, QState *targetState) + : QSignalTransition(helper, SIGNAL(setupBoardCompleted(bool))), + m_helper(helper) + { + QObject::connect(currentState, SIGNAL(entered()), this, SLOT(enterState())); + setTargetState(targetState); + } + + virtual bool eventTest(QEvent *e) + { + qDebug() << "BoardSetupTransition::eventTest"; + if (!QSignalTransition::eventTest(e)) + return false; + QStateMachine::SignalEvent *se = static_cast(e); + + // check wether status stave was successful and retry if not + qDebug() << "BoardSetupTransition::eventTest - " << se->arguments().at(0).toBool(); + if(se->arguments().at(0).toBool()){ + return true; + } else { + m_helper->setupBoard(); + } + return false; + } + + virtual void onTransition(QEvent *e) + { + QStateMachine::SignalEvent *se = static_cast(e); + qDebug() << "BoardSetupTransition::onTransition" << se->arguments().at(0).toBool(); + } +public slots: + void enterState(){ + m_helper->setupBoard(); + } +private: + ThermalCalibrationHelper *m_helper; +}; + + + +#endif // THERMALCALIBRATIONTRANSITIONS_H diff --git a/ground/openpilotgcs/src/plugins/config/config.pro b/ground/openpilotgcs/src/plugins/config/config.pro index ba153b8df..9b325194b 100644 --- a/ground/openpilotgcs/src/plugins/config/config.pro +++ b/ground/openpilotgcs/src/plugins/config/config.pro @@ -46,7 +46,10 @@ HEADERS += configplugin.h \ calibration/calibrationutils.h \ calibration/wizardstate.h \ calibration/wizardmodel.h \ + calibration/thermal/thermalcalibrationtransitions.h \ calibration/thermal/thermalcalibration.h \ + calibration/thermal/thermalcalibrationhelper.h \ + calibration/thermal/settinghandlingtransitions.h SOURCES += configplugin.cpp \ configgadgetwidget.cpp \ @@ -80,6 +83,7 @@ SOURCES += configplugin.cpp \ calibration/wizardstate.cpp \ calibration/wizardmodel.cpp \ calibration/thermal/thermalcalibration.cpp \ + calibration/thermal/thermalcalibrationhelper.cpp \ FORMS += airframe.ui \ airframe_ccpm.ui \