WimaController.cc 7.66 KB
Newer Older
1 2
#include "WimaController.h"

3 4 5 6
WimaController::WimaController(QObject *parent) :
    QObject             (parent)
  ,_planView            (true)
  ,_visualItems         (new QmlObjectListModel(parent))
7
  ,_currentPolygonIndex (-1)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
{
    connect(this, &WimaController::currentPolygonIndexChanged, this, &WimaController::recalcPolygonInteractivity);
}

void WimaController::setMasterController(PlanMasterController *masterC)
{
    _masterController = masterC;
    emit masterControllerChanged();
}

void WimaController::setMissionController(MissionController *missionC)
{
    _missionController = missionC;
    emit missionControllerChanged();
}

void WimaController::setCurrentPolygonIndex(int index)
{
    if(index >= 0 && index < _visualItems->count() && index != _currentPolygonIndex){
        _currentPolygonIndex = index;

        emit currentPolygonIndexChanged(index);
    }
}

void WimaController::removeArea(int index)
{
    if(index >= 0 && index < _visualItems->count()){
        _visualItems->removeAt(index);

        emit visualItemsChanged();

40 41 42 43 44 45 46
        if (_visualItems->count() == 0) {
            // this branch is reached if all items are removed
            // to guarentee proper behavior, _currentPolygonIndex must be set to a invalid value, as on constructor init.
            _currentPolygonIndex = -1;
            return;
        }

47 48 49 50 51 52 53 54 55 56 57
        if(_currentPolygonIndex >= _visualItems->count()){
            setCurrentPolygonIndex(_visualItems->count() - 1);
        }else{
            recalcPolygonInteractivity(_currentPolygonIndex);
        }
    }else{
        qWarning("Index out of bounds!");
    }

}

58 59 60 61 62 63 64 65 66
void WimaController::addGOperationArea()
{
    WimaGOperationArea* newPoly = new WimaGOperationArea(this);
    _visualItems->append(newPoly);
    int newIndex = _visualItems->count()-1;
    setCurrentPolygonIndex(newIndex);
    emit visualItemsChanged();
}

