MissionItem.cc 30 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
/*===================================================================
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"

Don Gagne's avatar
Don Gagne committed
28
QGC_LOGGING_CATEGORY(MissionItemLog, "MissionItemLog")
29

30 31 32 33 34 35 36
const double MissionItem::defaultPitch =                15.0;
const double MissionItem::defaultHeading =              0.0;
const double MissionItem::defaultAltitude =             25.0;
const double MissionItem::defaultAcceptanceRadius =     3.0;
const double MissionItem::defaultLoiterOrbitRadius =    10.0;
const double MissionItem::defaultLoiterTurns =          1.0;

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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;
}

Don Gagne's avatar
Don Gagne committed
53 54 55 56
const MissionItem::MavCmd2Name_t  MissionItem::_rgMavCmd2Name[_cMavCmd2Name] = {
    { MAV_CMD_NAV_WAYPOINT,         "Waypoint" },
    { MAV_CMD_NAV_LOITER_UNLIM,     "Loiter" },
    { MAV_CMD_NAV_LOITER_TURNS,     "Loiter (turns)" },
Don Gagne's avatar
Don Gagne committed
57
    { MAV_CMD_NAV_LOITER_TIME,      "Loiter (seconds)" },
Don Gagne's avatar
Don Gagne committed
58 59 60 61 62 63 64 65 66 67
    { MAV_CMD_NAV_RETURN_TO_LAUNCH, "Return Home" },
    { MAV_CMD_NAV_LAND,             "Land" },
    { MAV_CMD_NAV_TAKEOFF,          "Takeoff" },
    { MAV_CMD_CONDITION_DELAY,      "Delay" },
    { MAV_CMD_DO_JUMP,              "Jump To Command" },
};

MissionItem::MissionItem(QObject*       parent,
                         int            sequenceNumber,
                         QGeoCoordinate coordinate,
Don Gagne's avatar
Don Gagne committed
68
                         int            command,
Don Gagne's avatar
Don Gagne committed
69 70 71 72 73 74
                         double         param1,
                         double         param2,
                         double         param3,
                         double         param4,
                         bool           autocontinue,
                         bool           isCurrentItem,
Don Gagne's avatar
Don Gagne committed
75
                         int            frame)
76
    : QObject(parent)
Don Gagne's avatar
Don Gagne committed
77
    , _sequenceNumber(sequenceNumber)
Don Gagne's avatar
Don Gagne committed
78
    , _frame(-1)    // Forces set of _altitudeRelativeToHomeFact
Don Gagne's avatar
Don Gagne committed
79
    , _command((MavlinkQmlSingleton::Qml_MAV_CMD)command)
80
    , _autocontinue(autocontinue)
Don Gagne's avatar
Don Gagne committed
81
    , _isCurrentItem(isCurrentItem)
82
    , _reachedTime(0)
83
    , _distance(0.0)
84
    , _headingDegreesFact(NULL)
85
    , _dirty(false)
Don Gagne's avatar
Don Gagne committed
86
    , _homePositionSpecialCase(false)
87
    , _homePositionValid(false)
88
{
89 90 91
    _latitudeFact                   = new Fact(0, "Latitude:",                      FactMetaData::valueTypeDouble, this);
    _longitudeFact                  = new Fact(0, "Longitude:",                     FactMetaData::valueTypeDouble, this);
    _altitudeFact                   = new Fact(0, "Altitude:",                      FactMetaData::valueTypeDouble, this);
92
    _headingDegreesFact             = new Fact(0, "Heading:",                       FactMetaData::valueTypeDouble, this);
93 94 95 96
    _loiterOrbitRadiusFact          = new Fact(0, "Radius:",                        FactMetaData::valueTypeDouble, this);
    _param1Fact                     = new Fact(0, QString(),                        FactMetaData::valueTypeDouble, this);
    _param2Fact                     = new Fact(0, QString(),                        FactMetaData::valueTypeDouble, this);
    _altitudeRelativeToHomeFact     = new Fact(0, "Altitude is relative to home",   FactMetaData::valueTypeDouble, this);
Don Gagne's avatar
Don Gagne committed
97

98
    setFrame(frame);
Don Gagne's avatar
Don Gagne committed
99
    
100
    setCoordinate(coordinate);
Don Gagne's avatar
Don Gagne committed
101 102
    setParam1(param1);
    setParam2(param2);
103
    _setYawRadians(param4);
Don Gagne's avatar
Don Gagne committed
104
    setLoiterOrbitRadius(param3);
Don Gagne's avatar
Don Gagne committed
105 106 107
    
    // FIXME: Need to fill out more meta data
    
108 109
    FactMetaData* latitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _latitudeFact);
    latitudeMetaData->setUnits("deg");
110 111
    latitudeMetaData->setDecimalPlaces(7);

112 113
    FactMetaData* longitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _longitudeFact);
    longitudeMetaData->setUnits("deg");
114 115
    longitudeMetaData->setDecimalPlaces(7);

116 117 118
    FactMetaData* altitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _altitudeFact);
    altitudeMetaData->setUnits("meters");
    
119 120
    FactMetaData* headingMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _headingDegreesFact);
    headingMetaData->setUnits("deg");
Don Gagne's avatar
Don Gagne committed
121 122
    
    _pitchMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
123
    _pitchMetaData->setUnits("deg");
Don Gagne's avatar
Don Gagne committed
124 125 126 127 128 129 130
    
    _acceptanceRadiusMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
    _acceptanceRadiusMetaData->setUnits("meters");
    
    _holdTimeMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
    _holdTimeMetaData->setUnits("seconds");
    
Don Gagne's avatar
Don Gagne committed
131 132
    FactMetaData* loiterOrbitRadiusMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
    loiterOrbitRadiusMetaData->setUnits("meters");
Don Gagne's avatar
Don Gagne committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    
    _loiterTurnsMetaData = new FactMetaData(FactMetaData::valueTypeInt32, this);
    _loiterTurnsMetaData->setUnits("count");
    
    _loiterSecondsMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
    _loiterSecondsMetaData->setUnits("seconds");
    
    _delaySecondsMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
    _delaySecondsMetaData->setUnits("seconds");
    
    _jumpSequenceMetaData = new FactMetaData(FactMetaData::valueTypeInt32, this);
    _jumpSequenceMetaData->setUnits("#");
    
    _jumpRepeatMetaData = new FactMetaData(FactMetaData::valueTypeInt32, this);
    _jumpRepeatMetaData->setUnits("count");
    
149 150 151
    _latitudeFact->setMetaData(latitudeMetaData);
    _longitudeFact->setMetaData(longitudeMetaData);
    _altitudeFact->setMetaData(altitudeMetaData);
152
    _headingDegreesFact->setMetaData(headingMetaData);
Don Gagne's avatar
Don Gagne committed
153
    _loiterOrbitRadiusFact->setMetaData(loiterOrbitRadiusMetaData);
Don Gagne's avatar
Don Gagne committed
154

155
    _connectSignals();
156 157
}

Don Gagne's avatar
Don Gagne committed
158 159
MissionItem::MissionItem(const MissionItem& other, QObject* parent)
    : QObject(parent)
160
{
161 162 163
    _latitudeFact               = new Fact(this);
    _longitudeFact              = new Fact(this);
    _altitudeFact               = new Fact(this);
164
    _headingDegreesFact         = new Fact(this);
165 166 167 168
    _loiterOrbitRadiusFact      = new Fact(this);
    _param1Fact                 = new Fact(this);
    _param2Fact                 = new Fact(this);
    _altitudeRelativeToHomeFact = new Fact(this);
Don Gagne's avatar
Don Gagne committed
169 170 171 172 173 174 175 176 177 178 179
    
    _pitchMetaData = new FactMetaData(this);
    
    _acceptanceRadiusMetaData = new FactMetaData(this);
    _holdTimeMetaData = new FactMetaData(this);
    _loiterTurnsMetaData = new FactMetaData(this);
    _loiterSecondsMetaData = new FactMetaData(this);
    _delaySecondsMetaData = new FactMetaData(this);
    _jumpSequenceMetaData = new FactMetaData(this);
    _jumpRepeatMetaData = new FactMetaData(this);

180
    _connectSignals();
181

182
    *this = other;
183 184 185 186
}

const MissionItem& MissionItem::operator=(const MissionItem& other)
{
187 188 189 190 191 192
    _sequenceNumber             = other._sequenceNumber;
    _isCurrentItem              = other._isCurrentItem;
    _frame                      = other._frame;
    _command                    = other._command;
    _autocontinue               = other._autocontinue;
    _reachedTime                = other._reachedTime;
193
    _distance                   = other._distance;
194
    _altitudeRelativeToHomeFact = other._altitudeRelativeToHomeFact;
Don Gagne's avatar
Don Gagne committed
195
    _dirty                      = other._dirty;
Don Gagne's avatar
Don Gagne committed
196
    _homePositionSpecialCase    = other._homePositionSpecialCase;
197
    _homePositionValid          = other._homePositionValid;
198

199 200 201
    *_latitudeFact              = *other._latitudeFact;
    *_longitudeFact             = *other._longitudeFact;
    *_altitudeFact              = *other._altitudeFact;
202
    *_headingDegreesFact        = *other._headingDegreesFact;
Don Gagne's avatar
Don Gagne committed
203 204 205
    *_loiterOrbitRadiusFact     = *other._loiterOrbitRadiusFact;
    *_param1Fact                = *other._param1Fact;
    *_param2Fact                = *other._param2Fact;
206

Don Gagne's avatar
Don Gagne committed
207 208 209 210 211 212 213 214
    *_pitchMetaData             = *other._pitchMetaData;
    *_acceptanceRadiusMetaData  = *other._acceptanceRadiusMetaData;
    *_holdTimeMetaData          = *other._holdTimeMetaData;
    *_loiterTurnsMetaData       = *other._loiterTurnsMetaData;
    *_loiterSecondsMetaData     = *other._loiterSecondsMetaData;
    *_delaySecondsMetaData      = *other._delaySecondsMetaData;
    *_jumpSequenceMetaData      = *other._jumpSequenceMetaData;
    *_jumpRepeatMetaData        = *other._jumpRepeatMetaData;
215

216 217 218
    return *this;
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
void MissionItem::_connectSignals(void)
{
    // Connect to valueChanged to track dirty state
    connect(_latitudeFact,                  &Fact::valueChanged, this, &MissionItem::_factValueChanged);
    connect(_longitudeFact,                 &Fact::valueChanged, this, &MissionItem::_factValueChanged);
    connect(_altitudeFact,                  &Fact::valueChanged, this, &MissionItem::_factValueChanged);
    connect(_headingDegreesFact,            &Fact::valueChanged, this, &MissionItem::_factValueChanged);
    connect(_loiterOrbitRadiusFact,         &Fact::valueChanged, this, &MissionItem::_factValueChanged);
    connect(_param1Fact,                    &Fact::valueChanged, this, &MissionItem::_factValueChanged);
    connect(_param2Fact,                    &Fact::valueChanged, this, &MissionItem::_factValueChanged);
    connect(_altitudeRelativeToHomeFact,    &Fact::valueChanged, this, &MissionItem::_factValueChanged);

    // Connect valueChanged signals so we can output coordinateChanged signal
    connect(_latitudeFact,  &Fact::valueChanged, this, &MissionItem::_coordinateFactChanged);
    connect(_longitudeFact, &Fact::valueChanged, this, &MissionItem::_coordinateFactChanged);
    connect(_altitudeFact,  &Fact::valueChanged, this, &MissionItem::_coordinateFactChanged);

Don Gagne's avatar
Don Gagne committed
236 237
    connect(_headingDegreesFact,            &Fact::valueChanged, this, &MissionItem::_headingDegreesFactChanged);
    connect(_altitudeRelativeToHomeFact,    &Fact::valueChanged, this, &MissionItem::_altitudeRelativeToHomeFactChanged);
238 239 240 241 242 243
}

MissionItem::~MissionItem()
{    
}

244 245
bool MissionItem::isNavigationType()
{
Don Gagne's avatar
Don Gagne committed
246
    return (_command < MavlinkQmlSingleton::MAV_CMD_NAV_LAST);
247 248 249 250 251
}

void MissionItem::save(QTextStream &saveStream)
{
    QString position("%1\t%2\t%3");
Don Gagne's avatar
Don Gagne committed
252 253 254
    position = position.arg(x(), 0, 'g', 18);
    position = position.arg(y(), 0, 'g', 18);
    position = position.arg(z(), 0, 'g', 18);
255
    QString parameters("%1\t%2\t%3\t%4");
256
    parameters = parameters.arg(param1(), 0, 'g', 18).arg(param2(), 0, 'g', 18).arg(loiterOrbitRadius(), 0, 'g', 18).arg(_yawRadians(), 0, 'g', 18);
257 258
    // FORMAT: <INDEX> <CURRENT WP> <COORD FRAME> <COMMAND> <PARAM1> <PARAM2> <PARAM3> <PARAM4> <PARAM5/X/LONGITUDE> <PARAM6/Y/LATITUDE> <PARAM7/Z/ALTITUDE> <AUTOCONTINUE> <DESCRIPTION>
    // as documented here: http://qgroundcontrol.org/waypoint_protocol
Don Gagne's avatar
Don Gagne committed
259
    saveStream << this->sequenceNumber() << "\t" << this->isCurrentItem() << "\t" << this->frame() << "\t" << this->command() << "\t"  << parameters << "\t" << position  << "\t" << this->autoContinue() << "\r\n"; //"\t" << this->getDescription() << "\r\n";
260 261 262 263 264 265
}

bool MissionItem::load(QTextStream &loadStream)
{
    const QStringList &wpParams = loadStream.readLine().split("\t");
    if (wpParams.size() == 12) {
Don Gagne's avatar
Don Gagne committed
266 267
        setSequenceNumber(wpParams[0].toInt());
        setIsCurrentItem(wpParams[1].toInt() == 1 ? true : false);
268
        setFrame(wpParams[2].toInt());
Don Gagne's avatar
Don Gagne committed
269
        setAction(wpParams[3].toInt());
Don Gagne's avatar
Don Gagne committed
270 271 272
        setParam1(wpParams[4].toDouble());
        setParam2(wpParams[5].toDouble());
        setLoiterOrbitRadius(wpParams[6].toDouble());
273
        _setYawRadians(wpParams[7].toDouble());
Don Gagne's avatar
Don Gagne committed
274 275 276
        setLatitude(wpParams[8].toDouble());
        setLongitude(wpParams[9].toDouble());
        setAltitude(wpParams[10].toDouble());
277 278 279 280 281 282 283
        _autocontinue = (wpParams[11].toInt() == 1 ? true : false);
        return true;
    }
    return false;
}


Don Gagne's avatar
Don Gagne committed
284
void MissionItem::setSequenceNumber(int sequenceNumber)
285
{
Don Gagne's avatar
Don Gagne committed
286 287
    _sequenceNumber = sequenceNumber;
    emit sequenceNumberChanged(_sequenceNumber);
288 289 290 291 292 293
}

void MissionItem::setX(double x)
{
    if (!isinf(x) && !isnan(x) && ((_frame == MAV_FRAME_LOCAL_NED) || (_frame == MAV_FRAME_LOCAL_ENU)))
    {
Don Gagne's avatar
Don Gagne committed
294
        setLatitude(x);
295 296 297 298 299 300 301
    }
}

void MissionItem::setY(double y)
{
    if (!isinf(y) && !isnan(y) && ((_frame == MAV_FRAME_LOCAL_NED) || (_frame == MAV_FRAME_LOCAL_ENU)))
    {
Don Gagne's avatar
Don Gagne committed
302
        setLongitude(y);
303 304 305 306 307 308 309
    }
}

void MissionItem::setZ(double z)
{
    if (!isinf(z) && !isnan(z) && ((_frame == MAV_FRAME_LOCAL_NED) || (_frame == MAV_FRAME_LOCAL_ENU)))
    {
Don Gagne's avatar
Don Gagne committed
310
        setAltitude(z);
311 312 313 314 315
    }
}

void MissionItem::setLatitude(double lat)
{
Don Gagne's avatar
Don Gagne committed
316
    if (_latitudeFact->value().toDouble() != lat)
317
    {
318
        _latitudeFact->setValue(lat);
Don Gagne's avatar
Don Gagne committed
319
        emit coordinateChanged(coordinate());
320 321 322 323 324
    }
}

void MissionItem::setLongitude(double lon)
{
Don Gagne's avatar
Don Gagne committed
325
    if (_longitudeFact->value().toDouble() != lon)
326
    {
327
        _longitudeFact->setValue(lon);
Don Gagne's avatar
Don Gagne committed
328
        emit coordinateChanged(coordinate());
329 330 331 332 333
    }
}

void MissionItem::setAltitude(double altitude)
{
Don Gagne's avatar
Don Gagne committed
334
    if (_altitudeFact->value().toDouble() != altitude)
335
    {
336
        _altitudeFact->setValue(altitude);
337
        emit valueStringsChanged(valueStrings());
Don Gagne's avatar
Don Gagne committed
338
        emit coordinateChanged(coordinate());
339 340 341 342 343
    }
}

void MissionItem::setAction(int /*MAV_CMD*/ action)
{
Don Gagne's avatar
Don Gagne committed
344 345
    if (_command != action) {
        _command = (MavlinkQmlSingleton::Qml_MAV_CMD)action;
346

Don Gagne's avatar
Don Gagne committed
347
        // Fix defaults according to WP type
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
        switch (_command) {
            case MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF:
                setParam1(defaultPitch);
                break;
            case MavlinkQmlSingleton::MAV_CMD_NAV_WAYPOINT:
                setAcceptanceRadius(defaultAcceptanceRadius);
                break;
            case MavlinkQmlSingleton::MAV_CMD_NAV_LOITER_UNLIM:
            case MavlinkQmlSingleton::MAV_CMD_NAV_LOITER_TIME:
                setLoiterOrbitRadius(defaultLoiterOrbitRadius);
                break;
            case MavlinkQmlSingleton::MAV_CMD_NAV_LOITER_TURNS:
                setLoiterOrbitRadius(defaultLoiterOrbitRadius);
                setParam1(defaultLoiterTurns);
                break;
            default:
                break;
366
        }
367 368 369
        setHeadingDegrees(defaultHeading);
        setAltitude(defaultAltitude);

Don Gagne's avatar
Don Gagne committed
370 371 372 373 374 375 376
        if (specifiesCoordinate()) {
            if (_frame != MAV_FRAME_GLOBAL && _frame != MAV_FRAME_GLOBAL_RELATIVE_ALT) {
                setFrame(MAV_FRAME_GLOBAL_RELATIVE_ALT);
            }
        } else {
            setFrame(MAV_FRAME_MISSION);
        }
377 378

        emit commandNameChanged(commandName());
Don Gagne's avatar
Don Gagne committed
379
        emit commandChanged((MavlinkQmlSingleton::Qml_MAV_CMD)_command);
380 381 382 383 384
        emit valueLabelsChanged(valueLabels());
        emit valueStringsChanged(valueStrings());
    }
}

