MissionController.h 17.1 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 11 12 13


#ifndef MissionController_H
#define MissionController_H

14
#include "PlanElementController.h"
15 16
#include "QmlObjectListModel.h"
#include "Vehicle.h"
17 18
#include "QGCLoggingCategory.h"

19
#include "QGCGeoBoundingCube.h"
20

21 22
#include <QHash>

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

33
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
34

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

38
class MissionController : public PlanElementController
39 40
{
    Q_OBJECT
41

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

46
    typedef struct _MissionFlightStatus_t {
47 48 49 50 51 52 53 54 55 56
        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
57
        double  vehicleYaw;
58
        double  gimbalYaw;              ///< NaN signals yaw was never changed
59
        double  gimbalPitch;            ///< NaN signals pitch was never changed
60
        int     mAhBattery;             ///< 0 for not available
Don Gagne's avatar
Don Gagne committed
61 62
        double  hoverAmps;              ///< Amp consumption during hover
        double  cruiseAmps;             ///< Amp consumption during cruise
63 64 65 66 67
        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
68 69
    } MissionFlightStatus_t;

70
    Q_PROPERTY(QmlObjectListModel*  visualItems             READ visualItems                NOTIFY visualItemsChanged)
71 72
    Q_PROPERTY(QmlObjectListModel*  waypointLines           READ waypointLines              NOTIFY waypointLinesChanged)        ///< Used by Plan view only for interactive editing
    Q_PROPERTY(QVariantList         waypointPath            READ waypointPath               NOTIFY waypointPathChanged)         ///< Used by Fly view only for static display
73
    Q_PROPERTY(QStringList          complexMissionItemNames READ complexMissionItemNames    NOTIFY complexMissionItemNamesChanged)
74
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition     READ plannedHomePosition        NOTIFY plannedHomePositionChanged)
75

76 77
    Q_PROPERTY(double               progressPct             READ progressPct                NOTIFY progressPctChanged)

78
    Q_PROPERTY(int                  missionItemCount        READ missionItemCount           NOTIFY missionItemCountChanged)     ///< True mission item command count (only valid in Fly View)
79 80
    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.
81

82 83 84
    Q_PROPERTY(int                  currentPlanViewIndex    READ currentPlanViewIndex       NOTIFY currentPlanViewIndexChanged)
    Q_PROPERTY(VisualMissionItem*   currentPlanViewItem     READ currentPlanViewItem        NOTIFY currentPlanViewItemChanged)

85 86 87 88 89 90 91
    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)
92

93 94
    Q_PROPERTY(int                  batteryChangePoint      READ batteryChangePoint         NOTIFY batteryChangePointChanged)
    Q_PROPERTY(int                  batteriesRequired       READ batteriesRequired          NOTIFY batteriesRequiredChanged)
95
    Q_PROPERTY(QGCGeoBoundingCube*  travelBoundingCube      READ travelBoundingCube         NOTIFY missionBoundingCubeChanged)
96

97 98 99 100
    Q_PROPERTY(QString              surveyComplexItemName           READ surveyComplexItemName          CONSTANT)
    Q_PROPERTY(QString              corridorScanComplexItemName     READ corridorScanComplexItemName    CONSTANT)
    Q_PROPERTY(QString              structureScanComplexItemName    READ structureScanComplexItemName   CONSTANT)

101
    Q_INVOKABLE void removeMissionItem(int index);
102

103 104 105
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
106 107
    Q_INVOKABLE int insertSimpleMissionItem(QGeoCoordinate coordinate, int i);

108 109 110 111 112
    /// Add a new ROI mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
    Q_INVOKABLE int insertROIMissionItem(QGeoCoordinate coordinate, int i);

113
    /// Add a new complex mission item to the list
114 115
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
    ///     @param mapCenterCoordinate: coordinate for current center of map
116 117
    ///     @param i: index to insert at
    /// @return Sequence number for new item
118
    Q_INVOKABLE int insertComplexMissionItem(QString itemName, QGeoCoordinate mapCenterCoordinate, int i);
119

120 121
    /// Add a new complex mission item to the list
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
122
    ///     @param file: kml or shp file to load from shape from
