MissionItem.cc 17.2 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10 11 12 13 14 15 16


#include <QStringList>
#include <QDebug>

#include "MissionItem.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
17
#include "JsonHelper.h"
Don Gagne's avatar
Don Gagne committed
18
#include "VisualMissionItem.h"
19

20 21
const char*  MissionItem::_jsonFrameKey =           "frame";
const char*  MissionItem::_jsonCommandKey =         "command";
Don Gagne's avatar
Don Gagne committed
22 23 24 25 26 27
const char*  MissionItem::_jsonAutoContinueKey =    "autoContinue";
const char*  MissionItem::_jsonCoordinateKey =      "coordinate";
const char*  MissionItem::_jsonParamsKey =          "params";
const char*  MissionItem::_jsonDoJumpIdKey =        "doJumpId";

// Deprecated V1 format keys
28 29 30 31 32
const char*  MissionItem::_jsonParam1Key =          "param1";
const char*  MissionItem::_jsonParam2Key =          "param2";
const char*  MissionItem::_jsonParam3Key =          "param3";
const char*  MissionItem::_jsonParam4Key =          "param4";

33
MissionItem::MissionItem(QObject* parent)
34 35
    : QObject(parent)
    , _sequenceNumber(0)
Don Gagne's avatar
Don Gagne committed
36
    , _doJumpId(-1)
37 38
    , _isCurrentItem(false)
    , _autoContinueFact             (0, "AutoContinue",                 FactMetaData::valueTypeUint32)
39 40
    , _commandFact                  (0, "",                             FactMetaData::valueTypeUint32)
    , _frameFact                    (0, "",                             FactMetaData::valueTypeUint32)
41 42 43 44 45 46 47 48
    , _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)
{
Don Gagne's avatar
Don Gagne committed
49 50 51
    // 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);
52 53

    setAutoContinue(true);
54

55
    connect(&_param1Fact, &Fact::rawValueChanged, this, &MissionItem::_param1Changed);
56
    connect(&_param2Fact, &Fact::rawValueChanged, this, &MissionItem::_param2Changed);
DonLakeFlyer's avatar
DonLakeFlyer committed
57
    connect(&_param3Fact, &Fact::rawValueChanged, this, &MissionItem::_param3Changed);
58 59
}

60
MissionItem::MissionItem(int             sequenceNumber,
61 62 63 64 65 66 67 68 69 70 71 72 73 74
                         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)
    , _sequenceNumber(sequenceNumber)
Don Gagne's avatar
Don Gagne committed
75
    , _doJumpId(-1)
76
    , _isCurrentItem(isCurrentItem)
Don Gagne's avatar
Don Gagne committed
77 78
    , _commandFact                  (0, "",                             FactMetaData::valueTypeUint32)
    , _frameFact                    (0, "",                             FactMetaData::valueTypeUint32)
79 80 81 82 83 84 85 86
    , _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)
{
Don Gagne's avatar
Don Gagne committed
87 88 89
    // 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);
90 91 92 93 94 95 96 97 98

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

    _param1Fact.setRawValue(param1);
    _param2Fact.setRawValue(param2);
    _param3Fact.setRawValue(param3);
    _param4Fact.setRawValue(param4);
Don Gagne's avatar
Don Gagne committed
99 100 101
    _param5Fact.setRawValue(param5);
    _param6Fact.setRawValue(param6);
    _param7Fact.setRawValue(param7);
102 103

    connect(&_param2Fact, &Fact::rawValueChanged, this, &MissionItem::_param2Changed);
DonLakeFlyer's avatar
DonLakeFlyer committed
104
    connect(&_param3Fact, &Fact::rawValueChanged, this, &MissionItem::_param3Changed);
105 106 107 108
}

MissionItem::MissionItem(const MissionItem& other, QObject* parent)
    : QObject(parent)
109
    , _sequenceNumber(0)
Don Gagne's avatar
Don Gagne committed
110
    , _doJumpId(-1)
111
    , _isCurrentItem(false)
Don Gagne's avatar
Don Gagne committed
112 113
    , _commandFact                  (0, "",                             FactMetaData::valueTypeUint32)
    , _frameFact                    (0, "",                             FactMetaData::valueTypeUint32)