385 386 387 388 389 390 391 392 393
int MissionItem::frame(void) const
{
    if (_altitudeRelativeToHomeFact->value().toBool()) {
        return MAV_FRAME_GLOBAL_RELATIVE_ALT;
    } else {
        return _frame;
    }
}

394 395 396
void MissionItem::setFrame(int /*MAV_FRAME*/ frame)
{
    if (_frame != frame) {
Don Gagne's avatar
Don Gagne committed
397
        _altitudeRelativeToHomeFact->setValue(frame == MAV_FRAME_GLOBAL_RELATIVE_ALT);
398
        _frame = frame;
Don Gagne's avatar
Don Gagne committed
399
        emit frameChanged(_frame);
400 401 402 403 404 405 406
    }
}

void MissionItem::setAutocontinue(bool autoContinue)
{
    if (_autocontinue != autoContinue) {
        _autocontinue = autoContinue;
Don Gagne's avatar
Don Gagne committed
407
        emit autoContinueChanged(_autocontinue);
408 409 410
    }
}

Don Gagne's avatar
Don Gagne committed
411
void MissionItem::setIsCurrentItem(bool isCurrentItem)
412
{
Don Gagne's avatar
Don Gagne committed
413 414 415
    if (_isCurrentItem != isCurrentItem) {
        _isCurrentItem = isCurrentItem;
        emit isCurrentItemChanged(isCurrentItem);
416 417 418 419 420
    }
}

