MissionController.h 18 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 24


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

35
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
36

37 38
typedef QPair<VisualMissionItem*,VisualMissionItem*> VisualItemPair;
typedef QHash<VisualItemPair, CoordinateVector*> CoordVectHashTable;
39

40
class MissionController : public PlanElementController
41 42
{
    Q_OBJECT
43

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

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

73
    Q_PROPERTY(QmlObjectListModel*  visualItems             READ visualItems                NOTIFY visualItemsChanged)
74 75
    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
76
    Q_PROPERTY(QStringList          complexMissionItemNames READ complexMissionItemNames    NOTIFY complexMissionItemNamesChanged)
77
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition     READ plannedHomePosition        NOTIFY plannedHomePositionChanged)
78

79 80
    Q_PROPERTY(double               progressPct             READ progressPct                NOTIFY progressPctChanged)

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

85 86 87
    Q_PROPERTY(int                  currentPlanViewIndex    READ currentPlanViewIndex       NOTIFY currentPlanViewIndexChanged)
    Q_PROPERTY(VisualMissionItem*   currentPlanViewItem     READ currentPlanViewItem        NOTIFY currentPlanViewItemChanged)

88 89 90 91 92 93 94
    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)
95

96 97
    Q_PROPERTY(int                  batteryChangePoint      READ batteryChangePoint         NOTIFY batteryChangePointChanged)
    Q_PROPERTY(int                  batteriesRequired       READ batteriesRequired          NOTIFY batteriesRequiredChanged)
98
    Q_PROPERTY(QGCGeoBoundingCube*  travelBoundingCube      READ travelBoundingCube         NOTIFY missionBoundingCubeChanged)
99

100 101 102 103
    Q_PROPERTY(QString              surveyComplexItemName           READ surveyComplexItemName          CONSTANT)
    Q_PROPERTY(QString              corridorScanComplexItemName     READ corridorScanComplexItemName    CONSTANT)
    Q_PROPERTY(QString              structureScanComplexItemName    READ structureScanComplexItemName   CONSTANT)

Valentin Platzgummer's avatar
Valentin Platzgummer committed
104

105
    Q_INVOKABLE void removeMissionItem(int index);
106

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

112 113 114 115 116
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
                int insertSimpleMissionItem(const MissionItem &missionItem, int i);

117 118 119 120 121
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
                int insertSimpleMissionItem(SimpleMissionItem &missionItem, int i);

122 123 124 125 126
    /// 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);

127
    /// Add a new complex mission item to the list
128 129
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
    ///     @param mapCenterCoordinate: coordinate for current center of map
130 131
    ///     @param i: index to insert at
    /// @return Sequence number for new item
132
    Q_INVOKABLE int insertComplexMissionItem(QString itemName, QGeoCoordinate mapCenterCoordinate, int i);
133

134 135
    /// Add a new complex mission item to the list
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
136
    ///     @param file: kml or shp file to load from shape from
137 138
    ///     @param i: index to insert at, -1 for end
    /// @return Sequence number for new item
139
    Q_INVOKABLE int insertComplexMissionItemFromKMLOrSHP(QString itemName, QString file, int i);
140

141 142
    Q_INVOKABLE void resumeMission(int resumeIndex);

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

146 147 148 149
    /// 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);

150 151 152 153
    /// 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;

154
    /// sets the command in missionItem to a land command
155
    bool setLandCommand     (SimpleMissionItem &missionItem);
156
    /// sets the command in missionItem to a takeoff command
157
    bool setTakeoffCommand  (SimpleMissionItem &missionItem);
158

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

162 163 164
    static bool convertToMissionItems(QmlObjectListModel* visualMissionItems, QList<MissionItem*>& rgMissionItems, QObject* missionItemParent);


165

166 167 168
    bool loadJsonFile(QFile& file, QString& errorString);
    bool loadTextFile(QFile& file, QString& errorString);

169
    QGCGeoBoundingCube* travelBoundingCube  () { return &_travelBoundingCube; }
170 171
    QGeoCoordinate      takeoffCoordinate   () { return _takeoffCoordinate; }

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

188 189 190
    // Create KML file
    void convertToKMLDocument(QDomDocument& document);

191 192
    // Property accessors

193 194 195 196 197 198 199 200 201 202 203
    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; }
    QString             circularSurveyComplexItemName   (void) const { return _circularSurveyMissionItemName; }
    QString             corridorScanComplexItemName     (void) const { return patternCorridorScanName; }
    QString             structureScanComplexItemName    (void) const { return patternStructureScanName; }
204

205
    int missionItemCount            (void) const { return _missionItemCount; }
206 207 208
    int currentMissionIndex         (void) const;
    int resumeMissionIndex          (void) const;
    int currentPlanViewIndex        (void) const;
209

DonLakeFlyer's avatar
DonLakeFlyer committed
210 211 212 213 214 215 216
    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; }