114 115 116 117 118 119 120
    , _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)
121
{
Don Gagne's avatar
Don Gagne committed
122 123 124
    // 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);
125 126

    *this = other;
127 128

    connect(&_param2Fact, &Fact::rawValueChanged, this, &MissionItem::_param2Changed);
DonLakeFlyer's avatar
DonLakeFlyer committed
129
    connect(&_param3Fact, &Fact::rawValueChanged, this, &MissionItem::_param3Changed);
130 131 132 133
}

const MissionItem& MissionItem::operator=(const MissionItem& other)
{
Don Gagne's avatar
Don Gagne committed
134 135
    _doJumpId = other._doJumpId;

136 137 138 139 140 141
    setCommand(other.command());
    setFrame(other.frame());
    setSequenceNumber(other._sequenceNumber);
    setAutoContinue(other.autoContinue());
    setIsCurrentItem(other._isCurrentItem);

Don Gagne's avatar
Don Gagne committed
142 143 144 145 146 147 148
    _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());
149 150 151

    return *this;
}
152

153 154
MissionItem::~MissionItem()
{    
155

156 157
}

158
void MissionItem::save(QJsonObject& json) const
159
{
Don Gagne's avatar
Don Gagne committed
160
    json[VisualMissionItem::jsonTypeKey] = VisualMissionItem::jsonTypeSimpleItemValue;
161 162 163
    json[_jsonFrameKey] = frame();
    json[_jsonCommandKey] = command();
    json[_jsonAutoContinueKey] = autoContinue();
Don Gagne's avatar
Don Gagne committed
164 165
    json[_jsonDoJumpIdKey] = _sequenceNumber;

166
    QJsonArray rgParams =  { param1(), param2(), param3(), param4(), param5(), param6(), param7() };
Don Gagne's avatar
Don Gagne committed
167
    json[_jsonParamsKey] = rgParams;
168 169 170 171 172 173
}

bool MissionItem::load(QTextStream &loadStream)
{
    const QStringList &wpParams = loadStream.readLine().split("\t");
    if (wpParams.size() == 12) {
174
        setCommand((MAV_CMD)wpParams[3].toInt());   // Has to be first since it triggers defaults to be set, which are then override by below set calls
175 176 177 178 179 180 181 182 183 184 185 186 187
        setSequenceNumber(wpParams[0].toInt());
        setIsCurrentItem(wpParams[1].toInt() == 1 ? true : false);
        setFrame((MAV_FRAME)wpParams[2].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;
    }
188

189 190 191
    return false;
}

Don Gagne's avatar
Don Gagne committed
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
bool MissionItem::_convertJsonV1ToV2(const QJsonObject& json, QJsonObject& v2Json, QString& errorString)
{
    // V1 format type = "missionItem", V2 format type = "MissionItem"
    // V1 format has params in separate param[1-n] keys
    // V2 format has params in params array
    v2Json = json;

    if (json.contains(_jsonParamsKey)) {
        // Already V2 format
        return true;
    }        

    QList<JsonHelper::KeyValidateInfo> keyInfoList = {
        { VisualMissionItem::jsonTypeKey,   QJsonValue::String, true },
        { _jsonParam1Key,                   QJsonValue::Double, true },
        { _jsonParam2Key,                   QJsonValue::Double, true },
        { _jsonParam3Key,                   QJsonValue::Double, true },
        { _jsonParam4Key,                   QJsonValue::Double, true },
    };
    if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) {
        return false;
    }

    if (v2Json[VisualMissionItem::jsonTypeKey].toString() == QStringLiteral("missionItem")) {
        v2Json[VisualMissionItem::jsonTypeKey] = VisualMissionItem::jsonTypeSimpleItemValue;
    }

    QJsonArray rgParams =  { json[_jsonParam1Key].toDouble(),  json[_jsonParam2Key].toDouble(), json[_jsonParam3Key].toDouble(), json[_jsonParam4Key].toDouble() };
    v2Json[_jsonParamsKey] = rgParams;
    v2Json.remove(_jsonParam1Key);
    v2Json.remove(_jsonParam2Key);
    v2Json.remove(_jsonParam3Key);
    v2Json.remove(_jsonParam4Key);

    return true;
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
bool MissionItem::_convertJsonV2ToV3(QJsonObject& json, QString& errorString)
{
    // V2 format: param 5/6/7 stored in GeoCoordinate
    // V3 format: param 5/6/7 stored in params array

    if (!json.contains(_jsonCoordinateKey)) {
        // Already V3 format
        return true;
    }

    QList<JsonHelper::KeyValidateInfo> keyInfoList = {
        { _jsonCoordinateKey, QJsonValue::Array, true },
    };
    if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) {
        return false;
    }

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

    QJsonArray rgParam = json[_jsonParamsKey].toArray();
    rgParam.append(coordinate.latitude());
    rgParam.append(coordinate.longitude());
    rgParam.append(coordinate.altitude());
    json[_jsonParamsKey] = rgParam;

    json.remove(_jsonCoordinateKey);

    return true;
}

