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

28
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
29

30 31
typedef QPair<VisualMissionItem*,VisualMissionItem*> VisualItemPair;
typedef QHash<VisualItemPair, CoordinateVector*> CoordVectHashTable;
32
class MissionController : public PlanElementController
33 34
{
    Q_OBJECT
35

36 37 38 39
public:
    MissionController(QObject* parent = NULL);
    ~MissionController();

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

63 64 65
    Q_PROPERTY(QmlObjectListModel*  visualItems             READ visualItems                NOTIFY visualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  waypointLines           READ waypointLines              NOTIFY waypointLinesChanged)
    Q_PROPERTY(QStringList          complexMissionItemNames READ complexMissionItemNames    NOTIFY complexMissionItemNamesChanged)
66
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition     READ plannedHomePosition        NOTIFY plannedHomePositionChanged)
67

68
    Q_PROPERTY(int                  resumeMissionIndex      READ resumeMissionIndex         NOTIFY resumeMissionIndexChanged)
69

70 71 72 73 74 75 76
    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)
77

78 79 80
    Q_PROPERTY(int                  batteryChangePoint      READ batteryChangePoint         NOTIFY batteryChangePointChanged)
    Q_PROPERTY(int                  batteriesRequired       READ batteriesRequired          NOTIFY batteriesRequiredChanged)

81
    Q_INVOKABLE void removeMissionItem(int index);
82

83 84 85
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
86 87
    Q_INVOKABLE int insertSimpleMissionItem(QGeoCoordinate coordinate, int i);

88
    /// Add a new complex mission item to the list
89 90
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
    ///     @param mapCenterCoordinate: coordinate for current center of map
91 92
    ///     @param i: index to insert at
    /// @return Sequence number for new item
93
    Q_INVOKABLE int insertComplexMissionItem(QString itemName, QGeoCoordinate mapCenterCoordinate, int i);
94

95 96
    Q_INVOKABLE void resumeMission(int resumeIndex);

DonLakeFlyer's avatar
DonLakeFlyer committed
97 98 99
    /// 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
100 101 102 103 104
    /// Loads the mission items from the specified file
    ///     @param[in] vehicle Vehicle we are loading items for
    ///     @param[in] filename File to load from
    ///     @param[out] visualItems Visual items loaded, returns NULL if error
    /// @return success/fail
105
    static bool loadItemsFromFile(Vehicle* vehicle, const QString& filename, QmlObjectListModel** visualItems);
Don Gagne's avatar
Don Gagne committed
106 107 108 109

    /// Sends the mission items to the specified vehicle
    static void sendItemsToVehicle(Vehicle* vehicle, QmlObjectListModel* visualMissionItems);

110
    // Overrides from PlanElementController
111 112 113 114 115 116 117
    void start                      (bool editMode) final;
    void startStaticActiveVehicle   (Vehicle* vehicle) final;
    void loadFromVehicle            (void) final;
    void sendToVehicle              (void) final;
    void loadFromFile               (const QString& filename) final;
    void saveToFile                 (const QString& filename) final;
    void removeAll                  (void) final;
118
    void removeAllFromVehicle       (void) final;
119 120 121
    bool syncInProgress             (void) const final;
    bool dirty                      (void) const final;
    void setDirty                   (bool dirty) final;
122
    bool containsItems              (void) const final;
123

124 125
    QString fileExtension(void) const final;

126 127
    // Property accessors

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

    /// Returns the item index two which a mission should be resumed. -1 indicates resume mission not available.
134
    int resumeMissionIndex(void) const;
135

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

144 145 146
    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

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

165
private slots:
166
    void _newMissionItemsAvailableFromVehicle(bool removeAllRequested);
167
    void _itemCommandChanged(void);
168 169
    void _activeVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
    void _inProgressChanged(bool inProgress);
170
    void _currentMissionIndexChanged(int sequenceNumber);
171
    void _recalcWaypointLines(void);
DonLakeFlyer's avatar
DonLakeFlyer committed
172
    void _recalcMissionFlightStatus(void);
173
    void _updateContainsItems(void);
174 175

private:
176
    void _init(void);
177
    void _recalcSequence(void);
178 179
    void _recalcChildItems(void);
    void _recalcAll(void);
180 181 182 183
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
184
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
185
    static void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
186
    static double _calcDistanceToHome(VisualMissionItem* currentItem, VisualMissionItem* homeItem);
187
    bool _findPreviousAltitude(int newIndex, double* prevAltitude, MAV_FRAME* prevFrame);
Don Gagne's avatar
Don Gagne committed
188 189
    static double _normalizeLat(double lat);
    static double _normalizeLon(double lon);
190 191 192 193
    static void _addMissionSettings(Vehicle* vehicle, QmlObjectListModel* visualItems, bool addToCenter);
    static bool _loadJsonMissionFile(Vehicle* vehicle, const QByteArray& bytes, QmlObjectListModel* visualItems, QString& errorString);
    static bool _loadJsonMissionFileV1(Vehicle* vehicle, const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
    static bool _loadJsonMissionFileV2(Vehicle* vehicle, const QJsonObject& json, QmlObjectListModel* visualItems, QString& errorString);
Don Gagne's avatar
Don Gagne committed
194
    static bool _loadTextMissionFile(Vehicle* vehicle, QTextStream& stream, QmlObjectListModel* visualItems, QString& errorString);
195
    int _nextSequenceNumber(void);
196
    static void _scanForAdditionalSettings(QmlObjectListModel* visualItems, Vehicle* vehicle);
197
    static bool _convertToMissionItems(QmlObjectListModel* visualMissionItems, QList<MissionItem*>& rgMissionItems, QObject* missionItemParent);
198
    void _setPlannedHomePositionFromFirstCoordinate(void);
199 200 201 202
    void _resetMissionFlightStatus(void);
    void _addHoverTime(double hoverTime, double hoverDistance, int waypointIndex);
    void _addCruiseTime(double cruiseTime, double cruiseDistance, int wayPointIndex);
    void _updateBatteryInfo(int waypointIndex);
203

204
    // Overrides from PlanElementController
205
    void _activeVehicleBeingRemoved(void) final;
206 207
    void _activeVehicleSet(void) final;

208
private:
DonLakeFlyer's avatar
DonLakeFlyer committed
209
    QmlObjectListModel*     _visualItems;
210
    MissionSettingsItem*    _settingsItem;
DonLakeFlyer's avatar
DonLakeFlyer committed
211 212 213 214 215 216 217
    QmlObjectListModel      _waypointLines;
    CoordVectHashTable      _linesTable;
    bool                    _firstItemsFromVehicle;
    bool                    _missionItemsRequested;
    MissionFlightStatus_t   _missionFlightStatus;
    QString                 _surveyMissionItemName;
    QString                 _fwLandingMissionItemName;
218
    AppSettings*            _appSettings;
219

220
    static const char*  _settingsGroup;
221 222

    // Json file keys for persistence
Don Gagne's avatar
Don Gagne committed
223 224
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
225 226 227
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
228 229 230 231 232
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
233
    static const char*  _jsonMavAutopilotKey;
234
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
235 236

    static const int    _missionFileVersion;
237 238 239
};

#endif