void MissionItem::setAcceptanceRadius(double radius)
{
Don Gagne's avatar
Don Gagne committed
421
    setParam2(radius);
422 423
}

Don Gagne's avatar
Don Gagne committed
424
void MissionItem::setParam1(double param)
425
{
Don Gagne's avatar
Don Gagne committed
426
    if (param1() != param)
427
    {
Don Gagne's avatar
Don Gagne committed
428
        _param1Fact->setValue(param);
429 430 431 432
        emit valueStringsChanged(valueStrings());
    }
}

Don Gagne's avatar
Don Gagne committed
433
void MissionItem::setParam2(double param)
434
{
Don Gagne's avatar
Don Gagne committed
435
    if (param2() != param)
436
    {
Don Gagne's avatar
Don Gagne committed
437
        _param2Fact->setValue(param);
438 439 440 441 442 443
        emit valueStringsChanged(valueStrings());
    }
}

void MissionItem::setParam3(double param3)
{
Don Gagne's avatar
Don Gagne committed
444
    setLoiterOrbitRadius(param3);
445 446 447 448
}

void MissionItem::setParam4(double param4)
{
449
    _setYawRadians(param4);
450 451 452 453
}

void MissionItem::setParam5(double param5)
{
454
    setLatitude(param5);
455 456 457 458
}

