RTLManager.cpp 6.83 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include "RTLManager.h"
#include "Wima/Geometry/GeoUtilities.h"
#include "Wima/Geometry/PolygonCalculus.h"

#include "MissionSettingsItem.h"
#include "SimpleMissionItem.h"

WaypointManager::RTLManager::RTLManager(Settings &settings,
                                        AreaInterface &interface)
10
    : ManagerBase(settings), _areaInterface(&interface) {}
11

12 13 14 15
void WaypointManager::RTLManager::setWaypoints(
    const QVector<QGeoCoordinate> &waypoints) {
  (void)waypoints;
  return;
16 17
}

18 19 20
void WaypointManager::RTLManager::push_back(const QGeoCoordinate &wp) {
  (void)wp;
  return;
21 22
}

23 24 25
void WaypointManager::RTLManager::push_front(const QGeoCoordinate &wp) {
  (void)wp;
  return;
26 27
}

28 29 30 31 32 33 34 35
void WaypointManager::RTLManager::clear() {
  _dirty = true;
  _waypoints.clear();
  _currentWaypoints.clear();
  _missionItems.clearAndDeleteContents();
  _currentMissionItems.clearAndDeleteContents();
  _waypointsVariant.clear();
  _currentWaypointsVariant.clear();
36 37
}

38 39 40 41 42
void WaypointManager::RTLManager::insert(std::size_t i,
                                         const QGeoCoordinate &wp) {
  (void)i;
  (void)wp;
  return;
43 44
}

45
std::size_t WaypointManager::RTLManager::size() const { return 0; }
46

47
void WaypointManager::RTLManager::remove(std::size_t i) { (void)i; }
48

49 50 51
bool WaypointManager::RTLManager::update() {
  this->clear();
  return _worker();
52 53
}

54
bool WaypointManager::RTLManager::next() { return true; }
55

56
bool WaypointManager::RTLManager::previous() { return true; }
57

58
bool WaypointManager::RTLManager::reset() { return true; }
59

60 61 62
bool WaypointManager::RTLManager::checkPrecondition(QString &errorString) {
  // Be aware of the error message order!
  Vehicle *managerVehicle = _settings->masterController()->managerVehicle();
63

64 65 66 67
  if (managerVehicle->isOfflineEditingVehicle()) {
    errorString.append("No vehicle connected. Smart RTL not available.");
    return false;
  }
68

69 70 71 72
  if (!managerVehicle->flying()) {
    errorString.append("Vehicle is not flying. Smart RTL not available.");
    return false;
  }
73

74 75 76 77 78 79
  if (!_areaInterface->joinedArea()->containsCoordinate(
          managerVehicle->coordinate())) {
    errorString.append(
        "Vehicle not inside save area. Smart RTL not available.");
    return false;
  }
80

81
  return true;
82 83 84
}

bool WaypointManager::RTLManager::_insertMissionItem(const QGeoCoordinate &c,
85 86 87 88 89 90 91 92 93 94 95 96 97
                                                     size_t index,
                                                     QmlObjectListModel &list,
                                                     bool doUpdate) {
  using namespace WaypointManager::Utils;

  if (!insertMissionItem(c, index /*insertion index*/, list,
                         _settings->vehicle(), _settings->isFlyView(),
                         &list /*parent*/, doUpdate /*do update*/)) {
    qWarning("WaypointManager::RTLManager::next(): insertMissionItem failed.");
    Q_ASSERT(false);
    return false;
  }
  return true;
98 99 100
}

bool WaypointManager::RTLManager::_insertMissionItem(const QGeoCoordinate &c,
101 102 103
                                                     size_t index,
                                                     bool doUpdate) {
  return _insertMissionItem(c, index, _currentMissionItems, doUpdate);
104 105 106
}

