WimaController.h 14.8 KB
Newer Older
1
#pragma once
2 3

#include <QObject>
4 5 6 7
#include "QGCMapPolygon.h"
#include "QmlObjectListModel.h"

#include "WimaArea.h"
8
#include "WimaMeasurementArea.h"
9
#include "WimaServiceArea.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
10
#include "WimaCorridor.h"
11
#include "WimaDataContainer.h"
12 13 14
#include "WimaMeasurementAreaData.h"
#include "WimaCorridorData.h"
#include "WimaServiceAreaData.h"
15 16 17

#include "PlanMasterController.h"
#include "MissionController.h"
18
#include "SurveyComplexItem.h"
19 20
#include "SimpleMissionItem.h"
#include "MissionSettingsItem.h"
21 22
#include "JsonHelper.h"
#include "QGCApplication.h"
23
#include "SettingsFact.h"
24 25
#include "WimaSettings.h"
#include "SettingsManager.h"
26

27 28 29

class WimaController : public QObject
{
30 31
    Q_OBJECT

32 33
    enum FileType {WimaFile, PlanFile};

34
public:
35 36 37
    WimaController(QObject *parent = nullptr);


38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    Q_PROPERTY(PlanMasterController*    masterController        READ masterController       WRITE setMasterController       NOTIFY masterControllerChanged)
    Q_PROPERTY(MissionController*       missionController       READ missionController      WRITE setMissionController      NOTIFY missionControllerChanged)
    Q_PROPERTY(QmlObjectListModel*      visualItems             READ visualItems                                            NOTIFY visualItemsChanged)
    Q_PROPERTY(QString                  currentFile             READ currentFile                                            NOTIFY currentFileChanged)
    Q_PROPERTY(QStringList              loadNameFilters         READ loadNameFilters                                        CONSTANT)
    Q_PROPERTY(QStringList              saveNameFilters         READ saveNameFilters                                        CONSTANT)
    Q_PROPERTY(QString                  fileExtension           READ fileExtension                                          CONSTANT)
    Q_PROPERTY(WimaDataContainer*       dataContainer           READ dataContainer          WRITE setDataContainer          NOTIFY dataContainerChanged)
    Q_PROPERTY(QmlObjectListModel*      missionItems            READ missionItems                                           NOTIFY missionItemsChanged)
    Q_PROPERTY(QmlObjectListModel*      currentMissionItems     READ currentMissionItems                                    NOTIFY currentMissionItemsChanged)
    Q_PROPERTY(QVariantList             waypointPath            READ waypointPath                                           NOTIFY waypointPathChanged)
    Q_PROPERTY(QVariantList             currentWaypointPath     READ currentWaypointPath                                    NOTIFY currentWaypointPathChanged)
    Q_PROPERTY(Fact*                    enableWimaController    READ enableWimaController                                   CONSTANT)
    Q_PROPERTY(Fact*                    overlapWaypoints        READ overlapWaypoints                                       CONSTANT)
    Q_PROPERTY(Fact*                    maxWaypointsPerPhase    READ maxWaypointsPerPhase                                   CONSTANT)
    Q_PROPERTY(Fact*                    startWaypointIndex      READ startWaypointIndex                                     CONSTANT)
    Q_PROPERTY(Fact*                    showAllMissionItems     READ showAllMissionItems                                    CONSTANT)
    Q_PROPERTY(Fact*                    showCurrentMissionItems READ showCurrentMissionItems                                CONSTANT)
56 57
    Q_PROPERTY(Fact*                    flightSpeed             READ flightSpeed                                            CONSTANT)
    Q_PROPERTY(Fact*                    altitude                READ altitude                                               CONSTANT)
58
    Q_PROPERTY(Fact*                    reverse                 READ reverse                                                CONSTANT)
59
    Q_PROPERTY(bool                     uploadOverrideRequired  READ uploadOverrideRequired WRITE setUploadOverrideRequired NOTIFY uploadOverrideRequiredChanged)
60 61
    Q_PROPERTY(double                   phaseDistance           READ phaseDistance                                          NOTIFY phaseDistanceChanged)
    Q_PROPERTY(double                   phaseDuration           READ phaseDuration                                          NOTIFY phaseDurationChanged)
62
    Q_PROPERTY(bool                     vehicleHasLowBattery    READ vehicleHasLowBattery                                   NOTIFY vehicleHasLowBatteryChanged)
63

64 65 66


