UASParameterDataModel.cc 16.8 KB
Newer Older
1 2
#include "UASParameterDataModel.h"

3 4
#include <float.h>

5
#include <QDebug>
6
#include <QStringList>
7 8
#include <QVariant>

9 10
#include "QGCMAVLink.h"

11
UASParameterDataModel::UASParameterDataModel(QObject *parent) :
12 13
    QObject(parent),
    defaultComponentId(-1)
14
{
tstellanova's avatar
tstellanova committed
15 16
    onboardParameters.clear();
    pendingParameters.clear();
17

18
}
19

20 21


22 23
int UASParameterDataModel::countPendingParams()
{
24
    int total = 0;
25 26 27 28
    QMap<int, QMap<QString, QVariant>*>::iterator i;
    for (i = pendingParameters.begin(); i != pendingParameters.end(); ++i) {
        // Iterate through the parameters of the component
        QMap<QString, QVariant>* paramList = i.value();
29
        total += paramList->count();
30
    }
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45
    return total;
}

int UASParameterDataModel::countOnboardParams()
{
    int total = 0;
    QMap<int, QMap<QString, QVariant>*>::iterator i;
    for (i = onboardParameters.begin(); i != onboardParameters.end(); ++i) {
        // Iterate through the parameters of the component
        QMap<QString, QVariant>* paramList = i.value();
        total += paramList->count();
    }

    return total;
46
}
47

48

49
bool UASParameterDataModel::updatePendingParamWithValue(int compId, const QString& key, const QVariant& value, bool forceSend)
50
{
51
    bool pending = true;
52 53
    //ensure we have this component in our onboard and pending lists already
    addComponent(compId);
54

55 56 57 58 59 60 61 62 63
	if (!forceSend) {
		QMap<QString, QVariant>* existParams = getOnboardParamsForComponent(compId);
		if (existParams->contains(key)) {
			QVariant existValue = existParams->value(key);
			if (existValue == value) {
				pending = false;
			}
		}
	}
64

65 66 67 68 69 70
    if (pending) {
        setPendingParam(compId,key,value);
    }
    else {
        removePendingParam(compId,key);
    }
71

72 73
    return pending;
}
74

75 76

bool UASParameterDataModel::isParamChangePending(int compId, const QString& key)
77
{
78 79
    QMap<QString , QVariant>* pendingParms =  getPendingParamsForComponent(compId);
    return ((NULL != pendingParms) && pendingParms->contains(key));
80
}
81

82
void UASParameterDataModel::setPendingParam(int compId, const QString& key,  const QVariant &value)
83 84
{
    //ensure we have a placeholder map for this component
85
    addComponent(compId);
86 87
    setParamWithTypeInMap(compId,key,value,pendingParameters);
    emit pendingParamUpdate(compId, key, value, true);
88
}
89

90
void UASParameterDataModel::removePendingParam(int compId, const QString& key)
91 92 93 94 95 96 97 98 99 100 101
{
    qDebug() << "removePendingParam:" << key;

    QMap<QString, QVariant> *pendParams = getPendingParamsForComponent(compId);
    if (pendParams) {
        pendParams->remove(key);
        //broadcast the existing value
        QVariant existVal;
        getOnboardParamValue(compId,key,existVal);
        emit pendingParamUpdate(compId, key,existVal, false);
    }
102 103
}

104
void UASParameterDataModel::setOnboardParam(int compId, const QString &key,  const QVariant& value)
105 106
{
    //ensure we have a placeholder map for this component
107
    addComponent(compId);
108
    //TODO use setParamWithTypeInMap instead and verify
109
    QMap<QString, QVariant> *params = getOnboardParamsForComponent(compId);
110 111
    params->insert(key,value);
}
112

