MissionController.h 22 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

10
#pragma once
11

12
#include "PlanElementController.h"
13 14
#include "QmlObjectListModel.h"
#include "Vehicle.h"
15 16
#include "QGCLoggingCategory.h"

17
#include "QGCGeoBoundingCube.h"
18

19 20
#include <QHash>

21
class CoordinateVector;
DonLakeFlyer's avatar
DonLakeFlyer committed
22
class VisualMissionItem;
23
class MissionItem;
24
class MissionSettingsItem;
25
class AppSettings;
26
class MissionManager;
27
class SimpleMissionItem;
28
class ComplexMissionItem;
29
class MissionSettingsItem;
30
class QDomDocument;
31

32
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
33

34 35
typedef QPair<VisualMissionItem*,VisualMissionItem*> VisualItemPair;
typedef QHash<VisualItemPair, CoordinateVector*> CoordVectHashTable;
36

37
class MissionController : public PlanElementController
38 39
{
    Q_OBJECT
40

41
public:
42
    MissionController(PlanMasterController* masterController, QObject* parent = nullptr);
43 44
    ~MissionController();

45
    typedef struct _MissionFlightStatus_t {
46 47 48 49 50 51 52 53 54 55
        double  maxTelemetryDistance;
        double  totalDistance;
        double  totalTime;
        double  hoverDistance;
        double  hoverTime;
        double  cruiseDistance;
        double  cruiseTime;
        double  cruiseSpeed;
        double  hoverSpeed;
        double  vehicleSpeed;           ///< Either cruise or hover speed based on vehicle type and vtol state
56
        double  vehicleYaw;
57
        double  gimbalYaw;              ///< NaN signals yaw was never changed
58
        double  gimbalPitch;            ///< NaN signals pitch was never changed
59
        int     mAhBattery;             ///< 0 for not available
Don Gagne's avatar
Don Gagne committed
60 61
        double  hoverAmps;              ///< Amp consumption during hover
        double  cruiseAmps;             ///< Amp consumption during cruise
62 63 64 65 66
        double  ampMinutesAvailable;    ///< Amp minutes available from single battery
        double  hoverAmpsTotal;         ///< Total hover amps used
        double  cruiseAmpsTotal;        ///< Total cruise amps used
        int     batteryChangePoint;     ///< -1 for not supported, 0 for not needed
        int     batteriesRequired;      ///< -1 for not supported
DonLakeFlyer's avatar
DonLakeFlyer committed
67 68
    } MissionFlightStatus_t;

69 70 71 72 73 74 75 76 77 78 79
    Q_PROPERTY(QmlObjectListModel*  visualItems                     READ visualItems                    NOTIFY visualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  waypointLines                   READ waypointLines                  CONSTANT)                           ///< Used by Plan view only for interactive editing
    Q_PROPERTY(QVariantList         waypointPath                    READ waypointPath                   NOTIFY waypointPathChanged)         ///< Used by Fly view only for static display
    Q_PROPERTY(QmlObjectListModel*  directionArrows                 READ directionArrows                CONSTANT)
    Q_PROPERTY(QStringList          complexMissionItemNames         READ complexMissionItemNames        NOTIFY complexMissionItemNamesChanged)
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition             READ plannedHomePosition            NOTIFY plannedHomePositionChanged)
    Q_PROPERTY(CoordinateVector*    splitSegment                    MEMBER _splitSegment                NOTIFY splitSegmentChanged)         ///< Segment which show show + split ui element
    Q_PROPERTY(double               progressPct                     READ progressPct                    NOTIFY progressPctChanged)
    Q_PROPERTY(int                  missionItemCount                READ missionItemCount               NOTIFY missionItemCountChanged)     ///< True mission item command count (only valid in Fly View)
    Q_PROPERTY(int                  currentMissionIndex             READ currentMissionIndex            NOTIFY currentMissionIndexChanged)
    Q_PROPERTY(int                  resumeMissionIndex              READ resumeMissionIndex             NOTIFY resumeMissionIndexChanged)   ///< Returns the item index two which a mission should be resumed. -1 indicates resume mission not available.
