MissionItem.cc 28.1 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 46 47 48 49 50 51 52 53 54

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
55 56 57 58
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
59
    { MAV_CMD_NAV_LOITER_TIME,      "Loiter (seconds)" },
Don Gagne's avatar
Don Gagne committed
60 61 62 63 64 65 66 67 68 69
    { 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
70
                         int            command,
Don Gagne's avatar
Don Gagne committed
71 72 73 74 75 76
                         double         param1,
                         double         param2,
                         double         param3,
                         double         param4,
                         bool           autocontinue,
                         bool           isCurrentItem,
Don Gagne's avatar
Don Gagne committed
77
                         int            frame)
78
    : QObject(parent)
Don Gagne's avatar
Don Gagne committed
79
    , _sequenceNumber(sequenceNumber)
Don Gagne's avatar
Don Gagne committed
80
    , _frame(-1)    // Forces set of _altitudeRelativeToHomeFact
Don Gagne's avatar
Don Gagne committed
81
    , _command((MavlinkQmlSingleton::Qml_MAV_CMD)command)
82
    , _autocontinue(autocontinue)
Don Gagne's avatar
Don Gagne committed
83
    , _isCurrentItem(isCurrentItem)
84
    , _reachedTime(0)
Don Gagne's avatar
Don Gagne committed
85
    , _yawRadiansFact(NULL)
Don Gagne's avatar
Don Gagne committed
86
    ,_dirty(false)
Don Gagne's avatar
Don Gagne committed
87
    , _homePositionSpecialCase(false)
88
{
89 90 91 92 93 94 95 96
    _latitudeFact                   = new Fact(0, "Latitude:",                      FactMetaData::valueTypeDouble, this);
    _longitudeFact                  = new Fact(0, "Longitude:",                     FactMetaData::valueTypeDouble, this);
    _altitudeFact                   = new Fact(0, "Altitude:",                      FactMetaData::valueTypeDouble, this);
    _yawRadiansFact                 = new Fact(0, "Heading:",                       FactMetaData::valueTypeDouble, this);
    _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 103 104
    setParam1(param1);
    setParam2(param2);
    setYawRadians(param4);
    setLoiterOrbitRadius(param3);
Don Gagne's avatar
Don Gagne committed
105 106 107
    
    // FIXME: Need to fill out more meta data
    
108 109 110 111 112 113 114 115 116 117 118
    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");
    
    FactMetaData* yawMetaData = new FactMetaData(FactMetaData::valueTypeDouble, _yawRadiansFact);
    yawMetaData->setUnits("deg");
Don Gagne's avatar
Don Gagne committed
119 120
    
    _pitchMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
121
    _pitchMetaData->setUnits("deg");
Don Gagne's avatar
Don Gagne committed
122 123 124 125 126 127 128
    
    _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
129 130
    FactMetaData* loiterOrbitRadiusMetaData = new FactMetaData(FactMetaData::valueTypeDouble, this);
    loiterOrbitRadiusMetaData->setUnits("meters");
Don Gagne's avatar
Don Gagne committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    
    _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");
    
147 148 149
    _latitudeFact->setMetaData(latitudeMetaData);
    _longitudeFact->setMetaData(longitudeMetaData);
    _altitudeFact->setMetaData(altitudeMetaData);
Don Gagne's avatar
Don Gagne committed
150 151
    _yawRadiansFact->setMetaData(yawMetaData);
    _loiterOrbitRadiusFact->setMetaData(loiterOrbitRadiusMetaData);
Don Gagne's avatar
Don Gagne committed
152 153 154 155 156 157 158 159 160 161

    // 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(_yawRadiansFact, &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);
162 163 164 165 166
    
    // 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);
167 168
}

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

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    // 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(_yawRadiansFact, &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);
    
206 207 208 209 210 211 212 213 214
    *this = other;
}

MissionItem::~MissionItem()
{    
}