    // Property accessors
67 68
    PlanMasterController*       masterController       (void)          { return _masterController; }
    MissionController*          missionController      (void)          { return _missionController; }
69
    QmlObjectListModel*         visualItems            (void);
70
    QString                     currentFile            (void) const    { return _currentFile; }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
71
    QStringList                 loadNameFilters        (void) const;
72
    QStringList                 saveNameFilters        (void) const;
73
    QString                     fileExtension          (void) const    { return wimaFileExtension; }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
74
    QGCMapPolygon               joinedArea             (void) const;
75
    WimaDataContainer*          dataContainer          (void);
76
    QmlObjectListModel*         missionItems           (void);
77
    QmlObjectListModel*         currentMissionItems    (void);
78
    QVariantList                waypointPath           (void);
79
    QVariantList                currentWaypointPath    (void);
80 81 82 83
    Fact*                       enableWimaController   (void);
    Fact*                       overlapWaypoints       (void);
    Fact*                       maxWaypointsPerPhase   (void);
    Fact*                       startWaypointIndex     (void);
84 85
    Fact*                       showAllMissionItems    (void);
    Fact*                       showCurrentMissionItems(void);
86
    Fact*                       flightSpeed            (void);
87 88
    Fact*                       altitude               (void);    
    Fact*                       reverse                (void);
89
    bool                        uploadOverrideRequired (void) const;
90 91
    double                      phaseDistance          (void) const;
    double                      phaseDuration          (void) const;
92
    bool                        vehicleHasLowBattery   (void) const;
93 94 95 96 97


    // Property setters
    void setMasterController        (PlanMasterController* masterController);
    void setMissionController       (MissionController* missionController);
98
    void setDataContainer           (WimaDataContainer* container);
99
    void setUploadOverrideRequired  (bool overrideRequired);
100

101
    // Member Methodes
102
    Q_INVOKABLE void nextPhase();
103 104
    Q_INVOKABLE void previousPhase();
    Q_INVOKABLE void resetPhase();
105
    Q_INVOKABLE bool uploadToVehicle();
106
    Q_INVOKABLE bool forceUploadToVehicle();
107 108
    Q_INVOKABLE void removeFromVehicle();    
    Q_INVOKABLE bool checkSmartRTLPreCondition(); // wrapper for _checkSmartRTLPreCondition(QString &errorString)
109 110 111
    Q_INVOKABLE bool calcReturnPath(); // wrapper for _calcReturnPath(QString &errorSring)#
    Q_INVOKABLE bool executeSmartRTL(); // wrapper for _executeSmartRTL(QString &errorSring)
    Q_INVOKABLE bool initSmartRTL();
112

113

114 115
    Q_INVOKABLE void saveToCurrent  ();
    Q_INVOKABLE void saveToFile     (const QString& filename);
116
    Q_INVOKABLE bool loadFromCurrent();
117
    Q_INVOKABLE bool loadFromFile   (const QString& filename);
118 119


120 121
    // static Members
    static const char* wimaFileExtension;
122
    static const char* areaItemsName;
123 124 125 126 127 128 129 130 131
    static const char* missionItemsName;    
    static const char* settingsGroup;
    static const char* endWaypointIndexName;
    static const char* enableWimaControllerName;
    static const char* overlapWaypointsName;
    static const char* maxWaypointsPerPhaseName;
    static const char* startWaypointIndexName;
    static const char* showAllMissionItemsName;
    static const char* showCurrentMissionItemsName;
132
    static const char* flightSpeedName;
133 134
    static const char* altitudeName;    
    static const char* reverseName;
135 136

    // Member Methodes
137
    QJsonDocument saveToJson(FileType fileType);
138

139 140 141 142 143 144 145 146 147 148
    bool calcShortestPath(const QGeoCoordinate &start, const QGeoCoordinate &destination, QList<QGeoCoordinate> &path);
    /// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList
    bool extractCoordinateList(QmlObjectListModel &missionItems, QList<QGeoCoordinate> &coordinateList);
    /// extracts the coordinates (between startIndex and endIndex) stored in missionItems (list of MissionItems) and stores them in coordinateList.
    bool extractCoordinateList(QmlObjectListModel &missionItems, QList<QGeoCoordinate> &coordinateList, int startIndex, int endIndex);
    /// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVariantList &coordinateList);
    /// extracts the coordinates (between startIndex and endIndex) stored in missionItems (list of MissionItems) and stores them in coordinateList.
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVariantList &coordinateList, int startIndex, int endIndex);

149
signals:
150 151 152
    void masterControllerChanged        (void);
    void missionControllerChanged       (void);
    void visualItemsChanged             (void);
153
    void currentFileChanged             ();
154
    void dataContainerChanged           ();
155
    void readyForSaveSendChanged        (bool ready);
156
    void missionItemsChanged            (void);
157
    void currentMissionItemsChanged     (void);
158
    void waypointPathChanged            (void);
159
    void currentWaypointPathChanged     (void);
160
    void uploadOverrideRequiredChanged  (void);
161 162
    void phaseDistanceChanged           (void);
    void phaseDurationChanged           (void);