80 81
    Q_PROPERTY(int                  currentPlanViewSeqNum           READ currentPlanViewSeqNum          NOTIFY currentPlanViewSeqNumChanged)
    Q_PROPERTY(int                  currentPlanViewVIIndex          READ currentPlanViewVIIndex         NOTIFY currentPlanViewVIIndexChanged)
82 83 84 85 86 87 88 89 90 91 92
    Q_PROPERTY(VisualMissionItem*   currentPlanViewItem             READ currentPlanViewItem            NOTIFY currentPlanViewItemChanged)
    Q_PROPERTY(double               missionDistance                 READ missionDistance                NOTIFY missionDistanceChanged)
    Q_PROPERTY(double               missionTime                     READ missionTime                    NOTIFY missionTimeChanged)
    Q_PROPERTY(double               missionHoverDistance            READ missionHoverDistance           NOTIFY missionHoverDistanceChanged)
    Q_PROPERTY(double               missionCruiseDistance           READ missionCruiseDistance          NOTIFY missionCruiseDistanceChanged)
    Q_PROPERTY(double               missionHoverTime                READ missionHoverTime               NOTIFY missionHoverTimeChanged)
    Q_PROPERTY(double               missionCruiseTime               READ missionCruiseTime              NOTIFY missionCruiseTimeChanged)
    Q_PROPERTY(double               missionMaxTelemetry             READ missionMaxTelemetry            NOTIFY missionMaxTelemetryChanged)
    Q_PROPERTY(int                  batteryChangePoint              READ batteryChangePoint             NOTIFY batteryChangePointChanged)
    Q_PROPERTY(int                  batteriesRequired               READ batteriesRequired              NOTIFY batteriesRequiredChanged)
    Q_PROPERTY(QGCGeoBoundingCube*  travelBoundingCube              READ travelBoundingCube             NOTIFY missionBoundingCubeChanged)
93 94 95
    Q_PROPERTY(QString              surveyComplexItemName           READ surveyComplexItemName          CONSTANT)
    Q_PROPERTY(QString              corridorScanComplexItemName     READ corridorScanComplexItemName    CONSTANT)
    Q_PROPERTY(QString              structureScanComplexItemName    READ structureScanComplexItemName   CONSTANT)
96 97
    Q_PROPERTY(bool                 isInsertTakeoffValid            MEMBER _isInsertTakeoffValid        NOTIFY isInsertTakeoffValidChanged)
    Q_PROPERTY(bool                 isInsertLandValid               MEMBER _isInsertLandValid           NOTIFY isInsertLandValidChanged)
98
    Q_PROPERTY(bool                 isROIActive                     MEMBER _isROIActive                 NOTIFY isROIActiveChanged)
99
    Q_PROPERTY(bool                 isROIBeginCurrentItem           MEMBER _isROIBeginCurrentItem       NOTIFY isROIBeginCurrentItemChanged)
100
    Q_PROPERTY(bool                 flyThroughCommandsAllowed       MEMBER _flyThroughCommandsAllowed   NOTIFY flyThroughCommandsAllowedChanged)
101

102
    Q_INVOKABLE void removeMissionItem(int viIndex);
103

104
    /// Add a new simple mission item to the list
105 106 107 108 109
    ///     @param coordinate: Coordinate for item
    ///     @param visualItemIndex: index to insert at, -1 for end of list
    ///     @param makeCurrentItem: true: Make this item the current item
    /// @return Newly created item
    Q_INVOKABLE VisualMissionItem* insertSimpleMissionItem(QGeoCoordinate coordinate, int visualItemIndex, bool makeCurrentItem = false);
110

