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

#include <QStringList>
#include <QDebug>

#include "VisualMissionItem.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
#include "JsonHelper.h"
17
#include "TerrainQuery.h"
18
#include "TakeoffMissionItem.h"
19
#include "PlanMasterController.h"
20
#include "QGC.h"
21

Don Gagne's avatar
Don Gagne committed
22 23 24 25
const char* VisualMissionItem::jsonTypeKey =                "type";
const char* VisualMissionItem::jsonTypeSimpleItemValue =    "SimpleItem";
const char* VisualMissionItem::jsonTypeComplexItemValue =   "ComplexItem";

26 27 28 29
VisualMissionItem::VisualMissionItem(PlanMasterController* masterController, bool flyView, QObject* parent)
    : QObject           (parent)
    , _flyView          (flyView)
    , _masterController (masterController)
30
    , _missionController(masterController->missionController())
31
    , _controllerVehicle(masterController->controllerVehicle())
32
{
33
    _commonInit();
34 35
}

36
VisualMissionItem::VisualMissionItem(const VisualMissionItem& other, bool flyView, QObject* parent)
37
    : QObject                   (parent)
38
    , _flyView                  (flyView)
39 40
{
    *this = other;
41

42 43 44 45 46
    _commonInit();
}

void VisualMissionItem::_commonInit(void)
{
47
    // Don't get terrain altitude information for submarines or boats
48 49
    Vehicle* controllerVehicle = _masterController->controllerVehicle();
    if (controllerVehicle->vehicleType() != MAV_TYPE_SUBMARINE && controllerVehicle->vehicleType() != MAV_TYPE_SURFACE_BOAT) {
50 51 52 53
        _updateTerrainTimer.setInterval(500);
        _updateTerrainTimer.setSingleShot(true);
        connect(&_updateTerrainTimer, &QTimer::timeout, this, &VisualMissionItem::_reallyUpdateTerrainAltitude);

54 55
        connect(this, &VisualMissionItem::coordinateChanged, this, &VisualMissionItem::_updateTerrainAltitude);
    }
56 57 58 59
}

const VisualMissionItem& VisualMissionItem::operator=(const VisualMissionItem& other)
{
60 61
    _masterController = other._masterController;
    _controllerVehicle = other._controllerVehicle;
62 63 64

    setIsCurrentItem(other._isCurrentItem);
    setDirty(other._dirty);
65
    _homePositionSpecialCase = other._homePositionSpecialCase;
66
    _terrainAltitude = other._terrainAltitude;
67 68
    setAltDifference(other._altDifference);
    setAltPercent(other._altPercent);
69
    setTerrainPercent(other._terrainPercent);
70 71
    setAzimuth(other._azimuth);
    setDistance(other._distance);
72
    setDistanceFromStart(other._distance);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    return *this;
}

VisualMissionItem::~VisualMissionItem()
{    
}

void VisualMissionItem::setIsCurrentItem(bool isCurrentItem)
{
    if (_isCurrentItem != isCurrentItem) {
        _isCurrentItem = isCurrentItem;
        emit isCurrentItemChanged(isCurrentItem);
    }
}

89 90 91 92 93 94 95 96
void VisualMissionItem::setHasCurrentChildItem(bool hasCurrentChildItem)
{
    if (_hasCurrentChildItem != hasCurrentChildItem) {
        _hasCurrentChildItem = hasCurrentChildItem;
        emit hasCurrentChildItemChanged(hasCurrentChildItem);
    }
}

97 98
void VisualMissionItem::setDistance(double distance)
{
99
    if (!QGC::fuzzyCompare(_distance, distance)) {
100 101 102
        _distance = distance;
        emit distanceChanged(_distance);
    }
103 104
}

105 106
void VisualMissionItem::setDistanceFromStart(double distanceFromStart)
{
107
    if (!QGC::fuzzyCompare(_distanceFromStart, distanceFromStart)) {
108 109 110 111 112
        _distanceFromStart = distanceFromStart;
        emit distanceFromStartChanged(_distanceFromStart);
    }
}

113 114
void VisualMissionItem::setAltDifference(double altDifference)
{
115
    if (!QGC::fuzzyCompare(_altDifference, altDifference)) {
116 117 118
        _altDifference = altDifference;
        emit altDifferenceChanged(_altDifference);
    }
119 120 121 122
}

void VisualMissionItem::setAltPercent(double altPercent)
{
123
    if (!QGC::fuzzyCompare(_altPercent, altPercent)) {
124 125 126
        _altPercent = altPercent;
        emit altPercentChanged(_altPercent);
    }
127 128
}

129 130
void VisualMissionItem::setTerrainPercent(double terrainPercent)
{
131
    if (!QGC::fuzzyCompare(_terrainPercent, terrainPercent)) {
132 133 134 135 136
        _terrainPercent = terrainPercent;
        emit terrainPercentChanged(terrainPercent);
    }
}

137 138 139 140 141 142 143 144
void VisualMissionItem::setTerrainCollision(bool terrainCollision)
{
    if (terrainCollision != _terrainCollision) {
        _terrainCollision = terrainCollision;
        emit terrainCollisionChanged(terrainCollision);
    }
}

