QGCUASParamManager.cc 5.73 KB
Newer Older
1 2
#include "QGCUASParamManager.h"

3
#include <QApplication>
tstellanova's avatar
tstellanova committed
4
#include <QDir>
5
#include <QMessageBox>
tstellanova's avatar
tstellanova committed
6 7

#include "UASInterface.h"
tstellanova's avatar
tstellanova committed
8

9
QGCUASParamManager::QGCUASParamManager(QObject *parent) :
10
    QGCUASParamManagerInterface(parent),
11
    mav(NULL),
12 13
    paramDataModel(this),
    _parametersReady(false)
14
{
tstellanova's avatar
tstellanova committed
15

16 17 18 19 20 21 22 23

}

QGCUASParamManager* QGCUASParamManager::initWithUAS(UASInterface* uas)
{
    mav = uas;

    // Load default values and tooltips for data model
tstellanova's avatar
tstellanova committed
24
    loadParamMetaInfoCSV();
25

26
    paramDataModel.setUASID(mav->getUASID());
27
    paramCommsMgr.initWithUAS(uas);
28

29
    connectToModelAndComms();
30

31
    return this;
32 33
}

34
void QGCUASParamManager::connectToModelAndComms()
35 36
{
    // Pass along comms mgr status msgs
37
    connect(&paramCommsMgr, SIGNAL(parameterStatusMsgUpdated(QString,int)),
38 39
            this, SIGNAL(parameterStatusMsgUpdated(QString,int)));

40
    connect(&paramCommsMgr, SIGNAL(parameterListUpToDate()), this, SLOT(_parameterListUpToDate()));
41 42 43 44 45 46 47 48 49

    // Pass along data model updates
    connect(&paramDataModel, SIGNAL(parameterUpdated(int, QString , QVariant )),
            this, SIGNAL(parameterUpdated(int, QString , QVariant )));

    connect(&paramDataModel, SIGNAL(pendingParamUpdate(int , const QString& , QVariant , bool )),
            this, SIGNAL(pendingParamUpdate(int , const QString& , QVariant , bool )));


50
}
51 52


53 54 55
void QGCUASParamManager::clearAllPendingParams()
{
    paramDataModel.clearAllPendingParams();
56 57
    emit parameterStatusMsgUpdated(tr("Cleared all pending params"), UASParameterCommsMgr::ParamCommsStatusLevel_OK);

58 59
}

60 61 62

int QGCUASParamManager::getDefaultComponentId()
{
63
    return paramDataModel.getDefaultComponentId();
64 65
}

66 67 68 69
QList<int> QGCUASParamManager::getComponentForParam(const QString& parameter) const
{
    return paramDataModel.getComponentForOnboardParam(parameter);
}
70 71


72 73
bool QGCUASParamManager::getParameterValue(int component, const QString& parameter, QVariant& value) const
{
74
    return paramDataModel.getOnboardParamValue(component,parameter,value);
tstellanova's avatar
tstellanova committed
75
}
76

tstellanova's avatar
tstellanova committed
77

78 79 80
void QGCUASParamManager::requestParameterUpdate(int component, const QString& parameter)
{
    if (mav) {
81
        paramCommsMgr.requestParameterUpdate(component,parameter);
82 83 84
    }
}

tstellanova's avatar
tstellanova committed
85 86


tstellanova's avatar
tstellanova committed
87 88 89 90 91 92
/**
 * Send a request to deliver the list of onboard parameters
 * to the MAV.
 */
void QGCUASParamManager::requestParameterList()
{
93 94
    if (mav) {
        emit parameterStatusMsgUpdated(tr("Requested param list.. waiting"), UASParameterCommsMgr::ParamCommsStatusLevel_OK);
95
        paramCommsMgr.requestParameterList();
tstellanova's avatar
tstellanova committed
96 97
    }
}
98

99
void QGCUASParamManager::requestParameterListIfEmpty()
100
{
101 102 103 104 105 106
    if (mav) {
        int totalOnboard = paramDataModel.countOnboardParams();
        if (totalOnboard < 2) { //TODO arbitrary constant, maybe 0 is OK?
            requestParameterList();
        }
    }
107 108
}

109

