WimaController.h 17.2 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/snake.h"

29 30 31 32
#define CHECK_BATTERY_INTERVAL 1000
#define SMART_RTL_MAX_ATTEMPTS 3
#define SMART_RTL_ATTEMPT_INTERVAL 200 // ms

33

Valentin Platzgummer's avatar
Valentin Platzgummer committed
34 35 36
using namespace snake;


37 38
class WimaController : public QObject
{
39 40
    Q_OBJECT

41 42
    enum FileType {WimaFile, PlanFile};

43
public:
44 45 46
    WimaController(QObject *parent = nullptr);


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

74 75 76


    // Property accessors
77 78
    PlanMasterController*       masterController       (void)          { return _masterController; }
    MissionController*          missionController      (void)          { return _missionController; }
79
    QmlObjectListModel*         visualItems            (void);
80
    QString                     currentFile            (void) const    { return _currentFile; }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
81
    QStringList                 loadNameFilters        (void) const;
82
    QStringList                 saveNameFilters        (void) const;
83
    QString                     fileExtension          (void) const    { return wimaFileExtension; }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
84
    QGCMapPolygon               joinedArea             (void) const;
85
    WimaDataContainer*          dataContainer          (void);
86 87
    QmlObjectListModel*         missionItems           (void);
    QmlObjectListModel*         currentMissionItems    (void);
88
    QVariantList                waypointPath           (void);
89
    QVariantList                currentWaypointPath    (void);
90 91 92 93
    Fact*                       enableWimaController   (void);
    Fact*                       overlapWaypoints       (void);
    Fact*                       maxWaypointsPerPhase   (void);
    Fact*                       startWaypointIndex     (void);
94 95
    Fact*                       showAllMissionItems    (void);
    Fact*                       showCurrentMissionItems(void);
96
    Fact*                       flightSpeed            (void);
97
    Fact*                       arrivalReturnSpeed     (void);
98 99
    Fact*                       altitude               (void);    
    Fact*                       reverse                (void);
100
    bool                        uploadOverrideRequired (void) const;
101 102
    double                      phaseDistance          (void) const;
    double                      phaseDuration          (void) const;
103
    bool                        vehicleHasLowBattery   (void) const;
104 105 106 107 108


    // Property setters
    void setMasterController        (PlanMasterController* masterController);
    void setMissionController       (MissionController* missionController);
109
    void setDataContainer           (WimaDataContainer* container);
110
    void setUploadOverrideRequired  (bool overrideRequired);
111

112
    // Member Methodes
113
    Q_INVOKABLE void nextPhase();
114 115
    Q_INVOKABLE void previousPhase();
    Q_INVOKABLE void resetPhase();
116
    Q_INVOKABLE bool uploadToVehicle();
117
    Q_INVOKABLE bool forceUploadToVehicle();
118 119
    Q_INVOKABLE void removeFromVehicle();    
    Q_INVOKABLE bool checkSmartRTLPreCondition(); // wrapper for _checkSmartRTLPreCondition(QString &errorString)
120
    Q_INVOKABLE bool calcReturnPath(); // wrapper for _calcReturnPath(QString &errorSring)#
121 122
    Q_INVOKABLE void executeSmartRTL(); // wrapper for _executeSmartRTL(QString &errorSring)
    Q_INVOKABLE void initSmartRTL();
123
    Q_INVOKABLE void removeVehicleTrajectoryHistory();
124

125

126 127
    Q_INVOKABLE void saveToCurrent  ();
    Q_INVOKABLE void saveToFile     (const QString& filename);
128
    Q_INVOKABLE bool loadFromCurrent();
129
    Q_INVOKABLE bool loadFromFile   (const QString& filename);
130 131


132 133
    // static Members
    static const char* wimaFileExtension;
134
    static const char* areaItemsName;
135 136 137 138 139 140 141 142 143
    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;
144
    static const char* flightSpeedName;
145
    static const char* arrivalReturnSpeedName;
146 147
    static const char* altitudeName;    
    static const char* reverseName;
148 149

    // Member Methodes
150
    QJsonDocument saveToJson(FileType fileType);
151

152
    bool calcShortestPath(const QGeoCoordinate &start, const QGeoCoordinate &destination, QVector<QGeoCoordinate> &path);
153
    /// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList
154
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVector<QGeoCoordinate> &coordinateList);
155
    /// extracts the coordinates (between startIndex and endIndex) stored in missionItems (list of MissionItems) and stores them in coordinateList.
