MissionController.h 6.99 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
    Q_PROPERTY(QGeoCoordinate       plannedHomePosition READ plannedHomePosition                        NOTIFY plannedHomePositionChanged)
38 39 40
    Q_PROPERTY(QmlObjectListModel*  visualItems         READ visualItems                                NOTIFY visualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  complexVisualItems  READ complexVisualItems                         NOTIFY complexVisualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  waypointLines       READ waypointLines                              NOTIFY waypointLinesChanged)
41

42 43 44 45 46
    Q_PROPERTY(double               missionDistance     READ missionDistance                            NOTIFY missionDistanceChanged)
    Q_PROPERTY(double               missionMaxTelemetry READ missionMaxTelemetry                        NOTIFY missionMaxTelemetryChanged)
    Q_PROPERTY(double               cruiseDistance      READ cruiseDistance                             NOTIFY cruiseDistanceChanged)
    Q_PROPERTY(double               hoverDistance       READ hoverDistance                              NOTIFY hoverDistanceChanged)

47
    Q_INVOKABLE void removeMissionItem(int index);
48

49 50 51
    /// Add a new simple mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
52 53
    Q_INVOKABLE int insertSimpleMissionItem(QGeoCoordinate coordinate, int i);

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

59 60 61 62 63 64 65 66 67 68 69 70 71
    // Overrides from PlanElementController
    void start              (bool editMode) 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;

72 73
    // Property accessors

74
    QGeoCoordinate      plannedHomePosition (void);
75 76 77 78
    QmlObjectListModel* visualItems         (void) { return _visualItems; }
    QmlObjectListModel* complexVisualItems  (void) { return _complexItems; }
    QmlObjectListModel* waypointLines       (void) { return &_waypointLines; }

79 80 81 82 83 84 85 86 87 88
    double  missionDistance         (void) const { return _missionDistance; }
    double  missionMaxTelemetry     (void) const { return _missionMaxTelemetry; }
    double  cruiseDistance          (void) const { return _cruiseDistance; }
    double  hoverDistance           (void) const { return _hoverDistance; }

    void setMissionDistance         (double missionDistance );
    void setMissionMaxTelemetry     (double missionMaxTelemetry);
    void setCruiseDistance          (double cruiseDistance );
    void setHoverDistance           (double hoverDistance );

89 90
    static const char* jsonSimpleItemsKey;  ///< Key for simple items in a json file

91
signals:
92
    void plannedHomePositionChanged(QGeoCoordinate plannedHomePosition);
93 94
    void visualItemsChanged(void);
    void complexVisualItemsChanged(void);
95
    void waypointLinesChanged(void);
96
    void newItemsFromVehicle(void);
97 98 99 100
    void missionDistanceChanged(double missionDistance);
    void missionMaxTelemetryChanged(double missionMaxTelemetry);
    void cruiseDistanceChanged(double cruiseDistance);
    void hoverDistanceChanged(double hoverDistance);
101

102
private slots:
103
    void _newMissionItemsAvailableFromVehicle();
104
    void _itemCommandChanged(void);
105 106 107
    void _activeVehicleHomePositionAvailableChanged(bool homePositionAvailable);
    void _activeVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
    void _inProgressChanged(bool inProgress);
108
    void _currentMissionItemChanged(int sequenceNumber);
109
    void _recalcWaypointLines(void);
110 111
    void _recalcAltitudeRangeBearing(void);
    void _homeCoordinateChanged(void);
112 113

private:
114
    void _recalcSequence(void);
115 116
    void _recalcChildItems(void);
    void _recalcAll(void);
117 118 119 120
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
Don Gagne's avatar
Don Gagne committed
121
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
122
    static void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
123
    static void _calcHomeDist(VisualMissionItem* currentItem, VisualMissionItem* homeItem, double* distance);
124
    bool _findLastAltitude(double* lastAltitude, MAV_FRAME* frame);
125
    bool _findLastAcceptanceRadius(double* lastAcceptanceRadius);
126
    void _addPlannedHomePosition(QmlObjectListModel* visualItems, bool addToCenter);
127 128
    double _normalizeLat(double lat);
    double _normalizeLon(double lon);
129
    bool _loadJsonMissionFile(const QByteArray& bytes, QmlObjectListModel* visualItems, QmlObjectListModel* complexItems, QString& errorString);
130
    bool _loadTextMissionFile(QTextStream& stream, QmlObjectListModel* visualItems, QString& errorString);
131
    int _nextSequenceNumber(void);
132

133
    // Overrides from PlanElementController
134
    void _activeVehicleBeingRemoved(void) final;
135 136
    void _activeVehicleSet(void) final;

137
private:
138 139
    QmlObjectListModel* _visualItems;
    QmlObjectListModel* _complexItems;
140
    QmlObjectListModel  _waypointLines;
Nate Weibley's avatar
Nate Weibley committed
141
    CoordVectHashTable  _linesTable;
142
    bool                _firstItemsFromVehicle;
143 144
    bool                _missionItemsRequested;
    bool                _queuedSend;
145 146 147 148
    double              _missionDistance;
    double              _missionMaxTelemetry;
    double              _cruiseDistance;
    double              _hoverDistance;
149

150 151 152 153
    static const char*  _settingsGroup;
    static const char*  _jsonVersionKey;
    static const char*  _jsonGroundStationKey;
    static const char*  _jsonMavAutopilotKey;
154
    static const char*  _jsonComplexItemsKey;
155
    static const char*  _jsonPlannedHomePositionKey;
156 157 158
};

#endif