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

#include "MissionSettingsItem.h"

bool WaypointManager::DefaultManager::update()
{
    // extract waypoints
    _slice.clear();
11
    Slicer::update(_waypoints, _slice);
12 13 14 15 16 17 18
    return _worker();
}

bool WaypointManager::DefaultManager::next()
{
    // extract waypoints
    _slice.clear();
19
    Slicer::next(_waypoints, _slice);
20 21 22 23 24 25 26
    return _worker();
}

bool WaypointManager::DefaultManager::previous()
{
    // extract waypoints
    _slice.clear();
27
    Slicer::previous(_waypoints, _slice);
28 29 30 31 32 33 34
    return _worker();
}

bool WaypointManager::DefaultManager::reset()
{
    // extract waypoints
    _slice.clear();
35
    Slicer::reset(_waypoints, _slice);
36 37 38 39 40 41 42 43 44 45 46 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 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 178 179 180 181 182 183 184 185 186 187 188 189 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    return _worker();
}

bool WaypointManager::DefaultManager::_insertMissionItem(size_t index, const QGeoCoordinate &c, bool doUpdate)
{
    using namespace WaypointManager::Utils;

    if ( !insertMissionItem(c,
                            index /*insertion index*/,
                            _missionItems,
                            _settings->vehicle(),
                            _settings->isFlyView(),
                            &_missionItems /*parent*/,
                            doUpdate /*do update*/) )
    {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): insertMissionItem failed.");
        return false;
    }
    return true;
}

bool WaypointManager::DefaultManager::_calcShortestPath(
        const QGeoCoordinate &start,
        const QGeoCoordinate &destination,
        QVector<QGeoCoordinate> &path)
{
        using namespace GeoUtilities;
        using namespace PolygonCalculus;
        QVector<QPointF> path2D;
        bool retVal = PolygonCalculus::shortestPath(
                                       toQPolygonF(toCartesian2D(_areaInterface->joinedArea()->coordinateList(), /*origin*/ start)),
                                       /*start point*/ QPointF(0,0),
                                       /*destination*/ toCartesian2D(destination, start),
                                       /*shortest path*/ path2D);
        path.append(toGeo(path2D, /*origin*/ start));

        return  retVal;
}

bool WaypointManager::DefaultManager::_worker()
{
    using namespace WaypointManager::Utils;

    if (_waypoints.count() < 1) {
        return false;
    }

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

    // calculate path from home to first waypoint
    QVector<QGeoCoordinate> arrivalPath;
    if (!_settings->homePosition().isValid()){
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): home position invalid.");
        return false;
    }
    if ( !_calcShortestPath(_settings->homePosition(), _slice.first(), arrivalPath) ) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): Not able to calc path from home position to first waypoint.");
        return false;
    }
    arrivalPath.removeFirst();

    // calculate path from last waypoint to home
    QVector<QGeoCoordinate> returnPath;
    if ( !_calcShortestPath(_slice.last(), _settings->homePosition(), returnPath) ) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): Not able to calc path from home position to last waypoint.");
        return false;
    }
    returnPath.removeFirst();
    returnPath.removeLast();



    // Create mission items.
    // Set home position of MissionSettingsItem.
    MissionSettingsItem* settingsItem = _missionItems.value<MissionSettingsItem *>(0);
    if (settingsItem == nullptr) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): nullptr.");
        return false;
    }
    settingsItem->setCoordinate(_settings->homePosition());

    // Set takeoff position for first mission item (bug).
    _insertMissionItem(1 /*insertion index*/, _settings->homePosition(), false /*do update*/);
    SimpleMissionItem *takeoffItem = _missionItems.value<SimpleMissionItem*>(1);
    if (takeoffItem == nullptr) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): nullptr.");
        return false;
    }
    takeoffItem->setCoordinate(_settings->homePosition());

    // Create change speed item.
    _insertMissionItem(2 /*insertion index*/, _settings->homePosition(), false /*do update*/);
    SimpleMissionItem *speedItem = _missionItems.value<SimpleMissionItem*>(2);
    if (speedItem == nullptr) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): nullptr.");
        return false;
    }
    speedItem->setCommand(MAV_CMD_DO_CHANGE_SPEED);
    // Set coordinate must be after setCommand (setCommand sets coordinate to zero).
    speedItem->setCoordinate(_settings->homePosition());
    speedItem->missionItem().setParam2(_settings->arrivalReturnSpeed());

    // insert arrival path
    for (auto coordinate : arrivalPath) {
        _insertMissionItem(_missionItems.count() /*insertion index*/,
                           coordinate,
                           false /*do update*/);
    }

    // Create change speed item (after arrival path).
    int index = _missionItems.count();
    _insertMissionItem(index /*insertion index*/, _slice.first(), false /*do update*/);
    speedItem = _missionItems.value<SimpleMissionItem*>(index);
    if (speedItem == nullptr) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): nullptr.");
        return false;
    }
    speedItem->setCommand(MAV_CMD_DO_CHANGE_SPEED); // set coordinate must be after setCommand (setCommand sets coordinate to zero)
    speedItem->setCoordinate(_slice.first());
    speedItem->missionItem().setParam2(_settings->flightSpeed());

    // Insert slice.
    for (auto coordinate : _slice) {
        _insertMissionItem(_missionItems.count() /*insertion index*/,
                           coordinate,
                           false /*do update*/);
    }

    // Create change speed item, after slice.
    index = _missionItems.count();
    _insertMissionItem(index /*insertion index*/, _slice.last(), false /*do update*/);
    speedItem = _missionItems.value<SimpleMissionItem*>(index);
    if (speedItem == nullptr) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): nullptr.");
        return false;
    }
    speedItem->setCommand(MAV_CMD_DO_CHANGE_SPEED); // set coordinate must be after setCommand (setCommand sets coordinate to zero)
    speedItem->setCoordinate(_slice.last());
    speedItem->missionItem().setParam2(_settings->arrivalReturnSpeed());

    // Insert return path coordinates.
    for (auto coordinate : returnPath) {
        _insertMissionItem(_missionItems.count() /*insertion index*/,
                           coordinate,
                           false /*do update*/);
    }

    // Set land command for last mission item.
    index = _missionItems.count();
    _insertMissionItem(index /*insertion index*/, _settings->homePosition(), false /*do update*/);
    SimpleMissionItem *landItem = _missionItems.value<SimpleMissionItem*>(index);
    if (landItem == nullptr) {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): nullptr.");
        return false;
    }

    MAV_CMD landCmd = _settings->vehicle()->vtol() ? MAV_CMD_NAV_VTOL_LAND : MAV_CMD_NAV_LAND;
    if (_settings->vehicle()->firmwarePlugin()->supportedMissionCommands().contains(landCmd)) {
        landItem->setCommand(landCmd);
    } else {
        assert(false);
        qWarning("WaypointManager::DefaultManager::next(): Land command not supported!");
        return false;
    }

    // Prepend arrival path to slice.
    for ( long i = arrivalPath.size()-1; i >=0; --i )
        _slice.push_front(arrivalPath[i]);
    // Append return path to slice.
    for ( auto c : returnPath )
        _slice.push_back(c);


    // Set altitude.
    for (int i = 0; i < _missionItems.count(); ++i) {
        SimpleMissionItem *item = _missionItems.value<SimpleMissionItem *>(i);
        if (item == nullptr) {
            assert(false);
            qWarning("WimaController::updateAltitude(): nullptr");
            return false;
        }
        item->altitude()->setRawValue(_settings->altitude());
    }

    return true;
}