156
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVector<QGeoCoordinate> &coordinateList, int startIndex, int endIndex);
157
    /// extracts the coordinates stored in missionItems (list of MissionItems) and stores them in coordinateList
158
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVariantList &coordinateList);
159
    /// extracts the coordinates (between startIndex and endIndex) stored in missionItems (list of MissionItems) and stores them in coordinateList.
160
    bool extractCoordinateList(QmlObjectListModel &missionItems, QVariantList &coordinateList, int startIndex, int endIndex);
161

162
signals:
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    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);
private:
    enum SRTL_Reason {BatteryLow, UserRequest};
181
private slots:
182 183
    bool fetchContainerData();
    bool calcNextPhase(void);
184
    void updateWaypointPath                 (void);
185
    void updateCurrentPath                  (void);
186
    void updateNextWaypoint                 (void);
187
    void recalcCurrentPhase                 (void);
188
    bool setTakeoffLandPosition             (void);
189 190
    void updateflightSpeed                  (void);
    void updateArrivalReturnSpeed           (void);
191
    void updateAltitude                     (void);
192
    void checkBatteryLevel                  (void);
193
    void smartRTLCleanUp                    (bool flying); // cleans up after successfull smart RTL
194
    void enableDisableLowBatteryHandling    (QVariant enable);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
195
    void reverseChangedHandler              ();
196 197 198
    void _initSmartRTL                      ();
    void _executeSmartRTL                   ();

199 200 201 202

private:
    void _setPhaseDistance(double distance);
    void _setPhaseDuration(double duration);
203 204 205
    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);
206 207
    void _loadCurrentMissionItemsFromBuffer();
    void _saveCurrentMissionItemsToBuffer();
208

209
private:
210 211 212 213
    PlanMasterController   *_masterController;
    MissionController      *_missionController;
    QString                 _currentFile; // file for saveing
    WimaDataContainer      *_container; // container for data exchange with WimaController
214
    QmlObjectListModel      _visualItems; // contains all visible areas
215
    WimaJoinedAreaData      _joinedArea; // joined area fromed by opArea, serArea, _corridor
216 217 218 219
    WimaMeasurementAreaData _measurementArea; // measurement area
    WimaServiceAreaData     _serviceArea; // area for supplying
    WimaCorridorData        _corridor; // corridor connecting opArea and serArea
    bool                    _localPlanDataValid;
220 221 222 223
    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
224
    QVariantList            _waypointPath; // path connecting the items in _missionItems
225
    QVariantList            _currentWaypointPath; // path connecting the items in _currentMissionItems
226
    QGeoCoordinate          _takeoffLandPostion;
227

228 229 230 231 232

    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
233
    SettingsFact                 _nextPhaseStartWaypointIndex; // index (displayed on the map, -1 to get index of item in _missionItems) of the mission item
234
                                                      // defining the first element of the next phase
235 236
    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.
237
    SettingsFact                 _flightSpeed; // mission flight speed
238
    SettingsFact                 _arrivalReturnSpeed; // arrival and return path speed
239
    SettingsFact                 _altitude; // mission altitude
240
    SettingsFact                 _reverse; // Reverses the phase direction. Phases go from high to low waypoint numbers, if true.
241

242
    int                          _endWaypointIndex; // index of the mission item stored in _missionItems defining the last element
243
                                                    // (which is not part of the return path) of _currentMissionItem
244
    int                          _startWaypointIndex; // index of the mission item stored in _missionItems defining the first element
245
                                                      // (which is not part of the arrival path) of _currentMissionItem
246 247
    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.
248 249 250
    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
251 252
    double                       _phaseDistance; // the lenth in meters of the current phase
    double                       _phaseDuration; // the phase duration in seconds
253 254
    double                       _phaseDistanceBuffer; // buffer for storing _phaseDistance when doing smart RTL
    double                       _phaseDurationBuffer; // buffer for storing _phaseDuration when doing smart RTL
255 256

    QTimer                       _checkBatteryTimer;
257 258 259
    QTimer                       _smartRTLAttemptTimer;
    SRTL_Reason                  _srtlReason;

260 261
    bool                         _vehicleHasLowBattery;
    bool                         _lowBatteryHandlingTriggered;
262 263
    bool                         _executingSmartRTL;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
264 265
    Scenario                     _snakeScenario;
    FlightPlan                   _snakeFlightPlan;
266
};
267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
/*
 * 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
 *
292 293 294
 * _currentMissionItems is equal to
 * _missionController.visualItems() except that it
 * is missing the MissionSettingsItem
295 296
 */

297