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

3 4
const char* WimaController::wimaFileExtension = "wima";

5 6 7 8
WimaController::WimaController(QObject *parent) :
    QObject             (parent)
  ,_planView            (true)
  ,_visualItems         (new QmlObjectListModel(parent))
9
  ,_currentPolygonIndex (-1)
10 11 12 13
{
    connect(this, &WimaController::currentPolygonIndexChanged, this, &WimaController::recalcPolygonInteractivity);
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
14 15 16 17 18 19 20 21 22 23
QStringList WimaController::loadNameFilters() const
{
    QStringList filters;

    filters << tr("Supported types (*.%1)").arg(wimaFileExtension) <<
               tr("All Files (*.*)");
    return filters;

}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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()){
48 49 50 51 52 53 54 55
        WimaArea* area = qobject_cast<WimaArea*>(_visualItems->removeAt(index));

        if ( area == nullptr) {
            qWarning("WimaController::removeArea(): nullptr catched, internal error.");
            return;
        }

        disconnect(area, &WimaArea::pathChanged, this, &WimaController::updateJoinedArea);
56 57 58

        emit visualItemsChanged();

59 60 61 62 63 64 65
        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;
        }

66 67 68 69 70 71 72 73 74 75 76
        if(_currentPolygonIndex >= _visualItems->count()){
            setCurrentPolygonIndex(_visualItems->count() - 1);
        }else{
            recalcPolygonInteractivity(_currentPolygonIndex);
        }
    }else{
        qWarning("Index out of bounds!");
    }

}

77
bool WimaController::addGOperationArea()
78
{
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    // check if opArea exists already
    WimaGOperationArea* opArea = nullptr;
    for (int i = 0; i < _visualItems->count(); i++) {
        WimaGOperationArea* currentArea = qobject_cast<WimaGOperationArea*>(_visualItems->get(i));
        if ( currentArea != nullptr ) {
            opArea = currentArea;
            return false;
        }
    }

    // create one if no opArea available
    opArea = new WimaGOperationArea(this);
    connect(opArea, &WimaArea::pathChanged, this, &WimaController::updateJoinedArea);

    _visualItems->append(opArea);
94 95
    int newIndex = _visualItems->count()-1;
    setCurrentPolygonIndex(newIndex);
96

97
    emit visualItemsChanged();
98
    return true;
99 100
}

101
bool WimaController::addServiceArea()
102
{
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    // check if serArea exists already
    WimaServiceArea* serArea = nullptr;
    for (int i = 0; i < _visualItems->count(); i++) {
        WimaServiceArea* currentArea = qobject_cast<WimaServiceArea*>(_visualItems->get(i));
        if ( currentArea != nullptr ) {
            serArea = currentArea;
            return false;
        }
    }

    // create one if no serArea available
    serArea = new WimaServiceArea(this);
    connect(serArea, &WimaArea::pathChanged, this, &WimaController::updateJoinedArea);

    _visualItems->append(serArea);
118 119
    int newIndex = _visualItems->count()-1;
    setCurrentPolygonIndex(newIndex);
120

121
    emit visualItemsChanged();
122
    return true;
123 124
}

125
bool WimaController::addVehicleCorridor()
126
{
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    // check if corridor exists already
    WimaVCorridor* corridor = nullptr;
    for (int i = 0; i < _visualItems->count(); i++) {
        WimaVCorridor* currentArea = qobject_cast<WimaVCorridor*>(_visualItems->get(i));
        if ( currentArea != nullptr ) {
            corridor = currentArea;
            return false;
        }
    }

    // create one if no corridor available
    corridor = new WimaVCorridor(this);
    connect(corridor, &WimaArea::pathChanged, this, &WimaController::updateJoinedArea);

141 142 143
    _visualItems->append(corridor);
    int newIndex = _visualItems->count()-1;
    setCurrentPolygonIndex(newIndex);
144

145
    emit visualItemsChanged();
146
    return true;
147 148
}

