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)

104
    Q_INVOKABLE void removeMissionItem(int index);
105

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

111 112 113 114 115
    /// 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);

116 117 118 119 120
    /// 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);

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

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

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

140 141
    Q_INVOKABLE void resumeMission(int resumeIndex);

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

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

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

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

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

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


164

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

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

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

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

190 191
    // Property accessors

192 193 194 195 196 197 198 199 200 201 202
    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; }
203

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

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

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

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

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

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

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

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

325
    static const char*  _settingsGroup;
326 327

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

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

    static const int    _missionFileVersion;
342 343 344
};

#endif