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

#include <QObject>
4
#include "QGCMapPolygon.h"
5
#include "QmlObjectListModel.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

Valentin Platzgummer's avatar
Valentin Platzgummer committed
27 28
#include "snake.h"
#include "WimaControllerDetail.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
29

Valentin Platzgummer's avatar
Valentin Platzgummer committed
30 31
#define CHECK_BATTERY_INTERVAL 1000 // ms
#define SMART_RTL_MAX_ATTEMPTS 3 // times
32
#define SMART_RTL_ATTEMPT_INTERVAL 200 // ms
Valentin Platzgummer's avatar
Valentin Platzgummer committed
33 34
#define EVENT_TIMER_INTERVAL 50 // ms
#define SNAKE_EVENT_LOOP_INTERVAL 50 // ms
35

36

Valentin Platzgummer's avatar
Valentin Platzgummer committed
37 38
using namespace snake;

39 40
class WimaController : public QObject
{
41 42
    Q_OBJECT

43 44
    enum FileType {WimaFile, PlanFile};

45
public:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
46 47 48 49

    enum SnakeConnectionStatus {Connected = 1, NotConnected = 0};


50 51 52
    WimaController(QObject *parent = nullptr);


53 54
    Q_PROPERTY(PlanMasterController*    masterController        READ masterController       WRITE setMasterController       NOTIFY masterControllerChanged)
    Q_PROPERTY(MissionController*       missionController       READ missionController      WRITE setMissionController      NOTIFY missionControllerChanged)
55
    Q_PROPERTY(QmlObjectListModel*      visualItems             READ visualItems                                            NOTIFY visualItemsChanged)
56 57 58 59 60
    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)
61 62
    Q_PROPERTY(QmlObjectListModel*      missionItems            READ missionItems                                           NOTIFY missionItemsChanged)
    Q_PROPERTY(QmlObjectListModel*      currentMissionItems     READ currentMissionItems                                    NOTIFY currentMissionItemsChanged)
63
    Q_PROPERTY(QVariantList             waypointPath            READ waypointPath                                           NOTIFY waypointPathChanged)
64
    Q_PROPERTY(QVariantList             currentWaypointPath     READ currentWaypointPath                                    NOTIFY currentWaypointPathChanged)
65 66 67 68 69 70
    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)
71 72
    Q_PROPERTY(Fact*                    flightSpeed             READ flightSpeed                                            CONSTANT)
    Q_PROPERTY(Fact*                    altitude                READ altitude                                               CONSTANT)
73
    Q_PROPERTY(Fact*                    arrivalReturnSpeed      READ arrivalReturnSpeed                                     CONSTANT)
74
    Q_PROPERTY(Fact*                    reverse                 READ reverse                                                CONSTANT)
75
    Q_PROPERTY(bool                     uploadOverrideRequired  READ uploadOverrideRequired WRITE setUploadOverrideRequired NOTIFY uploadOverrideRequiredChanged)
76 77
    Q_PROPERTY(double                   phaseDistance           READ phaseDistance                                          NOTIFY phaseDistanceChanged)
    Q_PROPERTY(double                   phaseDuration           READ phaseDuration                                          NOTIFY phaseDurationChanged)
78
    Q_PROPERTY(bool                     vehicleHasLowBattery    READ vehicleHasLowBattery                                   NOTIFY vehicleHasLowBatteryChanged)
79

Valentin Platzgummer's avatar
Valentin Platzgummer committed
80 81 82 83 84 85 86 87 88 89
    // Snake
    Q_PROPERTY(Fact*                    enableSnake                 READ enableSnake                                            CONSTANT)
    Q_PROPERTY(long                     snakeConnectionStatus       READ snakeConnectionStatus                                  NOTIFY snakeConnectionStatusChanged)
    Q_PROPERTY(bool                     snakeCalcInProgress         READ snakeCalcInProgress                                    NOTIFY snakeCalcInProgressChanged)
    Q_PROPERTY(Fact*                    snakeTileWidth              READ snakeTileWidth                                         CONSTANT)
    Q_PROPERTY(Fact*                    snakeTileHeight             READ snakeTileHeight                                        CONSTANT)
    Q_PROPERTY(Fact*                    snakeMinTileArea            READ snakeMinTileArea                                       CONSTANT)
    Q_PROPERTY(Fact*                    snakeLineDistance           READ snakeLineDistance                                      CONSTANT)
    Q_PROPERTY(Fact*                    snakeMinTransectLength      READ snakeMinTransectLength                                 CONSTANT)