149
void WimaController::removeAll()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
150 151 152
{
    bool changesApplied = false;
    while (_visualItems->count() > 0) {
153
        removeArea(0);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
154 155 156
        changesApplied = true;
    }

157 158
    _missionController->removeAll();

Valentin Platzgummer's avatar
Valentin Platzgummer committed
159 160 161 162 163 164 165
    _currentFile = "";

    emit currentFileChanged();
    if ( changesApplied )
         emit visualItemsChanged();
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void WimaController::startMission()
{

}

void WimaController::abortMission()
{

}

void WimaController::pauseMission()
{

}

void WimaController::resumeMission()
{

}

186 187
bool WimaController::updateMission()
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
188
    #define debug 0
189
    // pick first WimaGOperationArea
190 191 192 193 194 195 196 197
    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;
        }
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
198 199
    if (opArea == nullptr)
        return false;
200

201
    // pick first WimaServiceArea
202 203 204 205 206 207 208 209
    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;
        }
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
210 211
    if ( serArea == nullptr )
        return false;
212

213 214 215 216 217 218 219
    // pick first WimaVCorridor
    WimaVCorridor* corridor = nullptr;
    for (int i = 0; i < _visualItems->count(); i++) {
        WimaVCorridor* currentArea = qobject_cast<WimaVCorridor*>(_visualItems->get(i));
        if (currentArea != nullptr){
            corridor = currentArea;
            break;
220 221
        }
    }
222

Valentin Platzgummer's avatar
Valentin Platzgummer committed
223

Valentin Platzgummer's avatar
Valentin Platzgummer committed
224 225 226 227
    #if debug
        WimaArea* joinedAreaPt = new WimaArea(joinedArea, this);
        _visualItems->append(joinedAreaPt);
    #endif
228

Valentin Platzgummer's avatar
Valentin Platzgummer committed
229

230 231 232 233 234 235 236 237 238 239 240 241 242
    // extract survey if present
    QmlObjectListModel* missionItems = _missionController->visualItems();
    SurveyComplexItem* oldSurveyItem;
    {
        int i = 0;
        while( i < missionItems->count() ) {
            oldSurveyItem = qobject_cast<SurveyComplexItem*>(missionItems->get(i));
            if (oldSurveyItem != nullptr){
                break;
            }
            i++;
        }
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
243

244 245
    // reset visual items
    _missionController->removeAll();
246
    missionItems = _missionController->visualItems();
247 248 249 250 251 252 253
    // 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());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
254 255 256 257 258

    // create take off position item
    int index = 1;
    _missionController->insertSimpleMissionItem(serArea->center(), index++);

259
    // create survey item, will be extened with more mission types in the future
Valentin Platzgummer's avatar
Valentin Platzgummer committed
260
    _missionController->insertComplexMissionItem(_missionController->surveyComplexItemName(), opArea->center(), index++);
261 262 263
    SurveyComplexItem* survey = qobject_cast<SurveyComplexItem*>(missionItems->get(missionItems->count()-1));
    if (survey == nullptr){
        qWarning("WimaController::updateMission(): survey == nullptr");
264
        return false;
265 266 267
    } else {
        survey->surveyAreaPolygon()->clear();
        survey->surveyAreaPolygon()->appendVertices(opArea->coordinateList());
268
        //survey->
269
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
270 271 272 273 274

    // calculate path from take off to opArea
    QGeoCoordinate start = serArea->center();
    QGeoCoordinate end = survey->visualTransectPoints().first().value<QGeoCoordinate>();
    QList<QGeoCoordinate> path;
275
    WimaArea::dijkstraPath(start, end, _joinedArea, path);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
276 277 278 279 280 281 282 283 284
    for (int i = 1; i < path.count()-1; i++) {
        _missionController->insertSimpleMissionItem(path.value(i), i+1);
        index++;
    }

    // calculate return path
    start   = survey->visualTransectPoints().last().value<QGeoCoordinate>();
    end     = serArea->center();
    path.clear();
285
    WimaArea::dijkstraPath(start, end, _joinedArea, path);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
286 287 288 289
    for (int i = 1; i < path.count()-1; i++) {
        _missionController->insertSimpleMissionItem(path.value(i), index++);
    }

290
    // create land position item
Valentin Platzgummer's avatar
Valentin Platzgummer committed
291
    _missionController->insertSimpleMissionItem(serArea->center(), index++);
292 293 294 295 296 297 298 299 300 301
    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);
        }
