MissionController.h 10 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
#include "VisualMissionItem.h"
20

21 22
#include <QHash>

23 24
class CoordinateVector;

25
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
26

27 28
typedef QPair<VisualMissionItem*,VisualMissionItem*> VisualItemPair;
typedef QHash<VisualItemPair, CoordinateVector*> CoordVectHashTable;
29
class MissionController : public PlanElementController
30 31
{
    Q_OBJECT
32

33 34 35 36
public:
    MissionController(QObject* parent = NULL);
    ~MissionController();

37 38 39 40 41
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition READ plannedHomePosition    NOTIFY plannedHomePositionChanged)
    Q_PROPERTY(QmlObjectListModel*  visualItems         READ visualItems            NOTIFY visualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  complexVisualItems  READ complexVisualItems     NOTIFY complexVisualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  waypointLines       READ waypointLines          NOTIFY waypointLinesChanged)

42 43
    Q_PROPERTY(QStringList          complexMissionItemNames MEMBER _complexMissionItemNames CONSTANT)

44 45 46 47 48 49 50
    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)
51

52
    Q_INVOKABLE void removeMissionItem(int index);
53

54 55 56
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
57 58
    Q_INVOKABLE int insertSimpleMissionItem(QGeoCoordinate coordinate, int i);

59
    /// Add a new complex mission item to the list
60 61
    ///     @param itemName: Name of complex item to create (from complexMissionItemNames)
    ///     @param mapCenterCoordinate: coordinate for current center of map
62 63
    ///     @param i: index to insert at
    /// @return Sequence number for new item
64
    Q_INVOKABLE int insertComplexMissionItem(QString itemName, QGeoCoordinate mapCenterCoordinate, int i);
65

Don Gagne's avatar
Don Gagne committed
66 67 68 69 70 71 72 73 74 75 76
    /// 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
    ///     @param[out] complexItems Complex items loaded, returns NULL if error
    /// @return success/fail
    static bool loadItemsFromFile(Vehicle* vehicle, const QString& filename, QmlObjectListModel** visualItems, QmlObjectListModel** complexItems);

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

77
    // Overrides from PlanElementController
78 79 80 81 82 83 84 85 86 87 88 89
    void start                      (bool editMode) final;
    void startStaticActiveVehicle   (Vehicle* vehicle) final;
    void loadFromVehicle            (void) final;
    void sendToVehicle              (void) final;
    void loadFromFilePicker         (void) final;
    void loadFromFile               (const QString& filename) final;
    void saveToFilePicker           (void) final;
    void saveToFile                 (const QString& filename) final;
    void removeAll                  (void) final;
    bool syncInProgress             (void) const final;
    bool dirty                      (void) const final;
    void setDirty                   (bool dirty) final;
90

91 92
    QString fileExtension(void) const final;

93 94
    // Property accessors

95
    QGeoCoordinate      plannedHomePosition (void);
96 97 98 99
    QmlObjectListModel* visualItems         (void) { return _visualItems; }
    QmlObjectListModel* complexVisualItems  (void) { return _complexItems; }
    QmlObjectListModel* waypointLines       (void) { return &_waypointLines; }

100
    double  missionDistance         (void) const { return _missionDistance; }
101 102 103 104 105
    double  missionTime             (void) const { return _missionTime; }
    double  missionHoverDistance    (void) const { return _missionHoverDistance; }
    double  missionHoverTime        (void) const { return _missionHoverTime; }
    double  missionCruiseDistance   (void) const { return _missionCruiseDistance; }
    double  missionCruiseTime       (void) const { return _missionCruiseTime; }
106
    double  missionMaxTelemetry     (void) const { return _missionMaxTelemetry; }
107 108
    double  cruiseSpeed             (void) const;
    double  hoverSpeed              (void) const;
109

110
signals:
111
    void plannedHomePositionChanged(QGeoCoordinate plannedHomePosition);
112 113
    void visualItemsChanged(void);
    void complexVisualItemsChanged(void);
114
    void waypointLinesChanged(void);
115
    void newItemsFromVehicle(void);
116
    void missionDistanceChanged(double missionDistance);
117 118 119 120 121
    void missionTimeChanged(void);
    void missionHoverDistanceChanged(double missionHoverDistance);
    void missionHoverTimeChanged(void);
    void missionCruiseDistanceChanged(double missionCruiseDistance);
    void missionCruiseTimeChanged(void);
122 123 124
    void missionMaxTelemetryChanged(double missionMaxTelemetry);
    void cruiseDistanceChanged(double cruiseDistance);
    void hoverDistanceChanged(double hoverDistance);
