MissionController.cc 22.5 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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2015 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 "MissionController.h"
#include "MultiVehicleManager.h"
#include "MissionManager.h"
27
#include "QGCFileDialog.h"
28
#include "CoordinateVector.h"
29
#include "FirmwarePlugin.h"
30
#include "QGCApplication.h"
31

32 33
QGC_LOGGING_CATEGORY(MissionControllerLog, "MissionControllerLog")

34
const char* MissionController::_settingsGroup = "MissionController";
35 36 37

MissionController::MissionController(QObject *parent)
    : QObject(parent)
38
    , _editMode(false)
39 40
    , _missionItems(NULL)
    , _activeVehicle(NULL)
41 42
    , _liveHomePositionAvailable(false)
    , _autoSync(false)
43
    , _firstItemsFromVehicle(false)
44 45
    , _missionItemsRequested(false)
    , _queuedSend(false)
46
{
47 48 49 50 51

}

MissionController::~MissionController()
{
52 53 54
    // Start with empty list
    _missionItems = new QmlObjectListModel(this);
    _initAllMissionItems();
55 56 57 58
}

void MissionController::start(bool editMode)
{
59 60
    qCDebug(MissionControllerLog) << "start editMode" << editMode;

61 62
    _editMode = editMode;

63
    MultiVehicleManager* multiVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
64

65
    connect(multiVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &MissionController::_activeVehicleChanged);
66

Don Gagne's avatar
Don Gagne committed
67 68
    _setupMissionItems(true /* loadFromVehicle */, true /* forceLoad */);
    _setupActiveVehicle(multiVehicleMgr->activeVehicle(), true /* forceLoadFromVehicle */);
69 70
}

71
void MissionController::_newMissionItemsAvailableFromVehicle(void)
72
{
73 74
    qCDebug(MissionControllerLog) << "_newMissionItemsAvailableFromVehicle";

Don Gagne's avatar
Don Gagne committed
75
    _setupMissionItems(true /* loadFromVehicle */, false /* forceLoad */);
76 77
}

Don Gagne's avatar
Don Gagne committed
78 79 80
/// @param loadFromVehicle true: load items from vehicle
/// @param forceLoad true: disregard any flags which may prevent load
void MissionController::_setupMissionItems(bool loadFromVehicle, bool forceLoad)
81
{
Don Gagne's avatar
Don Gagne committed
82 83
    qCDebug(MissionControllerLog) << "_setupMissionItems loadFromVehicle:forceLoad:_editMode:_firstItemsFromVehicle"
                                  << loadFromVehicle << forceLoad << _editMode << _firstItemsFromVehicle;
84 85 86 87 88 89 90 91

    MissionManager* missionManager = NULL;
    if (_activeVehicle) {
        missionManager = _activeVehicle->missionManager();
    } else {
        qCDebug(MissionControllerLog) << "running offline";
    }

Don Gagne's avatar
Don Gagne committed
92 93
    if (!forceLoad) {
        if (_editMode && loadFromVehicle) {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
            if (_firstItemsFromVehicle) {
                if (missionManager) {
                    if (missionManager->inProgress()) {
                        // Still in progress of retrieving items from vehicle, leave current set alone and wait for
                        // mission manager to finish
                        qCDebug(MissionControllerLog) << "disregarding due to MissionManager in progress";
                        return;
                    } else {
                        // We have the first set of items from the vehicle. If we haven't already started creating a
                        // new mission, switch to the items from the vehicle
                        _firstItemsFromVehicle = false;
                        if (_missionItems->count() != 1) {
                            qCDebug(MissionControllerLog) << "disregarding due to existing items";
                            return;
                        }
                    }
110
                }
111 112 113 114 115
            } else if (!_missionItemsRequested) {
                // We didn't specifically ask for new mission items. Disregard the new set since it is
                // the most likely the set we just sent to the vehicle.
                qCDebug(MissionControllerLog) << "disregarding due to unrequested notification";
                return;
116 117 118 119
            }
        }
    }

120 121
    qCDebug(MissionControllerLog) << "fell through to main setup";

122 123
    _missionItemsRequested = false;

124
    if (_missionItems) {
125
        _deinitAllMissionItems();
126 127
        _missionItems->deleteLater();
    }
128

Don Gagne's avatar
Don Gagne committed
129
    if (!missionManager || !loadFromVehicle || missionManager->inProgress()) {
130
        _missionItems = new QmlObjectListModel(this);
131
        qCDebug(MissionControllerLog) << "creating empty set";
132 133
    } else {
        _missionItems = missionManager->copyMissionItems();
134
        qCDebug(MissionControllerLog) << "loading from vehicle count"<< _missionItems->count();
135 136 137 138 139
    }

    _initAllMissionItems();
}

