MissionItem.cc 29.7 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 29 30 31 32 33 34 35 36
/*===================================================================
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/>.

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

/**
 * @file
 *   @brief MissionItem class
 *
 *   @author Benjamin Knecht <mavteam@student.ethz.ch>
 *   @author Petri Tanskanen <mavteam@student.ethz.ch>
 *
 */

#include <QStringList>
#include <QDebug>

#include "MissionItem.h"

Don Gagne's avatar
Don Gagne committed
37
QGC_LOGGING_CATEGORY(MissionItemLog, "MissionItemLog")
38

39 40 41 42 43 44 45
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;

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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
62 63 64 65
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
66
    { MAV_CMD_NAV_LOITER_TIME,      "Loiter (seconds)" },
Don Gagne's avatar
Don Gagne committed
67 68 69 70 71 72 73 74 75 76
    { 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
77
                         int            command,
Don Gagne's avatar
Don Gagne committed
78 79 80 81 82 83
                         double         param1,
                         double         param2,
                         double         param3,
                         double         param4,
                         bool           autocontinue,
                         bool           isCurrentItem,
Don Gagne's avatar
Don Gagne committed
84
                         int            frame)
85
    : QObject(parent)
Don Gagne's avatar
Don Gagne committed
86
    , _sequenceNumber(sequenceNumber)
Don Gagne's avatar
Don Gagne committed
87
    , _frame(-1)    // Forces set of _altitudeRelativeToHomeFact
Don Gagne's avatar
Don Gagne committed
88
    , _command((MavlinkQmlSingleton::Qml_MAV_CMD)command)
89
    , _autocontinue(autocontinue)
Don Gagne's avatar
Don Gagne committed
90
    , _isCurrentItem(isCurrentItem)
91
    , _reachedTime(0)
92
    , _headingDegreesFact(NULL)
93
    , _dirty(false)
Don Gagne's avatar
Don Gagne committed
94
    , _homePositionSpecialCase(false)
95
    , _homePositionValid(false)
96
{
97 98 99
    _latitudeFact                   = new Fact(0, "Latitude:",                      FactMetaData::valueTypeDouble, this);
    _longitudeFact                  = new Fact(0, "Longitude:",                     FactMetaData::valueTypeDouble, this);
    _altitudeFact                   = new Fact(0, "Altitude:",                      FactMetaData::valueTypeDouble, this);
100
    _headingDegreesFact             = new Fact(0, "Heading:",                       FactMetaData::valueTypeDouble, this);
101 102 103 104
    _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
105

106
    setFrame(frame);
Don Gagne's avatar
Don Gagne committed
107
    
108
    setCoordinate(coordinate);
Don Gagne's avatar
Don Gagne committed
109 110
    setParam1(param1);
    setParam2(param2);
111
    _setYawRadians(param4);
Don Gagne's avatar
Don Gagne committed
112
    setLoiterOrbitRadius(param3);
Don Gagne's avatar
Don Gagne committed
113 114 115
    
    // FIXME: Need to fill out more meta data
    
116 117 118 119 120 121 122 123 124
    FactMetaData* latitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _latitudeFact);
    latitudeMetaData->setUnits("deg");
    
    FactMetaData* longitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _longitudeFact);
    longitudeMetaData->setUnits("deg");
    
    FactMetaData* altitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _altitudeFact);
    altitudeMetaData->setUnits("meters");
    
125 126
    FactMetaData* headingMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _headingDegreesFact);
    headingMetaData->setUnits("deg");
Don Gagne's avatar
Don Gagne committed
127 128
    
    _pitchMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
129
    _pitchMetaData->setUnits("deg");
Don Gagne's avatar
Don Gagne committed
130 131 132 133 134 135 136
    
    _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
137 138
    FactMetaData* loiterOrbitRadiusMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
    loiterOrbitRadiusMetaData->setUnits("meters");
Don Gagne's avatar
Don Gagne committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    
    _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");
    
155 156 157
    _latitudeFact->setMetaData(latitudeMetaData);
    _longitudeFact->setMetaData(longitudeMetaData);
    _altitudeFact->setMetaData(altitudeMetaData);
158
    _headingDegreesFact->setMetaData(headingMetaData);
Don Gagne's avatar
Don Gagne committed
159
    _loiterOrbitRadiusFact->setMetaData(loiterOrbitRadiusMetaData);
Don Gagne's avatar
Don Gagne committed
160

161
    _connectSignals();
162 163
}

Don Gagne's avatar
Don Gagne committed
164 165
MissionItem::MissionItem(const MissionItem& other, QObject* parent)
    : QObject(parent)
