MissionItem.cc 32.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*===================================================================
QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

#include <QStringList>
#include <QDebug>

#include "MissionItem.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
29
#include "JsonHelper.h"
30 31 32 33 34 35 36 37 38 39 40 41

QGC_LOGGING_CATEGORY(MissionItemLog, "MissionItemLog")

const double MissionItem::defaultAltitude =             25.0;

FactMetaData* MissionItem::_altitudeMetaData =          NULL;
FactMetaData* MissionItem::_commandMetaData =           NULL;
FactMetaData* MissionItem::_defaultParamMetaData =      NULL;
FactMetaData* MissionItem::_frameMetaData =             NULL;
FactMetaData* MissionItem::_latitudeMetaData =          NULL;
FactMetaData* MissionItem::_longitudeMetaData =         NULL;

42 43 44 45 46 47 48 49 50 51 52 53
const char*  MissionItem::_itemType =               "missionItem";
const char*  MissionItem::_jsonTypeKey =            "type";
const char*  MissionItem::_jsonIdKey =              "id";
const char*  MissionItem::_jsonFrameKey =           "frame";
const char*  MissionItem::_jsonCommandKey =         "command";
const char*  MissionItem::_jsonParam1Key =          "param1";
const char*  MissionItem::_jsonParam2Key =          "param2";
const char*  MissionItem::_jsonParam3Key =          "param3";
const char*  MissionItem::_jsonParam4Key =          "param4";
const char*  MissionItem::_jsonAutoContinueKey =    "autoContinue";
const char*  MissionItem::_jsonCoordinateKey =      "coordinate";

54 55 56 57 58 59
struct EnumInfo_s {
    const char *    label;
    MAV_FRAME       frame;
};

static const struct EnumInfo_s _rgMavFrameInfo[] = {
60 61 62 63 64 65 66 67 68 69 70 71
{ "MAV_FRAME_GLOBAL",                   MAV_FRAME_GLOBAL },
{ "MAV_FRAME_LOCAL_NED",                MAV_FRAME_LOCAL_NED },
{ "MAV_FRAME_MISSION",                  MAV_FRAME_MISSION },
{ "MAV_FRAME_GLOBAL_RELATIVE_ALT",      MAV_FRAME_GLOBAL_RELATIVE_ALT },
{ "MAV_FRAME_LOCAL_ENU",                MAV_FRAME_LOCAL_ENU },
{ "MAV_FRAME_GLOBAL_INT",               MAV_FRAME_GLOBAL_INT },
{ "MAV_FRAME_GLOBAL_RELATIVE_ALT_INT",  MAV_FRAME_GLOBAL_RELATIVE_ALT_INT },
{ "MAV_FRAME_LOCAL_OFFSET_NED",         MAV_FRAME_LOCAL_OFFSET_NED },
{ "MAV_FRAME_BODY_NED",                 MAV_FRAME_BODY_NED },
{ "MAV_FRAME_BODY_OFFSET_NED",          MAV_FRAME_BODY_OFFSET_NED },
{ "MAV_FRAME_GLOBAL_TERRAIN_ALT",       MAV_FRAME_GLOBAL_TERRAIN_ALT },
{ "MAV_FRAME_GLOBAL_TERRAIN_ALT_INT",   MAV_FRAME_GLOBAL_TERRAIN_ALT_INT },
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
};

QDebug operator<<(QDebug dbg, const MissionItem& missionItem)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "MissionItem(" << missionItem.coordinate() << ")";
    
    return dbg;
}

QDebug operator<<(QDebug dbg, const MissionItem* missionItem)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace() << "MissionItem(" << missionItem->coordinate() << ")";
    
    return dbg;
}

90
MissionItem::MissionItem(Vehicle* vehicle, QObject* parent)
91
    : QObject(parent)
92
    , _vehicle(vehicle)
93 94 95 96
    , _rawEdit(false)
    , _dirty(false)
    , _sequenceNumber(0)
    , _isCurrentItem(false)
97 98
    , _altDifference(0.0)
    , _altPercent(0.0)
99
    , _azimuth(0.0)
100 101
    , _distance(0.0)
    , _homePositionSpecialCase(false)
102
    , _showHomePosition(false)
103 104
    , _altitudeRelativeToHomeFact   (0, "Altitude is relative to home", FactMetaData::valueTypeUint32)
    , _autoContinueFact             (0, "AutoContinue",                 FactMetaData::valueTypeUint32)
105 106
    , _commandFact                  (0, "",                             FactMetaData::valueTypeUint32)
    , _frameFact                    (0, "",                             FactMetaData::valueTypeUint32)