111 112 113 114 115 116 117
    /// Add a new takeoff item to the list
    ///     @param coordinate: Coordinate for item
    ///     @param visualItemIndex: index to insert at, -1 for end of list
    ///     @param makeCurrentItem: true: Make this item the current item
    /// @return Newly created item
    Q_INVOKABLE VisualMissionItem* insertTakeoffItem(QGeoCoordinate coordinate, int visualItemIndex, bool makeCurrentItem = false);

118 119 120 121 122 123 124
    /// Add a new land item to the list
    ///     @param coordinate: Coordinate for item
    ///     @param visualItemIndex: index to insert at, -1 for end of list
    ///     @param makeCurrentItem: true: Make this item the current item
    /// @return Newly created item
    Q_INVOKABLE VisualMissionItem* insertLandItem(QGeoCoordinate coordinate, int visualItemIndex, bool makeCurrentItem = false);

125
    /// Add a new ROI mission item to the list
126 127 128 129 130
    ///     @param coordinate: Coordinate for item
    ///     @param visualItemIndex: index to insert at, -1 for end of list
    ///     @param makeCurrentItem: true: Make this item the current item
    /// @return Newly created item
    Q_INVOKABLE VisualMissionItem*  insertROIMissionItem(QGeoCoordinate coordinate, int visualItemIndex, bool makeCurrentItem = false);
131

132 133 134 135 136 137
    /// Add a new Cancel ROI mission item to the list
    ///     @param visualItemIndex: index to insert at, -1 for end of list
    ///     @param makeCurrentItem: true: Make this item the current item
    /// @return Newly created item
    Q_INVOKABLE VisualMissionItem*  insertCancelROIMissionItem(int visualItemIndex, bool makeCurrentItem = false);

138
    /// Add a new complex mission item to the list
139 140
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
    ///     @param mapCenterCoordinate: coordinate for current center of map
141 142 143 144
    ///     @param visualItemIndex: index to insert at, -1 for end of list
    ///     @param makeCurrentItem: true: Make this item the current item
    /// @return Newly created item
    Q_INVOKABLE VisualMissionItem*  insertComplexMissionItem(QString itemName, QGeoCoordinate mapCenterCoordinate, int visualItemIndex, bool makeCurrentItem = false);
145

146 147
    /// Add a new complex mission item to the list
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
148
    ///     @param file: kml or shp file to load from shape from
149 150 151 152 153
    ///     @param coordinate: Coordinate for item
    ///     @param visualItemIndex: index to insert at, -1 for end of list
    ///     @param makeCurrentItem: true: Make this item the current item
    /// @return Newly created item
    Q_INVOKABLE VisualMissionItem*  insertComplexMissionItemFromKMLOrSHP(QString itemName, QString file, int visualItemIndex, bool makeCurrentItem = false);
154

155 156
    Q_INVOKABLE void resumeMission(int resumeIndex);

DonLakeFlyer's avatar
DonLakeFlyer committed
157 158 159
    /// Updates the altitudes of the items in the current mission to the new default altitude
    Q_INVOKABLE void applyDefaultMissionAltitude(void);

160 161
    /// Sets a new current mission item (PlanView).
    ///     @param sequenceNumber - index for new item, -1 to clear current item
162
    Q_INVOKABLE void setCurrentPlanViewSeqNum(int sequenceNumber, bool force);
Don Gagne's avatar
Don Gagne committed
163

164 165 166 167
    /// Determines if the mission has all data needed to be saved or sent to the vehicle.
    /// IMPORTANT NOTE: The return value is a VisualMissionItem::ReadForSaveState value. It is an int here to work around
    /// a nightmare of circular header dependency problems.
    int readyForSaveState(void) const;
168

Don Gagne's avatar
Don Gagne committed
169 170 171
    /// Sends the mission items to the specified vehicle
    static void sendItemsToVehicle(Vehicle* vehicle, QmlObjectListModel* visualMissionItems);