125 126
    void cruiseSpeedChanged(double cruiseSpeed);
    void hoverSpeedChanged(double hoverSpeed);
127

128
private slots:
129
    void _newMissionItemsAvailableFromVehicle();
130
    void _itemCommandChanged(void);
131 132 133
    void _activeVehicleHomePositionAvailableChanged(bool homePositionAvailable);
    void _activeVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
    void _inProgressChanged(bool inProgress);
134
    void _currentMissionItemChanged(int sequenceNumber);
135
    void _recalcWaypointLines(void);
136 137
    void _recalcAltitudeRangeBearing(void);
    void _homeCoordinateChanged(void);
138 139

private:
140
    void _init(void);
141
    void _recalcSequence(void);
142 143
    void _recalcChildItems(void);
    void _recalcAll(void);
144 145 146 147
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
148
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
149
    static void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
150
    static double _calcDistanceToHome(VisualMissionItem* currentItem, VisualMissionItem* homeItem);
151
    bool _findPreviousAltitude(int newIndex, double* prevAltitude, MAV_FRAME* prevFrame);
Don Gagne's avatar
Don Gagne committed
152 153 154 155 156 157 158
    static double _normalizeLat(double lat);
    static double _normalizeLon(double lon);
    static void _addPlannedHomePosition(Vehicle* vehicle, QmlObjectListModel* visualItems, bool addToCenter);
    static bool _loadJsonMissionFile(Vehicle* vehicle, const QByteArray& bytes, QmlObjectListModel* visualItems, QmlObjectListModel* complexItems, QString& errorString);
    static bool _loadJsonMissionFileV1(Vehicle* vehicle, const QJsonObject& json, QmlObjectListModel* visualItems, QmlObjectListModel* complexItems, QString& errorString);
    static bool _loadJsonMissionFileV2(Vehicle* vehicle, const QJsonObject& json, QmlObjectListModel* visualItems, QmlObjectListModel* complexItems, QString& errorString);
    static bool _loadTextMissionFile(Vehicle* vehicle, QTextStream& stream, QmlObjectListModel* visualItems, QString& errorString);
159
    int _nextSequenceNumber(void);
160 161 162 163 164 165 166
    void _setMissionDistance(double missionDistance);
    void _setMissionTime(double missionTime);
    void _setMissionHoverDistance(double missionHoverDistance);
    void _setMissionHoverTime(double missionHoverTime);
    void _setMissionCruiseDistance(double missionCruiseDistance);
    void _setMissionCruiseTime(double missionCruiseTime);
    void _setMissionMaxTelemetry(double missionMaxTelemetry);
167

168
    // Overrides from PlanElementController
169
    void _activeVehicleBeingRemoved(void) final;
170 171
    void _activeVehicleSet(void) final;

172
private:
173 174
    QmlObjectListModel* _visualItems;
    QmlObjectListModel* _complexItems;
175
    QmlObjectListModel  _waypointLines;
Nate Weibley's avatar
Nate Weibley committed
176
    CoordVectHashTable  _linesTable;
177
    bool                _firstItemsFromVehicle;
178 179
    bool                _missionItemsRequested;
    bool                _queuedSend;
180
    double              _missionDistance;
181 182 183 184 185
    double              _missionTime;
    double              _missionHoverDistance;
    double              _missionHoverTime;
    double              _missionCruiseDistance;
    double              _missionCruiseTime;
186 187 188 189
    double              _missionMaxTelemetry;    
    QString             _surveyMissionItemName;
    QString             _fwLandingMissionItemName;
    QStringList         _complexMissionItemNames;
190

191
    static const char*  _settingsGroup;
Don Gagne's avatar
Don Gagne committed
192 193
    static const char*  _jsonFileTypeValue;
    static const char*  _jsonFirmwareTypeKey;
194 195 196
    static const char*  _jsonVehicleTypeKey;
    static const char*  _jsonCruiseSpeedKey;
    static const char*  _jsonHoverSpeedKey;
Don Gagne's avatar
Don Gagne committed
197 198 199 200 201
    static const char*  _jsonItemsKey;
    static const char*  _jsonPlannedHomePositionKey;
    static const char*  _jsonParamsKey;

    // Deprecated V1 format keys
202
    static const char*  _jsonMavAutopilotKey;
203
    static const char*  _jsonComplexItemsKey;
Don Gagne's avatar
Don Gagne committed
204 205

    static const int    _missionFileVersion;
206 207 208
};

#endif