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

#include <QObject>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
4 5
#include <QScopedPointer>

6
#include "QGCMapPolygon.h"
7
#include "QmlObjectListModel.h"
8

9 10 11
#include "Geometry/WimaArea.h"
#include "Geometry/WimaCorridor.h"
#include "Geometry/WimaCorridorData.h"
12 13 14
#include "Geometry/WimaMeasurementArea.h"
#include "Geometry/WimaMeasurementAreaData.h"
#include "Geometry/WimaServiceArea.h"
15
#include "Geometry/WimaServiceAreaData.h"
16

17 18
#include "WimaPlanData.h"

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

29
#include "Geometry/GeoPoint3D.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
30 31
#include "Snake/NemoInterface.h"
#include "Snake/SnakeThread.h"
32 33
#include "Snake/SnakeTiles.h"
#include "Snake/SnakeTilesLocal.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
34

35
#include "WaypointManager/DefaultManager.h"
36
#include "WaypointManager/RTLManager.h"
37

Valentin Platzgummer's avatar
Valentin Platzgummer committed
38
#include "utilities.h"
39 40

#include <map>
41

Valentin Platzgummer's avatar
Valentin Platzgummer committed
42 43
typedef std::unique_ptr<rapidjson::Document> JsonDocUPtr;

44 45 46
class WimaController : public QObject {
  Q_OBJECT
  enum FileType { WimaFile, PlanFile };
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
public:
  WimaController(QObject *parent = nullptr);

  // Controllers.
  Q_PROPERTY(PlanMasterController *masterController READ masterController WRITE
                 setMasterController NOTIFY masterControllerChanged)
  Q_PROPERTY(MissionController *missionController READ missionController WRITE
                 setMissionController NOTIFY missionControllerChanged)
  // Wima Data.
  Q_PROPERTY(QmlObjectListModel *visualItems READ visualItems NOTIFY
                 visualItemsChanged)
  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)
  // Waypoint navigaton.
  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)
  // Waypoint settings.
  Q_PROPERTY(Fact *flightSpeed READ flightSpeed CONSTANT)
  Q_PROPERTY(Fact *altitude READ altitude CONSTANT)
  Q_PROPERTY(Fact *arrivalReturnSpeed READ arrivalReturnSpeed CONSTANT)
  // Waypoint statistics.
  Q_PROPERTY(
      double phaseDistance READ phaseDistance NOTIFY phaseDistanceChanged)
  Q_PROPERTY(
      double phaseDuration READ phaseDuration NOTIFY phaseDurationChanged)

  // Snake
  Q_PROPERTY(Fact *enableSnake READ enableSnake CONSTANT)
  Q_PROPERTY(int nemoStatus READ nemoStatus NOTIFY nemoStatusChanged)
  Q_PROPERTY(QString nemoStatusString READ nemoStatusString NOTIFY
                 nemoStatusStringChanged)
  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)
  Q_PROPERTY(
      QmlObjectListModel *snakeTiles READ snakeTiles NOTIFY snakeTilesChanged)
  Q_PROPERTY(QVariantList snakeTileCenterPoints READ snakeTileCenterPoints
                 NOTIFY snakeTileCenterPointsChanged)
  Q_PROPERTY(
      QVector<int> nemoProgress READ nemoProgress NOTIFY nemoProgressChanged)

  // Property accessors
  // Controllers.
  PlanMasterController *masterController(void);
  MissionController *missionController(void);
  // Wima Data
  QmlObjectListModel *visualItems(void);
  QGCMapPolygon joinedArea(void) const;
  // Waypoints.
  QmlObjectListModel *missionItems(void);
  QmlObjectListModel *currentMissionItems(void);
  QVariantList waypointPath(void);
  QVariantList currentWaypointPath(void);
  // Settings facts.
  Fact *enableWimaController(void);
  Fact *overlapWaypoints(void);
  Fact *maxWaypointsPerPhase(void);
  Fact *startWaypointIndex(void);
  Fact *showAllMissionItems(void);
  Fact *showCurrentMissionItems(void);
  Fact *flightSpeed(void);
  Fact *arrivalReturnSpeed(void);
  Fact *altitude(void);
  // Snake settings facts.
  Fact *enableSnake(void) { return &_enableSnake; }
  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; }
  // Snake data.
  QmlObjectListModel *snakeTiles(void);
  QVariantList snakeTileCenterPoints(void);
  QVector<int> nemoProgress(void);
  int nemoStatus(void) const;
  QString nemoStatusString(void) const;
  bool snakeCalcInProgress(void) const;

  // Smart RTL.
  bool uploadOverrideRequired(void) const;
  bool vehicleHasLowBattery(void) const;
  // Waypoint statistics.
  double phaseDistance(void) const;
  double phaseDuration(void) const;

  // Property setters
  void setMasterController(PlanMasterController *masterController);
  void setMissionController(MissionController *missionController);