const MissionItem& MissionItem::operator=(const MissionItem& other)
{
215 216 217 218 219 220 221
    _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
222
    _dirty                      = other._dirty;
Don Gagne's avatar
Don Gagne committed
223
    _homePositionSpecialCase    = other._homePositionSpecialCase;
Don Gagne's avatar
Don Gagne committed
224
    
225 226 227
    *_latitudeFact              = *other._latitudeFact;
    *_longitudeFact             = *other._longitudeFact;
    *_altitudeFact              = *other._altitudeFact;
Don Gagne's avatar
Don Gagne committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241
    *_yawRadiansFact            = *other._yawRadiansFact;
    *_loiterOrbitRadiusFact     = *other._loiterOrbitRadiusFact;
    *_param1Fact                = *other._param1Fact;
    *_param2Fact                = *other._param2Fact;
    
    *_pitchMetaData             = *other._pitchMetaData;
    *_acceptanceRadiusMetaData  = *other._acceptanceRadiusMetaData;
    *_holdTimeMetaData          = *other._holdTimeMetaData;
    *_loiterTurnsMetaData       = *other._loiterTurnsMetaData;
    *_loiterSecondsMetaData     = *other._loiterSecondsMetaData;
    *_delaySecondsMetaData      = *other._delaySecondsMetaData;
    *_jumpSequenceMetaData      = *other._jumpSequenceMetaData;
    *_jumpRepeatMetaData        = *other._jumpRepeatMetaData;
    
242 243 244 245 246
    return *this;
}

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

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

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


Don Gagne's avatar
Don Gagne committed
285
void MissionItem::setSequenceNumber(int sequenceNumber)
286
{
Don Gagne's avatar
Don Gagne committed
287 288
    _sequenceNumber = sequenceNumber;
    emit sequenceNumberChanged(_sequenceNumber);
289 290 291 292 293 294 295
    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
296
        setLatitude(x);
297 298 299 300 301 302 303
    }
}

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
304
        setLongitude(y);
305 306 307 308 309 310 311
    }
}

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
312
        setAltitude(z);
313 314 315 316 317
    }
}

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

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

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

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

Don Gagne's avatar
Don Gagne committed
352
        // Fix defaults according to WP type
353

Don Gagne's avatar
Don Gagne committed
354
        if (_command == MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF) {
355
            // We default to 15 degrees minimum takeoff pitch
Don Gagne's avatar
Don Gagne committed
356
            setParam1(15.0);
357
        }
Don Gagne's avatar
Don Gagne committed
358 359 360 361 362 363 364 365
        
        if (specifiesCoordinate()) {
            if (_frame != MAV_FRAME_GLOBAL && _frame != MAV_FRAME_GLOBAL_RELATIVE_ALT) {
                setFrame(MAV_FRAME_GLOBAL_RELATIVE_ALT);
            }
        } else {
            setFrame(MAV_FRAME_MISSION);
        }
366 367 368

        emit changed(this);
        emit commandNameChanged(commandName());
Don Gagne's avatar
Don Gagne committed
369
        emit commandChanged((MavlinkQmlSingleton::Qml_MAV_CMD)_command);
370 371 372 373 374
        emit valueLabelsChanged(valueLabels());
        emit valueStringsChanged(valueStrings());
    }
}

375 376 377 378 379 380 381 382 383
int MissionItem::frame(void) const
{
    if (_altitudeRelativeToHomeFact->value().toBool()) {
        return MAV_FRAME_GLOBAL_RELATIVE_ALT;
    } else {
        return _frame;
    }
}