bool WaypointManager::RTLManager::_calcShortestPath(
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 151 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
    const QGeoCoordinate &start, const QGeoCoordinate &destination,
    QVector<QGeoCoordinate> &path) {
  using namespace GeoUtilities;
  using namespace PolygonCalculus;
  QPolygonF joinedArea2D;
  toCartesianList(_areaInterface->joinedArea()->coordinateList(),
                  /*origin*/ start, joinedArea2D);
  QPointF start2D(0, 0);
  QPointF end2D;
  toCartesian(destination, start, end2D);
  QVector<QPointF> path2DOut;

  bool retVal =
      PolygonCalculus::shortestPath(joinedArea2D, start2D, end2D, path2DOut);
  toGeoList(path2DOut, /*origin*/ start, path);

  return retVal;
}

bool WaypointManager::RTLManager::_worker() {
  // Precondition: settings must be valid.

  using namespace WaypointManager::Utils;

  if (!_settings->valid()) {
    return false;
  }

  _currentMissionItems.clearAndDeleteContents();
  initialize(_currentMissionItems, _settings->vehicle(),
             _settings->isFlyView());

  // Calculate path from vehicle to home position.
  QVector<QGeoCoordinate> returnPath;
  auto vehicleCoordinate =
      _settings->masterController()->managerVehicle()->coordinate();
  if (!_calcShortestPath(vehicleCoordinate, _settings->homePosition(),
                         returnPath)) {
    qWarning("WaypointManager::RTLManager::next(): Not able to calc path from "
             "vehicle to home position.");
    return false;
  }

  // Create mission items.
  // Set home position of MissionSettingsItem.
  MissionSettingsItem *settingsItem =
      _currentMissionItems.value<MissionSettingsItem *>(0);
  if (settingsItem == nullptr) {
    Q_ASSERT(false);
    qWarning("WaypointManager::RTLManager::next(): nullptr.");
    return false;
  }
  settingsItem->setCoordinate(vehicleCoordinate);

  // Create change speed item.
  int index = _currentMissionItems.count();
  _insertMissionItem(vehicleCoordinate, index /*insertion index*/,
                     false /*do update*/);
  SimpleMissionItem *speedItem =
      _currentMissionItems.value<SimpleMissionItem *>(index);
  if (speedItem == nullptr) {
    qWarning("WaypointManager::RTLManager::next(): nullptr.");
    Q_ASSERT(speedItem != nullptr);
    return false;
  }
  makeSpeedCmd(speedItem, _settings->arrivalReturnSpeed());

  // Insert return path coordinates.
  for (auto coordinate : returnPath) {
    index = _currentMissionItems.count();
    _insertMissionItem(coordinate, index /*insertion index*/,
178
                       false /*do update*/);
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  }
  // Set land command for last mission item.
  index = _currentMissionItems.count();
  _insertMissionItem(_settings->homePosition(), index /*insertion index*/,
                     false /*do update*/);
  SimpleMissionItem *landItem =
      _currentMissionItems.value<SimpleMissionItem *>(index);
  if (landItem == nullptr) {
    Q_ASSERT(false);
    qWarning("WaypointManager::RTLManager::next(): nullptr.");
    return false;
  }
  makeLandCmd(landItem, _settings->masterController()->managerVehicle());

  // Set altitude.
  for (int i = 1; i < _currentMissionItems.count(); ++i) {
    SimpleMissionItem *item =
        _currentMissionItems.value<SimpleMissionItem *>(i);
    if (item == nullptr) {
      Q_ASSERT(false);
      qWarning("WimaController::updateAltitude(): nullptr");
      return false;
201
    }
202 203
    item->altitude()->setRawValue(_settings->altitude());
  }
204

205 206 207
  // Update list _currentMissionItems.
  updateHirarchy(_currentMissionItems);
  updateSequenceNumbers(_currentMissionItems);
208

209 210 211
  // Append return path to _currentWaypoints.
  for (auto c : returnPath)
    _currentWaypoints.push_back(c);
212

213 214 215 216
  // Create variant list.
  _currentWaypointsVariant.clear();
  for (auto c : _currentWaypoints)
    _currentWaypointsVariant.push_back(QVariant::fromValue(c));
217

218
  return true;
219
}