67 68 69 70
void WimaController::addServiceArea()
{
    WimaServiceArea* newPoly = new WimaServiceArea(this);
    _visualItems->append(newPoly);
71 72
    int newIndex = _visualItems->count()-1;
    setCurrentPolygonIndex(newIndex);
73 74 75
    emit visualItemsChanged();
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
bool WimaController::addVehicleCorridor(WimaGOperationArea *opArea, WimaServiceArea *serviceArea)
{
     WimaVCorridor* corridor = nullptr;
    if (opArea != nullptr && serviceArea != nullptr){
        for (int i = 0; i < _visualItems->count(); i++) {
            corridor = qobject_cast<WimaVCorridor*>(_visualItems->get(i));
            if (corridor != nullptr){
                if (corridor->serviceArea() == serviceArea && corridor->opArea() == opArea){
                    break;
                }else {
                    corridor = nullptr;
                }
            }
        }

        bool newCorridorCreated = false;
        if(corridor == nullptr){
93
            corridor = new WimaVCorridor(opArea);
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
            newCorridorCreated = true;
        }else {
            corridor->clear();
        }

        QList<QGeoCoordinate> opAreaPolyline = opArea->polyline()->coordinateList();
        QList<QGeoCoordinate> serAreaPolyline = serviceArea->polyline()->coordinateList();
        if (opAreaPolyline.size() > 1 && serAreaPolyline.size() > 1){
            corridor->appendVertices(opAreaPolyline);
            corridor->appendVertices(serAreaPolyline);

            if (newCorridorCreated){
                corridor->setServiceArea(serviceArea);
                corridor->setOpArea(opArea);
                serviceArea->setVehicleCorridor(corridor);
                opArea->setVehicleCorridor(corridor);
                _visualItems->append(corridor);
                emit visualItemsChanged();
            }

            return true;
        }else {
            qWarning("WimaController::addVehicleCorridor(): OpArea or serviceArea polyline size <= 1!");
            if (newCorridorCreated){
                corridor->deleteLater();
            }
            return false;
        }
    }else {
        return false;
    }
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
void WimaController::startMission()
{

}

void WimaController::abortMission()
{

}

void WimaController::pauseMission()
{

}

void WimaController::resumeMission()
{

}

147 148
bool WimaController::updateMission()
{
149
    // pick first WimaGOperationArea
150 151 152 153 154 155 156 157 158
    WimaGOperationArea* opArea = nullptr;
    for (int i = 0; i < _visualItems->count(); i++) {
        WimaGOperationArea* currentArea = qobject_cast<WimaGOperationArea*>(_visualItems->get(i));
        if (currentArea != nullptr){
            opArea = currentArea;
            break;
        }
    }

159
    // pick first WimaServiceArea
160 161 162 163 164 165 166 167 168
    WimaServiceArea* serArea = nullptr;
    for (int i = 0; i < _visualItems->count(); i++) {
        WimaServiceArea* currentArea = qobject_cast<WimaServiceArea*>(_visualItems->get(i));
        if (currentArea != nullptr){
            serArea = currentArea;
            break;
        }
    }

169
    // calc. vehicle corridor
170
    if (opArea != nullptr && serArea != nullptr){
171 172 173 174 175 176 177
        if(!addVehicleCorridor(opArea, serArea)){
            return false;
        }
    }else{
        return false;
    }
    
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    // reset visual items
    _missionController->removeAll();
    QmlObjectListModel* missionItems = _missionController->visualItems();
    // set home position to serArea center
    MissionSettingsItem* settingsItem= qobject_cast<MissionSettingsItem*>(missionItems->get(0));
    if (settingsItem == nullptr){
        qWarning("WimaController::updateMission(): settingsItem == nullptr");
        return false;
    }
    settingsItem->setCoordinate(serArea->center());
    // set take off position item
    _missionController->insertSimpleMissionItem(serArea->center(), 1);
    // create survey item, will be extened with more mission types in the future
    _missionController->insertComplexMissionItem(_missionController->surveyComplexItemName(), opArea->center(), 2);
    SurveyComplexItem* survey = qobject_cast<SurveyComplexItem*>(missionItems->get(missionItems->count()-1));
    if (survey == nullptr){
        qWarning("WimaController::updateMission(): survey == nullptr");
195
        return false;
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    } else {
        survey->surveyAreaPolygon()->clear();
        survey->surveyAreaPolygon()->appendVertices(opArea->coordinateList());
    }
    // create land position item
    _missionController->insertSimpleMissionItem(serArea->center(), 3);
    SimpleMissionItem* landItem = qobject_cast<SimpleMissionItem*>(missionItems->get(missionItems->count()-1));
    if (landItem == nullptr){
        qWarning("WimaController::updateMission(): landItem == nullptr");
        return false;
    } else {
        Vehicle* controllerVehicle = _masterController->controllerVehicle();
        MAV_CMD landCmd = controllerVehicle->vtol() ? MAV_CMD_NAV_VTOL_LAND : MAV_CMD_NAV_LAND;
        if (controllerVehicle->firmwarePlugin()->supportedMissionCommands().contains(landCmd)) {
            landItem->setCommand(landCmd);
        }
212
    }
213
    return true;
214 215
}

216 217 218 219 220 221 222 223 224 225 226
void WimaController::saveMission()
{

}

void WimaController::loadMission()
{

}

void WimaController::recalcVehicleCorridor()
227 228 229
{

}
230 231 232 233 234 235 236 237 238 239 240 241 242

void WimaController::recalcVehicleMeasurementAreas()
{

}

void WimaController::recalcAll()
{

}

void WimaController::recalcPolygonInteractivity(int index)
{
243 244 245 246 247
    if (index >= 0 && index < _visualItems->count()) {
        resetAllInteractive();
        WimaArea* interactivePoly = qobject_cast<WimaArea*>(_visualItems->get(index));
        interactivePoly->setInteractive(true);
    }
248 249
}

250
void WimaController::resetAllInteractive()
251 252
{
    int itemCount = _visualItems->count();
253 254 255 256 257
    if (itemCount > 0){
        for (int i = 0; i < itemCount; i++) {
            WimaArea* iteratorPoly = qobject_cast<WimaArea*>(_visualItems->get(i));
            iteratorPoly->setInteractive(false);
        }
258 259 260
    }
}

261 262 263 264 265
void WimaController::setInteractive()
{
    recalcPolygonInteractivity(_currentPolygonIndex);
}

266 267