172 173 174
    bool loadJsonFile(QFile& file, QString& errorString);
    bool loadTextFile(QFile& file, QString& errorString);

175
    QGCGeoBoundingCube* travelBoundingCube  () { return &_travelBoundingCube; }
176 177
    QGeoCoordinate      takeoffCoordinate   () { return _takeoffCoordinate; }

178
    // Overrides from PlanElementController
179
    bool supported                  (void) const final { return true; }
180
    void start                      (bool flyView) final;
181 182
    void save                       (QJsonObject& json) final;
    bool load                       (const QJsonObject& json, QString& errorString) final;
183 184 185
    void loadFromVehicle            (void) final;
    void sendToVehicle              (void) final;
    void removeAll                  (void) final;
186
    void removeAllFromVehicle       (void) final;
187 188 189
    bool syncInProgress             (void) const final;
    bool dirty                      (void) const final;
    void setDirty                   (bool dirty) final;
190
    bool containsItems              (void) const final;
191
    void managerVehicleChanged      (Vehicle* managerVehicle) final;
DonLakeFlyer's avatar
DonLakeFlyer committed
192
    bool showPlanFromManagerVehicle (void) final;
193

194 195 196
    // Create KML file
    void convertToKMLDocument(QDomDocument& document);

197 198
    // Property accessors

199 200
    QmlObjectListModel* visualItems                 (void) { return _visualItems; }
    QmlObjectListModel* waypointLines               (void) { return &_waypointLines; }
201
    QmlObjectListModel* directionArrows             (void) { return &_directionArrows; }
202 203 204
    QVariantList        waypointPath                (void) { return _waypointPath; }
    QStringList         complexMissionItemNames     (void) const;
    QGeoCoordinate      plannedHomePosition         (void) const;
205
    VisualMissionItem*  currentPlanViewItem         (void) const { return _currentPlanViewItem; }
206
    double              progressPct                 (void) const { return _progressPct; }
DonLakeFlyer's avatar
DonLakeFlyer committed
207
    QString             surveyComplexItemName       (void) const { return patternSurveyName; }
208 209
    QString             corridorScanComplexItemName (void) const { return patternCorridorScanName; }
    QString             structureScanComplexItemName(void) const { return patternStructureScanName; }
210
    bool                isInsertTakeoffValid        (void) const;
211

212
    int missionItemCount            (void) const { return _missionItemCount; }
213 214
    int currentMissionIndex         (void) const;
    int resumeMissionIndex          (void) const;
215 216
    int currentPlanViewSeqNum       (void) const { return _currentPlanViewSeqNum; }
    int currentPlanViewVIIndex      (void) const { return _currentPlanViewVIIndex; }
217

DonLakeFlyer's avatar
DonLakeFlyer committed
218 219 220 221 222 223 224
    double  missionDistance         (void) const { return _missionFlightStatus.totalDistance; }
    double  missionTime             (void) const { return _missionFlightStatus.totalTime; }
    double  missionHoverDistance    (void) const { return _missionFlightStatus.hoverDistance; }
    double  missionHoverTime        (void) const { return _missionFlightStatus.hoverTime; }
    double  missionCruiseDistance   (void) const { return _missionFlightStatus.cruiseDistance; }
    double  missionCruiseTime       (void) const { return _missionFlightStatus.cruiseTime; }
    double  missionMaxTelemetry     (void) const { return _missionFlightStatus.maxTelemetryDistance; }
225

226 227 228
    int  batteryChangePoint         (void) const { return _missionFlightStatus.batteryChangePoint; }    ///< -1 for not supported, 0 for not needed
    int  batteriesRequired          (void) const { return _missionFlightStatus.batteriesRequired; }     ///< -1 for not supported

229 230
    bool isEmpty                    (void) const;

231 232
    // These are the names shown in the UI for the pattern items. They are public so custom builds can remove the ones
    // they don't want through the QGCCorePlugin::