140
void MissionController::getMissionItems(void)
141
{
142
    Vehicle* activeVehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
143

144 145 146 147 148 149 150 151
    if (activeVehicle) {
        _missionItemsRequested = true;
        activeVehicle->missionManager()->requestMissionItems();
    }
}

void MissionController::sendMissionItems(void)
{
152
    Vehicle* activeVehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
153

154 155 156 157 158
    if (activeVehicle) {
        activeVehicle->missionManager()->writeMissionItems(*_missionItems);
        _missionItems->setDirty(false);
    }
}
159

160 161
int MissionController::addMissionItem(QGeoCoordinate coordinate)
{
162 163 164 165
    MissionItem * newItem = new MissionItem(this);
    newItem->setSequenceNumber(_missionItems->count());
    newItem->setCoordinate(coordinate);
    newItem->setCommand(MAV_CMD_NAV_WAYPOINT);
166 167 168 169
    _initMissionItem(newItem);
    if (_missionItems->count() == 1) {
        newItem->setCommand(MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF);
    }
170
    newItem->setDefaultsForCommand();
Don Gagne's avatar
Don Gagne committed
171
    if ((MAV_CMD)newItem->command() == MAV_CMD_NAV_WAYPOINT) {
172 173 174 175 176 177 178 179
        double lastValue;

        if (_findLastAcceptanceRadius(&lastValue)) {
            newItem->setParam2(lastValue);
        }
        if (_findLastAltitude(&lastValue)) {
            newItem->setParam7(lastValue);
        }
180
    }
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    _missionItems->append(newItem);

    _recalcAll();

    return _missionItems->count() - 1;
}

void MissionController::removeMissionItem(int index)
{
    MissionItem* item = qobject_cast<MissionItem*>(_missionItems->removeAt(index));

    _deinitMissionItem(item);

    _recalcAll();

    // Set the new current item

    if (index >= _missionItems->count()) {
        index--;
    }
    for (int i=0; i<_missionItems->count(); i++) {
        MissionItem* item =  qobject_cast<MissionItem*>(_missionItems->get(i));
        item->setIsCurrentItem(i == index);
    }
}

void MissionController::loadMissionFromFile(void)
{
    QString errorString;
    QString filename = QGCFileDialog::getOpenFileName(NULL, "Select Mission File to load");

    if (filename.isEmpty()) {
        return;
    }

    if (_missionItems) {
        _deinitAllMissionItems();
        _missionItems->deleteLater();
    }
    _missionItems = new QmlObjectListModel(this);

    // FIXME: This needs to handle APM files which have WP 0 in them

    QFile file(filename);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        errorString = file.errorString();
    } else {
        QTextStream in(&file);

        const QStringList& version = in.readLine().split(" ");

        if (!(version.size() == 3 && version[0] == "QGC" && version[1] == "WPL" && version[2] == "120")) {
            errorString = "The mission file is not compatible with the current version of QGroundControl.";
        } else {
            while (!in.atEnd()) {
                MissionItem* item = new MissionItem();

                if (item->load(in)) {
                    _missionItems->append(item);
241
                } else {
242 243
                    errorString = "The mission file is corrupted.";
                    break;
244 245 246
                }
            }
        }
247

248
    }
249 250 251 252 253 254

    if (!errorString.isEmpty()) {
        _missionItems->clear();
    }

    _initAllMissionItems();
255 256
}