384 385 386
void MissionItem::setFrame(int /*MAV_FRAME*/ frame)
{
    if (_frame != frame) {
Don Gagne's avatar
Don Gagne committed
387
        _altitudeRelativeToHomeFact->setValue(frame == MAV_FRAME_GLOBAL_RELATIVE_ALT);
388 389 390 391 392 393 394 395 396 397 398 399 400
        _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
401
void MissionItem::setIsCurrentItem(bool isCurrentItem)
402
{
Don Gagne's avatar
Don Gagne committed
403 404 405
    if (_isCurrentItem != isCurrentItem) {
        _isCurrentItem = isCurrentItem;
        emit isCurrentItemChanged(isCurrentItem);
406 407 408 409 410
    }
}

void MissionItem::setAcceptanceRadius(double radius)
{
Don Gagne's avatar
Don Gagne committed
411
    setParam2(radius);
412 413
}

Don Gagne's avatar
Don Gagne committed
414
void MissionItem::setParam1(double param)
415
{
Don Gagne's avatar
Don Gagne committed
416
    if (param1() != param)
417
    {
Don Gagne's avatar
Don Gagne committed
418
        _param1Fact->setValue(param);
419 420 421 422 423
        emit changed(this);
        emit valueStringsChanged(valueStrings());
    }
}

Don Gagne's avatar
Don Gagne committed
424
void MissionItem::setParam2(double param)
425
{
Don Gagne's avatar
Don Gagne committed
426
    if (param2() != param)
427
    {
Don Gagne's avatar
Don Gagne committed
428
        _param2Fact->setValue(param);
429 430 431 432 433 434 435
        emit valueStringsChanged(valueStrings());
        emit changed(this);
    }
}

void MissionItem::setParam3(double param3)
{
Don Gagne's avatar
Don Gagne committed
436
    setLoiterOrbitRadius(param3);
437 438 439 440
}

void MissionItem::setParam4(double param4)
{
Don Gagne's avatar
Don Gagne committed
441
    setYawRadians(param4);
442 443 444 445
}

void MissionItem::setParam5(double param5)
{
446
    setLatitude(param5);
447 448 449 450
}

void MissionItem::setParam6(double param6)
{
451
    setLongitude(param6);
452 453 454 455
}

void MissionItem::setParam7(double param7)
{
456
    setAltitude(param7);
457 458
}

Don Gagne's avatar
Don Gagne committed
459
void MissionItem::setLoiterOrbitRadius(double radius)
460
{
Don Gagne's avatar
Don Gagne committed
461 462
    if (loiterOrbitRadius() != radius) {
        _loiterOrbitRadiusFact->setValue(radius);
463 464 465 466 467 468 469
        emit valueStringsChanged(valueStrings());
        emit changed(this);
    }
}

void MissionItem::setHoldTime(int holdTime)
{
Don Gagne's avatar
Don Gagne committed
470
    setParam1(holdTime);
471 472 473 474
}

void MissionItem::setHoldTime(double holdTime)
{
Don Gagne's avatar
Don Gagne committed
475
    setParam1(holdTime);
476 477
}

Don Gagne's avatar
Don Gagne committed
478
bool MissionItem::specifiesCoordinate(void) const
479
{
Don Gagne's avatar
Don Gagne committed
480
    switch (_command) {
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        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
497
    switch (_command) {
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
        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
522
            type = QString("Unknown (%1)").arg(_command);
523 524 525 526 527 528 529 530 531 532
            break;
    }
    
    return type;
}

QStringList MissionItem::valueLabels(void)
{
    QStringList labels;
    
Don Gagne's avatar
Don Gagne committed
533
    switch (_command) {
534
        case MAV_CMD_NAV_WAYPOINT:
Don Gagne's avatar
Don Gagne committed
535
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
                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
554
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
555 556 557 558 559 560 561
                labels << "Alt (rel):";
            } else {
                labels << "Alt:";
            }
            labels << "Heading:";
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
562
            if (frame() == MAV_FRAME_GLOBAL_RELATIVE_ALT) {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
                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
591
    switch (_command) {
592
        case MAV_CMD_NAV_WAYPOINT:
593
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(yawDegrees()) << _oneDecimalString(param2()) << _oneDecimalString(param1());
594 595
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
Don Gagne's avatar
Don Gagne committed
596
            list << _oneDecimalString(yawRadians() * (180.0 / M_PI)) << _oneDecimalString(loiterOrbitRadius());
597 598
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
Don Gagne's avatar
Don Gagne committed
599
            list << _oneDecimalString(yawRadians() * (180.0 / M_PI)) << _oneDecimalString(loiterOrbitRadius()) << _oneDecimalString(param1());
600 601
            break;
        case MAV_CMD_NAV_LOITER_TIME:
Don Gagne's avatar
Don Gagne committed
602
            list << _oneDecimalString(yawRadians() * (180.0 / M_PI)) << _oneDecimalString(loiterOrbitRadius()) << _oneDecimalString(param1());
603 604 605 606
            break;
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
            break;
        case MAV_CMD_NAV_LAND:
607
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(yawRadians() * (180.0 / M_PI));
608 609
            break;
        case MAV_CMD_NAV_TAKEOFF:
610
            list << _oneDecimalString(_altitudeFact->value().toDouble()) << _oneDecimalString(yawRadians() * (180.0 / M_PI)) << _oneDecimalString(param1());
611 612
            break;
        case MAV_CMD_CONDITION_DELAY:
Don Gagne's avatar
Don Gagne committed
613
            list << _oneDecimalString(param1());
614 615
            break;
        case MAV_CMD_DO_JUMP:
Don Gagne's avatar
Don Gagne committed
616
            list << _oneDecimalString(param1()) << _oneDecimalString(param2());
617 618 619 620 621 622
            break;
        default:
            break;
    }
    
    return list;
Don Gagne's avatar
Don Gagne committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
}

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
638
        if (_rgMavCmd2Name[i].command == (MAV_CMD)_command) {
Don Gagne's avatar
Don Gagne committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
            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
655

656
QmlObjectListModel* MissionItem::textFieldFacts(void)
Don Gagne's avatar
Don Gagne committed
657 658 659
{
    QmlObjectListModel* model = new QmlObjectListModel(this);
    
Don Gagne's avatar
Don Gagne committed
660
    switch ((MAV_CMD)_command) {
Don Gagne's avatar
Don Gagne committed
661 662 663 664 665
        case MAV_CMD_NAV_WAYPOINT:
            _param2Fact->_setName("Radius:");
            _param2Fact->setMetaData(_acceptanceRadiusMetaData);
            _param1Fact->_setName("Hold:");
            _param1Fact->setMetaData(_holdTimeMetaData);
666 667 668
            model->append(_latitudeFact);
            model->append(_longitudeFact);
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
669 670 671 672 673
            if (!_homePositionSpecialCase) {
                model->append(_yawRadiansFact);
                model->append(_param2Fact);
                model->append(_param1Fact);
            }
Don Gagne's avatar
Don Gagne committed
674 675
            break;
        case MAV_CMD_NAV_LOITER_UNLIM:
676 677 678
            model->append(_latitudeFact);
            model->append(_longitudeFact);
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
679 680
            model->append(_yawRadiansFact);
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
681 682 683 684
            break;
        case MAV_CMD_NAV_LOITER_TURNS:
            _param1Fact->_setName("Turns:");
            _param1Fact->setMetaData(_loiterTurnsMetaData);
685 686 687
            model->append(_latitudeFact);
            model->append(_longitudeFact);
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
688 689
            model->append(_yawRadiansFact);
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
690 691 692 693 694
            model->append(_param1Fact);
            break;
        case MAV_CMD_NAV_LOITER_TIME:
            _param1Fact->_setName("Seconds:");
            _param1Fact->setMetaData(_loiterSecondsMetaData);
695 696 697
            model->append(_latitudeFact);
            model->append(_longitudeFact);
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
698 699
            model->append(_yawRadiansFact);
            model->append(_loiterOrbitRadiusFact);
Don Gagne's avatar
Don Gagne committed
700 701 702
            model->append(_param1Fact);
            break;
        case MAV_CMD_NAV_LAND:
703 704 705
            model->append(_latitudeFact);
            model->append(_longitudeFact);
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
706
            model->append(_yawRadiansFact);
Don Gagne's avatar
Don Gagne committed
707 708
            break;
        case MAV_CMD_NAV_TAKEOFF:
Don Gagne's avatar
Don Gagne committed
709 710
            _param1Fact->_setName("Pitch:");
            _param1Fact->setMetaData(_pitchMetaData);
711 712 713
            model->append(_latitudeFact);
            model->append(_longitudeFact);
            model->append(_altitudeFact);
Don Gagne's avatar
Don Gagne committed
714 715
            model->append(_yawRadiansFact);
            model->append(_param1Fact);
Don Gagne's avatar
Don Gagne committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729
            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
730 731
        default:
            break;
Don Gagne's avatar
Don Gagne committed
732 733 734 735 736
    }
    
    return model;
}

737
QmlObjectListModel* MissionItem::checkboxFacts(void)
Don Gagne's avatar
Don Gagne committed
738
{
739 740
    QmlObjectListModel* model = new QmlObjectListModel(this);
    
Don Gagne's avatar
Don Gagne committed
741 742
    switch ((MAV_CMD)_command) {
        case MAV_CMD_NAV_WAYPOINT:
Don Gagne's avatar
Don Gagne committed
743 744 745
            if (!_homePositionSpecialCase) {
                model->append(_altitudeRelativeToHomeFact);
            }
746
            break;
Don Gagne's avatar
Don Gagne committed
747
        case MAV_CMD_NAV_LOITER_UNLIM:
748 749
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
750
        case MAV_CMD_NAV_LOITER_TURNS:
751 752
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
753
        case MAV_CMD_NAV_LOITER_TIME:
754 755
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
756
        case MAV_CMD_NAV_RETURN_TO_LAUNCH:
757
            break;
Don Gagne's avatar
Don Gagne committed
758
        case MAV_CMD_NAV_LAND:
759 760
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
761
        case MAV_CMD_NAV_TAKEOFF:
762 763
            model->append(_altitudeRelativeToHomeFact);
            break;
Don Gagne's avatar
Don Gagne committed
764
        default:
765
            break;
Don Gagne's avatar
Don Gagne committed
766
    }
767 768
    
    return model;
Don Gagne's avatar
Don Gagne committed
769 770
}

Don Gagne's avatar
Don Gagne committed
771 772
double MissionItem::yawRadians(void) const
{
Don Gagne's avatar
Don Gagne committed
773
    return _yawRadiansFact->value().toDouble();
Don Gagne's avatar
Don Gagne committed
774 775 776 777
}

void MissionItem::setYawRadians(double yaw)
{
Don Gagne's avatar
Don Gagne committed
778
    if (yawRadians() != yaw)
Don Gagne's avatar
Don Gagne committed
779
    {
Don Gagne's avatar
Don Gagne committed
780
        _yawRadiansFact->setValue(yaw);
Don Gagne's avatar
Don Gagne committed
781 782 783 784 785 786 787 788
        emit changed(this);
        emit valueStringsChanged(valueStrings());
    }
}


double MissionItem::yawDegrees(void) const
{
Don Gagne's avatar
Don Gagne committed
789
    return yawRadians() * (180.0 / M_PI);
Don Gagne's avatar
Don Gagne committed
790 791 792 793 794 795 796
}

void MissionItem::setYawDegrees(double yaw)
{
    setYawRadians(yaw * (M_PI / 180.0));
}

797 798 799 800 801 802 803 804 805 806 807
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
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

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
837 838 839 840

void MissionItem::setDirty(bool dirty)
{
    _dirty = dirty;
841 842
    // We want to emit dirtyChanged even if _dirty didn't change. This can be handy signal for
    // any value within the item changing.
Don Gagne's avatar
Don Gagne committed
843 844 845 846 847 848 849 850
    emit dirtyChanged(_dirty);
}

void MissionItem::_factValueChanged(QVariant value)
{
    Q_UNUSED(value);
    setDirty(true);
}
851 852 853 854 855 856

void MissionItem::_coordinateFactChanged(QVariant value)
{
    Q_UNUSED(value);
    emit coordinateChanged(coordinate());
}