QGCUASParamManager.cc 6.12 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
#include "UASParameterCommsMgr.h"

10
QGCUASParamManager::QGCUASParamManager(QObject *parent) :
11
    QObject(parent),
12
    mav(NULL),
13
    paramDataModel(this),
14 15
    paramCommsMgr(NULL),
    defaultComponentId(-1)
16
{
tstellanova's avatar
tstellanova committed
17

18 19 20 21 22 23 24 25

}

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

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

28 29 30
    paramCommsMgr = new UASParameterCommsMgr(this);
    paramCommsMgr->initWithUAS(uas);

31
    connectToModelAndComms();
32

33
    return this;
34 35
}

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

    connect(paramCommsMgr, SIGNAL(parameterListUpToDate()),
            this, SIGNAL(parameterListUpToDate()));
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

int QGCUASParamManager::getDefaultComponentId()
{
    int result = 0;

    if (-1 != defaultComponentId)
        return defaultComponentId;

    QList<int> components = getComponentForParam("SYS_AUTOSTART");//TODO is this the best way to find the right component?

    // Guard against multiple components responding - this will never show in practice
    if (1 == components.count()) {
        result = components.first();
        defaultComponentId = result;
    }

    qDebug() << "Default compId: " << result;

    return result;
}

84 85 86 87
QList<int> QGCUASParamManager::getComponentForParam(const QString& parameter) const
{
    return paramDataModel.getComponentForOnboardParam(parameter);
}
88 89


90 91
bool QGCUASParamManager::getParameterValue(int component, const QString& parameter, QVariant& value) const
{
92
    return paramDataModel.getOnboardParamValue(component,parameter,value);
tstellanova's avatar
tstellanova committed
93
}
94

tstellanova's avatar
tstellanova committed
95

96 97 98
void QGCUASParamManager::requestParameterUpdate(int component, const QString& parameter)
{
    if (mav) {
tstellanova's avatar
tstellanova committed
99
        paramCommsMgr->requestParameterUpdate(component,parameter);
100 101 102
    }
}

tstellanova's avatar
tstellanova committed
103 104


tstellanova's avatar
tstellanova committed
105 106 107 108 109 110
/**
 * Send a request to deliver the list of onboard parameters
 * to the MAV.
 */
void QGCUASParamManager::requestParameterList()
{
111 112 113
    if (mav) {
        emit parameterStatusMsgUpdated(tr("Requested param list.. waiting"), UASParameterCommsMgr::ParamCommsStatusLevel_OK);
        paramCommsMgr->requestParameterList();
tstellanova's avatar
tstellanova committed
114 115
    }
}
116

117
void QGCUASParamManager::requestParameterListIfEmpty()
118
{
119 120 121
    if (mav) {
        int totalOnboard = paramDataModel.countOnboardParams();
        if (totalOnboard < 2) { //TODO arbitrary constant, maybe 0 is OK?
122
            defaultComponentId = -1; //reset this ...we have no idea what the default component ID is
123 124 125
            requestParameterList();
        }
    }
126 127
}

128

129
void QGCUASParamManager::setParamDescriptions(const QMap<QString,QString>& paramInfo) {
130
    paramDataModel.setParamDescriptions(paramInfo);
131 132 133
}


134
void QGCUASParamManager::setParameter(int compId, QString paramName, QVariant value)
tstellanova's avatar
tstellanova committed
135
{
136 137 138 139
    if ((0 == compId) || (-1 == compId)) {
        //attempt to get an actual component ID
        compId = getDefaultComponentId();
    }
140
    paramDataModel.updatePendingParamWithValue(compId,paramName,value);
tstellanova's avatar
tstellanova committed
141
}
tstellanova's avatar
tstellanova committed
142

143
void QGCUASParamManager::sendPendingParameters(bool persistAfterSend)
tstellanova's avatar
tstellanova committed
144
{
145
    paramCommsMgr->sendPendingParameters(persistAfterSend);
tstellanova's avatar
tstellanova committed
146 147
}

148 149 150 151



void QGCUASParamManager::setPendingParam(int compId,  const QString& paramName,  const QVariant& value)
152
{
153 154 155 156
    if ((0 == compId) || (-1 == compId)) {
        //attempt to get an actual component ID
        compId = getDefaultComponentId();
    }
157
    paramDataModel.updatePendingParamWithValue(compId,paramName,value);
158 159
}

tstellanova's avatar
tstellanova committed
160

tstellanova's avatar
tstellanova committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

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))  {
        qWarning() << "loadParamMetaInfoCSV couldn't open:" << fileName;
        return;
    }

    QTextStream in(&paramMetaFile);
180
    paramDataModel.loadParamMetaInfoFromStream(in);
tstellanova's avatar
tstellanova committed
181 182 183 184
    paramMetaFile.close();

}

185

tstellanova's avatar
tstellanova committed
186 187 188 189
UASInterface* QGCUASParamManager::getUAS()
{
    return mav;
}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

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 {
        paramCommsMgr->writeParamsToPersistentStorage();
    }
}


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


void QGCUASParamManager::requestRcCalibrationParamsUpdate() {
    paramCommsMgr->requestRcCalibrationParamsUpdate();
}