MissionController.h 14.8 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 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 QDomDocument;
29

30
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
31

32 33
typedef QPair<VisualMissionItem*,VisualMissionItem*> VisualItemPair;
typedef QHash<VisualItemPair, CoordinateVector*> CoordVectHashTable;
34

35
class MissionController : public PlanElementController
36 37
{
    Q_OBJECT
38

39
public:
40
    MissionController(PlanMasterController* masterController, QObject* parent = NULL);
41 42
    ~MissionController();

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

67
    Q_PROPERTY(QmlObjectListModel*  visualItems             READ visualItems                NOTIFY visualItemsChanged)
68 69
    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
70
    Q_PROPERTY(QStringList          complexMissionItemNames READ complexMissionItemNames    NOTIFY complexMissionItemNamesChanged)
71
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition     READ plannedHomePosition        NOTIFY plannedHomePositionChanged)
72

73 74
    Q_PROPERTY(double               progressPct             READ progressPct                NOTIFY progressPctChanged)

75 76
    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.
77

78 79 80
    Q_PROPERTY(int                  currentPlanViewIndex    READ currentPlanViewIndex       NOTIFY currentPlanViewIndexChanged)
    Q_PROPERTY(VisualMissionItem*   currentPlanViewItem     READ currentPlanViewItem        NOTIFY currentPlanViewItemChanged)

81 82 83 84 85 86 87
    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)
88

89 90 91
    Q_PROPERTY(int                  batteryChangePoint      READ batteryChangePoint         NOTIFY batteryChangePointChanged)
    Q_PROPERTY(int                  batteriesRequired       READ batteriesRequired          NOTIFY batteriesRequiredChanged)

92
    Q_INVOKABLE void removeMissionItem(int index);
93

94 95 96
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
97 98
    Q_INVOKABLE int insertSimpleMissionItem(QGeoCoordinate coordinate, int i);

99 100 101 102 103
    /// 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);

104
    /// Add a new complex mission item to the list
105 106
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
    ///     @param mapCenterCoordinate: coordinate for current center of map
107 108
    ///     @param i: index to insert at
    /// @return Sequence number for new item
109
    Q_INVOKABLE int insertComplexMissionItem(QString itemName, QGeoCoordinate mapCenterCoordinate, int i);
110

111 112
    Q_INVOKABLE void resumeMission(int resumeIndex);

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

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

120 121 122 123
    /// 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
124 125 126
    /// Sends the mission items to the specified vehicle
    static void sendItemsToVehicle(Vehicle* vehicle, QmlObjectListModel* visualMissionItems);

127 128 129
    bool loadJsonFile(QFile& file, QString& errorString);
    bool loadTextFile(QFile& file, QString& errorString);

130
    // Overrides from PlanElementController
131
    bool supported                  (void) const final { return true; }
132
    void start                      (bool editMode) final;
133 134
    void save                       (QJsonObject& json) final;
    bool load                       (const QJsonObject& json, QString& errorString) final;
135 136 137
    void loadFromVehicle            (void) final;
    void sendToVehicle              (void) final;
    void removeAll                  (void) final;
138
    void removeAllFromVehicle       (void) final;
139 140 141
    bool syncInProgress             (void) const final;
    bool dirty                      (void) const final;
    void setDirty                   (bool dirty) final;
142
    bool containsItems              (void) const final;
143
    void managerVehicleChanged      (Vehicle* managerVehicle) final;
DonLakeFlyer's avatar
DonLakeFlyer committed
144
    bool showPlanFromManagerVehicle (void) final;
145

146 147 148
    // Create KML file
    void convertToKMLDocument(QDomDocument& document);

149 150
    // Property accessors

151 152
    QmlObjectListModel* visualItems             (void) { return _visualItems; }
    QmlObjectListModel* waypointLines           (void) { return &_waypointLines; }
153
    QVariantList        waypointPath            (void) { return _waypointPath; }
154
    QStringList         complexMissionItemNames (void) const;
155
    QGeoCoordinate      plannedHomePosition     (void) const;
156
    VisualMissionItem*  currentPlanViewItem     (void) const;
157
    double              progressPct             (void) const { return _progressPct; }
158

159 160 161
    int currentMissionIndex         (void) const;
    int resumeMissionIndex          (void) const;
    int currentPlanViewIndex        (void) const;
162