166
{
167 168 169
    _latitudeFact               = new Fact(this);
    _longitudeFact              = new Fact(this);
    _altitudeFact               = new Fact(this);
170
    _headingDegreesFact         = new Fact(this);
171 172 173 174
    _loiterOrbitRadiusFact      = new Fact(this);
    _param1Fact                 = new Fact(this);
    _param2Fact                 = new Fact(this);
    _altitudeRelativeToHomeFact = new Fact(this);
Don Gagne's avatar
Don Gagne committed
175 176 177 178 179 180 181 182 183 184 185
    
    _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);

186
    _connectSignals();
187

188
    *this = other;
189 190 191 192
}

const MissionItem& MissionItem::operator=(const MissionItem& other)
{
193 194 195 196 197 198 199
    _sequenceNumber             = other._sequenceNumber;
    _isCurrentItem              = other._isCurrentItem;
    _frame                      = other._frame;
    _command                    = other._command;
    _autocontinue               = other._autocontinue;
    _reachedTime                = other._reachedTime;
    _altitudeRelativeToHomeFact = other._altitudeRelativeToHomeFact;
Don Gagne's avatar
Don Gagne committed
200
    _dirty                      = other._dirty;
Don Gagne's avatar
Don Gagne committed
201
    _homePositionSpecialCase    = other._homePositionSpecialCase;
202
    _homePositionValid          = other._homePositionValid;
203

204 205 206
    *_latitudeFact              = *other._latitudeFact;
    *_longitudeFact             = *other._longitudeFact;
    *_altitudeFact              = *other._altitudeFact;
207
    *_headingDegreesFact        = *other._headingDegreesFact;
Don Gagne's avatar
Don Gagne committed
208 209 210
    *_loiterOrbitRadiusFact     = *other._loiterOrbitRadiusFact;
    *_param1Fact                = *other._param1Fact;
    *_param2Fact                = *other._param2Fact;
211

Don Gagne's avatar
Don Gagne committed
212 213 214 215 216 217 218 219
    *_pitchMetaData             = *other._pitchMetaData;
    *_acceptanceRadiusMetaData  = *other._acceptanceRadiusMetaData;
    *_holdTimeMetaData          = *other._holdTimeMetaData;
    *_loiterTurnsMetaData       = *other._loiterTurnsMetaData;
    *_loiterSecondsMetaData     = *other._loiterSecondsMetaData;
    *_delaySecondsMetaData      = *other._delaySecondsMetaData;
    *_jumpSequenceMetaData      = *other._jumpSequenceMetaData;
    *_jumpRepeatMetaData        = *other._jumpRepeatMetaData;
220

221 222 223
    return *this;
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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);

    connect(_headingDegreesFact, &Fact::valueChanged, this, &MissionItem::_headingDegreesFactChanged);
}

MissionItem::~MissionItem()
{    
}

248 249
bool MissionItem::isNavigationType()
{
Don Gagne's avatar
Don Gagne committed
250
    return (_command < MavlinkQmlSingleton::MAV_CMD_NAV_LAST);
251 252 253 254 255
}

void MissionItem::save(QTextStream &saveStream)
{
    QString position("%1\t%2\t%3");
Don Gagne's avatar
Don Gagne committed
256 257 258
    position = position.arg(x(), 0, 'g', 18);
    position = position.arg(y(), 0, 'g', 18);
    position = position.arg(z(), 0, 'g', 18);
259
    QString parameters("%1\t%2\t%3\t%4");
260
    parameters = parameters.arg(param1(), 0, 'g', 18).arg(param2(), 0, 'g', 18).arg(loiterOrbitRadius(), 0, 'g', 18).arg(_yawRadians(), 0, 'g', 18);
261 262
    // 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
263
    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";
264 265 266 267 268 269
}

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


Don Gagne's avatar
Don Gagne committed
288
void MissionItem::setSequenceNumber(int sequenceNumber)
289
{
Don Gagne's avatar
Don Gagne committed
290 291
    _sequenceNumber = sequenceNumber;
    emit sequenceNumberChanged(_sequenceNumber);
292 293 294 295 296 297 298
    emit changed(this);
}

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
299
        setLatitude(x);
300 301 302 303 304 305 306
    }
}

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
307
        setLongitude(y);
308 309 310 311 312 313 314
    }
}

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
315
        setAltitude(z);
316 317 318 319 320
    }
}