void MissionItem::setParam6(double param6)
{
459
    setLongitude(param6);
460 461 462 463
}

void MissionItem::setParam7(double param7)
{
464
    setAltitude(param7);
465 466
}

Don Gagne's avatar
Don Gagne committed
467
void MissionItem::setLoiterOrbitRadius(double radius)
468
{
Don Gagne's avatar
Don Gagne committed
469 470
    if (loiterOrbitRadius() != radius) {
        _loiterOrbitRadiusFact->setValue(radius);
471 472 473 474 475 476
        emit valueStringsChanged(valueStrings());
    }
}

void MissionItem::setHoldTime(int holdTime)
{
Don Gagne's avatar
Don Gagne committed
477
    setParam1(holdTime);
478 479 480 481
}

void MissionItem::setHoldTime(double holdTime)
{
Don Gagne's avatar
Don Gagne committed
482
    setParam1(holdTime);
483 484
}

Don Gagne's avatar
Don Gagne committed
485
bool MissionItem::specifiesCoordinate(void) const
486
{
Don Gagne's avatar
Don Gagne committed
487
    switch (_command) {
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
        case MAV_CMD_NAV_WAYPOINT:
        case MAV_CMD_NAV_LOITER_UNLIM:
        case MAV_CMD_NAV_LOITER_TURNS:
        case MAV_CMD_NAV_LOITER_TIME:
        case MAV_CMD_NAV_LAND:
        case MAV_CMD_NAV_TAKEOFF:
            return true;
        default:
            return false;
    }
}

