MissionController.h 12.6 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

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();

DonLakeFlyer's avatar
DonLakeFlyer committed
43
    typedef struct {
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 56
        double  gimbalYaw;              ///< NaN signals yaw was never changed
        int     mAhBattery;             ///< 0 for not available
Don Gagne's avatar
Don Gagne committed
57 58
        double  hoverAmps;              ///< Amp consumption during hover
        double  cruiseAmps;             ///< Amp consumption during cruise
59 60 61 62 63
        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
64 65
    } MissionFlightStatus_t;

66 67 68
    Q_PROPERTY(QmlObjectListModel*  visualItems             READ visualItems                NOTIFY visualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  waypointLines           READ waypointLines              NOTIFY waypointLinesChanged)
    Q_PROPERTY(QStringList          complexMissionItemNames READ complexMissionItemNames    NOTIFY complexMissionItemNamesChanged)
69
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition     READ plannedHomePosition        NOTIFY plannedHomePositionChanged)
70

71 72
    Q_PROPERTY(double               progressPct             READ progressPct                NOTIFY progressPctChanged)

73 74
    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.
75

76 77 78 79 80 81 82
    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)
83

84 85 86
    Q_PROPERTY(int                  batteryChangePoint      READ batteryChangePoint         NOTIFY batteryChangePointChanged)
    Q_PROPERTY(int                  batteriesRequired       READ batteriesRequired          NOTIFY batteriesRequiredChanged)

87
    Q_INVOKABLE void removeMissionItem(int index);
88

89 90 91
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
92 93
    Q_INVOKABLE int insertSimpleMissionItem(QGeoCoordinate coordinate, int i);

94
    /// Add a new complex mission item to the list
95 96
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
    ///     @param mapCenterCoordinate: coordinate for current center of map
97 98
    ///     @param i: index to insert at
    /// @return Sequence number for new item
99
    Q_INVOKABLE int insertComplexMissionItem(QString itemName, QGeoCoordinate mapCenterCoordinate, int i);
100

101 102
    Q_INVOKABLE void resumeMission(int resumeIndex);

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

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

109 110 111
    bool loadJsonFile(QFile& file, QString& errorString);
    bool loadTextFile(QFile& file, QString& errorString);

112
    // Overrides from PlanElementController
113
    void start                      (bool editMode) final;
114 115
    void save                       (QJsonObject& json) final;
    bool load                       (const QJsonObject& json, QString& errorString) final;
116 117 118
    void loadFromVehicle            (void) final;
    void sendToVehicle              (void) final;
    void removeAll                  (void) final;
119
    void removeAllFromVehicle       (void) final;
120 121 122
    bool syncInProgress             (void) const final;
    bool dirty                      (void) const final;
    void setDirty                   (bool dirty) final;
123
    bool containsItems              (void) const final;
124
    void managerVehicleChanged      (Vehicle* managerVehicle) final;
DonLakeFlyer's avatar
DonLakeFlyer committed
125
    bool showPlanFromManagerVehicle (void) final;
126

127 128
    // Property accessors

129 130 131
    QmlObjectListModel* visualItems             (void) { return _visualItems; }
    QmlObjectListModel* waypointLines           (void) { return &_waypointLines; }
    QStringList         complexMissionItemNames (void) const;
132
    QGeoCoordinate      plannedHomePosition     (void) const;
133
    double              progressPct             (void) const { return _progressPct; }
134

135
    int currentMissionIndex(void) const;
136
    int resumeMissionIndex(void) const;
137

DonLakeFlyer's avatar
DonLakeFlyer committed
138 139 140 141 142 143 144
    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; }
145

146 147 148
    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

149
signals:
150
    void visualItemsChanged(void);
151
    void waypointLinesChanged(void);
152
    void newItemsFromVehicle(void);
153
    void missionDistanceChanged(double missionDistance);
154 155 156 157 158
    void missionTimeChanged(void);
    void missionHoverDistanceChanged(double missionHoverDistance);
    void missionHoverTimeChanged(void);
    void missionCruiseDistanceChanged(double missionCruiseDistance);
    void missionCruiseTimeChanged(void);
159
    void missionMaxTelemetryChanged(double missionMaxTelemetry);
160
    void complexMissionItemNamesChanged(void);
161
    void resumeMissionIndexChanged(void);