void MissionItem::setLatitude(double lat)
{
Don Gagne's avatar
Don Gagne committed
321
    if (_latitudeFact->value().toDouble() != lat)
322
    {
323
        _latitudeFact->setValue(lat);
324
        emit changed(this);
Don Gagne's avatar
Don Gagne committed
325
        emit coordinateChanged(coordinate());
326 327 328 329 330
    }
}

void MissionItem::setLongitude(double lon)
{
Don Gagne's avatar
Don Gagne committed
331
    if (_longitudeFact->value().toDouble() != lon)
332
    {
333
        _longitudeFact->setValue(lon);
334
        emit changed(this);
Don Gagne's avatar
Don Gagne committed
335
        emit coordinateChanged(coordinate());
336 337 338 339 340
    }
}

void MissionItem::setAltitude(double altitude)
{
Don Gagne's avatar
Don Gagne committed
341
    if (_altitudeFact->value().toDouble() != altitude)
342
    {
343
        _altitudeFact->setValue(altitude);
344 345
        emit changed(this);
        emit valueStringsChanged(valueStrings());
Don Gagne's avatar
Don Gagne committed
346
        emit coordinateChanged(coordinate());
347 348 349 350 351
    }
}

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

Don Gagne's avatar
Don Gagne committed
355
        // Fix defaults according to WP type
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        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;
374
        }
375 376 377
        setHeadingDegrees(defaultHeading);
        setAltitude(defaultAltitude);

Don Gagne's avatar
Don Gagne committed
378 379 380 381 382 383 384
        if (specifiesCoordinate()) {
            if (_frame != MAV_FRAME_GLOBAL && _frame != MAV_FRAME_GLOBAL_RELATIVE_ALT) {
                setFrame(MAV_FRAME_GLOBAL_RELATIVE_ALT);
            }
        } else {
            setFrame(MAV_FRAME_MISSION);
        }
385 386 387

        emit changed(this);
        emit commandNameChanged(commandName());
Don Gagne's avatar
Don Gagne committed
388
        emit commandChanged((MavlinkQmlSingleton::Qml_MAV_CMD)_command);
389 390 391 392 393
        emit valueLabelsChanged(valueLabels());
        emit valueStringsChanged(valueStrings());
    }
}

394 395 396 397 398 399 400 401 402
int MissionItem::frame(void) const
{
    if (_altitudeRelativeToHomeFact->value().toBool()) {
        return MAV_FRAME_GLOBAL_RELATIVE_ALT;
    } else {
        return _frame;
    }
}

403 404 405
void MissionItem::setFrame(int /*MAV_FRAME*/ frame)
{
    if (_frame != frame) {
Don Gagne's avatar
Don Gagne committed
406
        _altitudeRelativeToHomeFact->setValue(frame == MAV_FRAME_GLOBAL_RELATIVE_ALT);
407 408 409 410 411 412 413 414 415 416 417 418 419
        _frame = frame;
        emit changed(this);
    }
}

void MissionItem::setAutocontinue(bool autoContinue)
{
    if (_autocontinue != autoContinue) {
        _autocontinue = autoContinue;
        emit changed(this);
    }
}

Don Gagne's avatar
Don Gagne committed
420
void MissionItem::setIsCurrentItem(bool isCurrentItem)
421
{
Don Gagne's avatar
Don Gagne committed
422 423 424
    if (_isCurrentItem != isCurrentItem) {
        _isCurrentItem = isCurrentItem;
        emit isCurrentItemChanged(isCurrentItem);
425 426 427 428 429
    }
}

void MissionItem::setAcceptanceRadius(double radius)
{
Don Gagne's avatar
Don Gagne committed
430
    setParam2(radius);
431 432
}

Don Gagne's avatar
Don Gagne committed
433
void MissionItem::setParam1(double param)
434
{
Don Gagne's avatar
Don Gagne committed
435
    if (param1() != param)
436
    {
Don Gagne's avatar
Don Gagne committed
437
        _param1Fact->setValue(param);
438 439 440 441 442
        emit changed(this);
        emit valueStringsChanged(valueStrings());
    }
}

Don Gagne's avatar
Don Gagne committed
443
void MissionItem::setParam2(double param)
444
{
Don Gagne's avatar
Don Gagne committed
445
    if (param2() != param)
446
    {
Don Gagne's avatar
Don Gagne committed
447
        _param2Fact->setValue(param);
448 449 450 451 452 453 454
        emit valueStringsChanged(valueStrings());
        emit changed(this);
    }
}

void MissionItem::setParam3(double param3)
{
Don Gagne's avatar
Don Gagne committed
455
    setLoiterOrbitRadius(param3);
456 457 458 459
}

