Commit 8c3e750b authored by lm's avatar lm

Added support for saving onboard parameters to a file

parent 109d6890
...@@ -31,6 +31,8 @@ This file is part of the QGROUNDCONTROL project ...@@ -31,6 +31,8 @@ This file is part of the QGROUNDCONTROL project
#include <QGridLayout> #include <QGridLayout>
#include <QPushButton> #include <QPushButton>
#include <QFileDialog>
#include <QFile>
#include "QGCParamWidget.h" #include "QGCParamWidget.h"
#include "UASInterface.h" #include "UASInterface.h"
...@@ -46,7 +48,8 @@ QGCParamWidget::QGCParamWidget(UASInterface* uas, QWidget *parent) : ...@@ -46,7 +48,8 @@ QGCParamWidget::QGCParamWidget(UASInterface* uas, QWidget *parent) :
mav(uas), mav(uas),
components(new QMap<int, QTreeWidgetItem*>()), components(new QMap<int, QTreeWidgetItem*>()),
paramGroups(), paramGroups(),
changedValues() changedValues(),
parameters()
{ {
// Create tree widget // Create tree widget
tree = new QTreeWidget(this); tree = new QTreeWidget(this);
...@@ -73,6 +76,14 @@ QGCParamWidget::QGCParamWidget(UASInterface* uas, QWidget *parent) : ...@@ -73,6 +76,14 @@ QGCParamWidget::QGCParamWidget(UASInterface* uas, QWidget *parent) :
connect(writeButton, SIGNAL(clicked()), this, SLOT(writeParameters())); connect(writeButton, SIGNAL(clicked()), this, SLOT(writeParameters()));
horizontalLayout->addWidget(writeButton, 1, 2); horizontalLayout->addWidget(writeButton, 1, 2);
QPushButton* loadFileButton = new QPushButton(tr("Load File"));
connect(loadFileButton, SIGNAL(clicked()), this, SLOT(loadParameters()));
horizontalLayout->addWidget(loadFileButton, 2, 0);
QPushButton* saveFileButton = new QPushButton(tr("Save File"));
connect(saveFileButton, SIGNAL(clicked()), this, SLOT(saveParameters()));
horizontalLayout->addWidget(saveFileButton, 2, 1);
// Set layout // Set layout
this->setLayout(horizontalLayout); this->setLayout(horizontalLayout);
...@@ -128,6 +139,16 @@ void QGCParamWidget::addComponent(int uas, int component, QString componentName) ...@@ -128,6 +139,16 @@ void QGCParamWidget::addComponent(int uas, int component, QString componentName)
paramGroups.insert(component, new QMap<QString, QTreeWidgetItem*>()); paramGroups.insert(component, new QMap<QString, QTreeWidgetItem*>());
tree->addTopLevelItem(comp); tree->addTopLevelItem(comp);
tree->update(); tree->update();
// Create map in parameters
if (!parameters.contains(component))
{
parameters.insert(component, new QMap<QString, float>());
}
// Create map in changed parameters
if (!changedValues.contains(component))
{
changedValues.insert(component, new QMap<QString, float>());
}
} }
} }
...@@ -148,6 +169,13 @@ void QGCParamWidget::addParameter(int uas, int component, QString parameterName, ...@@ -148,6 +169,13 @@ void QGCParamWidget::addParameter(int uas, int component, QString parameterName,
addComponent(uas, component, "Component #" + QString::number(component)); addComponent(uas, component, "Component #" + QString::number(component));
} }
// Replace value in map
// FIXME
if (parameters.value(component)->contains(parameterName)) parameters.value(component)->remove(parameterName);
parameters.value(component)->insert(parameterName, value);
QString splitToken = "_"; QString splitToken = "_";
// Check if auto-grouping can work // Check if auto-grouping can work
if (parameterName.contains(splitToken)) if (parameterName.contains(splitToken))
...@@ -267,16 +295,101 @@ void QGCParamWidget::parameterItemChanged(QTreeWidgetItem* current, int column) ...@@ -267,16 +295,101 @@ void QGCParamWidget::parameterItemChanged(QTreeWidgetItem* current, int column)
if (ok) if (ok)
{ {
qDebug() << "PARAM CHANGED: COMP:" << key << "KEY:" << str << "VALUE:" << value; qDebug() << "PARAM CHANGED: COMP:" << key << "KEY:" << str << "VALUE:" << value;
// Changed values list
if (map->contains(str)) map->remove(str); if (map->contains(str)) map->remove(str);
map->insert(str, value); map->insert(str, value);
//current->setBackground(0, QBrush(QColor(QGC::colorGreen)));
//current->setBackground(1, QBrush(QColor(QGC::colorGreen))); // Check if the value was numerically changed
if (!parameters.value(key)->contains(str) || parameters.value(key)->value(str, 0.0f) != value)
{
current->setBackground(0, QBrush(QColor(QGC::colorGreen)));
current->setBackground(1, QBrush(QColor(QGC::colorGreen)));
}
// All parameters list
if (parameters.value(key)->contains(str)) parameters.value(key)->remove(str);
parameters.value(key)->insert(str, value);
} }
} }
} }
} }
} }
void QGCParamWidget::saveParameters()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "./parameters.txt", tr("Parameter File (*.txt)"));
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
return;
}
QTextStream in(&file);
in << "# Onboard parameters for system " << mav->getUASName() << "\n";
in << "#\n";
in << "# MAV ID COMPONENT ID PARAM NAME VALUE (FLOAT)\n";
// Iterate through all components, through all parameters and emit them
QMap<int, QMap<QString, float>*>::iterator i;
for (i = parameters.begin(); i != parameters.end(); ++i)
{
// Iterate through the parameters of the component
int compid = i.key();
QMap<QString, float>* comp = i.value();
{
QMap<QString, float>::iterator j;
for (j = comp->begin(); j != comp->end(); ++j)
{
in << mav->getUASID() << "\t" << compid << "\t" << j.key() << "\t" << j.value() << "\n";
in.flush();
}
}
}
file.close();
}
void QGCParamWidget::loadParameters()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Load File"), ".", tr("Parameter file (*.txt)"));
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
// Clear list
clear();
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
if (!line.startsWith("#"))
{
QStringList wpParams = line.split("\t");
if (wpParams.size() == 4)
{
// Only load parameters for right mav
if (mav->getUASID() == wpParams.at(0).toInt())
{
addParameter(wpParams.at(0).toInt(), wpParams.at(1).toInt(), wpParams.at(2), wpParams.at(3).toDouble());
if (changedValues.contains(wpParams.at(1).toInt()))
{
if (changedValues.value(wpParams.at(1).toInt())->contains(wpParams.at(1)))
{
changedValues.value(wpParams.at(1).toInt())->remove(wpParams.at(1));
}
changedValues.value(wpParams.at(1).toInt())->insert(wpParams.at(1), (float)wpParams.at(2).toDouble());
}
}
}
}
}
file.close();
}
/** /**
* @param component the subsystem which has the parameter * @param component the subsystem which has the parameter
* @param parameterName name of the parameter, as delivered by the system * @param parameterName name of the parameter, as delivered by the system
......
...@@ -69,12 +69,18 @@ public slots: ...@@ -69,12 +69,18 @@ public slots:
void clear(); void clear();
/** @brief Update when user changes parameters */ /** @brief Update when user changes parameters */
void parameterItemChanged(QTreeWidgetItem* prev, int column); void parameterItemChanged(QTreeWidgetItem* prev, int column);
/** @brief Store parameters to a file */
void saveParameters();
/** @brief Load parameters from a file */
void loadParameters();
protected: protected:
UASInterface* mav; ///< The MAV this widget is controlling UASInterface* mav; ///< The MAV this widget is controlling
QTreeWidget* tree; ///< The parameter tree QTreeWidget* tree; ///< The parameter tree
QMap<int, QTreeWidgetItem*>* components; ///< The list of components QMap<int, QTreeWidgetItem*>* components; ///< The list of components
QMap<int, QMap<QString, QTreeWidgetItem*>* > paramGroups; ///< Parameter groups QMap<int, QMap<QString, QTreeWidgetItem*>* > paramGroups; ///< Parameter groups
QMap<int, QMap<QString, float>* > changedValues; ///< Changed values QMap<int, QMap<QString, float>* > changedValues; ///< Changed values
QMap<int, QMap<QString, float>* > parameters; ///< All parameters
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment