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
#include "QGCLoggingCategory.h"
18
#include "MavlinkQmlSingleton.h"
19

20 21
#include <QHash>

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

31
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
32

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

36
class MissionController : public PlanElementController
37 38
{
    Q_OBJECT
39

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

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

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

74 75
    Q_PROPERTY(double               progressPct             READ progressPct                NOTIFY progressPctChanged)

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

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

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

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

93
    Q_INVOKABLE void removeMissionItem(int index);
94

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

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

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

112 113
    Q_INVOKABLE void resumeMission(int resumeIndex);

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

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

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

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

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

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

150 151
    // Property accessors

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

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

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

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

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

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

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

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

266
    static const char*  _settingsGroup;
267 268

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

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

    static const int    _missionFileVersion;
283 284 285
};

#endif