217

218 219 220
    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

221 222
    // 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::
223 224 225
    static const QString patternFWLandingName;
    static const QString patternStructureScanName;
    static const QString patternCorridorScanName;
226

227
signals:
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    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);
250
    void missionBoundingCubeChanged     (void);
251
    void missionItemCountChanged        (int missionItemCount);
252

253
private slots:
254
    void _newMissionItemsAvailableFromVehicle(bool removeAllRequested);
255
    void _itemCommandChanged(void);
256
    void _managerVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
257
    void _inProgressChanged(bool inProgress);
258
    void _currentMissionIndexChanged(int sequenceNumber);
259
    void _recalcWaypointLines(void);
DonLakeFlyer's avatar
DonLakeFlyer committed
260
    void _recalcMissionFlightStatus(void);
261
    void _updateContainsItems(void);
262
    void _progressPctChanged(double progressPct);
263
    void _visualItemsDirtyChanged(bool dirty);
264 265
    void _managerSendComplete(bool error);
    void _managerRemoveAllComplete(bool error);
266 267
    void _updateTimeout();
    void _complexBoundingBoxChanged();
268
    void _recalcAll(void);
269 270

private:
271
    void _init(void);
272
    void _recalcSequence(void);
273
    void _recalcChildItems(void);
274
    void _recalcAllWithClickCoordinate(QGeoCoordinate& clickCoordinate);
275 276 277 278
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
279
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
280
    void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
281
    static double _calcDistanceToHome(VisualMissionItem* currentItem, VisualMissionItem* homeItem);
282
    bool _findPreviousAltitude(int newIndex, double* prevAltitude, int* prevAltitudeMode);
Don Gagne's avatar
Don Gagne committed
283 284
    static double _normalizeLat(double lat);
    static double _normalizeLon(double lon);
DonLakeFlyer's avatar
DonLakeFlyer committed
285
    void _addMissionSettings(QmlObjectListModel* visualItems, bool addToCenter);
286 287 288 289
    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);
290
    int _nextSequenceNumber(void);
291
    void _scanForAdditionalSettings(QmlObjectListModel* visualItems, Vehicle* vehicle);
292
    void _setPlannedHomePositionFromFirstCoordinate(const QGeoCoordinate& clickCoordinate);
293 294 295 296
    void _resetMissionFlightStatus(void);
    void _addHoverTime(double hoverTime, double hoverDistance, int waypointIndex);
    void _addCruiseTime(double cruiseTime, double cruiseDistance, int wayPointIndex);
    void _updateBatteryInfo(int waypointIndex);
297
    bool _loadItemsFromJson(const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
298
    void _initLoadedVisualItems(QmlObjectListModel* loadedVisualItems);
299
    void _addWaypointLineSegment(CoordVectHashTable& prevItemPairHashTable, VisualItemPair& pair);
300
    void _addTimeDistance(bool vtolInHover, double hoverTime, double cruiseTime, double extraTime, double distance, int seqNum);
301
    int _insertComplexMissionItemWorker(ComplexMissionItem* complexItem, int i);
302
    void _warnIfTerrainFrameUsed(void);
303

304
private:
305
    MissionManager*         _missionManager;
306
    int                     _missionItemCount;
DonLakeFlyer's avatar
DonLakeFlyer committed
307
    QmlObjectListModel*     _visualItems;
308
    MissionSettingsItem*    _settingsItem;
DonLakeFlyer's avatar
DonLakeFlyer committed
309
    QmlObjectListModel      _waypointLines;
310
    QVariantList            _waypointPath;
DonLakeFlyer's avatar
DonLakeFlyer committed
311 312
    CoordVectHashTable      _linesTable;
    bool                    _firstItemsFromVehicle;
DonLakeFlyer's avatar
DonLakeFlyer committed
313
    bool                    _itemsRequested;
314
    bool                    _inRecalcSequence;
DonLakeFlyer's avatar
DonLakeFlyer committed
315 316
    MissionFlightStatus_t   _missionFlightStatus;
    QString                 _surveyMissionItemName;
317
    QString                 _circularSurveyMissionItemName;
318
    AppSettings*            _appSettings;
319
    double                  _progressPct;
320 321
    int                     _currentPlanViewIndex;
    VisualMissionItem*      _currentPlanViewItem;
322
    QTimer                  _updateTimer;
323
    QGCGeoBoundingCube      _travelBoundingCube;
324
    QGeoCoordinate          _takeoffCoordinate;
325

326
    static const char*  _settingsGroup;
327 328

    // Json file keys for persistence
Don Gagne's avatar
Don Gagne committed
329 330
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
331 332 333
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
334 335 336 337 338
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
339
    static const char*  _jsonMavAutopilotKey;
340
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
341 342

    static const int    _missionFileVersion;
343 344 345
};

#endif