151
  bool setWimaPlanData(QSharedPointer<WimaPlanData> planData);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  // Member Methodes
  Q_INVOKABLE WimaController *thisPointer();
  // Waypoint navigation.
  Q_INVOKABLE void nextPhase();
  Q_INVOKABLE void previousPhase();
  Q_INVOKABLE void resetPhase();
  // Smart RTL.
  Q_INVOKABLE void requestSmartRTL();
  Q_INVOKABLE void initSmartRTL();
  Q_INVOKABLE void executeSmartRTL();
  // Other.
  Q_INVOKABLE void removeVehicleTrajectoryHistory();
  Q_INVOKABLE bool upload();
  Q_INVOKABLE bool forceUpload();
  Q_INVOKABLE void removeFromVehicle();

  // static Members
  static const char *areaItemsName;
  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;
  static const char *flightSpeedName;
  static const char *arrivalReturnSpeedName;
  static const char *altitudeName;
  static const char *snakeTileWidthName;
  static const char *snakeTileHeightName;
  static const char *snakeMinTileAreaName;
  static const char *snakeLineDistanceName;
  static const char *snakeMinTransectLengthName;
188

189
signals:
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  // Controllers.
  void masterControllerChanged(void);
  void missionControllerChanged(void);
  // Wima data.
  void visualItemsChanged(void);
  // Waypoints.
  void missionItemsChanged(void);
  void currentMissionItemsChanged(void);
  void waypointPathChanged(void);
  void currentWaypointPathChanged(void);
  // Smart RTL.
  void smartRTLRequestConfirm(void);
  void smartRTLPathConfirm(void);
  // Upload.
  void forceUploadConfirm(void);
  // Waypoint statistics.
  void phaseDistanceChanged(void);
  void phaseDurationChanged(void);
  // Snake.
  void snakeCalcInProgressChanged(void);
  void snakeTilesChanged(void);
  void snakeTileCenterPointsChanged(void);
  void nemoProgressChanged(void);
  void nemoStatusChanged(void);
  void nemoStatusStringChanged(void);
215

216
private slots:
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  // Waypoint navigation / helper.
  bool _calcNextPhase(void);
  void _recalcCurrentPhase(void);
  bool _calcShortestPath(const QGeoCoordinate &start,
                         const QGeoCoordinate &destination,
                         QVector<QGeoCoordinate> &path);
  // Slicing parameters
  bool _setStartIndex(void);
  void _updateOverlap(void);
  void _updateMaxWaypoints(void);
  // Waypoint settings.
  void _updateflightSpeed(void);
  void _updateArrivalReturnSpeed(void);
  void _updateAltitude(void);
  // Waypoint Statistics.
  void _setPhaseDistance(double distance);
  void _setPhaseDuration(double duration);
  // SMART RTL
  void _checkBatteryLevel(void);
  bool _checkSmartRTLPreCondition(QString &errorString);
  void _initSmartRTL();
  void _smartRTLCleanUp(bool flying);
  // Snake.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
241 242
  void _threadFinishedHandler();
  void _switchWaypointManager(WaypointManager::ManagerBase &manager);
243
  void _switchToSnakeWaypointManager(QVariant variant);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
244
  void _switchThreadObject(SnakeThread &thread);
245 246 247 248
  void _progressChangedHandler();
  void _enableSnakeChangedHandler();
  // Periodic tasks.
  void _eventTimerHandler(void);
249 250

private:
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  // Controllers.
  PlanMasterController *_masterController;
  MissionController *_missionController;

  // Wima Data.
  QmlObjectListModel _areas; // contains all visible areas
  WimaJoinedAreaData
      _joinedArea; // joined area fromed by opArea, serArea, _corridor
  WimaMeasurementAreaData _measurementArea; // measurement area
  WimaServiceAreaData _serviceArea;         // area for supplying
  WimaCorridorData _corridor; // corridor connecting opArea and serArea
  bool _localPlanDataValid;

  // Waypoint Managers.
  WaypointManager::AreaInterface _areaInterface;
  WaypointManager::Settings _WMSettings; // Waypoint Manager Settings
  WaypointManager::DefaultManager _defaultWM;
  WaypointManager::DefaultManager _snakeWM;
  WaypointManager::RTLManager _rtlWM;
  WaypointManager::ManagerBase *_currentWM;
  using ManagerList = QList<WaypointManager::ManagerBase *>;
  ManagerList _WMList;

  // Settings Facts.
  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
  SettingsFact
      _nextPhaseStartWaypointIndex; // index (displayed on the map, -1 to get
                                    // index of item in _missionItems) of the
                                    // mission item defining the first element
                                    // of the next phase
  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.
  SettingsFact _flightSpeed;             // mission flight speed
  SettingsFact _arrivalReturnSpeed;      // arrival and return path speed
  SettingsFact _altitude;                // mission altitude
  SettingsFact _enableSnake;             // Enable Snake (see snake.h)
  SettingsFact _snakeTileWidth;
  SettingsFact _snakeTileHeight;
  SettingsFact _snakeMinTileArea;
  SettingsFact _snakeLineDistance;
  SettingsFact _snakeMinTransectLength;

  // Smart RTL.
  QTimer _smartRTLTimer;
  bool _lowBatteryHandlingTriggered;

  // Waypoint statistics.
  double _measurementPathLength; // the lenght of the phase in meters

  // Snake
Valentin Platzgummer's avatar
Valentin Platzgummer committed
311 312 313 314 315 316
  QmlObjectListModel tiles;
  SnakeThread _snakeThread; // Snake Data Manager
  SnakeThread _emptyThread;
  SnakeThread *_currentThread;
  NemoInterface _nemoInterface;
  using StatusMap = std::map<int, QString>;
317 318 319 320 321
  static StatusMap _nemoStatusMap;

  // Periodic tasks.
  QTimer _eventTimer;
  EventTicker _batteryLevelTicker;
322
};