257
void MissionController::saveMissionToFile(void)
258
{
259 260 261 262 263 264
    QString errorString;
    QString filename = QGCFileDialog::getSaveFileName(NULL, "Select file to save mission to");

    if (filename.isEmpty()) {
        return;
    }
265

266
    QFile file(filename);
267

268 269 270 271
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        errorString = file.errorString();
    } else {
        QTextStream out(&file);
272

273
        out << "QGC WPL 120\r\n";   // Version string
274

275 276 277 278 279 280 281 282
        for (int i=1; i<_missionItems->count(); i++) {
            qobject_cast<MissionItem*>(_missionItems->get(i))->save(out);
        }
    }

    _missionItems->setDirty(false);
}

Don Gagne's avatar
Don Gagne committed
283
void MissionController::_calcPrevWaypointValues(bool homePositionValid, double homeAlt, MissionItem* currentItem, MissionItem* prevItem, double* azimuth, double* distance, double* altDifference)
284
{
Don Gagne's avatar
Don Gagne committed
285 286
    QGeoCoordinate  currentCoord =  currentItem->coordinate();
    QGeoCoordinate  prevCoord =     prevItem->coordinate();
287 288 289 290 291
    bool            distanceOk =    false;

    // Convert to fixed altitudes

    qCDebug(MissionControllerLog) << homePositionValid << homeAlt
Don Gagne's avatar
Don Gagne committed
292 293
                                  << currentItem->relativeAltitude() << currentItem->coordinate().altitude()
                                  << prevItem->relativeAltitude() << prevItem->coordinate().altitude();
294 295 296

    if (homePositionValid) {
        distanceOk = true;
Don Gagne's avatar
Don Gagne committed
297 298
        if (currentItem->relativeAltitude()) {
            currentCoord.setAltitude(homeAlt + currentCoord.altitude());
299
        }
Don Gagne's avatar
Don Gagne committed
300 301
        if (prevItem->relativeAltitude()) {
            prevCoord.setAltitude(homeAlt + prevCoord.altitude());
302 303
        }
    } else {
Don Gagne's avatar
Don Gagne committed
304
        if (prevItem->relativeAltitude() && currentItem->relativeAltitude()) {
305 306 307 308 309 310 311
            distanceOk = true;
        }
    }

    qCDebug(MissionControllerLog) << "distanceOk" << distanceOk;

    if (distanceOk) {
Don Gagne's avatar
Don Gagne committed
312 313 314
        *altDifference = currentCoord.altitude() - prevCoord.altitude();
        *distance = prevCoord.distanceTo(currentCoord);
        *azimuth = prevCoord.azimuthTo(currentCoord);
315
    } else {
Don Gagne's avatar
Don Gagne committed
316
        *altDifference = 0.0;
317
        *azimuth = 0.0;
Don Gagne's avatar
Don Gagne committed
318
        *distance = -1.0;   // Signals no values
319 320 321
    }
}

322 323 324
void MissionController::_recalcWaypointLines(void)
{
    MissionItem*    lastCoordinateItem =    qobject_cast<MissionItem*>(_missionItems->get(0));
325
    MissionItem*    homeItem =              lastCoordinateItem;
326
    bool            firstCoordinateItem =   true;
327 328 329 330 331 332 333 334
    bool            homePositionValid =     homeItem->homePositionValid();
    double          homeAlt =               homeItem->coordinate().altitude();

    qCDebug(MissionControllerLog) << "_recalcWaypointLines";

    // If home position is valid we can calculate distances between all waypoints.
    // If home position is not valid we can only calculate distances between waypoints which are
    // both relative altitude.
335

336 337
    // No values for first item
    lastCoordinateItem->setAzimuth(0.0);
338 339
    lastCoordinateItem->setDistance(-1.0);

340 341 342 343 344
    _waypointLines.clear();

    for (int i=1; i<_missionItems->count(); i++) {
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));

345 346 347
        // Assume the worst
        item->setAzimuth(0.0);
        item->setDistance(-1.0);
348