302
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
303

304
    return true;
305 306
}

307
void WimaController::saveToCurrent()
308
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
309
    saveToFile(_currentFile);
310 311
}

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
void WimaController::saveToFile(const QString& filename)
{
    if (filename.isEmpty()) {
        return;
    }

    QString planFilename = filename;
    if (!QFileInfo(filename).fileName().contains(".")) {
        planFilename += QString(".%1").arg(wimaFileExtension);
    }

    QFile file(planFilename);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qgcApp()->showMessage(tr("Plan save error %1 : %2").arg(filename).arg(file.errorString()));
        _currentFile.clear();
        emit currentFileChanged();
    } else {
        QJsonDocument saveDoc = saveToJson();
        file.write(saveDoc.toJson());
        if(_currentFile != planFilename) {
            _currentFile = planFilename;
            emit currentFileChanged();
        }
    }
}

339
bool WimaController::loadFromCurrent()
340
{
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    return loadFromFile(_currentFile);
}

bool WimaController::loadFromFile(const QString &filename)
{
    QString errorString;
    QString errorMessage = tr("Error loading Plan file (%1). %2").arg(filename).arg("%1");

    if (filename.isEmpty()) {
        return false;
    }

    QFileInfo fileInfo(filename);
    QFile file(filename);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        errorString = file.errorString() + QStringLiteral(" ") + filename;
        qgcApp()->showMessage(errorMessage.arg(errorString));
        return false;
    }

    if(fileInfo.suffix() == wimaFileExtension) {
        QJsonDocument   jsonDoc;
        QByteArray      bytes = file.readAll();
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        if (!JsonHelper::isJsonFile(bytes, jsonDoc, errorString)) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
            return false;
        }

        QJsonObject json = jsonDoc.object();
        QJsonArray areaArray = json["AreaItems"].toArray();
        _visualItems->clear();

        for( int i = 0; i < areaArray.size(); i++) {
            QJsonObject jsonArea = areaArray[i].toObject();

            if (jsonArea.contains(WimaArea::areaTypeName) && jsonArea[WimaArea::areaTypeName].isString()) {
                if ( jsonArea[WimaArea::areaTypeName] == WimaArea::wimaAreaName ) {
                    WimaArea* area = new WimaArea(this);
                    bool success = area->loadFromJson(jsonArea, errorString);

                    if ( !success ) {
                        qgcApp()->showMessage(errorMessage.arg(errorString));
                        return false;
                    }

                    _visualItems->append(area);
                    emit visualItemsChanged();
                } else if ( jsonArea[WimaArea::areaTypeName] == WimaGOperationArea::wimaGOperationAreaName) {
                    WimaGOperationArea* opArea = new WimaGOperationArea(this);
                    bool success = opArea->loadFromJson(jsonArea, errorString);

                    if ( !success ) {
                        qgcApp()->showMessage(errorMessage.arg(errorString));
                        return false;
                    }

                    _visualItems->append(opArea);
                    emit visualItemsChanged();
                } else if ( jsonArea[WimaArea::areaTypeName] == WimaServiceArea::wimaServiceAreaName) {
                    WimaServiceArea* serArea = new WimaServiceArea(this);
                    bool success = serArea->loadFromJson(jsonArea, errorString);

                    if ( !success ) {
                        qgcApp()->showMessage(errorMessage.arg(errorString));
                        return false;
                    }

                    _visualItems->append(serArea);
                    emit visualItemsChanged();
                } else if ( jsonArea[WimaArea::areaTypeName] == WimaVCorridor::wimaVCorridorName) {
                    WimaVCorridor* corridor = new WimaVCorridor(this);
                    bool success = corridor->loadFromJson(jsonArea, errorString);

                    if ( !success ) {
                        qgcApp()->showMessage(errorMessage.arg(errorString));
                        return false;
                    }

                    _visualItems->append(corridor);
                    emit visualItemsChanged();
                } else {
                    errorString += QString(tr("%s not supported.\n").arg(WimaArea::areaTypeName));
                    qgcApp()->showMessage(errorMessage.arg(errorString));
                    return false;
                }
            } else {
                errorString += QString(tr("Invalid or non existing entry for %s.\n").arg(WimaArea::areaTypeName));
                return false;
            }
        }

        _currentFile.sprintf("%s/%s.%s", fileInfo.path().toLocal8Bit().data(), fileInfo.completeBaseName().toLocal8Bit().data(), wimaFileExtension);

        emit currentFileChanged();