233 234 235
    static const QString patternFWLandingName;
    static const QString patternStructureScanName;
    static const QString patternCorridorScanName;
DonLakeFlyer's avatar
DonLakeFlyer committed
236
    static const QString patternSurveyName;
237

238
signals:
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    void visualItemsChanged                 (void);
    void waypointPathChanged                (void);
    void splitSegmentChanged                (void);
    void newItemsFromVehicle                (void);
    void missionDistanceChanged             (double missionDistance);
    void missionTimeChanged                 (void);
    void missionHoverDistanceChanged        (double missionHoverDistance);
    void missionHoverTimeChanged            (void);
    void missionCruiseDistanceChanged       (double missionCruiseDistance);
    void missionCruiseTimeChanged           (void);
    void missionMaxTelemetryChanged         (double missionMaxTelemetry);
    void complexMissionItemNamesChanged     (void);
    void resumeMissionIndexChanged          (void);
    void resumeMissionReady                 (void);
    void resumeMissionUploadFail            (void);
    void batteryChangePointChanged          (int batteryChangePoint);
    void batteriesRequiredChanged           (int batteriesRequired);
    void plannedHomePositionChanged         (QGeoCoordinate plannedHomePosition);
    void progressPctChanged                 (double progressPct);
    void currentMissionIndexChanged         (int currentMissionIndex);
259 260
    void currentPlanViewSeqNumChanged       (void);
    void currentPlanViewVIIndexChanged      (void);
261 262 263 264 265
    void currentPlanViewItemChanged         (void);
    void missionBoundingCubeChanged         (void);
    void missionItemCountChanged            (int missionItemCount);
    void isInsertTakeoffValidChanged        (void);
    void isInsertLandValidChanged           (void);
266
    void isROIActiveChanged                 (void);
267
    void isROIBeginCurrentItemChanged       (void);
268
    void flyThroughCommandsAllowedChanged   (void);
269

270
private slots:
271
    void _newMissionItemsAvailableFromVehicle(bool removeAllRequested);
272
    void _itemCommandChanged(void);
273
    void _inProgressChanged(bool inProgress);
274
    void _currentMissionIndexChanged(int sequenceNumber);
275
    void _recalcWaypointLines(void);
DonLakeFlyer's avatar
DonLakeFlyer committed
276
    void _recalcMissionFlightStatus(void);
277
    void _updateContainsItems(void);
278
    void _progressPctChanged(double progressPct);
279
    void _visualItemsDirtyChanged(bool dirty);
280 281
    void _managerSendComplete(bool error);
    void _managerRemoveAllComplete(bool error);
282 283
    void _updateTimeout();
    void _complexBoundingBoxChanged();
284
    void _recalcAll(void);
285 286

private:
287
    void _init(void);
288
    void _recalcSequence(void);
289
    void _recalcChildItems(void);
290
    void _recalcAllWithCoordinate(const QGeoCoordinate& coordinate);
291
    void _recalcROISpecialVisuals(void);
292 293 294 295
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
296
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
297
    void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
298
    static double _calcDistanceToHome(VisualMissionItem* currentItem, VisualMissionItem* homeItem);
299
    bool _findPreviousAltitude(int newIndex, double* prevAltitude, int* prevAltitudeMode);
Don Gagne's avatar
Don Gagne committed
300 301
    static double _normalizeLat(double lat);
    static double _normalizeLon(double lon);
302
    MissionSettingsItem* _addMissionSettings(QmlObjectListModel* visualItems);
303
    void _centerHomePositionOnMissionItems(QmlObjectListModel* visualItems);