QString MissionItem::commandName(void)
{
    QString type;
    
Don Gagne's avatar
Don Gagne committed
504
    switch (_command) {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
        case MAV_CMD_NAV_WAYPOINT:
            type = "Waypoint";
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
        case MAV_CMD_NAV_LOITER_TURNS:
        case MAV_CMD_NAV_LOITER_TIME:
            type = "Loiter";
            break;
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
            type = "Return Home";
            break;
        case MAV_CMD_NAV_LAND:
            type = "Land";
            break;
        case MAV_CMD_NAV_TAKEOFF:
            type = "Takeoff";
            break;
        case MAV_CMD_CONDITION_DELAY:
            type = "Delay";
            break;
        case MAV_CMD_DO_JUMP:
            type = "Jump To Command";
            break;
        default:
Don Gagne's avatar
Don Gagne committed
529
            type = QString("Unknown (%1)").arg(_command);
530 531 532 533 534 535
            break;
    }
    
    return type;
}

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
QString MissionItem::commandDescription(void)
{
    QString description;

    switch (_command) {
        case MAV_CMD_NAV_WAYPOINT:
            description = "Travel to a position in 3D space.";
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
            description = "Travel to a position and Loiter around the specified radius indefinitely.";
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
            description = "Travel to a position and Loiter around the specified radius for a number of turns.";
            break;
        case MAV_CMD_NAV_LOITER_TIME:
            description = "Travel to a position and Loiter around the specified radius for an amount of time.";
            break;
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
Don Gagne's avatar
Don Gagne committed
554
            description = "Send the vehicle back to the home position.";
555 556
            break;
        case MAV_CMD_NAV_LAND:
Don Gagne's avatar
Don Gagne committed
557
            description = "Land vehicle at the specified location.";
558 559
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
560
            description = "Take off from the ground and travel towards the specified position.";
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
            break;
        case MAV_CMD_CONDITION_DELAY:
            description = "Delay";
            break;
        case MAV_CMD_DO_JUMP:
            description = "Jump To Command";
            break;
        default:
            description = QString("Unknown (%1)").arg(_command);
            break;
    }

    return description;
}