107 108 109 110 111 112 113 114 115 116 117 118
    , _param1Fact                   (0, "Param1:",                      FactMetaData::valueTypeDouble)
    , _param2Fact                   (0, "Param2:",                      FactMetaData::valueTypeDouble)
    , _param3Fact                   (0, "Param3:",                      FactMetaData::valueTypeDouble)
    , _param4Fact                   (0, "Param4:",                      FactMetaData::valueTypeDouble)
    , _param5Fact                   (0, "Latitude:",                    FactMetaData::valueTypeDouble)
    , _param6Fact                   (0, "Longitude:",                   FactMetaData::valueTypeDouble)
    , _param7Fact                   (0, "Altitude:",                    FactMetaData::valueTypeDouble)
    , _supportedCommandFact         (0, "Command:",                     FactMetaData::valueTypeUint32)
    , _param1MetaData(FactMetaData::valueTypeDouble)
    , _param2MetaData(FactMetaData::valueTypeDouble)
    , _param3MetaData(FactMetaData::valueTypeDouble)
    , _param4MetaData(FactMetaData::valueTypeDouble)
119 120 121
    , _param5MetaData(FactMetaData::valueTypeDouble)
    , _param6MetaData(FactMetaData::valueTypeDouble)
    , _param7MetaData(FactMetaData::valueTypeDouble)
122 123
    , _syncingAltitudeRelativeToHomeAndFrame    (false)
    , _syncingHeadingDegreesAndParam4           (false)
124
    , _missionCommands(qgcApp()->toolbox()->missionCommands())
125
{
Don Gagne's avatar
Don Gagne committed
126 127 128
    // Need a good command and frame before we start passing signals around
    _commandFact.setRawValue(MAV_CMD_NAV_WAYPOINT);
    _frameFact.setRawValue(MAV_FRAME_GLOBAL_RELATIVE_ALT);
129
    _altitudeRelativeToHomeFact.setRawValue(true);
Don Gagne's avatar
Don Gagne committed
130

131 132 133 134
    _setupMetaData();
    _connectSignals();

    setAutoContinue(true);
135
    setDefaultsForCommand();
136 137
}

138 139
MissionItem::MissionItem(Vehicle*       vehicle,
                         int             sequenceNumber,
140 141 142 143 144 145 146 147 148 149 150 151 152
                         MAV_CMD         command,
                         MAV_FRAME       frame,
                         double          param1,
                         double          param2,
                         double          param3,
                         double          param4,
                         double          param5,
                         double          param6,
                         double          param7,
                         bool            autoContinue,
                         bool            isCurrentItem,
                         QObject*        parent)
    : QObject(parent)
153
    , _vehicle(vehicle)
154 155 156 157
    , _rawEdit(false)
    , _dirty(false)
    , _sequenceNumber(sequenceNumber)
    , _isCurrentItem(isCurrentItem)
Don Gagne's avatar
Don Gagne committed
158
    , _altDifference(0.0)
159
    , _altPercent(0.0)
160
    , _azimuth(0.0)
161 162
    , _distance(0.0)
    , _homePositionSpecialCase(false)
163
    , _showHomePosition(false)
164
    , _altitudeRelativeToHomeFact   (0, "Altitude is relative to home", FactMetaData::valueTypeUint32)
Don Gagne's avatar
Don Gagne committed
165 166
    , _commandFact                  (0, "",                             FactMetaData::valueTypeUint32)
    , _frameFact                    (0, "",                             FactMetaData::valueTypeUint32)
167 168 169 170 171 172 173 174 175 176 177 178
    , _param1Fact                   (0, "Param1:",                      FactMetaData::valueTypeDouble)
    , _param2Fact                   (0, "Param2:",                      FactMetaData::valueTypeDouble)
    , _param3Fact                   (0, "Param3:",                      FactMetaData::valueTypeDouble)
    , _param4Fact                   (0, "Param4:",                      FactMetaData::valueTypeDouble)
    , _param5Fact                   (0, "Lat/X:",                       FactMetaData::valueTypeDouble)
    , _param6Fact                   (0, "Lon/Y:",                       FactMetaData::valueTypeDouble)
    , _param7Fact                   (0, "Alt/Z:",                       FactMetaData::valueTypeDouble)
    , _supportedCommandFact         (0, "Command:",                     FactMetaData::valueTypeUint32)
    , _param1MetaData(FactMetaData::valueTypeDouble)
    , _param2MetaData(FactMetaData::valueTypeDouble)
    , _param3MetaData(FactMetaData::valueTypeDouble)
    , _param4MetaData(FactMetaData::valueTypeDouble)
179 180 181
    , _param5MetaData(FactMetaData::valueTypeDouble)
    , _param6MetaData(FactMetaData::valueTypeDouble)
    , _param7MetaData(FactMetaData::valueTypeDouble)
182 183
    , _syncingAltitudeRelativeToHomeAndFrame    (false)
    , _syncingHeadingDegreesAndParam4           (false)
184
    , _missionCommands(qgcApp()->toolbox()->missionCommands())