90 91 92


    // Property accessors
93 94
    PlanMasterController*       masterController       (void)          { return _masterController; }
    MissionController*          missionController      (void)          { return _missionController; }
95
    QmlObjectListModel*         visualItems            (void);
96
    QString                     currentFile            (void) const    { return _currentFile; }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
97
    QStringList                 loadNameFilters        (void) const;
98
    QStringList                 saveNameFilters        (void) const;
99
    QString                     fileExtension          (void) const    { return wimaFileExtension; }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
100
    QGCMapPolygon               joinedArea             (void) const;
101
    WimaDataContainer*          dataContainer          (void);
102 103
    QmlObjectListModel*         missionItems           (void);
    QmlObjectListModel*         currentMissionItems    (void);
104
    QVariantList                waypointPath           (void);
105
    QVariantList                currentWaypointPath    (void);
106 107 108 109
    Fact*                       enableWimaController   (void);
    Fact*                       overlapWaypoints       (void);
    Fact*                       maxWaypointsPerPhase   (void);
    Fact*                       startWaypointIndex     (void);
110 111
    Fact*                       showAllMissionItems    (void);
    Fact*                       showCurrentMissionItems(void);
112
    Fact*                       flightSpeed            (void);
113
    Fact*                       arrivalReturnSpeed     (void);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
114
    Fact*                       altitude               (void);
115
    Fact*                       reverse                (void);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
116 117 118 119 120 121
    Fact*                       enableSnake            (void);
    Fact*                       snakeTileWidth         (void) {return &_snakeTileWidth;}
    Fact*                       snakeTileHeight        (void) {return &_snakeTileHeight;}
    Fact*                       snakeMinTileArea       (void) {return &_snakeMinTileArea;}
    Fact*                       snakeLineDistance      (void) {return &_snakeLineDistance;}
    Fact*                       snakeMinTransectLength (void) {return &_snakeMinTransectLength;}
122
    bool                        uploadOverrideRequired (void) const;
123 124
    double                      phaseDistance          (void) const;
    double                      phaseDuration          (void) const;
125
    bool                        vehicleHasLowBattery   (void) const;
126

Valentin Platzgummer's avatar
Valentin Platzgummer committed
127 128 129
    long                        snakeConnectionStatus (void) const;
    bool                        snakeCalcInProgress   (void) const;

130 131 132 133

    // Property setters
    void setMasterController        (PlanMasterController* masterController);
    void setMissionController       (MissionController* missionController);
134
    void setDataContainer           (WimaDataContainer* container);
135
    void setUploadOverrideRequired  (bool overrideRequired);
136

137
    // Member Methodes
138
    Q_INVOKABLE void nextPhase();
139 140
    Q_INVOKABLE void previousPhase();
    Q_INVOKABLE void resetPhase();
141
    Q_INVOKABLE bool uploadToVehicle();
142
    Q_INVOKABLE bool forceUploadToVehicle();
143 144
    Q_INVOKABLE void removeFromVehicle();    
    Q_INVOKABLE bool checkSmartRTLPreCondition(); // wrapper for _checkSmartRTLPreCondition(QString &errorString)
145
    Q_INVOKABLE bool calcReturnPath(); // wrapper for _calcReturnPath(QString &errorSring)#
146 147
    Q_INVOKABLE void executeSmartRTL(); // wrapper for _executeSmartRTL(QString &errorSring)
    Q_INVOKABLE void initSmartRTL();
148
    Q_INVOKABLE void removeVehicleTrajectoryHistory();