576 577 578 579
QStringList MissionItem::valueLabels(void)
{
    QStringList labels;
    
Don Gagne's avatar
Don Gagne committed
580
    switch (_command) {
581
        case MAV_CMD_NAV_WAYPOINT:
Don Gagne's avatar
Don Gagne committed
582
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
                labels << "Alt (rel):";
            } else {
                labels << "Alt:";
            }
            labels << "Heading:" << "Radius:" << "Hold:";
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
            labels << "Heading:" << "Radius:";
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
            labels << "Heading:"  << "Radius:"<< "Turns:";
            break;
        case MAV_CMD_NAV_LOITER_TIME:
            labels << "Heading:" << "Radius:" << "Seconds:";
            break;
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
            break;
        case MAV_CMD_NAV_LAND:
Don Gagne's avatar
Don Gagne committed
601
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
602 603 604 605 606 607 608
                labels << "Alt (rel):";
            } else {
                labels << "Alt:";
            }
            labels << "Heading:";
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
609
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
                labels << "Alt (rel):";
            } else {
                labels << "Alt:";
            }
            labels << "Heading:" << "Pitch:";
            break;
        case MAV_CMD_CONDITION_DELAY:
            labels << "Seconds:";
            break;
        case MAV_CMD_DO_JUMP:
            labels << "Jump to:" << "Repeat:";
            break;
        default:
            break;
    }
    
    return labels;
}

QString MissionItem::_oneDecimalString(double value)
{
    return QString("%1").arg(value, 0 /* min field width */, 'f' /* format */, 1 /* precision */);
}

QStringList MissionItem::valueStrings(void)
{
    QStringList list;
    
Don Gagne's avatar
Don Gagne committed
638
    switch (_command) {
639
        case MAV_CMD_NAV_WAYPOINT:
640
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(headingDegrees()) << _oneDecimalString(param2()) << _oneDecimalString(param1());
641 642
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
643
            list << _oneDecimalString(headingDegrees()) << _oneDecimalString(loiterOrbitRadius());
644 645
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
646
            list << _oneDecimalString(headingDegrees()) << _oneDecimalString(loiterOrbitRadius()) << _oneDecimalString(param1());
647 648
            break;
        case MAV_CMD_NAV_LOITER_TIME:
649
            list << _oneDecimalString(headingDegrees()) << _oneDecimalString(loiterOrbitRadius()) << _oneDecimalString(param1());
650 651 652 653
            break;
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
            break;
        case MAV_CMD_NAV_LAND:
654
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(headingDegrees());
655 656
            break;
        case MAV_CMD_NAV_TAKEOFF:
657
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(headingDegrees()) << _oneDecimalString(param1());
658 659
            break;
        case MAV_CMD_CONDITION_DELAY:
Don Gagne's avatar
Don Gagne committed
660
            list << _oneDecimalString(param1());
661 662
            break;
        case MAV_CMD_DO_JUMP:
Don Gagne's avatar
Don Gagne committed
663
            list << _oneDecimalString(param1()) << _oneDecimalString(param2());
664 665 666 667 668 669
            break;
        default:
            break;
    }
    
    return list;
Don Gagne's avatar
Don Gagne committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
}