123 124
    ///     @param i: index to insert at, -1 for end
    /// @return Sequence number for new item
125
    Q_INVOKABLE int insertComplexMissionItemFromKMLOrSHP(QString itemName, QString file, int i);
126

127 128
    Q_INVOKABLE void resumeMission(int resumeIndex);

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

132 133 134 135
    /// Sets a new current mission item (PlanView).
    ///     @param sequenceNumber - index for new item, -1 to clear current item
    Q_INVOKABLE void setCurrentPlanViewIndex(int sequenceNumber, bool force);

136 137 138 139
    /// Determines if the mission has all data needed to be saved or sent to the vehicle. Currently the only case where this
    /// would return false is when it is still waiting on terrain data to determine correct altitudes.
    bool readyForSaveSend(void) const;

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

143 144 145
    bool loadJsonFile(QFile& file, QString& errorString);
    bool loadTextFile(QFile& file, QString& errorString);

146
    QGCGeoBoundingCube* travelBoundingCube  () { return &_travelBoundingCube; }
147 148
    QGeoCoordinate      takeoffCoordinate   () { return _takeoffCoordinate; }

149
    // Overrides from PlanElementController
150
    bool supported                  (void) const final { return true; }
151
    void start                      (bool flyView) final;
152 153
    void save                       (QJsonObject& json) final;
    bool load                       (const QJsonObject& json, QString& errorString) final;
154 155 156
    void loadFromVehicle            (void) final;
    void sendToVehicle              (void) final;
    void removeAll                  (void) final;
157
    void removeAllFromVehicle       (void) final;
158 159 160
    bool syncInProgress             (void) const final;
    bool dirty                      (void) const final;
    void setDirty                   (bool dirty) final;
161
    bool containsItems              (void) const final;
162
    void managerVehicleChanged      (Vehicle* managerVehicle) final;
DonLakeFlyer's avatar
DonLakeFlyer committed
163
    bool showPlanFromManagerVehicle (void) final;
164

165 166 167
    // Create KML file
    void convertToKMLDocument(QDomDocument& document);

168 169
    // Property accessors

170 171 172 173 174 175 176 177
    QmlObjectListModel* visualItems                 (void) { return _visualItems; }
    QmlObjectListModel* waypointLines               (void) { return &_waypointLines; }
    QVariantList        waypointPath                (void) { return _waypointPath; }
    QStringList         complexMissionItemNames     (void) const;
    QGeoCoordinate      plannedHomePosition         (void) const;
    VisualMissionItem*  currentPlanViewItem         (void) const;
    double              progressPct                 (void) const { return _progressPct; }
    QString             surveyComplexItemName       (void) const { return _surveyMissionItemName; }
178 179
    QString             corridorScanComplexItemName (void) const { return patternCorridorScanName; }
    QString             structureScanComplexItemName(void) const { return patternStructureScanName; }
180

181
    int missionItemCount            (void) const { return _missionItemCount; }
182 183 184
    int currentMissionIndex         (void) const;
    int resumeMissionIndex          (void) const;
    int currentPlanViewIndex        (void) const;
185

DonLakeFlyer's avatar
DonLakeFlyer committed
186 187 188 189 190 191 192
    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; }
193

194 195 196
    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

197 198
    // 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::
199 200 201
    static const QString patternFWLandingName;
    static const QString patternStructureScanName;
    static const QString patternCorridorScanName;
202

203
signals:
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    void visualItemsChanged             (void);
    void waypointLinesChanged           (void);
    void waypointPathChanged            (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);
    void currentPlanViewIndexChanged    (void);
    void currentPlanViewItemChanged     (void);
226
    void missionBoundingCubeChanged     (void);
227
    void missionItemCountChanged        (int missionItemCount);
228

229
private slots:
230
    void _newMissionItemsAvailableFromVehicle(bool removeAllRequested);
231
    void _itemCommandChanged(void);
232
    void _managerVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
233
    void _inProgressChanged(bool inProgress);
234
    void _currentMissionIndexChanged(int sequenceNumber);
235
    void _recalcWaypointLines(void);
DonLakeFlyer's avatar
DonLakeFlyer committed
236
    void _recalcMissionFlightStatus(void);