185
{
Don Gagne's avatar
Don Gagne committed
186 187 188
    // Need a good command and frame before we start passing signals around
    _commandFact.setRawValue(MAV_CMD_NAV_WAYPOINT);
    _frameFact.setRawValue(MAV_FRAME_GLOBAL_RELATIVE_ALT);
189
    _altitudeRelativeToHomeFact.setRawValue(true);
Don Gagne's avatar
Don Gagne committed
190

191 192 193 194 195 196 197 198 199 200 201 202 203
    _setupMetaData();
    _connectSignals();

    setCommand(command);
    setFrame(frame);
    setAutoContinue(autoContinue);

    _syncFrameToAltitudeRelativeToHome();

    _param1Fact.setRawValue(param1);
    _param2Fact.setRawValue(param2);
    _param3Fact.setRawValue(param3);
    _param4Fact.setRawValue(param4);
Don Gagne's avatar
Don Gagne committed
204 205 206
    _param5Fact.setRawValue(param5);
    _param6Fact.setRawValue(param6);
    _param7Fact.setRawValue(param7);
207 208 209 210
}

MissionItem::MissionItem(const MissionItem& other, QObject* parent)
    : QObject(parent)
211
    , _vehicle(NULL)
212 213 214 215
    , _rawEdit(false)
    , _dirty(false)
    , _sequenceNumber(0)
    , _isCurrentItem(false)
Don Gagne's avatar
Don Gagne committed
216
    , _altDifference(0.0)
217
    , _altPercent(0.0)
218
    , _azimuth(0.0)
219 220
    , _distance(0.0)
    , _homePositionSpecialCase(false)
221
    , _showHomePosition(false)
222
    , _altitudeRelativeToHomeFact   (0, "Altitude is relative to home", FactMetaData::valueTypeUint32)
Don Gagne's avatar
Don Gagne committed
223 224
    , _commandFact                  (0, "",                             FactMetaData::valueTypeUint32)
    , _frameFact                    (0, "",                             FactMetaData::valueTypeUint32)
225 226 227 228 229 230 231 232 233 234 235 236
    , _param1Fact                   (0, "Param1:",                      FactMetaData::valueTypeDouble)
    , _param2Fact                   (0, "Param2:",                      FactMetaData::valueTypeDouble)
    , _param3Fact                   (0, "Param3:",                      FactMetaData::valueTypeDouble)
    , _param4Fact                   (0, "Param4:",                      FactMetaData::valueTypeDouble)
    , _param5Fact                   (0, "Lat/X:",                       FactMetaData::valueTypeDouble)
    , _param6Fact                   (0, "Lon/Y:",                       FactMetaData::valueTypeDouble)
    , _param7Fact                   (0, "Alt/Z:",                       FactMetaData::valueTypeDouble)
    , _supportedCommandFact         (0, "Command:",                     FactMetaData::valueTypeUint32)
    , _param1MetaData(FactMetaData::valueTypeDouble)
    , _param2MetaData(FactMetaData::valueTypeDouble)
    , _param3MetaData(FactMetaData::valueTypeDouble)
    , _param4MetaData(FactMetaData::valueTypeDouble)
237 238
    , _syncingAltitudeRelativeToHomeAndFrame    (false)
    , _syncingHeadingDegreesAndParam4           (false)
239
    , _missionCommands(qgcApp()->toolbox()->missionCommands())
240
{
Don Gagne's avatar
Don Gagne committed
241 242 243
    // Need a good command and frame before we start passing signals around
    _commandFact.setRawValue(MAV_CMD_NAV_WAYPOINT);
    _frameFact.setRawValue(MAV_FRAME_GLOBAL_RELATIVE_ALT);
244
    _altitudeRelativeToHomeFact.setRawValue(true);
Don Gagne's avatar
Don Gagne committed
245

246 247 248 249 250 251 252 253
    _setupMetaData();
    _connectSignals();

    *this = other;
}

const MissionItem& MissionItem::operator=(const MissionItem& other)
{
254 255
    _vehicle = other._vehicle;

256 257 258 259 260 261 262
    setCommand(other.command());
    setFrame(other.frame());
    setRawEdit(other._rawEdit);
    setDirty(other._dirty);
    setSequenceNumber(other._sequenceNumber);
    setAutoContinue(other.autoContinue());
    setIsCurrentItem(other._isCurrentItem);
Don Gagne's avatar
Don Gagne committed
263
    setAltDifference(other._altDifference);
264
    setAltPercent(other._altPercent);
Don Gagne's avatar
Don Gagne committed
265
    setAzimuth(other._azimuth);
266 267
    setDistance(other._distance);
    setHomePositionSpecialCase(other._homePositionSpecialCase);
268
    setShowHomePosition(other._showHomePosition);
269 270 271

    _syncFrameToAltitudeRelativeToHome();

Don Gagne's avatar
Don Gagne committed
272 273 274 275 276 277 278
    _param1Fact.setRawValue(other._param1Fact.rawValue());
    _param2Fact.setRawValue(other._param2Fact.rawValue());
    _param3Fact.setRawValue(other._param3Fact.rawValue());
    _param4Fact.setRawValue(other._param4Fact.rawValue());
    _param5Fact.setRawValue(other._param5Fact.rawValue());
    _param6Fact.setRawValue(other._param6Fact.rawValue());
    _param7Fact.setRawValue(other._param7Fact.rawValue());
279 280 281 282 283 284 285 286 287 288 289 290 291 292

    return *this;
}