110
void QGCUASParamManager::setParamDescriptions(const QMap<QString,QString>& paramInfo) {
111
    paramDataModel.setParamDescriptions(paramInfo);
112 113 114
}


115
void QGCUASParamManager::setParameter(int compId, QString paramName, QVariant value)
tstellanova's avatar
tstellanova committed
116
{
117 118
    if ((0 == compId) || (-1 == compId)) {
        //attempt to get an actual component ID
119
        compId = paramDataModel.getDefaultComponentId();
120
    }
121
    paramDataModel.updatePendingParamWithValue(compId,paramName,value);
tstellanova's avatar
tstellanova committed
122
}
tstellanova's avatar
tstellanova committed
123

124
void QGCUASParamManager::sendPendingParameters(bool persistAfterSend, bool forceSend)
tstellanova's avatar
tstellanova committed
125
{
126
    paramCommsMgr.sendPendingParameters(persistAfterSend, forceSend);
tstellanova's avatar
tstellanova committed
127 128
}

129 130 131



132
void QGCUASParamManager::setPendingParam(int compId,  const QString& paramName,  const QVariant& value, bool forceSend)
133
{
134 135
    if ((0 == compId) || (-1 == compId)) {
        //attempt to get an actual component ID
136
        compId = paramDataModel.getDefaultComponentId();
137
    }
138
    paramDataModel.updatePendingParamWithValue(compId,paramName,value, forceSend);
139 140
}

tstellanova's avatar
tstellanova committed
141

tstellanova's avatar
tstellanova committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155

void QGCUASParamManager::loadParamMetaInfoCSV()
{
    // Load default values and tooltips
    QString autopilot(mav->getAutopilotTypeName());

    QDir appDir = QApplication::applicationDirPath();
    appDir.cd("files");
    QString fileName = QString("%1/%2/parameter_tooltips/tooltips.txt").arg(appDir.canonicalPath()).arg(autopilot.toLower());
    QFile paramMetaFile(fileName);

    qDebug() << "loadParamMetaInfoCSV for autopilot: " << autopilot << " from file: " << fileName;

    if (!paramMetaFile.open(QIODevice::ReadOnly | QIODevice::Text))  {
156
        qDebug() << "loadParamMetaInfoCSV couldn't open:" << fileName;
tstellanova's avatar
tstellanova committed
157 158 159 160
        return;
    }

    QTextStream in(&paramMetaFile);
161
    paramDataModel.loadParamMetaInfoFromStream(in);
tstellanova's avatar
tstellanova committed
162 163 164 165
    paramMetaFile.close();

}

166

tstellanova's avatar
tstellanova committed
167 168 169 170
UASInterface* QGCUASParamManager::getUAS()
{
    return mav;
}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

UASParameterDataModel* QGCUASParamManager::dataModel()
{
    return &paramDataModel;
}



void QGCUASParamManager::writeOnboardParamsToStream(QTextStream &stream, const QString& uasName)
{
    paramDataModel.writeOnboardParamsToStream(stream,uasName);
}

void QGCUASParamManager::readPendingParamsFromStream(QTextStream &stream)
{
    paramDataModel.readUpdateParamsFromStream(stream);
}



void QGCUASParamManager::copyVolatileParamsToPersistent()
{
    int changedParamCount = paramDataModel.countPendingParams();

    if (changedParamCount > 0) {
        QMessageBox msgBox;
        msgBox.setText(tr("There are locally changed parameters. Please transmit them first (<TRANSMIT>) or update them with the onboard values (<REFRESH>) before storing onboard from RAM to ROM."));
        msgBox.exec();
    }
    else {
201
        paramCommsMgr.writeParamsToPersistentStorage();
202 203 204 205 206 207 208 209 210 211 212 213 214
    }
}


void QGCUASParamManager::copyPersistentParamsToVolatile()
{
    if (mav) {
        mav->readParametersFromStorage(); //TODO use comms mgr instead?
    }
}


void QGCUASParamManager::requestRcCalibrationParamsUpdate() {
215
    paramCommsMgr.requestRcCalibrationParamsUpdate();
216
}
217 218 219 220 221 222

void QGCUASParamManager::_parameterListUpToDate(void)
{
    _parametersReady = true;
    emit parameterListUpToDate();
}