void MissionItem::setParam4(double param4)
{
460
    _setYawRadians(param4);
461 462 463 464
}

void MissionItem::setParam5(double param5)
{
465
    setLatitude(param5);
466 467 468 469
}

void MissionItem::setParam6(double param6)
{
470
    setLongitude(param6);
471 472 473 474
}

void MissionItem::setParam7(double param7)
{
475
    setAltitude(param7);
476 477
}

Don Gagne's avatar
Don Gagne committed
478
void MissionItem::setLoiterOrbitRadius(double radius)
479
{
Don Gagne's avatar
Don Gagne committed
480 481
    if (loiterOrbitRadius() != radius) {
        _loiterOrbitRadiusFact->setValue(radius);
482 483 484 485 486 487 488
        emit valueStringsChanged(valueStrings());
        emit changed(this);
    }
}

void MissionItem::setHoldTime(int holdTime)
{
Don Gagne's avatar
Don Gagne committed
489
    setParam1(holdTime);
490 491 492 493
}

void MissionItem::setHoldTime(double holdTime)
{
Don Gagne's avatar
Don Gagne committed
494
    setParam1(holdTime);
495 496
}

Don Gagne's avatar
Don Gagne committed
497
bool MissionItem::specifiesCoordinate(void) const
498
{
Don Gagne's avatar
Don Gagne committed
499
    switch (_command) {
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
        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
516
    switch (_command) {
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
        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
541
            type = QString("Unknown (%1)").arg(_command);
542 543 544 545 546 547
            break;
    }
    
    return type;
}

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
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
566
            description = "Send the vehicle back to the home position.";
567 568
            break;
        case MAV_CMD_NAV_LAND:
Don Gagne's avatar
Don Gagne committed
569
            description = "Land vehicle at the specified location.";
570 571
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
572
            description = "Take off from the ground and travel towards the specified position.";
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
            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;
}