149

150

151 152
    Q_INVOKABLE void saveToCurrent  ();
    Q_INVOKABLE void saveToFile     (const QString& filename);
153
    Q_INVOKABLE bool loadFromCurrent();
154
    Q_INVOKABLE bool loadFromFile   (const QString& filename);
155 156


157 158
    // static Members
    static const char* wimaFileExtension;
159
    static const char* areaItemsName;
160 161 162 163 164 165 166 167 168
    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;
169
    static const char* flightSpeedName;
170
    static const char* arrivalReturnSpeedName;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
171
    static const char* altitudeName;
172
    static const char* reverseName;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
173 174 175 176 177
    static const char* snakeTileWidthName;
    static const char* snakeTileHeightName;
    static const char* snakeMinTileAreaName;
    static const char* snakeLineDistanceName;
    static const char* snakeMinTransectLengthName;
178 179

    // Member Methodes
180
    QJsonDocument saveToJson(FileType fileType);
181

182
    bool calcShortestPath(const QGeoCoordinate &start, const QGeoCoordinate &destination, QVector<QGeoCoordinate> &path);
183
    /// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList
184
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVector<QGeoCoordinate> &coordinateList);
185
    /// extracts the coordinates (between startIndex and endIndex) stored in missionItems (list of MissionItems) and stores them in coordinateList.
186
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVector<QGeoCoordinate> &coordinateList, int startIndex, int endIndex);
187
    /// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList
188
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVariantList &coordinateList);
189
    /// extracts the coordinates (between startIndex and endIndex) stored in missionItems (list of MissionItems) and stores them in coordinateList.
190
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVariantList &coordinateList, int startIndex, int endIndex);
191

192
signals:
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    void masterControllerChanged            (void);
    void missionControllerChanged           (void);
    void visualItemsChanged                 (void);
    void currentFileChanged                 ();
    void dataContainerChanged               ();
    void readyForSaveSendChanged            (bool ready);
    void missionItemsChanged                (void);
    void currentMissionItemsChanged         (void);
    void waypointPathChanged                (void);
    void currentWaypointPathChanged         (void);
    void uploadOverrideRequiredChanged      (void);
    void phaseDistanceChanged               (void);
    void phaseDurationChanged               (void);
    void vehicleHasLowBatteryChanged        (void);
    void returnBatteryLowConfirmRequired    (void);
    void returnUserRequestConfirmRequired   (void);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
209 210
    void snakeConnectionStatusChanged       (void);
    void snakeCalcInProgressChanged         (void);
211 212
private:
    enum SRTL_Reason {BatteryLow, UserRequest};
213
private slots:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    bool _fetchContainerData();
    bool _calcNextPhase(void);
    void _updateWaypointPath                 (void);
    void _updateCurrentPath                  (void);
    void _updateNextWaypoint                 (void);
    void _recalcCurrentPhase                 (void);
    bool _setTakeoffLandPosition             (void);
    void _updateflightSpeed                  (void);
    void _updateArrivalReturnSpeed           (void);
    void _updateAltitude                     (void);
    void _checkBatteryLevel                  (void);
    void _eventTimerHandler                  (void);
    void _smartRTLCleanUp                    (bool flying); // cleans up after successfull smart RTL
    void _enableDisableLowBatteryHandling    (QVariant enable);
    void _reverseChangedHandler              ();
    void _initSmartRTL                       ();
    void _executeSmartRTL                    ();
    void _setSnakeConnectionStatus           (SnakeConnectionStatus status);
    void _setSnakeCalcInProgress             (bool inProgress);
    bool _verifyScenarioDefined                    (void);
    bool _verifyScenarioDefinedWithErrorMessage    (void);
235
    void _snakeStoreWorkerResults            (const WorkerResult_t &r);
236

237 238 239 240

private:
    void _setPhaseDistance(double distance);
    void _setPhaseDuration(double duration);
241 242 243
    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);
244 245
    void _loadCurrentMissionItemsFromBuffer();
    void _saveCurrentMissionItemsToBuffer();