Don Gagne's avatar
Don Gagne committed
262
bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& errorString)
263
{
264 265 266 267 268
    QJsonObject convertedJson;
    if (!_convertJsonV1ToV2(json, convertedJson, errorString)) {
        return false;
    }
    if (!_convertJsonV2ToV3(convertedJson, errorString)) {
Don Gagne's avatar
Don Gagne committed
269 270 271 272 273 274 275 276 277 278 279
        return false;
    }

    QList<JsonHelper::KeyValidateInfo> keyInfoList = {
        { VisualMissionItem::jsonTypeKey,   QJsonValue::String, true },
        { _jsonFrameKey,                    QJsonValue::Double, true },
        { _jsonCommandKey,                  QJsonValue::Double, true },
        { _jsonParamsKey,                   QJsonValue::Array,  true },
        { _jsonAutoContinueKey,             QJsonValue::Bool,   true },
        { _jsonDoJumpIdKey,                 QJsonValue::Double, false },
    };
280
    if (!JsonHelper::validateKeys(convertedJson, keyInfoList, errorString)) {
Don Gagne's avatar
Don Gagne committed
281 282
        return false;
    }
283

284 285
    if (convertedJson[VisualMissionItem::jsonTypeKey] != VisualMissionItem::jsonTypeSimpleItemValue) {
        errorString = tr("Type found: %1 must be: %2").arg(convertedJson[VisualMissionItem::jsonTypeKey].toString()).arg(VisualMissionItem::jsonTypeSimpleItemValue);
286 287 288
        return false;
    }

289 290 291
    QJsonArray rgParams = convertedJson[_jsonParamsKey].toArray();
    if (rgParams.count() != 7) {
        errorString = tr("%1 key must contains 7 values").arg(_jsonParamsKey);
292 293 294
        return false;
    }

295 296 297 298 299 300 301
    for (int i=0; i<4; i++) {
        if (rgParams[i].type() != QJsonValue::Double && rgParams[i].type() != QJsonValue::Null) {
            errorString = tr("Param %1 incorrect type %2, must be double or null").arg(i+1).arg(rgParams[i].type());
            return false;
        }
    }

302
    // Make sure to set these first since they can signal other changes
303
    setCommand((MAV_CMD)convertedJson[_jsonCommandKey].toInt());
304
    setFrame((MAV_FRAME)convertedJson[_jsonFrameKey].toInt());
305

Don Gagne's avatar
Don Gagne committed
306
    _doJumpId = -1;
307 308
    if (convertedJson.contains(_jsonDoJumpIdKey)) {
        _doJumpId = convertedJson[_jsonDoJumpIdKey].toInt();
Don Gagne's avatar
Don Gagne committed
309
    }
310
    setIsCurrentItem(false);
Don Gagne's avatar
Don Gagne committed
311
    setSequenceNumber(sequenceNumber);
312
    setAutoContinue(convertedJson[_jsonAutoContinueKey].toBool());
Don Gagne's avatar
Don Gagne committed
313

314 315 316 317
    setParam1(JsonHelper::possibleNaNJsonValue(rgParams[0]));
    setParam2(JsonHelper::possibleNaNJsonValue(rgParams[1]));
    setParam3(JsonHelper::possibleNaNJsonValue(rgParams[2]));
    setParam4(JsonHelper::possibleNaNJsonValue(rgParams[3]));
318 319 320
    setParam5(JsonHelper::possibleNaNJsonValue(rgParams[4]));
    setParam6(JsonHelper::possibleNaNJsonValue(rgParams[5]));
    setParam7(JsonHelper::possibleNaNJsonValue(rgParams[6]));
321 322 323 324

    return true;
}