349
        if (item->specifiesCoordinate() && !item->standaloneCoordinate()) {
350 351 352
            if (firstCoordinateItem) {
                if (item->command() == MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF) {
                    // The first coordinate we hit is a takeoff command so link back to home position if valid
353
                    if (homePositionValid) {
Don Gagne's avatar
Don Gagne committed
354
                        double azimuth, distance, altDifference;
355

356
                        _waypointLines.append(new CoordinateVector(homeItem->coordinate(), item->coordinate()));
Don Gagne's avatar
Don Gagne committed
357 358
                        _calcPrevWaypointValues(homePositionValid, homeAlt, item, homeItem, &azimuth, &distance, &altDifference);
                        item->setAltDifference(altDifference);
359 360
                        item->setAzimuth(azimuth);
                        item->setDistance(distance);
361 362 363 364 365 366
                    }
                } else {
                    // First coordiante is not a takeoff command, it does not link backwards to anything
                }
                firstCoordinateItem = false;
            } else if (!lastCoordinateItem->homePosition() || lastCoordinateItem->homePositionValid()) {
Don Gagne's avatar
Don Gagne committed
367
                double azimuth, distance, altDifference;
368

369 370
                // Subsequent coordinate items link to last coordinate item. If the last coordinate item
                // is an invalid home position we skip the line
Don Gagne's avatar
Don Gagne committed
371 372
                _calcPrevWaypointValues(homePositionValid, homeAlt, item, lastCoordinateItem, &azimuth, &distance, &altDifference);
                item->setAltDifference(altDifference);
373 374
                item->setAzimuth(azimuth);
                item->setDistance(distance);
375
                _waypointLines.append(new CoordinateVector(lastCoordinateItem->coordinate(), item->coordinate()));
376
            }
377
            lastCoordinateItem = item;
378 379
        }
    }
380 381

    emit waypointLinesChanged();
382 383
}

384 385 386 387 388 389 390 391 392 393 394
// This will update the sequence numbers to be sequential starting from 0
void MissionController::_recalcSequence(void)
{
    for (int i=0; i<_missionItems->count(); i++) {
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));

        // Setup ascending sequence numbers
        item->setSequenceNumber(i);
    }
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
// This will update the child item hierarchy
void MissionController::_recalcChildItems(void)
{
    MissionItem* currentParentItem = qobject_cast<MissionItem*>(_missionItems->get(0));

    currentParentItem->childItems()->clear();

    for (int i=1; i<_missionItems->count(); i++) {
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));

        // Set up non-coordinate item child hierarchy
        if (item->specifiesCoordinate()) {
            item->childItems()->clear();
            currentParentItem = item;
        } else {
            currentParentItem->childItems()->append(item);
        }
    }
}

415 416
void MissionController::_recalcAll(void)
{
417
    _recalcSequence();
418 419 420 421 422 423 424
    _recalcChildItems();
    _recalcWaypointLines();
}

/// Initializes a new set of mission items which may have come from the vehicle or have been loaded from a file
void MissionController::_initAllMissionItems(void)
{
425 426 427 428 429 430 431
    MissionItem* homeItem = NULL;

    if (_activeVehicle && _activeVehicle->firmwarePlugin()->sendHomePositionToVehicle() && _missionItems->count() != 0) {
        homeItem = qobject_cast<MissionItem*>(_missionItems->get(0));
    } else {
        // Add the home position item to the front
        homeItem = new MissionItem(this);
432
        homeItem->setSequenceNumber(0);
433 434
        _missionItems->insert(0, homeItem);
    }
Don Gagne's avatar
Don Gagne committed
435 436 437 438 439 440 441 442 443
    homeItem->setHomePositionSpecialCase(true);
    if (_activeVehicle) {
        homeItem->setCoordinate(_activeVehicle->homePosition());
        homeItem->setHomePositionValid(_activeVehicle->homePositionAvailable());
    } else {
        homeItem->setHomePositionValid(false);
    }
    homeItem->setCommand(MAV_CMD_NAV_WAYPOINT);
    homeItem->setFrame(MAV_FRAME_GLOBAL);
444 445 446 447 448

    for (int i=0; i<_missionItems->count(); i++) {
        _initMissionItem(qobject_cast<MissionItem*>(_missionItems->get(i)));
    }

449
    _recalcAll();
450

451
    emit missionItemsChanged();
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

    _missionItems->setDirty(false);

    connect(_missionItems, &QmlObjectListModel::dirtyChanged, this, &MissionController::_dirtyChanged);
}