113
void UASParameterDataModel::setParamWithTypeInMap(int compId, const QString& key, const QVariant &value, QMap<int, QMap<QString, QVariant>* >& map)
114 115 116 117 118 119 120
{

    switch ((int)value.type())
    {
    case QVariant::Int:
    {
        QVariant fixedValue(value.toInt());
121
        map.value(compId)->insert(key, fixedValue);
122 123 124 125 126
    }
        break;
    case QVariant::UInt:
    {
        QVariant fixedValue(value.toUInt());
127
        map.value(compId)->insert(key, fixedValue);
128 129 130 131 132
    }
        break;
    case QMetaType::Float:
    {
        QVariant fixedValue(value.toFloat());
133
        map.value(compId)->insert(key, fixedValue);
134 135 136 137 138
    }
        break;
    case QMetaType::QChar:
    {
        QVariant fixedValue(QChar((unsigned char)value.toUInt()));
139 140 141 142 143 144 145 146 147 148 149
        map.value(compId)->insert(key, fixedValue);
    }
        break;
    case QMetaType::QString:
    {
        QString strVal = value.toString();
        float floatVal = strVal.toFloat();
        QVariant fixedValue( floatVal );
        //TODO track down WHY we're getting unexpected QString values here...this is a workaround
        qDebug() << "Unexpected string QVariant:" << key << " val:" << value << "fixedVal:" << fixedValue;
        map.value(compId)->insert(key, fixedValue);
150 151 152 153 154 155 156 157
    }
        break;
    default:
        qCritical() << "ABORTED PARAM UPDATE, NO VALID QVARIANT TYPE";
        return;
    }
}

tstellanova's avatar
tstellanova committed
158
void UASParameterDataModel::addComponent(int compId)
159
{
tstellanova's avatar
tstellanova committed
160 161
    if (!onboardParameters.contains(compId)) {
        onboardParameters.insert(compId, new QMap<QString, QVariant>());
162
    }
tstellanova's avatar
tstellanova committed
163 164
    if (!pendingParameters.contains(compId)) {
        pendingParameters.insert(compId, new QMap<QString, QVariant>());
165 166
    }
}
167 168


169
void UASParameterDataModel::handleParamUpdate(int compId, const QString &paramName, const QVariant &value)
170 171 172
{
    //verify that the value requested by the user matches the set value
    //if it doesn't match, leave the pending parameter in the pending list!
tstellanova's avatar
tstellanova committed
173 174
    if (pendingParameters.contains(compId)) {
        QMap<QString , QVariant> *pendingParams = pendingParameters.value(compId);
175 176
        if ((NULL != pendingParams) && pendingParams->contains(paramName)) {
            QVariant reqVal = pendingParams->value(paramName);
177
            if (reqVal == value) {
178
                //notify everyone that this item is being removed from the pending parameters list since it's now confirmed
179
                removePendingParam(compId,paramName);
180 181
            }
            else {
182
                qDebug() << "Pending commit for " << paramName << " want: " << reqVal << " got: " << value;
183 184 185 186
            }
        }
    }

187 188
    emit parameterUpdated(compId,paramName,value);
    setOnboardParam(compId,paramName,value);
tstellanova's avatar
tstellanova committed
189

190
}
191