304 305 306 307
    bool _loadJsonMissionFile(const QByteArray& bytes, QmlObjectListModel* visualItems, QString& errorString);
    bool _loadJsonMissionFileV1(const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
    bool _loadJsonMissionFileV2(const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
    bool _loadTextMissionFile(QTextStream& stream, QmlObjectListModel* visualItems, QString& errorString);
308
    int _nextSequenceNumber(void);
309
    void _scanForAdditionalSettings(QmlObjectListModel* visualItems, Vehicle* vehicle);
310
    static bool _convertToMissionItems(QmlObjectListModel* visualMissionItems, QList<MissionItem*>& rgMissionItems, QObject* missionItemParent);
311
    void _setPlannedHomePositionFromFirstCoordinate(const QGeoCoordinate& clickCoordinate);
312 313 314 315
    void _resetMissionFlightStatus(void);
    void _addHoverTime(double hoverTime, double hoverDistance, int waypointIndex);
    void _addCruiseTime(double cruiseTime, double cruiseDistance, int wayPointIndex);
    void _updateBatteryInfo(int waypointIndex);
316
    bool _loadItemsFromJson(const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
317
    void _initLoadedVisualItems(QmlObjectListModel* loadedVisualItems);
318
    CoordinateVector* _addWaypointLineSegment(CoordVectHashTable& prevItemPairHashTable, VisualItemPair& pair);
319
    void _addTimeDistance(bool vtolInHover, double hoverTime, double cruiseTime, double extraTime, double distance, int seqNum);
320
    VisualMissionItem* _insertSimpleMissionItemWorker(QGeoCoordinate coordinate, MAV_CMD command, int visualItemIndex, bool makeCurrentItem);
321
    void _insertComplexMissionItemWorker(const QGeoCoordinate& mapCenterCoordinate, ComplexMissionItem* complexItem, int visualItemIndex, bool makeCurrentItem);
322
    void _warnIfTerrainFrameUsed(void);
323 324
    bool _isROIBeginItem(SimpleMissionItem* simpleItem);
    bool _isROICancelItem(SimpleMissionItem* simpleItem);
325

326
private:
327
    MissionManager*         _missionManager;
328
    int                     _missionItemCount;
DonLakeFlyer's avatar
DonLakeFlyer committed
329
    QmlObjectListModel*     _visualItems;
330
    MissionSettingsItem*    _settingsItem;
DonLakeFlyer's avatar
DonLakeFlyer committed
331
    QmlObjectListModel      _waypointLines;
332
    QVariantList            _waypointPath;
333
    QmlObjectListModel      _directionArrows;
DonLakeFlyer's avatar
DonLakeFlyer committed
334 335
    CoordVectHashTable      _linesTable;
    bool                    _firstItemsFromVehicle;
DonLakeFlyer's avatar
DonLakeFlyer committed
336
    bool                    _itemsRequested;
337
    bool                    _inRecalcSequence;
DonLakeFlyer's avatar
DonLakeFlyer committed
338
    MissionFlightStatus_t   _missionFlightStatus;
339
    AppSettings*            _appSettings;
340
    double                  _progressPct;
341 342
    int                     _currentPlanViewSeqNum;
    int                     _currentPlanViewVIIndex;
343
    VisualMissionItem*      _currentPlanViewItem;
344
    QTimer                  _updateTimer;
345
    QGCGeoBoundingCube      _travelBoundingCube;
346
    QGeoCoordinate          _takeoffCoordinate;
347
    CoordinateVector*       _splitSegment;
348 349
    bool                    _isInsertTakeoffValid =         true;
    bool                    _isInsertLandValid =            true;
350
    bool                    _isROIActive =                  false;
351
    bool                    _flyThroughCommandsAllowed =    true;
352
    bool                    _isROIBeginCurrentItem =        false;
353

354
    static const char*  _settingsGroup;
355 356

    // Json file keys for persistence
Don Gagne's avatar
Don Gagne committed
357 358
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
359 360 361
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
362 363 364 365 366
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
367
    static const char*  _jsonMavAutopilotKey;
368
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
369 370

    static const int    _missionFileVersion;
371
};