QStringList MissionItem::commandNames(void) {
    QStringList list;
    
    for (int i=0; i<_cMavCmd2Name; i++) {
        list += _rgMavCmd2Name[i].name;
    }
    
    return list;
}

int MissionItem::commandByIndex(void)
{
    for (int i=0; i<_cMavCmd2Name; i++) {
Don Gagne's avatar
Don Gagne committed
685
        if (_rgMavCmd2Name[i].command == (MAV_CMD)_command) {
Don Gagne's avatar
Don Gagne committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
            return i;
        }
    }
    
    return -1;
}

void MissionItem::setCommandByIndex(int index)
{
    if (index < 0 || index >= _cMavCmd2Name) {
        qWarning() << "Invalid index" << index;
        return;
    }
    
    setCommand((MavlinkQmlSingleton::Qml_MAV_CMD)_rgMavCmd2Name[index].command);
}
Don Gagne's avatar
Don Gagne committed
702

703
QmlObjectListModel* MissionItem::textFieldFacts(void)
Don Gagne's avatar
Don Gagne committed
704 705 706
{
    QmlObjectListModel* model = new QmlObjectListModel(this);
    
Don Gagne's avatar
Don Gagne committed
707
    switch ((MAV_CMD)_command) {
Don Gagne's avatar
Don Gagne committed
708 709 710
        case MAV_CMD_NAV_WAYPOINT:
            _param1Fact->_setName("Hold:");
            _param1Fact->setMetaData(_holdTimeMetaData);
711
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
712 713 714
            if (!_homePositionSpecialCase) {
                model->append(_param1Fact);
            }
Don Gagne's avatar
Don Gagne committed
715 716
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
717
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
718
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
719 720 721 722
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
            _param1Fact->_setName("Turns:");
            _param1Fact->setMetaData(_loiterTurnsMetaData);
723
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
724
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
725 726 727 728 729
            model->append(_param1Fact);
            break;
        case MAV_CMD_NAV_LOITER_TIME:
            _param1Fact->_setName("Seconds:");
            _param1Fact->setMetaData(_loiterSecondsMetaData);
730
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
731
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
732 733 734
            model->append(_param1Fact);
            break;
        case MAV_CMD_NAV_LAND:
735
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
736 737
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
738 739
            _param1Fact->_setName("Pitch:");
            _param1Fact->setMetaData(_pitchMetaData);
740
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
741
            model->append(_param1Fact);
Don Gagne's avatar
Don Gagne committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755
            break;
        case MAV_CMD_CONDITION_DELAY:
            _param1Fact->_setName("Seconds:");
            _param1Fact->setMetaData(_delaySecondsMetaData);
            model->append(_param1Fact);
            break;
        case MAV_CMD_DO_JUMP:
            _param1Fact->_setName("Seq #:");
            _param1Fact->setMetaData(_jumpSequenceMetaData);
            _param2Fact->_setName("Repeat:");
            _param2Fact->setMetaData(_jumpRepeatMetaData);
            model->append(_param1Fact);
            model->append(_param2Fact);
            break;
Don Gagne's avatar
Don Gagne committed
756 757
        default:
            break;
Don Gagne's avatar
Don Gagne committed
758
    }
759 760 761 762 763

    if (specifiesHeading()) {
        model->append(_headingDegreesFact);
    }

Don Gagne's avatar
Don Gagne committed
764 765 766 767
    
    return model;
}

768
QmlObjectListModel* MissionItem::checkboxFacts(void)
Don Gagne's avatar
Don Gagne committed
769
{
770 771
    QmlObjectListModel* model = new QmlObjectListModel(this);
    
Don Gagne's avatar
Don Gagne committed
772 773
    switch ((MAV_CMD)_command) {
        case MAV_CMD_NAV_WAYPOINT:
Don Gagne's avatar
Don Gagne committed
774 775 776
            if (!_homePositionSpecialCase) {
                model->append(_altitudeRelativeToHomeFact);
            }
777
            break;
Don Gagne's avatar
Don Gagne committed
778
        case MAV_CMD_NAV_LOITER_UNLIM:
779 780
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
781
        case MAV_CMD_NAV_LOITER_TURNS:
782 783
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
784
        case MAV_CMD_NAV_LOITER_TIME:
785 786
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
787
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
788
            break;
Don Gagne's avatar
Don Gagne committed
789
        case MAV_CMD_NAV_LAND:
790 791
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
792
        case MAV_CMD_NAV_TAKEOFF:
793 794
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
795
        default:
796
            break;
Don Gagne's avatar
Don Gagne committed
797
    }
798 799
    
    return model;
Don Gagne's avatar
Don Gagne committed
800 801
}