void MissionItem::_connectSignals(void)
{
    // Connect to change signals to track dirty state
    connect(&_param1Fact,   &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
    connect(&_param2Fact,   &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
    connect(&_param3Fact,   &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
    connect(&_param4Fact,   &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
    connect(&_param5Fact,   &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
    connect(&_param6Fact,   &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
    connect(&_param7Fact,   &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
Don Gagne's avatar
Don Gagne committed
293 294
    connect(&_frameFact,    &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
    connect(&_commandFact,  &Fact::valueChanged,                    this, &MissionItem::_setDirtyFromSignal);
295 296 297 298 299 300 301 302 303 304 305 306 307 308
    connect(this,           &MissionItem::sequenceNumberChanged,    this, &MissionItem::_setDirtyFromSignal);

    // Values from these facts must propogate back and forth between the real object storage
    connect(&_altitudeRelativeToHomeFact,   &Fact::valueChanged,        this, &MissionItem::_syncAltitudeRelativeToHomeToFrame);
    connect(this,                           &MissionItem::frameChanged, this, &MissionItem::_syncFrameToAltitudeRelativeToHome);

    // These are parameter coordinates, they must emit coordinateChanged signal
    connect(&_param5Fact, &Fact::valueChanged, this, &MissionItem::_sendCoordinateChanged);
    connect(&_param6Fact, &Fact::valueChanged, this, &MissionItem::_sendCoordinateChanged);
    connect(&_param7Fact, &Fact::valueChanged, this, &MissionItem::_sendCoordinateChanged);

    // The following changes may also change friendlyEditAllowed
    connect(&_autoContinueFact, &Fact::valueChanged,        this, &MissionItem::_sendFriendlyEditAllowedChanged);
    connect(&_commandFact,      &Fact::valueChanged,        this, &MissionItem::_sendFriendlyEditAllowedChanged);
Don Gagne's avatar
Don Gagne committed
309
    connect(&_frameFact,        &Fact::valueChanged,        this, &MissionItem::_sendFriendlyEditAllowedChanged);
310

311 312 313
    // When the command changes we need to set defaults. This must go out before the signals below so it must be registered first.
    connect(&_commandFact,  &Fact::valueChanged, this, &MissionItem::setDefaultsForCommand);

314 315 316 317
    // Whenever these properties change the ui model changes as well
    connect(this, &MissionItem::commandChanged, this, &MissionItem::_sendUiModelChanged);
    connect(this, &MissionItem::rawEditChanged, this, &MissionItem::_sendUiModelChanged);

Don Gagne's avatar
Don Gagne committed
318
    // These fact signals must alway signal out through MissionItem signals
319 320
    connect(&_commandFact,  &Fact::valueChanged, this, &MissionItem::_sendCommandChanged);
    connect(&_frameFact,    &Fact::valueChanged, this, &MissionItem::_sendFrameChanged);
Don Gagne's avatar
Don Gagne committed
321

322 323 324 325 326 327 328 329 330
}

void MissionItem::_setupMetaData(void)
{
    QStringList enumStrings;
    QVariantList enumValues;

    if (!_altitudeMetaData) {
        _altitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
331
        _altitudeMetaData->setRawUnits("meters");
332 333 334 335
        _altitudeMetaData->setDecimalPlaces(3);

        enumStrings.clear();
        enumValues.clear();
336 337
        foreach (const MAV_CMD command, _missionCommands->commandsIds()) {
            const MavCmdInfo* mavCmdInfo = _missionCommands->getMavCmdInfo(command, _vehicle);
Don Gagne's avatar
Don Gagne committed
338 339
            enumStrings.append(mavCmdInfo->rawName());
            enumValues.append(QVariant(mavCmdInfo->command()));
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        }
        _commandMetaData = new FactMetaData(FactMetaData::valueTypeUint32);
        _commandMetaData->setEnumInfo(enumStrings, enumValues);

        _defaultParamMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
        _defaultParamMetaData->setDecimalPlaces(7);

        enumStrings.clear();
        enumValues.clear();
        for (size_t i=0; i<sizeof(_rgMavFrameInfo)/sizeof(_rgMavFrameInfo[0]); i++) {
            const struct EnumInfo_s* mavFrameInfo = &_rgMavFrameInfo[i];

            enumStrings.append(mavFrameInfo->label);
            enumValues.append(QVariant(mavFrameInfo->frame));
        }
        _frameMetaData = new FactMetaData(FactMetaData::valueTypeUint32);
        _frameMetaData->setEnumInfo(enumStrings, enumValues);

        _latitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
359
        _latitudeMetaData->setRawUnits("deg");
360 361 362
        _latitudeMetaData->setDecimalPlaces(7);

        _longitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
363
        _longitudeMetaData->setRawUnits("deg");
364 365 366 367 368 369 370 371 372 373 374 375
        _longitudeMetaData->setDecimalPlaces(7);

    }

    _commandFact.setMetaData(_commandMetaData);
    _frameFact.setMetaData(_frameMetaData);
}

MissionItem::~MissionItem()
{    
}

376
void MissionItem::save(QJsonObject& json)
377
{
378 379 380 381 382 383 384 385 386 387 388 389 390
    json[_jsonTypeKey] = _itemType;
    json[_jsonIdKey] = sequenceNumber();
    json[_jsonFrameKey] = frame();
    json[_jsonCommandKey] = command();
    json[_jsonParam1Key] = param1();
    json[_jsonParam2Key] = param2();
    json[_jsonParam3Key] = param3();
    json[_jsonParam4Key] = param4();
    json[_jsonAutoContinueKey] = autoContinue();

    QJsonArray coordinateArray;
    coordinateArray << param5() << param6() << param7();
    json[_jsonCoordinateKey] = coordinateArray;
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
}

bool MissionItem::load(QTextStream &loadStream)
{
    const QStringList &wpParams = loadStream.readLine().split("\t");
    if (wpParams.size() == 12) {
        setSequenceNumber(wpParams[0].toInt());
        setIsCurrentItem(wpParams[1].toInt() == 1 ? true : false);
        setFrame((MAV_FRAME)wpParams[2].toInt());
        setCommand((MAV_CMD)wpParams[3].toInt());
        setParam1(wpParams[4].toDouble());
        setParam2(wpParams[5].toDouble());
        setParam3(wpParams[6].toDouble());
        setParam4(wpParams[7].toDouble());
        setParam5(wpParams[8].toDouble());
        setParam6(wpParams[9].toDouble());
        setParam7(wpParams[10].toDouble());
        setAutoContinue(wpParams[11].toInt() == 1 ? true : false);
        return true;
    }
411

412 413 414
    return false;
}

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
bool MissionItem::load(const QJsonObject& json, QString& errorString)
{
    QStringList requiredKeys;

    requiredKeys << _jsonTypeKey << _jsonIdKey << _jsonFrameKey << _jsonCommandKey <<
                    _jsonParam1Key << _jsonParam2Key << _jsonParam3Key << _jsonParam4Key <<
                    _jsonAutoContinueKey << _jsonCoordinateKey;
    if (!JsonHelper::validateRequiredKeys(json, requiredKeys, errorString)) {
        return false;
    }

    if (json[_jsonTypeKey] != _itemType) {
        errorString = QString("type found: %1 must be: %2").arg(json[_jsonTypeKey].toString()).arg(_itemType);
        return false;
    }

    QGeoCoordinate coordinate;
    if (!JsonHelper::toQGeoCoordinate(json[_jsonCoordinateKey], coordinate, true /* altitudeRequired */, errorString)) {
        return false;
    }
    setCoordinate(coordinate);

    setIsCurrentItem(false);
    setSequenceNumber(json[_jsonIdKey].toInt());
    setFrame((MAV_FRAME)json[_jsonFrameKey].toInt());
    setCommand((MAV_CMD)json[_jsonCommandKey].toInt());
    setParam1(json[_jsonParam1Key].toDouble());
    setParam2(json[_jsonParam2Key].toDouble());
    setParam3(json[_jsonParam3Key].toDouble());
    setParam4(json[_jsonParam4Key].toDouble());
    setAutoContinue(json[_jsonAutoContinueKey].toBool());

    return true;
}

450 451 452 453 454 455 456 457 458 459

void MissionItem::setSequenceNumber(int sequenceNumber)
{
    _sequenceNumber = sequenceNumber;
    emit sequenceNumberChanged(_sequenceNumber);
}

void MissionItem::setCommand(MAV_CMD command)
{
    if ((MAV_CMD)this->command() != command) {
Don Gagne's avatar
Don Gagne committed
460
        _commandFact.setRawValue(command);
Don Gagne's avatar
Don Gagne committed
461
        setDefaultsForCommand();
462 463 464 465 466 467 468 469 470 471 472 473 474
        emit commandChanged(this->command());
    }
}

void MissionItem::setCommand(MavlinkQmlSingleton::Qml_MAV_CMD command)
{
    setCommand((MAV_CMD)command);
}


void MissionItem::setFrame(MAV_FRAME frame)
{
    if (this->frame() != frame) {
Don Gagne's avatar
Don Gagne committed
475
        _frameFact.setRawValue(frame);
Don Gagne's avatar
Don Gagne committed
476
        frameChanged(frame);
477 478 479 480 481 482
    }
}

void MissionItem::setAutoContinue(bool autoContinue)
{
    if (this->autoContinue() != autoContinue) {
Don Gagne's avatar
Don Gagne committed
483
        _autoContinueFact.setRawValue(autoContinue);
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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    }
}

void MissionItem::setIsCurrentItem(bool isCurrentItem)
{
    if (_isCurrentItem != isCurrentItem) {
        _isCurrentItem = isCurrentItem;
        emit isCurrentItemChanged(isCurrentItem);
    }
}

void MissionItem::setParam1(double param)
{
    if (param1() != param) {
        _param1Fact.setRawValue(param);
    }
}

void MissionItem::setParam2(double param)
{
    if (param2() != param) {
        _param2Fact.setRawValue(param);
    }
}

void MissionItem::setParam3(double param)
{
    if (param3() != param) {
        _param3Fact.setRawValue(param);
    }
}

void MissionItem::setParam4(double param)
{
    if (param4() != param) {
        _param4Fact.setRawValue(param);
    }
}

void MissionItem::setParam5(double param)
{
    if (param5() != param) {
        _param5Fact.setRawValue(param);
    }
}

void MissionItem::setParam6(double param)
{
    if (param6() != param) {
        _param6Fact.setRawValue(param);
    }
}

void MissionItem::setParam7(double param)
{
    if (param7() != param) {
        _param7Fact.setRawValue(param);
    }
}

544 545
bool MissionItem::standaloneCoordinate(void) const
{
546 547
    if (_missionCommands->contains((MAV_CMD)command())) {
        return _missionCommands->getMavCmdInfo((MAV_CMD)command(), _vehicle)->standaloneCoordinate();
548 549 550
    } else {
        return false;
    }
551 552
}

553 554
bool MissionItem::specifiesCoordinate(void) const
{
555 556
    if (_missionCommands->contains((MAV_CMD)command())) {
        return _missionCommands->getMavCmdInfo((MAV_CMD)command(), _vehicle)->specifiesCoordinate();
557 558 559
    } else {
        return false;
    }
560 561 562 563
}

QString MissionItem::commandDescription(void) const
{
564 565
    if (_missionCommands->contains((MAV_CMD)command())) {
        return _missionCommands->getMavCmdInfo((MAV_CMD)command(), _vehicle)->description();
566 567 568 569
    } else {
        qWarning() << "Should not ask for command description on unknown command";
        return QString();
    }
570 571 572 573
}

void MissionItem::_clearParamMetaData(void)
{
574
    _param1MetaData.setRawUnits("");
575
    _param1MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces);
576
    _param2MetaData.setRawUnits("");
577
    _param2MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces);
578
    _param3MetaData.setRawUnits("");
579
    _param3MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces);
580
    _param4MetaData.setRawUnits("");
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    _param4MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces);
}

QmlObjectListModel* MissionItem::textFieldFacts(void)
{
    QmlObjectListModel* model = new QmlObjectListModel(this);
    
    if (rawEdit()) {
        _param1Fact._setName("Param1:");
        _param1Fact.setMetaData(_defaultParamMetaData);
        model->append(&_param1Fact);
        _param2Fact._setName("Param2:");
        _param2Fact.setMetaData(_defaultParamMetaData);
        model->append(&_param2Fact);
        _param3Fact._setName("Param3:");
        _param3Fact.setMetaData(_defaultParamMetaData);
        model->append(&_param3Fact);
        _param4Fact._setName("Param4:");
        _param4Fact.setMetaData(_defaultParamMetaData);
        model->append(&_param4Fact);
        _param5Fact._setName("Lat/X:");
        _param5Fact.setMetaData(_defaultParamMetaData);
        model->append(&_param5Fact);
        _param6Fact._setName("Lon/Y:");
        _param6Fact.setMetaData(_defaultParamMetaData);
        model->append(&_param6Fact);
        _param7Fact._setName("Alt/Z:");
        _param7Fact.setMetaData(_defaultParamMetaData);
        model->append(&_param7Fact);
    } else {
        _clearParamMetaData();

613 614 615 616 617 618
        MAV_CMD command;
        if (_homePositionSpecialCase) {
            command = MAV_CMD_NAV_LAST;
        } else {
            command = (MAV_CMD)this->command();
        }
619

620 621
        Fact*           rgParamFacts[7] =       { &_param1Fact, &_param2Fact, &_param3Fact, &_param4Fact, &_param5Fact, &_param6Fact, &_param7Fact };
        FactMetaData*   rgParamMetaData[7] =    { &_param1MetaData, &_param2MetaData, &_param3MetaData, &_param4MetaData, &_param5MetaData, &_param6MetaData, &_param7MetaData };
622

623
        for (int i=1; i<=7; i++) {
624
            const QMap<int, MavCmdParamInfo*>& paramInfoMap = _missionCommands->getMavCmdInfo(command, _vehicle)->paramInfoMap();
Don Gagne's avatar
Don Gagne committed
625 626 627 628 629 630 631 632 633

            if (paramInfoMap.contains(i) && paramInfoMap[i]->enumStrings().count() == 0) {
                Fact*               paramFact =     rgParamFacts[i-1];
                FactMetaData*       paramMetaData = rgParamMetaData[i-1];
                MavCmdParamInfo*    paramInfo =     paramInfoMap[i];

                paramFact->_setName(paramInfo->label());
                paramMetaData->setDecimalPlaces(paramInfo->decimalPlaces());
                paramMetaData->setEnumInfo(paramInfo->enumStrings(), paramInfo->enumValues());
634
                paramMetaData->setRawUnits(paramInfo->units());
635 636
                paramFact->setMetaData(paramMetaData);
                model->append(paramFact);
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
            }
        }

        if (specifiesCoordinate()) {
            _param7Fact._setName("Altitude:");
            _param7Fact.setMetaData(_altitudeMetaData);
            model->append(&_param7Fact);
        }
    }
    
    return model;
}

QmlObjectListModel* MissionItem::checkboxFacts(void)
{
    QmlObjectListModel* model = new QmlObjectListModel(this);
    

    if (rawEdit()) {
        model->append(&_autoContinueFact);
657
    } else if (specifiesCoordinate() && !_homePositionSpecialCase) {
658 659 660 661 662 663 664 665 666 667 668 669 670
        model->append(&_altitudeRelativeToHomeFact);
    }

    return model;
}

QmlObjectListModel* MissionItem::comboboxFacts(void)
{
    QmlObjectListModel* model = new QmlObjectListModel(this);

    if (rawEdit()) {
        model->append(&_commandFact);
        model->append(&_frameFact);
671 672 673 674
    } else {
        Fact*           rgParamFacts[7] =       { &_param1Fact, &_param2Fact, &_param3Fact, &_param4Fact, &_param5Fact, &_param6Fact, &_param7Fact };
        FactMetaData*   rgParamMetaData[7] =    { &_param1MetaData, &_param2MetaData, &_param3MetaData, &_param4MetaData, &_param5MetaData, &_param6MetaData, &_param7MetaData };

675 676 677 678 679 680
        MAV_CMD command;
        if (_homePositionSpecialCase) {
            command = MAV_CMD_NAV_LAST;
        } else {
            command = (MAV_CMD)this->command();
        }
681 682

        for (int i=1; i<=7; i++) {
683
            const QMap<int, MavCmdParamInfo*>& paramInfoMap = _missionCommands->getMavCmdInfo(command, _vehicle)->paramInfoMap();
Don Gagne's avatar
Don Gagne committed
684 685 686 687 688 689 690 691 692

            if (paramInfoMap.contains(i) && paramInfoMap[i]->enumStrings().count() != 0) {
                Fact*               paramFact =     rgParamFacts[i-1];
                FactMetaData*       paramMetaData = rgParamMetaData[i-1];
                MavCmdParamInfo*    paramInfo =     paramInfoMap[i];

                paramFact->_setName(paramInfo->label());
                paramMetaData->setDecimalPlaces(paramInfo->decimalPlaces());
                paramMetaData->setEnumInfo(paramInfo->enumStrings(), paramInfo->enumValues());
693
                paramMetaData->setRawUnits(paramInfo->units());
694 695 696 697
                paramFact->setMetaData(paramMetaData);
                model->append(paramFact);
            }
        }
698 699 700 701 702 703 704
    }

    return model;
}

QGeoCoordinate MissionItem::coordinate(void) const
{
Don Gagne's avatar
Don Gagne committed
705
    return QGeoCoordinate(_param5Fact.rawValue().toDouble(), _param6Fact.rawValue().toDouble(), _param7Fact.rawValue().toDouble());
706 707 708 709 710 711 712 713 714 715 716
}

void MissionItem::setCoordinate(const QGeoCoordinate& coordinate)
{
    setParam5(coordinate.latitude());
    setParam6(coordinate.longitude());
    setParam7(coordinate.altitude());
}

bool MissionItem::friendlyEditAllowed(void) const
{
717
    if (_missionCommands->contains((MAV_CMD)command()) && _missionCommands->getMavCmdInfo((MAV_CMD)command(), _vehicle)->friendlyEdit()) {
718 719 720 721 722 723 724
        if (!autoContinue()) {
            return false;
        }

        if (specifiesCoordinate()) {
            return frame() == MAV_FRAME_GLOBAL || frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT;
        }
725 726

        return true;
727 728 729 730 731 732 733 734 735 736 737 738
    }

    return false;
}

bool MissionItem::rawEdit(void) const
{
    return _rawEdit || !friendlyEditAllowed();
}

void MissionItem::setRawEdit(bool rawEdit)
{
739
    if (this->rawEdit() != rawEdit) {
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
        _rawEdit = rawEdit;
        emit rawEditChanged(this->rawEdit());
    }
}

void MissionItem::setDirty(bool dirty)
{
    if (!_homePositionSpecialCase || !dirty) {
        // Home position never affects dirty bit

        _dirty = dirty;
        // We want to emit dirtyChanged even if _dirty didn't change. This can be handy signal for
        // any value within the item changing.
        emit dirtyChanged(_dirty);
    }
}

void MissionItem::_setDirtyFromSignal(void)
{
    setDirty(true);
}

void MissionItem::setDistance(double distance)
{
    _distance = distance;
    emit distanceChanged(_distance);
}

Don Gagne's avatar
Don Gagne committed
768 769 770 771 772 773
void MissionItem::setAltDifference(double altDifference)
{
    _altDifference = altDifference;
    emit altDifferenceChanged(_altDifference);
}

774 775 776 777 778 779
void MissionItem::setAltPercent(double altPercent)
{
    _altPercent = altPercent;
    emit altPercentChanged(_altPercent);
}

780 781 782 783 784 785
void MissionItem::setAzimuth(double azimuth)
{
    _azimuth = azimuth;
    emit azimuthChanged(_azimuth);
}

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
void MissionItem::_sendCoordinateChanged(void)
{
    emit coordinateChanged(coordinate());
}

void MissionItem::_syncAltitudeRelativeToHomeToFrame(const QVariant& value)
{
    if (!_syncingAltitudeRelativeToHomeAndFrame) {
        _syncingAltitudeRelativeToHomeAndFrame = true;
        setFrame(value.toBool() ? MAV_FRAME_GLOBAL_RELATIVE_ALT : MAV_FRAME_GLOBAL);
        _syncingAltitudeRelativeToHomeAndFrame = false;
    }
}

void MissionItem::_syncFrameToAltitudeRelativeToHome(void)
{
    if (!_syncingAltitudeRelativeToHomeAndFrame) {
        _syncingAltitudeRelativeToHomeAndFrame = true;
Don Gagne's avatar
Don Gagne committed
804
        _altitudeRelativeToHomeFact.setRawValue(relativeAltitude());
805 806 807 808 809 810
        _syncingAltitudeRelativeToHomeAndFrame = false;
    }
}

void MissionItem::setDefaultsForCommand(void)
{
811 812 813
    // We set these global defaults first, then if there are param defaults they will get reset
    setParam7(defaultAltitude);

814 815
    MAV_CMD command = (MAV_CMD)this->command();
    if (_missionCommands->contains(command)) {
816 817
        MavCmdInfo* mavCmdInfo = _missionCommands->getMavCmdInfo(command, _vehicle);
        foreach (const MavCmdParamInfo* paramInfo, mavCmdInfo->paramInfoMap()) {
818
            Fact* rgParamFacts[7] = { &_param1Fact, &_param2Fact, &_param3Fact, &_param4Fact, &_param5Fact, &_param6Fact, &_param7Fact };
819

820 821
            rgParamFacts[paramInfo->param()-1]->setRawValue(paramInfo->defaultValue());
        }
822 823
    }

Don Gagne's avatar
Don Gagne committed
824
    setAutoContinue(true);
825
    setFrame(specifiesCoordinate() ? MAV_FRAME_GLOBAL_RELATIVE_ALT : MAV_FRAME_MISSION);
Don Gagne's avatar
Don Gagne committed
826
    setRawEdit(false);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
}

void MissionItem::_sendUiModelChanged(void)
{
    emit uiModelChanged();
}

void MissionItem::_sendFrameChanged(void)
{
    emit frameChanged(frame());
}

void MissionItem::_sendCommandChanged(void)
{
    emit commandChanged(command());
}

QString MissionItem::commandName(void) const
{
846 847 848
    MAV_CMD command = (MAV_CMD)this->command();
    if (_missionCommands->contains(command)) {
        const MavCmdInfo* mavCmdInfo = _missionCommands->getMavCmdInfo(command, _vehicle);
849 850
        return mavCmdInfo->friendlyName().isEmpty() ? mavCmdInfo->rawName() : mavCmdInfo->friendlyName();
    } else {
851
        return QString("Unknown: %1").arg(command);
852
    }
853 854 855 856 857 858
}

void MissionItem::_sendFriendlyEditAllowedChanged(void)
{
    emit friendlyEditAllowedChanged(friendlyEditAllowed());
}
Don Gagne's avatar
Don Gagne committed
859 860 861 862 863

QString MissionItem::category(void) const
{
    return qgcApp()->toolbox()->missionCommands()->categoryFromCommand(command());
}
864 865 866 867 868 869 870 871

void MissionItem::setShowHomePosition(bool showHomePosition)
{
    if (showHomePosition != _showHomePosition) {
        _showHomePosition = showHomePosition;
        emit showHomePositionChanged(_showHomePosition);
    }
}