145 146
void VisualMissionItem::setAzimuth(double azimuth)
{
147
    if (!QGC::fuzzyCompare(_azimuth, azimuth)) {
148 149 150
        _azimuth = azimuth;
        emit azimuthChanged(_azimuth);
    }
151
}
152

Valentin Platzgummer's avatar
Valentin Platzgummer committed
153
void VisualMissionItem::setMissionFlightStatus(const MissionController::MissionFlightStatus_t& missionFlightStatus)
DonLakeFlyer's avatar
DonLakeFlyer committed
154
{
155
    if (!QGC::fuzzyCompare(missionFlightStatus.gimbalYaw, _missionGimbalYaw)) {
156
        _missionGimbalYaw = missionFlightStatus.gimbalYaw;
DonLakeFlyer's avatar
DonLakeFlyer committed
157 158
        emit missionGimbalYawChanged(_missionGimbalYaw);
    }
159 160 161 162
    if (missionFlightStatus.vtolMode != _previousVTOLMode) {
        _previousVTOLMode = missionFlightStatus.vtolMode;
        emit previousVTOLModeChanged();
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
163 164 165 166
}

void VisualMissionItem::setMissionVehicleYaw(double vehicleYaw)
{
167
    if (!QGC::fuzzyCompare(_missionVehicleYaw, vehicleYaw)) {
DonLakeFlyer's avatar
DonLakeFlyer committed
168 169 170 171
        _missionVehicleYaw = vehicleYaw;
        emit missionVehicleYawChanged(_missionVehicleYaw);
    }
}
172 173 174

void VisualMissionItem::_updateTerrainAltitude(void)
{
175 176 177 178
    if (coordinate().latitude() == 0 && coordinate().longitude() == 0) {
        // This is an intermediate state we don't react to
        return;
    }
179 180 181 182

    _terrainAltitude = qQNaN();
    emit terrainAltitudeChanged(qQNaN());

183
    if (!_flyView && specifiesCoordinate() && coordinate().isValid()) {
Alexey's avatar
Alexey committed
184 185
        // We use a timer so that any additional requests before the timer fires result in only a single request
        _updateTerrainTimer.start();
186 187 188 189 190 191
    }
}

void VisualMissionItem::_reallyUpdateTerrainAltitude(void)
{
    QGeoCoordinate coord = coordinate();
192
    if (specifiesCoordinate() && coord.isValid() && (qIsNaN(_terrainAltitude) || !QGC::fuzzyCompare(_lastLatTerrainQuery, coord.latitude()) || QGC::fuzzyCompare(_lastLonTerrainQuery, coord.longitude()))) {
193 194
        _lastLatTerrainQuery = coord.latitude();
        _lastLonTerrainQuery = coord.longitude();
195 196 197 198 199 200
        if (_currentTerrainAtCoordinateQuery) {
            disconnect(_currentTerrainAtCoordinateQuery, &TerrainAtCoordinateQuery::terrainDataReceived, this, &VisualMissionItem::_terrainDataReceived);
            _currentTerrainAtCoordinateQuery = nullptr;
        }
        _currentTerrainAtCoordinateQuery = new TerrainAtCoordinateQuery(true /* autoDelet */);
        connect(_currentTerrainAtCoordinateQuery, &TerrainAtCoordinateQuery::terrainDataReceived, this, &VisualMissionItem::_terrainDataReceived);
201 202
        QList<QGeoCoordinate> rgCoord;
        rgCoord.append(coordinate());
203
        _currentTerrainAtCoordinateQuery->requestData(rgCoord);
204 205 206
    }
}

207
void VisualMissionItem::_terrainDataReceived(bool success, QList<double> heights)
208
{
Don Gagne's avatar
Don Gagne committed
209 210
    _terrainAltitude = success ? heights[0] : qQNaN();
    emit terrainAltitudeChanged(_terrainAltitude);
211
    _currentTerrainAtCoordinateQuery = nullptr;
212
}
213 214 215 216 217 218 219 220 221

void VisualMissionItem::_setBoundingCube(QGCGeoBoundingCube bc)
{
    if (bc != _boundingCube) {
        _boundingCube = bc;
        emit boundingCubeChanged();
    }
}

222 223 224 225 226 227 228
void VisualMissionItem::setWizardMode(bool wizardMode)
{
    if (wizardMode != _wizardMode) {
        _wizardMode = wizardMode;
        emit wizardModeChanged(_wizardMode);
    }
}
229 230 231 232 233 234 235 236

void VisualMissionItem::setParentItem(VisualMissionItem* parentItem)
{
    if (_parentItem != parentItem) {
        _parentItem = parentItem;
        emit parentItemChanged(parentItem);
    }
}
237 238 239 240 241 242 243 244 245 246

void VisualMissionItem::_amslEntryAltChanged(void)
{
    emit amslEntryAltChanged(amslEntryAlt());
}

void VisualMissionItem::_amslExitAltChanged(void)
{
    emit amslExitAltChanged(amslExitAlt());
}