MissionController.h 14.5 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);

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

124 125 126
    bool loadJsonFile(QFile& file, QString& errorString);
    bool loadTextFile(QFile& file, QString& errorString);

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

143 144 145
    // Create KML file
    void convertToKMLDocument(QDomDocument& document);

146 147
    // Property accessors

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

156 157 158
    int currentMissionIndex         (void) const;
    int resumeMissionIndex          (void) const;
    int currentPlanViewIndex        (void) const;
159

DonLakeFlyer's avatar
DonLakeFlyer committed
160 161 162 163 164 165 166
    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; }
167

168 169 170
    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

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

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

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

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

261
    static const char*  _settingsGroup;
262 263

    // Json file keys for persistence
Don Gagne's avatar
Don Gagne committed
264 265
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
266 267 268
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
269 270 271 272 273
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
274
    static const char*  _jsonMavAutopilotKey;
275
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
276 277

    static const int    _missionFileVersion;
278 279 280
};

#endif