192
bool UASParameterDataModel::getOnboardParamValue(int componentId, const QString& key, QVariant& value) const
193 194 195 196 197 198 199 200 201 202 203 204
{

    if (onboardParameters.contains(componentId)) {
        if (onboardParameters.value(componentId)->contains(key)) {
            value = onboardParameters.value(componentId)->value(key);
            return true;
        }
    }

    return false;
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
int UASParameterDataModel::getDefaultComponentId()
{
    int result = 0;

    if (-1 != defaultComponentId)
        return defaultComponentId;

    QList<int> components = getComponentForOnboardParam("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;
}

225 226 227 228 229 230 231 232 233 234 235 236 237
QList<int> UASParameterDataModel::getComponentForOnboardParam(const QString& parameter) const
{
    QList<int> components;
    // Iterate through all components
    foreach (int comp, onboardParameters.keys())
    {
        if (onboardParameters.value(comp)->contains(parameter))
            components.append(comp);
    }

    return components;
}

238
void UASParameterDataModel::forgetAllOnboardParams()
239 240 241 242
{
    onboardParameters.clear();
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256
void UASParameterDataModel::clearAllPendingParams()
{
    QList<int> compIds =   pendingParameters.keys();
    foreach (int compId , compIds) {
        QMap<QString, QVariant>* compParams = pendingParameters.value(compId);
        QList<QString> paramNames = compParams->keys();
        foreach (QString paramName, paramNames) {
            //remove this item from pending status and broadcast update
            removePendingParam(compId,paramName);
        }
    }

}

257
void UASParameterDataModel::readUpdateParamsFromStream( QTextStream& stream)
258 259 260 261 262 263 264 265 266 267 268
{
    bool userWarned = false;

    while (!stream.atEnd()) {
        QString line = stream.readLine();
        if (!line.startsWith("#")) {
            QStringList wpParams = line.split("\t");
            int lineMavId = wpParams.at(0).toInt();
            if (wpParams.size() == 5) {
                // Only load parameters for right mav
                if (!userWarned && (uasId != lineMavId)) {
269
                    //TODO warn the user somehow ?? Appears these are saved currently with mav ID 0 but mav ID is often nonzero?
270
                    QString msg = tr("The parameters in the stream have been saved from system %1, but the currently selected system has the ID %2.").arg(lineMavId).arg(uasId);
271
                    qDebug() << msg ;
272 273 274
                    //MainWindow::instance()->showCriticalMessage(
                    //            tr("Parameter loading warning"),
                    //            tr("The parameters from the file %1 have been saved from system %2, but the currently selected system has the ID %3. If this is unintentional, please click on <READ> to revert to the parameters that are currently onboard").arg(fileName).arg(wpParams.at(0).toInt()).arg(mav->getUASID()));
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
                    userWarned = true;
                }

                bool changed = false;
                int componentId = wpParams.at(1).toInt();
                QString key = wpParams.at(2);
                QString valStr = wpParams.at(3);
                double dblVal = wpParams.at(3).toDouble();
                uint paramType = wpParams.at(4).toUInt();


                if (!onboardParameters.contains(componentId)) {
                    addComponent(componentId);
                    changed = true;
                }
                else {
291 292 293
                    QMap<QString,QVariant>* compParams = onboardParameters.value(componentId);
                    if (!compParams->contains(key) ||
                        (fabs((static_cast<float>(compParams->value(key).toDouble())) - (dblVal)) > 2.0f * FLT_EPSILON)) {
294 295 296 297 298 299 300 301 302 303
                            changed = true;
                            qDebug() << "Changed" << key << "VAL" << dblVal;
                       }
                }


                if (changed) {
                    switch (paramType)
                    {
                    case MAV_PARAM_TYPE_REAL32:
304
                        updatePendingParamWithValue(componentId,key,QVariant(valStr.toFloat()));
305 306
                        break;
                    case MAV_PARAM_TYPE_UINT32:
307
                        updatePendingParamWithValue(componentId,key, QVariant(valStr.toUInt()));
308 309
                        break;
                    case MAV_PARAM_TYPE_INT32:
310
                        updatePendingParamWithValue(componentId,key,QVariant(valStr.toInt()));
311
                        break;
312 313 314
                    case MAV_PARAM_TYPE_INT8:
                        updatePendingParamWithValue(componentId,key,QVariant((unsigned char) valStr.toUInt()));
                        break;
315 316 317 318 319 320 321 322 323 324 325 326
                    default:
                        qDebug() << "FAILED LOADING PARAM" << key << "UNKNOWN DATA TYPE";
                    }
                }


            }
        }
    }

}

327
void UASParameterDataModel::writeOnboardParamsToStream( QTextStream &stream, const QString& name)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
{
    stream << "# Onboard parameters for system " << name << "\n";
    stream << "#\n";
    stream << "# MAV ID  COMPONENT ID  PARAM NAME  VALUE (FLOAT)\n";

    // Iterate through all components, through all parameters and emit them
    QMap<int, QMap<QString, QVariant>*>::iterator i;
    for (i = onboardParameters.begin(); i != onboardParameters.end(); ++i) {
        // Iterate through the parameters of the component
        int compid = i.key();
        QMap<QString, QVariant>* comp = i.value();
        {
            QMap<QString, QVariant>::iterator j;
            for (j = comp->begin(); j != comp->end(); ++j)
            {
                QString paramValue("%1");
                QString paramType("%1");
                switch ((int)j.value().type())
                {
                case QVariant::Int:
                    paramValue = paramValue.arg(j.value().toInt());
                    paramType = paramType.arg(MAV_PARAM_TYPE_INT32);
                    break;
                case QVariant::UInt:
                    paramValue = paramValue.arg(j.value().toUInt());
                    paramType = paramType.arg(MAV_PARAM_TYPE_UINT32);
                    break;
                case QMetaType::Float:
                    // We store parameters as floats, with only 6 digits of precision guaranteed for decimal string conversion
                    // (see IEEE 754, 32 bit single-precision)
                    paramValue = paramValue.arg((double)j.value().toFloat(), 25, 'g', 6);
                    paramType = paramType.arg(MAV_PARAM_TYPE_REAL32);
                    break;
361 362 363 364 365 366
                case QMetaType::QChar:
                case QMetaType::Char:
                    // see UAS::setParameter()
                    paramValue = paramValue.arg((unsigned char)j.value().toUInt());
                    paramType = paramType.arg(MAV_PARAM_TYPE_INT8);
                    break;
367
                default:
368
                    qCritical() << "ABORTED PARAM WRITE TO FILE, PARAM '" << j.key() << "' NO VALID QVARIANT TYPE" << j.value();
369 370 371 372 373 374 375 376
                    return;
                }
                stream << this->uasId << "\t" << compid << "\t" << j.key() << "\t" << paramValue << "\t" << paramType << "\n";
                stream.flush();
            }
        }
    }
}
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516


void UASParameterDataModel::loadParamMetaInfoFromStream(QTextStream& stream)
{

    // First line is header
    // there might be more lines, but the first
    // line is assumed to be at least header
    QString header = stream.readLine();

    // Ignore top-level comment lines
    while (header.startsWith('#') || header.startsWith('/')
           || header.startsWith('=') || header.startsWith('^'))
    {
        header = stream.readLine();
    }

    bool charRead = false;
    QString separator = "";
    QList<QChar> sepCandidates;
    sepCandidates << '\t';
    sepCandidates << ',';
    sepCandidates << ';';
    //sepCandidates << ' ';
    sepCandidates << '~';
    sepCandidates << '|';

    // Iterate until separator is found
    // or full header is parsed
    for (int i = 0; i < header.length(); i++)
    {
        if (sepCandidates.contains(header.at(i)))
        {
            // Separator found
            if (charRead)
            {
                separator += header[i];
            }
        }
        else
        {
            // Char found
            charRead = true;
            // If the separator is not empty, this char
            // has been read after a separator, so detection
            // is now complete
            if (separator != "") break;
        }
    }

    bool stripFirstSeparator = false;
    bool stripLastSeparator = false;

    // Figure out if the lines start with the separator (e.g. wiki syntax)
    if (header.startsWith(separator)) stripFirstSeparator = true;

    // Figure out if the lines end with the separator (e.g. wiki syntax)
    if (header.endsWith(separator)) stripLastSeparator = true;

    QString out = separator;
    out.replace("\t", "<tab>");
    //qDebug() << " Separator: \"" << out << "\"";
    //qDebug() << "READING CSV:" << header;


    // Read data
    while (!stream.atEnd())
    {
        QString line = stream.readLine();

        //qDebug() << "LINE PRE-STRIP" << line;

        // Strip separtors if necessary
        if (stripFirstSeparator) line.remove(0, separator.length());
        if (stripLastSeparator) line.remove(line.length()-separator.length(), line.length()-1);

        //qDebug() << "LINE POST-STRIP" << line;

        // Keep empty parts here - we still have to act on them
        QStringList parts = line.split(separator, QString::KeepEmptyParts);

        // Each line is:
        // variable name, Min, Max, Default, Multiplier, Enabled (0 = no, 1 = yes), Comment


        // Fill in min, max and default values
        if (parts.count() > 1)
        {
            // min
            paramMin.insert(parts.at(0).trimmed(), parts.at(1).toDouble());
        }
        if (parts.count() > 2)
        {
            // max
            paramMax.insert(parts.at(0).trimmed(), parts.at(2).toDouble());
        }
        if (parts.count() > 3)
        {
            // default
            paramDefault.insert(parts.at(0).trimmed(), parts.at(3).toDouble());
        }
        // IGNORING 4 and 5 for now
        if (parts.count() > 6)
        {
            // tooltip
            paramDescriptions.insert(parts.at(0).trimmed(), parts.at(6).trimmed());
            qDebug() << "PARAM META:" << parts.at(0).trimmed();
        }
    }
}

 void UASParameterDataModel::setParamDescriptions(const QMap<QString,QString>& paramInfo)
{
    if (paramInfo.isEmpty()) {
        qDebug() << __FILE__ << ":" << __LINE__ << "setParamDescriptions with empty";
    }

    paramDescriptions = paramInfo;
}

bool UASParameterDataModel::isValueGreaterThanParamMax(const QString& paramName, double dblVal)
{
    if (paramMax.contains(paramName)) {
        if (dblVal > paramMax.value(paramName))
            return true;
    }

    return false;
}

bool UASParameterDataModel::isValueLessThanParamMin(const QString& paramName, double dblVal)
{
     if (paramMin.contains(paramName)) {
         if (dblVal < paramMin.value(paramName))
             return true;
     }

     return false;
 }