588 589 590 591
QStringList MissionItem::valueLabels(void)
{
    QStringList labels;
    
Don Gagne's avatar
Don Gagne committed
592
    switch (_command) {
593
        case MAV_CMD_NAV_WAYPOINT:
Don Gagne's avatar
Don Gagne committed
594
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
                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
613
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
614 615 616 617 618 619 620
                labels << "Alt (rel):";
            } else {
                labels << "Alt:";
            }
            labels << "Heading:";
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
621
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
                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
650
    switch (_command) {
651
        case MAV_CMD_NAV_WAYPOINT:
652
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(headingDegrees()) << _oneDecimalString(param2()) << _oneDecimalString(param1());
653 654
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
655
            list << _oneDecimalString(headingDegrees()) << _oneDecimalString(loiterOrbitRadius());
656 657
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
658
            list << _oneDecimalString(headingDegrees()) << _oneDecimalString(loiterOrbitRadius()) << _oneDecimalString(param1());
659 660
            break;
        case MAV_CMD_NAV_LOITER_TIME:
661
            list << _oneDecimalString(headingDegrees()) << _oneDecimalString(loiterOrbitRadius()) << _oneDecimalString(param1());
662 663 664 665
            break;
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
            break;
        case MAV_CMD_NAV_LAND:
666
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(headingDegrees());
667 668
            break;
        case MAV_CMD_NAV_TAKEOFF:
669
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(headingDegrees()) << _oneDecimalString(param1());
670 671
            break;
        case MAV_CMD_CONDITION_DELAY:
Don Gagne's avatar
Don Gagne committed
672
            list << _oneDecimalString(param1());
673 674
            break;
        case MAV_CMD_DO_JUMP:
Don Gagne's avatar
Don Gagne committed
675
            list << _oneDecimalString(param1()) << _oneDecimalString(param2());
676 677 678 679 680 681
            break;
        default:
            break;
    }
    
    return list;
Don Gagne's avatar
Don Gagne committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
}

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
697
        if (_rgMavCmd2Name[i].command == (MAV_CMD)_command) {
Don Gagne's avatar
Don Gagne committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
            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
714

715
QmlObjectListModel* MissionItem::textFieldFacts(void)
Don Gagne's avatar
Don Gagne committed
716 717 718
{
    QmlObjectListModel* model = new QmlObjectListModel(this);
    
Don Gagne's avatar
Don Gagne committed
719
    switch ((MAV_CMD)_command) {
Don Gagne's avatar
Don Gagne committed
720 721 722
        case MAV_CMD_NAV_WAYPOINT:
            _param1Fact->_setName("Hold:");
            _param1Fact->setMetaData(_holdTimeMetaData);
723
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
724 725 726
            if (!_homePositionSpecialCase) {
                model->append(_param1Fact);
            }
Don Gagne's avatar
Don Gagne committed
727 728
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
729
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
730
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
731 732 733 734
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
            _param1Fact->_setName("Turns:");
            _param1Fact->setMetaData(_loiterTurnsMetaData);
735
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
736
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
737 738 739 740 741
            model->append(_param1Fact);
            break;
        case MAV_CMD_NAV_LOITER_TIME:
            _param1Fact->_setName("Seconds:");
            _param1Fact->setMetaData(_loiterSecondsMetaData);
742
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
743
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
744 745 746
            model->append(_param1Fact);
            break;
        case MAV_CMD_NAV_LAND:
747
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
748 749
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
750 751
            _param1Fact->_setName("Pitch:");
            _param1Fact->setMetaData(_pitchMetaData);
752
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
753
            model->append(_param1Fact);
Don Gagne's avatar
Don Gagne committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767
            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
768 769
        default:
            break;
Don Gagne's avatar
Don Gagne committed
770
    }
771 772 773 774 775

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

Don Gagne's avatar
Don Gagne committed
776 777 778 779
    
    return model;
}

780
QmlObjectListModel* MissionItem::checkboxFacts(void)
Don Gagne's avatar
Don Gagne committed
781
{
782 783
    QmlObjectListModel* model = new QmlObjectListModel(this);
    
Don Gagne's avatar
Don Gagne committed
784 785
    switch ((MAV_CMD)_command) {
        case MAV_CMD_NAV_WAYPOINT:
Don Gagne's avatar
Don Gagne committed
786 787 788
            if (!_homePositionSpecialCase) {
                model->append(_altitudeRelativeToHomeFact);
            }
789
            break;
Don Gagne's avatar
Don Gagne committed
790
        case MAV_CMD_NAV_LOITER_UNLIM:
791 792
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
793
        case MAV_CMD_NAV_LOITER_TURNS:
794 795
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
796
        case MAV_CMD_NAV_LOITER_TIME:
797 798
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
799
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
800
            break;
Don Gagne's avatar
Don Gagne committed
801
        case MAV_CMD_NAV_LAND:
802 803
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
804
        case MAV_CMD_NAV_TAKEOFF:
805 806
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
807
        default:
808
            break;
Don Gagne's avatar
Don Gagne committed
809
    }
810 811
    
    return model;
Don Gagne's avatar
Don Gagne committed
812 813
}

814
double MissionItem::headingDegrees(void) const
Don Gagne's avatar
Don Gagne committed
815
{
816
    return _headingDegreesFact->value().toDouble();
Don Gagne's avatar
Don Gagne committed
817 818
}

819
void MissionItem::setHeadingDegrees(double headingDegrees)
Don Gagne's avatar
Don Gagne committed
820
{
821 822
    if (_headingDegreesFact->value().toDouble() != headingDegrees) {
        _headingDegreesFact->setValue(headingDegrees);
Don Gagne's avatar
Don Gagne committed
823 824
        emit changed(this);
        emit valueStringsChanged(valueStrings());
825
        emit headingDegreesChanged(headingDegrees);
Don Gagne's avatar
Don Gagne committed
826 827 828 829
    }
}


830
double MissionItem::_yawRadians(void) const
Don Gagne's avatar
Don Gagne committed
831
{
832
    return _headingDegreesFact->value().toDouble() * (M_PI / 180.0);
Don Gagne's avatar
Don Gagne committed
833 834
}

835
void MissionItem::_setYawRadians(double yawRadians)
Don Gagne's avatar
Don Gagne committed
836
{
837
    setHeadingDegrees(yawRadians * (180 / M_PI));
Don Gagne's avatar
Don Gagne committed
838 839
}

840 841 842 843 844 845 846 847 848 849 850
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
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

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
880 881 882

void MissionItem::setDirty(bool dirty)
{
Don Gagne's avatar
Don Gagne committed
883 884 885 886 887 888 889 890
    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
891 892 893 894 895 896 897
}

void MissionItem::_factValueChanged(QVariant value)
{
    Q_UNUSED(value);
    setDirty(true);
}
898 899 900 901 902 903

void MissionItem::_coordinateFactChanged(QVariant value)
{
    Q_UNUSED(value);
    emit coordinateChanged(coordinate());
}
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

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());
}
920 921 922 923 924 925

void MissionItem::setHomePositionValid(bool homePositionValid)
{
    _homePositionValid = homePositionValid;
    emit homePositionValidChanged(_homePositionValid);
}