325 326 327

void MissionItem::setSequenceNumber(int sequenceNumber)
{
328 329 330 331
    if (_sequenceNumber != sequenceNumber) {
        _sequenceNumber = sequenceNumber;
        emit sequenceNumberChanged(_sequenceNumber);
    }
332 333 334 335 336
}

void MissionItem::setCommand(MAV_CMD command)
{
    if ((MAV_CMD)this->command() != command) {
Don Gagne's avatar
Don Gagne committed
337
        _commandFact.setRawValue(command);
338 339 340 341 342 343
    }
}

void MissionItem::setFrame(MAV_FRAME frame)
{
    if (this->frame() != frame) {
Don Gagne's avatar
Don Gagne committed
344
        _frameFact.setRawValue(frame);
345 346 347 348 349 350
    }
}

void MissionItem::setAutoContinue(bool autoContinue)
{
    if (this->autoContinue() != autoContinue) {
Don Gagne's avatar
Don Gagne committed
351
        _autoContinueFact.setRawValue(autoContinue);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 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
    }
}

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

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

419
QGeoCoordinate MissionItem::coordinate(void) const
420
{
421
    return QGeoCoordinate(param5(), param6(), param7());
422
}
423

DonLakeFlyer's avatar
DonLakeFlyer committed
424
double MissionItem::specifiedFlightSpeed(void) const
425 426 427 428 429 430 431 432 433 434
{
    double flightSpeed = std::numeric_limits<double>::quiet_NaN();

    if (_commandFact.rawValue().toInt() == MAV_CMD_DO_CHANGE_SPEED && _param2Fact.rawValue().toDouble() > 0) {
        flightSpeed = _param2Fact.rawValue().toDouble();
    }

    return flightSpeed;
}

DonLakeFlyer's avatar
DonLakeFlyer committed
435 436 437 438 439 440 441 442 443 444 445
double MissionItem::specifiedGimbalYaw(void) const
{
    double gimbalYaw = std::numeric_limits<double>::quiet_NaN();

    if (_commandFact.rawValue().toInt() == MAV_CMD_DO_MOUNT_CONTROL && _param7Fact.rawValue().toInt() == MAV_MOUNT_MODE_MAVLINK_TARGETING) {
        gimbalYaw = _param3Fact.rawValue().toDouble();
    }

    return gimbalYaw;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
double MissionItem::specifiedGimbalPitch(void) const
{
    double gimbalPitch = std::numeric_limits<double>::quiet_NaN();

    if (_commandFact.rawValue().toInt() == MAV_CMD_DO_MOUNT_CONTROL && _param7Fact.rawValue().toInt() == MAV_MOUNT_MODE_MAVLINK_TARGETING) {
        gimbalPitch = _param1Fact.rawValue().toDouble();
    }

    return gimbalPitch;
}

void MissionItem::_param1Changed(QVariant value)
{
    Q_UNUSED(value);

    double gimbalPitch = specifiedGimbalPitch();
    if (!qIsNaN(gimbalPitch)) {
        emit specifiedGimbalPitchChanged(gimbalPitch);
    }
}

467 468
void MissionItem::_param2Changed(QVariant value)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    Q_UNUSED(value);

    double flightSpeed = specifiedFlightSpeed();
    if (!qIsNaN(flightSpeed)) {
        emit specifiedFlightSpeedChanged(flightSpeed);
    }
}

void MissionItem::_param3Changed(QVariant value)
{
    Q_UNUSED(value);

    double gimbalYaw = specifiedGimbalYaw();
    if (!qIsNaN(gimbalYaw)) {
        emit specifiedGimbalYawChanged(gimbalYaw);
484 485
    }
}