437
        updateJoinedArea();
438 439 440 441 442 443 444 445

        return true;

    } else {
        errorString += QString(tr("File extension not supported.\n"));
        qgcApp()->showMessage(errorMessage.arg(errorString));
        return false;
    }
446 447 448
}

void WimaController::recalcVehicleCorridor()
449 450 451
{

}
452 453 454 455 456 457 458 459 460 461 462 463 464

void WimaController::recalcVehicleMeasurementAreas()
{

}

void WimaController::recalcAll()
{

}

void WimaController::recalcPolygonInteractivity(int index)
{
465 466 467 468 469
    if (index >= 0 && index < _visualItems->count()) {
        resetAllInteractive();
        WimaArea* interactivePoly = qobject_cast<WimaArea*>(_visualItems->get(index));
        interactivePoly->setInteractive(true);
    }
470 471
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
void WimaController::updateJoinedArea()
{
    // pick first WimaGOperationArea
    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;
        }
    }
    if (opArea == nullptr)
        return;

    // pick first WimaServiceArea
    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;
        }
    }
    if ( serArea == nullptr )
        return;

    // pick first WimaVCorridor
    WimaVCorridor* corridor = nullptr;
    for (int i = 0; i < _visualItems->count(); i++) {
        WimaVCorridor* currentArea = qobject_cast<WimaVCorridor*>(_visualItems->get(i));
        if (currentArea != nullptr){
            corridor = currentArea;
            break;
        }
    }

    // join service area, op area and corridor
    if (corridor != nullptr) {
        WimaArea::join(*corridor, *serArea, _joinedArea);
        _joinedArea.join(*opArea);
    } else {
        WimaArea::join(*serArea, *opArea, _joinedArea);
    }
}

517
void WimaController::resetAllInteractive()
518 519
{
    int itemCount = _visualItems->count();
520 521 522 523 524
    if (itemCount > 0){
        for (int i = 0; i < itemCount; i++) {
            WimaArea* iteratorPoly = qobject_cast<WimaArea*>(_visualItems->get(i));
            iteratorPoly->setInteractive(false);
        }
525 526 527
    }
}

528 529 530 531 532
void WimaController::setInteractive()
{
    recalcPolygonInteractivity(_currentPolygonIndex);
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
QJsonDocument WimaController::saveToJson()
{
    QJsonArray jsonArray;

    for (int i = 0; i < _visualItems->count(); i++) {
        QJsonObject json;

        WimaArea* area = qobject_cast<WimaArea*>(_visualItems->get(i));

        if (area == nullptr) {
            qWarning("WimaController::saveToJson(): Internal error, area == nullptr!");
            return QJsonDocument();
        }

        WimaGOperationArea* opArea =  qobject_cast<WimaGOperationArea*>(area);
        if (opArea != nullptr) {
            opArea->saveToJson(json);
            jsonArray.append(json);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
551
            continue;
552 553 554 555 556 557
        }

        WimaServiceArea* serArea =  qobject_cast<WimaServiceArea*>(area);
        if (serArea != nullptr) {
            serArea->saveToJson(json);
            jsonArray.append(json);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
558
            continue;
559 560 561 562 563 564
        }

        WimaVCorridor* corridor =  qobject_cast<WimaVCorridor*>(area);
        if (corridor != nullptr) {
            corridor->saveToJson(json);
            jsonArray.append(json);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
565
            continue;
566 567 568 569 570 571 572
        }

        // if non of the obove branches was trigger, type must be WimaArea
        area->saveToJson(json);
        jsonArray.append(json);
    }

573 574 575
    QJsonObject json;
    json["AreaItems"] = jsonArray;

576

577
    return QJsonDocument(json);
578 579
}

580 581