246

247 248 249 250
    PlanMasterController   *_masterController;
    MissionController      *_missionController;
    QString                 _currentFile; // file for saveing
    WimaDataContainer      *_container; // container for data exchange with WimaController
251
    QmlObjectListModel      _visualItems; // contains all visible areas
252
    WimaJoinedAreaData      _joinedArea; // joined area fromed by opArea, serArea, _corridor
253 254 255 256
    WimaMeasurementAreaData _measurementArea; // measurement area
    WimaServiceAreaData     _serviceArea; // area for supplying
    WimaCorridorData        _corridor; // corridor connecting opArea and serArea
    bool                    _localPlanDataValid;
257 258 259 260
    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
    QmlObjectListModel      _missionItemsBuffer; // Buffer to store mission items, e.g. for storing _currentMissionItems when smartRTL() is invoked
261
    QVariantList            _waypointPath; // path connecting the items in _missionItems
262
    QVariantList            _currentWaypointPath; // path connecting the items in _currentMissionItems
263
    QGeoCoordinate          _takeoffLandPostion;
264

265 266 267 268 269

    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
270
    SettingsFact                 _nextPhaseStartWaypointIndex; // index (displayed on the map, -1 to get index of item in _missionItems) of the mission item
271
                                                      // defining the first element of the next phase
272 273
    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.
274
    SettingsFact                 _flightSpeed; // mission flight speed
275
    SettingsFact                 _arrivalReturnSpeed; // arrival and return path speed
276
    SettingsFact                 _altitude; // mission altitude
277
    SettingsFact                 _reverse; // Reverses the phase direction. Phases go from high to low waypoint numbers, if true.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
278
    SettingsFact                 _enableSnake; // Enable Snake (see snake.h)
279

280
    int                          _endWaypointIndex; // index of the mission item stored in _missionItems defining the last element
281
                                                    // (which is not part of the return path) of _currentMissionItem
282
    int                          _startWaypointIndex; // index of the mission item stored in _missionItems defining the first element
283
                                                      // (which is not part of the arrival path) of _currentMissionItem
284 285
    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.
286 287 288
    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
289 290
    double                       _phaseDistance; // the lenth in meters of the current phase
    double                       _phaseDuration; // the phase duration in seconds
291 292
    double                       _phaseDistanceBuffer; // buffer for storing _phaseDistance when doing smart RTL
    double                       _phaseDurationBuffer; // buffer for storing _phaseDuration when doing smart RTL
293

Valentin Platzgummer's avatar
Valentin Platzgummer committed
294
    QTimer                       _eventTimer;
295 296 297
    QTimer                       _smartRTLAttemptTimer;
    SRTL_Reason                  _srtlReason;

298 299
    bool                         _vehicleHasLowBattery;
    bool                         _lowBatteryHandlingTriggered;
300 301
    bool                         _executingSmartRTL;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
302 303 304 305 306 307 308 309 310 311 312 313
    // Snake
    SnakeConnectionStatus        _snakeConnectionStatus;
    bool                         _snakeCalcInProgress;
    bool                         _snakeRecalcNecessary;
    bool                         _scenarioDefinedBool;
    SnakeWorker                  _snakeWorker;
    Scenario                     _scenario;
    SettingsFact                 _snakeTileWidth;
    SettingsFact                 _snakeTileHeight;
    SettingsFact                 _snakeMinTileArea;
    SettingsFact                 _snakeLineDistance;
    SettingsFact                 _snakeMinTransectLength;
314
    QmlObjectListModel           _snakeTiles; // tiles
Valentin Platzgummer's avatar
Valentin Platzgummer committed
315 316


317
};
318

319 320
/*
 * The following explains the structure of
Valentin Platzgummer's avatar
Valentin Platzgummer committed
321
 * _missionController.visualItems().
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
 *
 * 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
 *
341 342 343
 * _currentMissionItems is equal to
 * _missionController.visualItems() except that it
 * is missing the MissionSettingsItem
344 345
 */

346