void MissionController::_deinitAllMissionItems(void)
{
    for (int i=0; i<_missionItems->count(); i++) {
        _deinitMissionItem(qobject_cast<MissionItem*>(_missionItems->get(i)));
    }

    connect(_missionItems, &QmlObjectListModel::dirtyChanged, this, &MissionController::_dirtyChanged);
}

void MissionController::_initMissionItem(MissionItem* item)
{
    _missionItems->setDirty(false);

    connect(item, &MissionItem::commandChanged,     this, &MissionController::_itemCommandChanged);
    connect(item, &MissionItem::coordinateChanged,  this, &MissionController::_itemCoordinateChanged);
473
    connect(item, &MissionItem::frameChanged,       this, &MissionController::_itemFrameChanged);
474 475 476 477 478 479
}

void MissionController::_deinitMissionItem(MissionItem* item)
{
    disconnect(item, &MissionItem::commandChanged,     this, &MissionController::_itemCommandChanged);
    disconnect(item, &MissionItem::coordinateChanged,  this, &MissionController::_itemCoordinateChanged);
480
    disconnect(item, &MissionItem::frameChanged,       this, &MissionController::_itemFrameChanged);
481 482 483 484 485 486 487 488
}

void MissionController::_itemCoordinateChanged(const QGeoCoordinate& coordinate)
{
    Q_UNUSED(coordinate);
    _recalcWaypointLines();
}

489 490 491 492 493 494
void MissionController::_itemFrameChanged(int frame)
{
    Q_UNUSED(frame);
    _recalcWaypointLines();
}

495 496 497 498 499
void MissionController::_itemCommandChanged(MavlinkQmlSingleton::Qml_MAV_CMD command)
{
    Q_UNUSED(command);;
    _recalcChildItems();
    _recalcWaypointLines();
500 501 502 503
}

void MissionController::_activeVehicleChanged(Vehicle* activeVehicle)
{
504 505
    qCDebug(MissionControllerLog) << "_activeVehicleChanged activeVehicle" << activeVehicle;

506 507
    if (_activeVehicle) {
        MissionManager* missionManager = _activeVehicle->missionManager();
508

509
        disconnect(missionManager, &MissionManager::newMissionItemsAvailable,   this, &MissionController::_newMissionItemsAvailableFromVehicle);
510 511 512
        disconnect(missionManager, &MissionManager::inProgressChanged,          this, &MissionController::_inProgressChanged);
        disconnect(_activeVehicle, &Vehicle::homePositionAvailableChanged,      this, &MissionController::_activeVehicleHomePositionAvailableChanged);
        disconnect(_activeVehicle, &Vehicle::homePositionChanged,               this, &MissionController::_activeVehicleHomePositionChanged);
513
        _activeVehicle = NULL;
514 515

        // When the active vehicle goes away we toss the editor items
Don Gagne's avatar
Don Gagne committed
516
        _setupMissionItems(false /* loadFromVehicle */, true /* forceLoad */);
517
        _activeVehicleHomePositionAvailableChanged(false);
518
    }
519

Don Gagne's avatar
Don Gagne committed
520 521 522 523 524 525 526 527 528 529 530 531
    _setupActiveVehicle(activeVehicle, false /* forceLoadFromVehicle */);
}

void MissionController::_setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle)
{
    qCDebug(MissionControllerLog) << "_setupActiveVehicle activeVehicle:forceLoadFromVehicle"
                                  << activeVehicle << forceLoadFromVehicle;

    if (_activeVehicle) {
        qCWarning(MissionControllerLog) << "_activeVehicle != NULL";
    }

532
    _activeVehicle = activeVehicle;
533

534 535
    if (_activeVehicle) {
        MissionManager* missionManager = activeVehicle->missionManager();
536

537
        connect(missionManager, &MissionManager::newMissionItemsAvailable,  this, &MissionController::_newMissionItemsAvailableFromVehicle);
538
        connect(missionManager, &MissionManager::inProgressChanged,         this, &MissionController::_inProgressChanged);
539 540 541
        connect(_activeVehicle, &Vehicle::homePositionAvailableChanged,     this, &MissionController::_activeVehicleHomePositionAvailableChanged);
        connect(_activeVehicle, &Vehicle::homePositionChanged,              this, &MissionController::_activeVehicleHomePositionChanged);

542
        _firstItemsFromVehicle = true;
Don Gagne's avatar
Don Gagne committed
543
        _setupMissionItems(true /* fromVehicle */, forceLoadFromVehicle);
544 545 546 547 548 549 550 551 552

        _activeVehicleHomePositionChanged(_activeVehicle->homePosition());
        _activeVehicleHomePositionAvailableChanged(_activeVehicle->homePositionAvailable());
    }
}

