MissionController.h 6.95 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
    QString fileExtension(void) const final;

74 75
    // Property accessors

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

81 82 83 84 85 86 87 88 89 90
    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 );

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

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

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

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

135
    // Overrides from PlanElementController
136
    void _activeVehicleBeingRemoved(void) final;
137 138
    void _activeVehicleSet(void) final;

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

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

#endif