162
    void resumeMissionReady(void);
163 164
    void batteryChangePointChanged(int batteryChangePoint);
    void batteriesRequiredChanged(int batteriesRequired);
165
    void plannedHomePositionChanged(QGeoCoordinate plannedHomePosition);
166
    void progressPctChanged(double progressPct);
167
    void currentMissionIndexChanged(int currentMissionIndex);
168

169
private slots:
170
    void _newMissionItemsAvailableFromVehicle(bool removeAllRequested);
171
    void _itemCommandChanged(void);
172
    void _managerVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
173
    void _inProgressChanged(bool inProgress);
174
    void _currentMissionIndexChanged(int sequenceNumber);
175
    void _recalcWaypointLines(void);
DonLakeFlyer's avatar
DonLakeFlyer committed
176
    void _recalcMissionFlightStatus(void);
177
    void _updateContainsItems(void);
178
    void _progressPctChanged(double progressPct);
179
    void _visualItemsDirtyChanged(bool dirty);
180 181
    void _managerSendComplete(bool error);
    void _managerRemoveAllComplete(bool error);
182 183

private:
184
    void _init(void);
185
    void _recalcSequence(void);
186 187
    void _recalcChildItems(void);
    void _recalcAll(void);
188 189 190 191
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
192
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
193
    void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
194
    static double _calcDistanceToHome(VisualMissionItem* currentItem, VisualMissionItem* homeItem);
195
    bool _findPreviousAltitude(int newIndex, double* prevAltitude, MAV_FRAME* prevFrame);
Don Gagne's avatar
Don Gagne committed
196 197
    static double _normalizeLat(double lat);
    static double _normalizeLon(double lon);
DonLakeFlyer's avatar
DonLakeFlyer committed
198
    void _addMissionSettings(QmlObjectListModel* visualItems, bool addToCenter);
199 200 201 202
    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);
203
    int _nextSequenceNumber(void);
204
    static void _scanForAdditionalSettings(QmlObjectListModel* visualItems, Vehicle* vehicle);
205
    static bool _convertToMissionItems(QmlObjectListModel* visualMissionItems, QList<MissionItem*>& rgMissionItems, QObject* missionItemParent);
206
    void _setPlannedHomePositionFromFirstCoordinate(void);
207 208 209 210
    void _resetMissionFlightStatus(void);
    void _addHoverTime(double hoverTime, double hoverDistance, int waypointIndex);
    void _addCruiseTime(double cruiseTime, double cruiseDistance, int wayPointIndex);
    void _updateBatteryInfo(int waypointIndex);
211
    bool _loadItemsFromJson(const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
212
    void _initLoadedVisualItems(QmlObjectListModel* loadedVisualItems);
213
    void _addWaypointLineSegment(CoordVectHashTable& prevItemPairHashTable, VisualItemPair& pair);
214 215
    void _addCommandTimeDelay(SimpleMissionItem* simpleItem, bool vtolInHover);
    void _addTimeDistance(bool vtolInHover, double hoverTime, double cruiseTime, double extraTime, double distance, int seqNum);
216

217
private:
218
    MissionManager*         _missionManager;
DonLakeFlyer's avatar
DonLakeFlyer committed
219
    QmlObjectListModel*     _visualItems;
220
    MissionSettingsItem*    _settingsItem;
DonLakeFlyer's avatar
DonLakeFlyer committed
221 222 223
    QmlObjectListModel      _waypointLines;
    CoordVectHashTable      _linesTable;
    bool                    _firstItemsFromVehicle;
DonLakeFlyer's avatar
DonLakeFlyer committed
224
    bool                    _itemsRequested;
DonLakeFlyer's avatar
DonLakeFlyer committed
225 226 227
    MissionFlightStatus_t   _missionFlightStatus;
    QString                 _surveyMissionItemName;
    QString                 _fwLandingMissionItemName;
228
    AppSettings*            _appSettings;
229
    double                  _progressPct;
230

231
    static const char*  _settingsGroup;
232 233

    // Json file keys for persistence
Don Gagne's avatar
Don Gagne committed
234 235
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
236 237 238
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
239 240 241 242 243
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
244
    static const char*  _jsonMavAutopilotKey;
245
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
246 247

    static const int    _missionFileVersion;
248 249 250
};

#endif