163 164 165
    void uploadAndExecuteConfirmRequired(void);
    void vehicleHasLowBatteryChanged    (void);
    void returnBatteryLowConfirmRequired(void);
166 167

private slots:
168 169
    bool fetchContainerData();
    bool calcNextPhase(void);
170
    void updateWaypointPath                 (void);
171
    void updateCurrentPath                  (void);
172
    void updateNextWaypoint                 (void);
173
    void recalcCurrentPhase                 (void);
174 175 176
    bool setTakeoffLandPosition             (void);
    void updateSpeed                        (void);
    void updateAltitude                     (void);
177
    void checkBatteryLevel                  (void);
178
    void smartRTLCleanUp                    (bool flying); // cleans up after successfull smart RTL
179
    void enableDisableLowBatteryHandling    (QVariant enable);
180 181 182 183

private:
    void _setPhaseDistance(double distance);
    void _setPhaseDuration(double duration);
184 185 186
    bool _checkSmartRTLPreCondition(QString &errorString); // should be called from gui, befor calcReturnPath()
    bool _calcReturnPath(QString &errorSring); // Calculates return path (destination: service area center) for a flying vehicle
    void _setVehicleHasLowBattery(bool batteryLow);
187 188 189
    bool _executeSmartRTL(QString &errorSring);
    void _loadCurrentMissionItemsFromBuffer();
    void _saveCurrentMissionItemsToBuffer();
190

191
private:
192 193 194 195 196
    PlanMasterController   *_masterController;
    MissionController      *_missionController;
    QString                 _currentFile; // file for saveing
    WimaDataContainer      *_container; // container for data exchange with WimaController
    QmlObjectListModel      _visualItems; // contains all visible areas
197
    WimaJoinedAreaData      _joinedArea; // joined area fromed by opArea, serArea, _corridor
198 199 200 201
    WimaMeasurementAreaData _measurementArea; // measurement area
    WimaServiceAreaData     _serviceArea; // area for supplying
    WimaCorridorData        _corridor; // corridor connecting opArea and serArea
    bool                    _localPlanDataValid;
202 203 204
    QmlObjectListModel      _missionItems; // all mission itmes (Mission Items) generaded by wimaPlaner, displayed in flightView
    QmlObjectListModel      _currentMissionItems; // contains the current mission items, which are a sub set of _missionItems,
                                                  // _currentMissionItems contains a number of mission items which can be worked off with a single battery chrage
205
    QmlObjectListModel      _missionItemsBuffer; // Buffer to store mission items, e.g. for storing _currentMissionItems when smartRTL() is invoked
206 207
    QVariantList            _waypointPath; // path connecting the items in _missionItems
    QVariantList            _currentWaypointPath; // path connecting the items in _currentMissionItems
208
    QGeoCoordinate          _takeoffLandPostion;
209

210 211 212 213 214

    QMap<QString, FactMetaData*> _metaDataMap;
    SettingsFact                 _enableWimaController; // enables or disables the wimaControler
    SettingsFact                 _overlapWaypoints; // determines the number of overlapping waypoints between two consecutive mission phases
    SettingsFact                 _maxWaypointsPerPhase; // determines the maximum number waypoints per phase
215
    SettingsFact                 _nextPhaseStartWaypointIndex; // index (displayed on the map, -1 to get index of item in _missionItems) of the mission item
216
                                                      // defining the first element of the next phase
217 218
    SettingsFact                 _showAllMissionItems; // bool value, Determines whether the mission items of the overall mission are displayed or not.
    SettingsFact                 _showCurrentMissionItems; // bool value, Determines whether the mission items of the current mission phase are displayed or not.
219 220
    SettingsFact                 _flightSpeed; // mission flight speed
    SettingsFact                 _altitude; // mission altitude
221
    SettingsFact                 _reverse; // Reverses the phase direction. Phases go from high to low waypoint numbers, if true.
222 223 224

    int                          _endWaypointIndex; // indes of the mission item stored in _missionItems defining the last element
                                                    // (which is not part of the return path) of _currentMissionItem
225 226
    int                          _startWaypointIndex; // indes of the mission item stored in _missionItems defining the first element
                                                      // (which is not part of the arrival path) of _currentMissionItem
227
    bool                         _lastMissionPhaseReached;
228 229
    bool                         _uploadOverrideRequired; // Is set to true if uploadToVehicle() did not suceed because the vehicle is not inside the service area.
                                                          // The user can override the upload lock with a slider, this will reset this variable to false.
230 231
    double                       _phaseDistance; // the lenth in meters of the current phase
    double                       _phaseDuration; // the phase duration in seconds
232 233 234 235

    QTimer                       _checkBatteryTimer;
    bool                         _vehicleHasLowBattery;
    bool                         _lowBatteryHandlingTriggered;
236 237 238
    bool                         _executingSmartRTL;


239
};
240 241