802
double MissionItem::headingDegrees(void) const
Don Gagne's avatar
Don Gagne committed
803
{
804
    return _headingDegreesFact->value().toDouble();
Don Gagne's avatar
Don Gagne committed
805 806
}

807
void MissionItem::setHeadingDegrees(double headingDegrees)
Don Gagne's avatar
Don Gagne committed
808
{
809 810
    if (_headingDegreesFact->value().toDouble() != headingDegrees) {
        _headingDegreesFact->setValue(headingDegrees);
Don Gagne's avatar
Don Gagne committed
811
        emit valueStringsChanged(valueStrings());
812
        emit headingDegreesChanged(headingDegrees);
Don Gagne's avatar
Don Gagne committed
813 814 815 816
    }
}


817
double MissionItem::_yawRadians(void) const
Don Gagne's avatar
Don Gagne committed
818
{
819
    return _headingDegreesFact->value().toDouble() * (M_PI / 180.0);
Don Gagne's avatar
Don Gagne committed
820 821
}

822
void MissionItem::_setYawRadians(double yawRadians)
Don Gagne's avatar
Don Gagne committed
823
{
824
    setHeadingDegrees(yawRadians * (180 / M_PI));
Don Gagne's avatar
Don Gagne committed
825 826
}

827 828 829 830 831 832 833 834 835 836 837
QGeoCoordinate MissionItem::coordinate(void) const
{
    return QGeoCoordinate(latitude(), longitude(), altitude());
}

void MissionItem::setCoordinate(const QGeoCoordinate& coordinate)
{
    setLatitude(coordinate.latitude());
    setLongitude(coordinate.longitude());
    setAltitude(coordinate.altitude());
}
Don Gagne's avatar
Don Gagne committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866

bool MissionItem::canEdit(void)
{
    bool found = false;
    
    for (int i=0; i<_cMavCmd2Name; i++) {
        if (_rgMavCmd2Name[i].command == (MAV_CMD)_command) {
            found = true;
            break;
        }
    }
    
    if (found) {
        if (!_autocontinue) {
            qCDebug(MissionItemLog) << "canEdit false due to _autocontinue != true";
            return false;
        }
        
        if (_frame != MAV_FRAME_GLOBAL && _frame != MAV_FRAME_GLOBAL_RELATIVE_ALT && _frame != MAV_FRAME_MISSION) {
            qCDebug(MissionItemLog) << "canEdit false due unsupported frame type:" << _frame;
            return false;
        }
        
        return true;
    } else {
        qCDebug(MissionItemLog) << "canEdit false due unsupported command:" << _command;
        return false;
    }
}
Don Gagne's avatar
Don Gagne committed
867 868 869

void MissionItem::setDirty(bool dirty)
{
Don Gagne's avatar
Don Gagne committed
870 871 872 873 874 875 876 877
    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);
    }
Don Gagne's avatar
Don Gagne committed
878 879 880 881 882 883 884
}

void MissionItem::_factValueChanged(QVariant value)
{
    Q_UNUSED(value);
    setDirty(true);
}
885 886 887 888 889 890

void MissionItem::_coordinateFactChanged(QVariant value)
{
    Q_UNUSED(value);
    emit coordinateChanged(coordinate());
}
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

bool MissionItem::specifiesHeading(void) const
{
    switch ((MAV_CMD)_command) {
        case MAV_CMD_NAV_LAND:
        case MAV_CMD_NAV_TAKEOFF:
            return true;
        default:
            return false;
    }
}

void MissionItem::_headingDegreesFactChanged(QVariant value)
{
    emit headingDegreesChanged(value.toDouble());
}
907

Don Gagne's avatar
Don Gagne committed
908 909 910 911 912 913 914 915 916 917 918
void MissionItem::_altitudeRelativeToHomeFactChanged(QVariant value)
{
    // Don't call setFrame, that will cause a signalling loop

    int frame = value.toBool() ? MAV_FRAME_GLOBAL_RELATIVE_ALT : MAV_FRAME_GLOBAL;
    if (_frame != frame) {
        _frame = frame;
        emit frameChanged(_frame);
    }
}

919 920 921 922 923
void MissionItem::setHomePositionValid(bool homePositionValid)
{
    _homePositionValid = homePositionValid;
    emit homePositionValidChanged(_homePositionValid);
}
924 925 926 927 928 929

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