237
    void _updateContainsItems(void);
238
    void _progressPctChanged(double progressPct);
239
    void _visualItemsDirtyChanged(bool dirty);
240 241
    void _managerSendComplete(bool error);
    void _managerRemoveAllComplete(bool error);
242 243
    void _updateTimeout();
    void _complexBoundingBoxChanged();
244
    void _recalcAll(void);
245 246

private:
247
    void _init(void);
248
    void _recalcSequence(void);
249
    void _recalcChildItems(void);
250
    void _recalcAllWithClickCoordinate(QGeoCoordinate& clickCoordinate);
251 252 253 254
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
255
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
256
    void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
257
    static double _calcDistanceToHome(VisualMissionItem* currentItem, VisualMissionItem* homeItem);
258
    bool _findPreviousAltitude(int newIndex, double* prevAltitude, int* prevAltitudeMode);
Don Gagne's avatar
Don Gagne committed
259 260
    static double _normalizeLat(double lat);
    static double _normalizeLon(double lon);
DonLakeFlyer's avatar
DonLakeFlyer committed
261
    void _addMissionSettings(QmlObjectListModel* visualItems, bool addToCenter);
262 263 264 265
    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);
266
    int _nextSequenceNumber(void);
267
    void _scanForAdditionalSettings(QmlObjectListModel* visualItems, Vehicle* vehicle);
268
    static bool _convertToMissionItems(QmlObjectListModel* visualMissionItems, QList<MissionItem*>& rgMissionItems, QObject* missionItemParent);
269
    void _setPlannedHomePositionFromFirstCoordinate(const QGeoCoordinate& clickCoordinate);
270 271 272 273
    void _resetMissionFlightStatus(void);
    void _addHoverTime(double hoverTime, double hoverDistance, int waypointIndex);
    void _addCruiseTime(double cruiseTime, double cruiseDistance, int wayPointIndex);
    void _updateBatteryInfo(int waypointIndex);
274
    bool _loadItemsFromJson(const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
275
    void _initLoadedVisualItems(QmlObjectListModel* loadedVisualItems);
276
    void _addWaypointLineSegment(CoordVectHashTable& prevItemPairHashTable, VisualItemPair& pair);
277
    void _addTimeDistance(bool vtolInHover, double hoverTime, double cruiseTime, double extraTime, double distance, int seqNum);
278
    int _insertComplexMissionItemWorker(ComplexMissionItem* complexItem, int i);
279
    void _warnIfTerrainFrameUsed(void);
280

281
private:
282
    MissionManager*         _missionManager;
283
    int                     _missionItemCount;
DonLakeFlyer's avatar
DonLakeFlyer committed
284
    QmlObjectListModel*     _visualItems;
285
    MissionSettingsItem*    _settingsItem;
DonLakeFlyer's avatar
DonLakeFlyer committed
286
    QmlObjectListModel      _waypointLines;
287
    QVariantList            _waypointPath;
DonLakeFlyer's avatar
DonLakeFlyer committed
288 289
    CoordVectHashTable      _linesTable;
    bool                    _firstItemsFromVehicle;
DonLakeFlyer's avatar
DonLakeFlyer committed
290
    bool                    _itemsRequested;
291
    bool                    _inRecalcSequence;
DonLakeFlyer's avatar
DonLakeFlyer committed
292 293
    MissionFlightStatus_t   _missionFlightStatus;
    QString                 _surveyMissionItemName;
294
    AppSettings*            _appSettings;
295
    double                  _progressPct;
296 297
    int                     _currentPlanViewIndex;
    VisualMissionItem*      _currentPlanViewItem;
298
    QTimer                  _updateTimer;
299
    QGCGeoBoundingCube      _travelBoundingCube;
300
    QGeoCoordinate          _takeoffCoordinate;
301

302
    static const char*  _settingsGroup;
303 304

    // Json file keys for persistence
Don Gagne's avatar
Don Gagne committed
305 306
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
307 308 309
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
310 311 312 313 314
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
315
    static const char*  _jsonMavAutopilotKey;
316
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
317 318

    static const int    _missionFileVersion;
319 320 321
};

#endif