MissionController.h 5.49 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 14


#ifndef MissionController_H
#define MissionController_H

#include <QObject>
15
#include <QHash>
16 17 18

#include "QmlObjectListModel.h"
#include "Vehicle.h"
19
#include "QGCLoggingCategory.h"
20
#include "MavlinkQmlSingleton.h"
21
#include "VisualMissionItem.h"
22

23 24
class CoordinateVector;

25
Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)
26

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

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

37 38 39 40 41
    Q_PROPERTY(QmlObjectListModel*  visualItems         READ visualItems                                NOTIFY visualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  complexVisualItems  READ complexVisualItems                         NOTIFY complexVisualItemsChanged)
    Q_PROPERTY(QmlObjectListModel*  waypointLines       READ waypointLines                              NOTIFY waypointLinesChanged)
    Q_PROPERTY(bool                 autoSync            READ autoSync               WRITE setAutoSync   NOTIFY autoSyncChanged)
    Q_PROPERTY(bool                 syncInProgress      READ syncInProgress                             NOTIFY syncInProgressChanged)
42

43
    Q_INVOKABLE void start(bool editMode);
44 45
    Q_INVOKABLE void getMissionItems(void);
    Q_INVOKABLE void sendMissionItems(void);
Don Gagne's avatar
Don Gagne committed
46 47 48 49
    Q_INVOKABLE void loadMissionFromFilePicker(void);
    Q_INVOKABLE void loadMissionFromFile(const QString& filename);
    Q_INVOKABLE void saveMissionToFilePicker(void);
    Q_INVOKABLE void saveMissionToFile(const QString& filename);
50
    Q_INVOKABLE void removeMissionItem(int index);
51
    Q_INVOKABLE void removeAllMissionItems(void);
52

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

58 59 60
    /// Add a new complex mission item to the list
    ///     @param i: index to insert at
    /// @return Sequence number for new item
61
    Q_INVOKABLE int insertComplexMissionItem(QGeoCoordinate coordinate, int i);
62

63 64
    // Property accessors

65 66 67 68
    QmlObjectListModel* visualItems         (void) { return _visualItems; }
    QmlObjectListModel* complexVisualItems  (void) { return _complexItems; }
    QmlObjectListModel* waypointLines       (void) { return &_waypointLines; }

69 70
    bool autoSync(void) { return _autoSync; }
    void setAutoSync(bool autoSync);
71
    bool syncInProgress(void);
72

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

75
signals:
76 77
    void visualItemsChanged(void);
    void complexVisualItemsChanged(void);
78
    void waypointLinesChanged(void);
79
    void autoSyncChanged(bool autoSync);
80
    void newItemsFromVehicle(void);
81
    void syncInProgressChanged(bool syncInProgress);
82

83
private slots:
84
    void _newMissionItemsAvailableFromVehicle();
85
    void _itemCommandChanged(void);
86
    void _activeVehicleChanged(Vehicle* activeVehicle);
87 88 89 90
    void _activeVehicleHomePositionAvailableChanged(bool homePositionAvailable);
    void _activeVehicleHomePositionChanged(const QGeoCoordinate& homePosition);
    void _dirtyChanged(bool dirty);
    void _inProgressChanged(bool inProgress);
91
    void _currentMissionItemChanged(int sequenceNumber);
92
    void _recalcWaypointLines(void);
93
    void _recalcAltitudeRangeBearing();
94 95

private:
96
    void _recalcSequence(void);
97 98
    void _recalcChildItems(void);
    void _recalcAll(void);
99 100 101 102
    void _initAllVisualItems(void);
    void _deinitAllVisualItems(void);
    void _initVisualItem(VisualMissionItem* item);
    void _deinitVisualItem(VisualMissionItem* item);
103
    void _autoSyncSend(void);
Don Gagne's avatar
Don Gagne committed
104
    void _setupActiveVehicle(Vehicle* activeVehicle, bool forceLoadFromVehicle);
105
    static void _calcPrevWaypointValues(double homeAlt, VisualMissionItem* currentItem, VisualMissionItem* prevItem, double* azimuth, double* distance, double* altDifference);
106
    bool _findLastAltitude(double* lastAltitude, MAV_FRAME* frame);
107
    bool _findLastAcceptanceRadius(double* lastAcceptanceRadius);
108
    void _addPlannedHomePosition(QmlObjectListModel* visualItems, bool addToCenter);
109 110
    double _normalizeLat(double lat);
    double _normalizeLon(double lon);
111
    bool _loadJsonMissionFile(const QByteArray& bytes, QmlObjectListModel* visualItems, QmlObjectListModel* complexItems, QString& errorString);
112
    bool _loadTextMissionFile(QTextStream& stream, QmlObjectListModel* visualItems, QString& errorString);
113
    int _nextSequenceNumber(void);
114 115

private:
116
    bool                _editMode;
117 118
    QmlObjectListModel* _visualItems;
    QmlObjectListModel* _complexItems;
119
    QmlObjectListModel  _waypointLines;
Nate Weibley's avatar
Nate Weibley committed
120
    CoordVectHashTable  _linesTable;
121
    Vehicle*            _activeVehicle;
122
    bool                _autoSync;
123
    bool                _firstItemsFromVehicle;
124 125 126
    bool                _missionItemsRequested;
    bool                _queuedSend;

127 128 129 130
    static const char*  _settingsGroup;
    static const char*  _jsonVersionKey;
    static const char*  _jsonGroundStationKey;
    static const char*  _jsonMavAutopilotKey;
131
    static const char*  _jsonComplexItemsKey;
132
    static const char*  _jsonPlannedHomePositionKey;
133 134 135
};

#endif