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

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

#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
#include "QmlObjectVectorModel.h"
27

28 29 30

class WimaController : public QObject
{
31 32
    Q_OBJECT

33 34
    enum FileType {WimaFile, PlanFile};

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


39 40
    Q_PROPERTY(PlanMasterController*    masterController        READ masterController       WRITE setMasterController       NOTIFY masterControllerChanged)
    Q_PROPERTY(MissionController*       missionController       READ missionController      WRITE setMissionController      NOTIFY missionControllerChanged)
41
    Q_PROPERTY(QmlObjectVectorModel*      visualItems             READ visualItems                                            NOTIFY visualItemsChanged)
42 43 44 45 46
    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)
47
    Q_PROPERTY(QmlObjectVectorModel*      missionItems            READ missionItems                                           NOTIFY missionItemsChanged)
48 49 50 51 52 53 54
    Q_PROPERTY(QVariantList             waypointPath            READ waypointPath                                           NOTIFY waypointPathChanged)
    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)
55 56
    Q_PROPERTY(Fact*                    flightSpeed             READ flightSpeed                                            CONSTANT)
    Q_PROPERTY(Fact*                    altitude                READ altitude                                               CONSTANT)
57
    Q_PROPERTY(Fact*                    arrivalReturnSpeed      READ arrivalReturnSpeed                                     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
    QmlObjectVectorModel*         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
    QmlObjectVectorModel*         missionItems           (void);
77
    QVariantList                waypointPath           (void);
78 79 80 81
    Fact*                       enableWimaController   (void);
    Fact*                       overlapWaypoints       (void);
    Fact*                       maxWaypointsPerPhase   (void);
    Fact*                       startWaypointIndex     (void);
82 83
    Fact*                       showAllMissionItems    (void);
    Fact*                       showCurrentMissionItems(void);
84
    Fact*                       flightSpeed            (void);
85
    Fact*                       arrivalReturnSpeed     (void);
86 87
    Fact*                       altitude               (void);    
    Fact*                       reverse                (void);
88
    bool                        uploadOverrideRequired (void) const;
89 90
    double                      phaseDistance          (void) const;
    double                      phaseDuration          (void) const;
91
    bool                        vehicleHasLowBattery   (void) const;
92 93 94 95 96


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

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

112

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


119 120
    // static Members
    static const char* wimaFileExtension;
121
    static const char* areaItemsName;
122 123 124 125 126 127 128 129 130
    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;
131
    static const char* flightSpeedName;
132
    static const char* arrivalReturnSpeedName;
133 134
    static const char* altitudeName;    
    static const char* reverseName;
135 136

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

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

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 waypointPathChanged            (void);
158
    void uploadOverrideRequiredChanged  (void);
159 160
    void phaseDistanceChanged           (void);
    void phaseDurationChanged           (void);
161 162 163
    void uploadAndExecuteConfirmRequired(void);
    void vehicleHasLowBatteryChanged    (void);
    void returnBatteryLowConfirmRequired(void);
164 165

private slots:
166 167
    bool fetchContainerData();
    bool calcNextPhase(void);
168
    void updateWaypointPath                 (void);
169
    void updateNextWaypoint                 (void);
170
    void recalcCurrentPhase                 (void);
171
    bool setTakeoffLandPosition             (void);
172 173
    void updateflightSpeed                  (void);
    void updateArrivalReturnSpeed           (void);
174
    void updateAltitude                     (void);
175
    void checkBatteryLevel                  (void);
176
    void smartRTLCleanUp                    (bool flying); // cleans up after successfull smart RTL
177
    void enableDisableLowBatteryHandling    (QVariant enable);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
178
    void reverseChangedHandler              ();
179 180 181 182

private:
    void _setPhaseDistance(double distance);
    void _setPhaseDuration(double duration);
183 184 185
    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);
186 187 188
    bool _executeSmartRTL(QString &errorSring);
    void _loadCurrentMissionItemsFromBuffer();
    void _saveCurrentMissionItemsToBuffer();
189

190
private:
191 192 193 194
    PlanMasterController   *_masterController;
    MissionController      *_missionController;
    QString                 _currentFile; // file for saveing
    WimaDataContainer      *_container; // container for data exchange with WimaController
195
    QmlObjectVectorModel    _visualItems; // contains all visible areas
196
    WimaJoinedAreaData      _joinedArea; // joined area fromed by opArea, serArea, _corridor
197 198 199 200
    WimaMeasurementAreaData _measurementArea; // measurement area
    WimaServiceAreaData     _serviceArea; // area for supplying
    WimaCorridorData        _corridor; // corridor connecting opArea and serArea
    bool                    _localPlanDataValid;
201 202
    QmlObjectVectorModel    _missionItems; // all mission itmes (Mission Items) generaded by wimaPlaner, displayed in flightView
    QmlObjectVectorModel    _missionItemsBuffer; // Buffer to store mission items, e.g. for storing _missionController->visualItems() when smartRTL() is invoked
203
    QVariantList            _waypointPath; // path connecting the items in _missionItems
204
    QGeoCoordinate          _takeoffLandPostion;
205

206 207 208 209 210

    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
211
    SettingsFact                 _nextPhaseStartWaypointIndex; // index (displayed on the map, -1 to get index of item in _missionItems) of the mission item
212
                                                      // defining the first element of the next phase
213 214
    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.
215
    SettingsFact                 _flightSpeed; // mission flight speed
216
    SettingsFact                 _arrivalReturnSpeed; // arrival and return path speed
217
    SettingsFact                 _altitude; // mission altitude
218
    SettingsFact                 _reverse; // Reverses the phase direction. Phases go from high to low waypoint numbers, if true.
219

220
    int                          _endWaypointIndex; // index of the mission item stored in _missionItems defining the last element
221
                                                    // (which is not part of the return path) of _missionController.visualItems()
222
    int                          _startWaypointIndex; // index of the mission item stored in _missionItems defining the first element
223
                                                      // (which is not part of the arrival path) of _missionController.visualItems()
224 225
    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.
226 227 228
    double                       _measurementPathLength; // the lenght of the phase in meters
    double                       _arrivalPathLength; // the length of the arrival and return path in meters
    double                       _returnPathLength; // the length of the arrival and return path in meters
229 230
    double                       _phaseDistance; // the lenth in meters of the current phase
    double                       _phaseDuration; // the phase duration in seconds
231 232 233 234

    QTimer                       _checkBatteryTimer;
    bool                         _vehicleHasLowBattery;
    bool                         _lowBatteryHandlingTriggered;
235 236
    bool                         _executingSmartRTL;

237
};
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/*
 * The following explains the structure of
 * _missionController.visualItems(). The indices
 * are not that important and only specified for
 * reasons of completeness.
 *
 * Index        Description
 * --------------------------------------------
 * 0            MissionSettingsItem
 * 1            Takeoff Command
 * 2            Speed Command: arrivalReturnSpeed
 * 3            Arrival Path Waypoint 0
 * ...
 * 3+n-1        Arrival Path Waypoint n-1
 * 3+n          Speed Command: flightSpeed
 * 3+n+1        Circular Survey Waypoint 0
 * ...
 * 3+n+m        Circular Survey Waypoint m-1
 * 3+n+m+1      Speed Command: arrivalReturnSpeed
 * 3+n+m+2      Return Path Waypoint 0
 * ...
 * 3+n+m+2+l    Return Path Waypoint l-1
 * 3+n+m+2+l+1  Land command
 *
 */

265