DonLakeFlyer's avatar
DonLakeFlyer committed
163 164 165 166 167 168 169
    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; }
170

171 172 173
    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

174
signals:
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    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);
197

198
private slots:
199
    void _newMissionItemsAvailableFromVehicle(bool removeAllRequested);
200
    void _itemCommandChanged(void);
201
    void _managerVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
202
    void _inProgressChanged(bool inProgress);
203
    void _currentMissionIndexChanged(int sequenceNumber);
204
    void _recalcWaypointLines(void);
DonLakeFlyer's avatar
DonLakeFlyer committed
205
    void _recalcMissionFlightStatus(void);
206
    void _updateContainsItems(void);
207
    void _progressPctChanged(double progressPct);
208
    void _visualItemsDirtyChanged(bool dirty);
209 210
    void _managerSendComplete(bool error);
    void _managerRemoveAllComplete(bool error);
211 212

private:
213
    void _init(void);
214
    void _recalcSequence(void);
215 216
    void _recalcChildItems(void);
    void _recalcAll(void);
217 218 219 220
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
221
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
222
    void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
223
    static double _calcDistanceToHome(VisualMissionItem* currentItem, VisualMissionItem* homeItem);
224
    bool _findPreviousAltitude(int newIndex, double* prevAltitude, int* prevAltitudeMode);
Don Gagne's avatar
Don Gagne committed
225 226
    static double _normalizeLat(double lat);
    static double _normalizeLon(double lon);
DonLakeFlyer's avatar
DonLakeFlyer committed
227
    void _addMissionSettings(QmlObjectListModel* visualItems, bool addToCenter);
228 229 230 231
    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);
232
    int _nextSequenceNumber(void);
233
    void _scanForAdditionalSettings(QmlObjectListModel* visualItems, Vehicle* vehicle);
234
    static bool _convertToMissionItems(QmlObjectListModel* visualMissionItems, QList<MissionItem*>& rgMissionItems, QObject* missionItemParent);
235
    void _setPlannedHomePositionFromFirstCoordinate(void);
236 237 238 239
    void _resetMissionFlightStatus(void);
    void _addHoverTime(double hoverTime, double hoverDistance, int waypointIndex);
    void _addCruiseTime(double cruiseTime, double cruiseDistance, int wayPointIndex);
    void _updateBatteryInfo(int waypointIndex);
240
    bool _loadItemsFromJson(const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
241
    void _initLoadedVisualItems(QmlObjectListModel* loadedVisualItems);
242
    void _addWaypointLineSegment(CoordVectHashTable& prevItemPairHashTable, VisualItemPair& pair);
243 244
    void _addCommandTimeDelay(SimpleMissionItem* simpleItem, bool vtolInHover);
    void _addTimeDistance(bool vtolInHover, double hoverTime, double cruiseTime, double extraTime, double distance, int seqNum);
245

246
private:
247
    MissionManager*         _missionManager;
DonLakeFlyer's avatar
DonLakeFlyer committed
248
    QmlObjectListModel*     _visualItems;
249
    MissionSettingsItem*    _settingsItem;
DonLakeFlyer's avatar
DonLakeFlyer committed
250
    QmlObjectListModel      _waypointLines;
251
    QVariantList            _waypointPath;
DonLakeFlyer's avatar
DonLakeFlyer committed
252 253
    CoordVectHashTable      _linesTable;
    bool                    _firstItemsFromVehicle;
DonLakeFlyer's avatar
DonLakeFlyer committed
254
    bool                    _itemsRequested;
DonLakeFlyer's avatar
DonLakeFlyer committed
255 256 257
    MissionFlightStatus_t   _missionFlightStatus;
    QString                 _surveyMissionItemName;
    QString                 _fwLandingMissionItemName;
258
    QString                 _structureScanMissionItemName;
259
    QString                 _corridorScanMissionItemName;
260
    AppSettings*            _appSettings;
261
    double                  _progressPct;
262 263
    int                     _currentPlanViewIndex;
    VisualMissionItem*      _currentPlanViewItem;
264

265
    static const char*  _settingsGroup;
266 267

    // Json file keys for persistence
Don Gagne's avatar
Don Gagne committed
268 269
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
270 271 272
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
273 274 275 276 277
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
278
    static const char*  _jsonMavAutopilotKey;
279
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
280 281

    static const int    _missionFileVersion;
282 283 284
};

#endif