void MissionController::_activeVehicleHomePositionAvailableChanged(bool homePositionAvailable)
{
    _liveHomePositionAvailable = homePositionAvailable;
Don Gagne's avatar
Don Gagne committed
553
    qobject_cast<MissionItem*>(_missionItems->get(0))->setHomePositionValid(homePositionAvailable);
554
    emit liveHomePositionAvailableChanged(_liveHomePositionAvailable);
555
    _recalcWaypointLines();
556 557 558 559 560
}

void MissionController::_activeVehicleHomePositionChanged(const QGeoCoordinate& homePosition)
{
    _liveHomePosition = homePosition;
Don Gagne's avatar
Don Gagne committed
561
    qobject_cast<MissionItem*>(_missionItems->get(0))->setCoordinate(_liveHomePosition);
562
    emit liveHomePositionChanged(_liveHomePosition);
563
    _recalcWaypointLines();
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 591 592 593 594
}

void MissionController::deleteCurrentMissionItem(void)
{
    for (int i=0; i<_missionItems->count(); i++) {
        MissionItem* item =  qobject_cast<MissionItem*>(_missionItems->get(i));
        if (item->isCurrentItem() && i != 0) {
            removeMissionItem(i);
            return;
        }
    }
}

void MissionController::setAutoSync(bool autoSync)
{
    // FIXME: AutoSync temporarily turned off
#if 0
    _autoSync = autoSync;
    emit autoSyncChanged(_autoSync);

    if (_autoSync) {
        _dirtyChanged(true);
    }
#else
    Q_UNUSED(autoSync)
#endif
}

void MissionController::_dirtyChanged(bool dirty)
{
    if (dirty && _autoSync) {
595
        Vehicle* activeVehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

        if (activeVehicle && !activeVehicle->armed()) {
            if (_activeVehicle->missionManager()->inProgress()) {
                _queuedSend = true;
            } else {
                _autoSyncSend();
            }
        }
    }
}

void MissionController::_autoSyncSend(void)
{
    qDebug() << "Auto-syncing with vehicle";
    _queuedSend = false;
    if (_missionItems) {
        sendMissionItems();
        _missionItems->setDirty(false);
614 615
    }
}
616

617
void MissionController::_inProgressChanged(bool inProgress)
618
{
619 620 621 622
    if (!inProgress && _queuedSend) {
        _autoSyncSend();
    }
}
623

624 625 626
QmlObjectListModel* MissionController::missionItems(void)
{
    return _missionItems;
627
}
628

629
bool MissionController::_findLastAltitude(double* lastAltitude)
630
{
631 632
    bool found = false;
    double foundAltitude;
633 634 635 636

    for (int i=0; i<_missionItems->count(); i++) {
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));

637
        if (item->specifiesCoordinate() && !item->standaloneCoordinate()) {
638 639
            foundAltitude = item->param7();
            found = true;
640 641 642
        }
    }

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    if (found) {
        *lastAltitude = foundAltitude;
    }

    return found;
}

bool MissionController::_findLastAcceptanceRadius(double* lastAcceptanceRadius)
{
    bool found = false;
    double foundAcceptanceRadius;

    for (int i=0; i<_missionItems->count(); i++) {
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));

        if ((MAV_CMD)item->command() == MAV_CMD_NAV_WAYPOINT) {
            foundAcceptanceRadius = item->param2();
            found = true;
        }
    }

    if (found) {
        *lastAcceptanceRadius = foundAcceptanceRadius;
    }

    return found;
669
}