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

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

#include "UASInterface.h"
Don Gagne's avatar
Don Gagne committed
8
#include "QGCMessageBox.h"
tstellanova's avatar
tstellanova committed
9

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

17 18 19 20 21 22 23 24

}

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

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

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

30
    connectToModelAndComms();
31

32
    return this;
33 34
}

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

41
    connect(&paramCommsMgr, SIGNAL(parameterListUpToDate()), this, SLOT(_parameterListUpToDate()));
42 43
    
    connect(&paramCommsMgr, SIGNAL(parameterListProgress(float)), this, SIGNAL(parameterListProgress(float)));
44 45 46 47 48 49 50 51 52

    // 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 )));


53
}
54 55


56 57 58
void QGCUASParamManager::clearAllPendingParams()
{
    paramDataModel.clearAllPendingParams();
59 60
    emit parameterStatusMsgUpdated(tr("Cleared all pending params"), UASParameterCommsMgr::ParamCommsStatusLevel_OK);

61 62
}

63 64 65

int QGCUASParamManager::getDefaultComponentId()
{
66
    return paramDataModel.getDefaultComponentId();
67 68
}

69 70 71 72
QList<int> QGCUASParamManager::getComponentForParam(const QString& parameter) const
{
    return paramDataModel.getComponentForOnboardParam(parameter);
}
73 74


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

tstellanova's avatar
tstellanova committed
80

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

tstellanova's avatar
tstellanova committed
88 89


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

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

112

113
void QGCUASParamManager::setParamDescriptions(const QMap<QString,QString>& paramInfo) {
114
    paramDataModel.setParamDescriptions(paramInfo);
115 116 117
}


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

127
void QGCUASParamManager::sendPendingParameters(bool persistAfterSend, bool forceSend)
tstellanova's avatar
tstellanova committed
128
{
129
    paramCommsMgr.sendPendingParameters(persistAfterSend, forceSend);
tstellanova's avatar
tstellanova committed
130 131
}

132 133 134



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

tstellanova's avatar
tstellanova committed
144

tstellanova's avatar
tstellanova committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158

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))  {
159
        qDebug() << "loadParamMetaInfoCSV couldn't open:" << fileName;
tstellanova's avatar
tstellanova committed
160 161 162 163
        return;
    }

    QTextStream in(&paramMetaFile);
164
    paramDataModel.loadParamMetaInfoFromStream(in);
tstellanova's avatar
tstellanova committed
165 166 167 168
    paramMetaFile.close();

}

169

tstellanova's avatar
tstellanova committed
170 171 172 173
UASInterface* QGCUASParamManager::getUAS()
{
    return mav;
}
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

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) {
Don Gagne's avatar
Don Gagne committed
199 200
        QGCMessageBox::warning(tr("Warning"),
                                   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."));
201 202
    }
    else {
203
        paramCommsMgr.writeParamsToPersistentStorage();
204 205 206 207 208 209 210 211 212 213 214 215 216
    }
}


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


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

void QGCUASParamManager::_parameterListUpToDate(void)
{
222
    //qDebug() << "Emitting parameters ready, count:" << paramDataModel.countOnboardParams();
223

224 225 226
    _parametersReady = true;
    emit parameterListUpToDate();
}