APMSensorsComponentController.cc 19.9 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14

#include "APMSensorsComponentController.h"
#include "QGCMAVLink.h"
#include "UAS.h"
#include "QGCApplication.h"
15
#include "APMAutoPilotPlugin.h"
16
#include "ParameterManager.h"
Don Gagne's avatar
Don Gagne committed
17 18 19 20 21 22 23 24 25 26 27

#include <QVariant>
#include <QQmlProperty>

QGC_LOGGING_CATEGORY(APMSensorsComponentControllerLog, "APMSensorsComponentControllerLog")

APMSensorsComponentController::APMSensorsComponentController(void) :
    _statusLog(NULL),
    _progressBar(NULL),
    _compassButton(NULL),
    _accelButton(NULL),
28
    _compassMotButton(NULL),
Don Gagne's avatar
Don Gagne committed
29
    _levelButton(NULL),
Don Gagne's avatar
Don Gagne committed
30 31
    _nextButton(NULL),
    _cancelButton(NULL),
Don Gagne's avatar
Don Gagne committed
32
    _setOrientationsButton(NULL),
Don Gagne's avatar
Don Gagne committed
33 34 35
    _showOrientationCalArea(false),
    _magCalInProgress(false),
    _accelCalInProgress(false),
36
    _compassMotCalInProgress(false),
Don Gagne's avatar
Don Gagne committed
37
    _levelInProgress(false),
Don Gagne's avatar
Don Gagne committed
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
    _orientationCalDownSideDone(false),
    _orientationCalUpsideDownSideDone(false),
    _orientationCalLeftSideDone(false),
    _orientationCalRightSideDone(false),
    _orientationCalNoseDownSideDone(false),
    _orientationCalTailDownSideDone(false),
    _orientationCalDownSideVisible(false),
    _orientationCalUpsideDownSideVisible(false),
    _orientationCalLeftSideVisible(false),
    _orientationCalRightSideVisible(false),
    _orientationCalNoseDownSideVisible(false),
    _orientationCalTailDownSideVisible(false),
    _orientationCalDownSideInProgress(false),
    _orientationCalUpsideDownSideInProgress(false),
    _orientationCalLeftSideInProgress(false),
    _orientationCalRightSideInProgress(false),
    _orientationCalNoseDownSideInProgress(false),
    _orientationCalTailDownSideInProgress(false),
    _orientationCalDownSideRotate(false),
    _orientationCalUpsideDownSideRotate(false),
    _orientationCalLeftSideRotate(false),
    _orientationCalRightSideRotate(false),
    _orientationCalNoseDownSideRotate(false),
    _orientationCalTailDownSideRotate(false),
    _waitingForCancel(false)
{
64 65 66
    _compassCal.setVehicle(_vehicle);
    connect(&_compassCal, &APMCompassCal::vehicleTextMessage, this, &APMSensorsComponentController::_handleUASTextMessage);

67
    APMAutoPilotPlugin * apmPlugin = qobject_cast<APMAutoPilotPlugin*>(_vehicle->autopilotPlugin());
Don Gagne's avatar
Don Gagne committed
68

69
    _sensorsComponent = apmPlugin->sensorsComponent();
70
    connect(_sensorsComponent, &VehicleComponent::setupCompleteChanged, this, &APMSensorsComponentController::setupNeededChanged);
Don Gagne's avatar
Don Gagne committed
71 72

    connect(qgcApp()->toolbox()->mavlinkProtocol(), &MAVLinkProtocol::messageReceived, this, &APMSensorsComponentController::_mavlinkMessageReceived);
Don Gagne's avatar
Don Gagne committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

/// Appends the specified text to the status log area in the ui
void APMSensorsComponentController::_appendStatusLog(const QString& text)
{
    Q_ASSERT(_statusLog);
    
    QVariant returnedValue;
    QVariant varText = text;
    QMetaObject::invokeMethod(_statusLog,
                              "append",
                              Q_RETURN_ARG(QVariant, returnedValue),
                              Q_ARG(QVariant, varText));
}

void APMSensorsComponentController::_startLogCalibration(void)
{
    _hideAllCalAreas();
    
    connect(_uas, &UASInterface::textMessageReceived, this, &APMSensorsComponentController::_handleUASTextMessage);
    
    _compassButton->setEnabled(false);
    _accelButton->setEnabled(false);
96
    _compassMotButton->setEnabled(false);
Don Gagne's avatar
Don Gagne committed
97
    _levelButton->setEnabled(false);
Don Gagne's avatar
Don Gagne committed
98
    _setOrientationsButton->setEnabled(false);
99
    if (_accelCalInProgress || _compassMotCalInProgress) {
100 101
        _nextButton->setEnabled(true);
    }
Don Gagne's avatar
Don Gagne committed
102 103 104 105 106 107 108
    _cancelButton->setEnabled(false);
}

void APMSensorsComponentController::_startVisualCalibration(void)
{
    _compassButton->setEnabled(false);
    _accelButton->setEnabled(false);
109
    _compassMotButton->setEnabled(false);
Don Gagne's avatar
Don Gagne committed
110
    _levelButton->setEnabled(false);
Don Gagne's avatar
Don Gagne committed
111
    _setOrientationsButton->setEnabled(false);
Don Gagne's avatar
Don Gagne committed
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
    _cancelButton->setEnabled(true);

    _resetInternalState();
    
    _progressBar->setProperty("value", 0);
}

void APMSensorsComponentController::_resetInternalState(void)
{
    _orientationCalDownSideDone = true;
    _orientationCalUpsideDownSideDone = true;
    _orientationCalLeftSideDone = true;
    _orientationCalRightSideDone = true;
    _orientationCalTailDownSideDone = true;
    _orientationCalNoseDownSideDone = true;
    _orientationCalDownSideInProgress = false;
    _orientationCalUpsideDownSideInProgress = false;
    _orientationCalLeftSideInProgress = false;
    _orientationCalRightSideInProgress = false;
    _orientationCalNoseDownSideInProgress = false;
    _orientationCalTailDownSideInProgress = false;
    _orientationCalDownSideRotate = false;
    _orientationCalUpsideDownSideRotate = false;
    _orientationCalLeftSideRotate = false;
    _orientationCalRightSideRotate = false;
    _orientationCalNoseDownSideRotate = false;
    _orientationCalTailDownSideRotate = false;

    emit orientationCalSidesRotateChanged();
    emit orientationCalSidesDoneChanged();
    emit orientationCalSidesInProgressChanged();
}

void APMSensorsComponentController::_stopCalibration(APMSensorsComponentController::StopCalibrationCode code)
{
147
    _vehicle->setConnectionLostEnabled(true);
148

Don Gagne's avatar
Don Gagne committed
149 150 151 152
    disconnect(_uas, &UASInterface::textMessageReceived, this, &APMSensorsComponentController::_handleUASTextMessage);
    
    _compassButton->setEnabled(true);
    _accelButton->setEnabled(true);
153
    _compassMotButton->setEnabled(true);
Don Gagne's avatar
Don Gagne committed
154
    _levelButton->setEnabled(true);
Don Gagne's avatar
Don Gagne committed
155
    _setOrientationsButton->setEnabled(true);
Don Gagne's avatar
Don Gagne committed
156 157
    _nextButton->setEnabled(false);
    _cancelButton->setEnabled(false);
158

Don Gagne's avatar
Don Gagne committed
159 160 161
    if (code == StopCalibrationSuccess) {
        _resetInternalState();
        _progressBar->setProperty("value", 1);
162 163 164
        if (_vehicle->parameterManager()->parameterExists(FactSystem::defaultComponentId, QStringLiteral("COMPASS_LEARN"))) {
            _vehicle->parameterManager()->getParameter(FactSystem::defaultComponentId, QStringLiteral("COMPASS_LEARN"))->setRawValue(0);
        }
Don Gagne's avatar
Don Gagne committed
165 166 167 168 169 170 171 172 173 174
    } else {
        _progressBar->setProperty("value", 0);
    }
    
    _waitingForCancel = false;
    emit waitingForCancelChanged();

    _refreshParams();
    
    switch (code) {
175 176 177
    case StopCalibrationSuccess:
        _orientationCalAreaHelpText->setProperty("text", "Calibration complete");
        emit resetStatusTextArea();
Don Gagne's avatar
Don Gagne committed
178
        emit calibrationComplete();
179 180
        break;

Don Gagne's avatar
Don Gagne committed
181 182 183 184
    case StopCalibrationSuccessShowLog:
        emit calibrationComplete();
        break;

185 186 187 188 189 190 191 192
    case StopCalibrationCancelled:
        emit resetStatusTextArea();
        _hideAllCalAreas();
        break;

    default:
        // Assume failed
        _hideAllCalAreas();
193
        qgcApp()->showMessage(QStringLiteral("Calibration failed. Calibration log will be displayed."));
194
        break;
Don Gagne's avatar
Don Gagne committed
195 196 197 198
    }
    
    _magCalInProgress = false;
    _accelCalInProgress = false;
199
    _compassMotCalInProgress = false;
Don Gagne's avatar
Don Gagne committed
200
    _levelInProgress = false;
Don Gagne's avatar
Don Gagne committed
201 202 203 204 205
}

void APMSensorsComponentController::calibrateCompass(void)
{
    _startLogCalibration();
206
    _compassCal.startCalibration();
Don Gagne's avatar
Don Gagne committed
207 208 209 210
}

void APMSensorsComponentController::calibrateAccel(void)
{
211
    _accelCalInProgress = true;
212
    _vehicle->setConnectionLostEnabled(false);
Don Gagne's avatar
Don Gagne committed
213 214 215 216
    _startLogCalibration();
    _uas->startCalibration(UASInterface::StartCalibrationAccel);
}

217 218 219 220 221 222 223 224 225 226 227
void APMSensorsComponentController::calibrateMotorInterference(void)
{
    _compassMotCalInProgress = true;
    _vehicle->setConnectionLostEnabled(false);
    _startLogCalibration();
    _appendStatusLog(tr("Raise the throttle slowly to between 50% ~ 75% (the props will spin!) for 5 ~ 10 seconds."));
    _appendStatusLog(tr("Quickly bring the throttle back down to zero"));
    _appendStatusLog(tr("Press the Next button to complete the calibration"));
    _uas->startCalibration(UASInterface::StartCalibrationCompassMot);
}

Don Gagne's avatar
Don Gagne committed
228 229 230 231 232 233 234 235 236
void APMSensorsComponentController::levelHorizon(void)
{
    _levelInProgress = true;
    _vehicle->setConnectionLostEnabled(false);
    _startLogCalibration();
    _appendStatusLog(tr("Hold the vehicle in its level flight position."));
    _uas->startCalibration(UASInterface::StartCalibrationLevel);
}

Don Gagne's avatar
Don Gagne committed
237 238 239 240 241
void APMSensorsComponentController::_handleUASTextMessage(int uasId, int compId, int severity, QString text)
{
    Q_UNUSED(compId);
    Q_UNUSED(severity);
    
242
    if (uasId != _vehicle->id()) {
Don Gagne's avatar
Don Gagne committed
243 244 245
        return;
    }

246 247
    if (text.startsWith(QLatin1Literal("PreArm:")) || text.startsWith(QLatin1Literal("EKF"))
            || text.startsWith(QLatin1Literal("Arm")) || text.startsWith(QLatin1Literal("Initialising"))) {
Don Gagne's avatar
Don Gagne committed
248 249 250
        return;
    }

251
    if (text.contains(QLatin1Literal("progress <"))) {
252 253 254 255 256 257 258 259 260 261
        QString percent = text.split("<").last().split(">").first();
        bool ok;
        int p = percent.toInt(&ok);
        if (ok) {
            Q_ASSERT(_progressBar);
            _progressBar->setProperty("value", (float)(p / 100.0));
        }
        return;
    }

262
    QString anyKey(QStringLiteral("and press any"));
Don Gagne's avatar
Don Gagne committed
263
    if (text.contains(anyKey)) {
264
        text = text.left(text.indexOf(anyKey)) + QStringLiteral("and click Next to continue.");
265
        _nextButton->setEnabled(true);
Don Gagne's avatar
Don Gagne committed
266 267 268 269 270
    }

    _appendStatusLog(text);
    qCDebug(APMSensorsComponentControllerLog) << text << severity;

271
    if (text.contains(QLatin1String("Calibration successful"))) {
Don Gagne's avatar
Don Gagne committed
272 273 274 275
        _stopCalibration(StopCalibrationSuccess);
        return;
    }

276
    if (text.contains(QLatin1String("FAILED"))) {
Don Gagne's avatar
Don Gagne committed
277 278 279 280 281
        _stopCalibration(StopCalibrationFailed);
        return;
    }

    // All calibration messages start with [cal]
282
    QString calPrefix(QStringLiteral("[cal] "));
Don Gagne's avatar
Don Gagne committed
283 284 285 286 287
    if (!text.startsWith(calPrefix)) {
        return;
    }
    text = text.right(text.length() - calPrefix.length());

288
    QString calStartPrefix(QStringLiteral("calibration started: "));
Don Gagne's avatar
Don Gagne committed
289 290 291 292 293
    if (text.startsWith(calStartPrefix)) {
        text = text.right(text.length() - calStartPrefix.length());
        
        _startVisualCalibration();
        
294
        if (text == QLatin1Literal("accel") || text == QLatin1Literal("mag") || text == QLatin1Literal("gyro")) {
Don Gagne's avatar
Don Gagne committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 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 339 340 341 342 343 344 345
            // Reset all progress indication
            _orientationCalDownSideDone = false;
            _orientationCalUpsideDownSideDone = false;
            _orientationCalLeftSideDone = false;
            _orientationCalRightSideDone = false;
            _orientationCalTailDownSideDone = false;
            _orientationCalNoseDownSideDone = false;
            _orientationCalDownSideInProgress = false;
            _orientationCalUpsideDownSideInProgress = false;
            _orientationCalLeftSideInProgress = false;
            _orientationCalRightSideInProgress = false;
            _orientationCalNoseDownSideInProgress = false;
            _orientationCalTailDownSideInProgress = false;
            
            // Reset all visibility
            _orientationCalDownSideVisible = false;
            _orientationCalUpsideDownSideVisible = false;
            _orientationCalLeftSideVisible = false;
            _orientationCalRightSideVisible = false;
            _orientationCalTailDownSideVisible = false;
            _orientationCalNoseDownSideVisible = false;
            
            _orientationCalAreaHelpText->setProperty("text", "Place your vehicle into one of the Incomplete orientations shown below and hold it still");
            
            if (text == "accel") {
                _accelCalInProgress = true;
                _orientationCalDownSideVisible = true;
                _orientationCalUpsideDownSideVisible = true;
                _orientationCalLeftSideVisible = true;
                _orientationCalRightSideVisible = true;
                _orientationCalTailDownSideVisible = true;
                _orientationCalNoseDownSideVisible = true;
            } else if (text == "mag") {
                _magCalInProgress = true;
                _orientationCalDownSideVisible = true;
                _orientationCalUpsideDownSideVisible = true;
                _orientationCalLeftSideVisible = true;
                _orientationCalRightSideVisible = true;
                _orientationCalTailDownSideVisible = true;
                _orientationCalNoseDownSideVisible = true;
            } else {
                Q_ASSERT(false);
            }
            emit orientationCalSidesDoneChanged();
            emit orientationCalSidesVisibleChanged();
            emit orientationCalSidesInProgressChanged();
            _updateAndEmitShowOrientationCalArea(true);
        }
        return;
    }
    
346
    if (text.endsWith(QLatin1Literal("orientation detected"))) {
Don Gagne's avatar
Don Gagne committed
347 348 349
        QString side = text.section(" ", 0, 0);
        qDebug() << "Side started" << side;
        
350
        if (side == QLatin1Literal("down")) {
Don Gagne's avatar
Don Gagne committed
351 352 353 354
            _orientationCalDownSideInProgress = true;
            if (_magCalInProgress) {
                _orientationCalDownSideRotate = true;
            }
355
        } else if (side == QLatin1Literal("up")) {
Don Gagne's avatar
Don Gagne committed
356 357 358 359
            _orientationCalUpsideDownSideInProgress = true;
            if (_magCalInProgress) {
                _orientationCalUpsideDownSideRotate = true;
            }
360
        } else if (side == QLatin1Literal("left")) {
Don Gagne's avatar
Don Gagne committed
361 362 363 364
            _orientationCalLeftSideInProgress = true;
            if (_magCalInProgress) {
                _orientationCalLeftSideRotate = true;
            }
365
        } else if (side == QLatin1Literal("right")) {
Don Gagne's avatar
Don Gagne committed
366 367 368 369
            _orientationCalRightSideInProgress = true;
            if (_magCalInProgress) {
                _orientationCalRightSideRotate = true;
            }
370
        } else if (side == QLatin1Literal("front")) {
Don Gagne's avatar
Don Gagne committed
371 372 373 374
            _orientationCalNoseDownSideInProgress = true;
            if (_magCalInProgress) {
                _orientationCalNoseDownSideRotate = true;
            }
375
        } else if (side == QLatin1Literal("back")) {
Don Gagne's avatar
Don Gagne committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
            _orientationCalTailDownSideInProgress = true;
            if (_magCalInProgress) {
                _orientationCalTailDownSideRotate = true;
            }
        }
        
        if (_magCalInProgress) {
            _orientationCalAreaHelpText->setProperty("text", "Rotate the vehicle continuously as shown in the diagram until marked as Completed");
        } else {
            _orientationCalAreaHelpText->setProperty("text", "Hold still in the current orientation");
        }
        
        emit orientationCalSidesInProgressChanged();
        emit orientationCalSidesRotateChanged();
        return;
    }
    
393
    if (text.endsWith(QLatin1Literal("side done, rotate to a different side"))) {
Don Gagne's avatar
Don Gagne committed
394 395 396
        QString side = text.section(" ", 0, 0);
        qDebug() << "Side finished" << side;
        
397
        if (side == QLatin1Literal("down")) {
Don Gagne's avatar
Don Gagne committed
398 399 400
            _orientationCalDownSideInProgress = false;
            _orientationCalDownSideDone = true;
            _orientationCalDownSideRotate = false;
401
        } else if (side == QLatin1Literal("up")) {
Don Gagne's avatar
Don Gagne committed
402 403
            _orientationCalUpsideDownSideInProgress = false;
            _orientationCalUpsideDownSideDone = true;
404
            _orientationCalUpsideDownSideRotate = false;
405
        } else if (side == QLatin1Literal("left")) {
Don Gagne's avatar
Don Gagne committed
406 407 408
            _orientationCalLeftSideInProgress = false;
            _orientationCalLeftSideDone = true;
            _orientationCalLeftSideRotate = false;
409
        } else if (side == QLatin1Literal("right")) {
Don Gagne's avatar
Don Gagne committed
410 411
            _orientationCalRightSideInProgress = false;
            _orientationCalRightSideDone = true;
412
            _orientationCalRightSideRotate = false;
413
        } else if (side == QLatin1Literal("front")) {
Don Gagne's avatar
Don Gagne committed
414 415 416
            _orientationCalNoseDownSideInProgress = false;
            _orientationCalNoseDownSideDone = true;
            _orientationCalNoseDownSideRotate = false;
417
        } else if (side == QLatin1Literal("back")) {
Don Gagne's avatar
Don Gagne committed
418 419
            _orientationCalTailDownSideInProgress = false;
            _orientationCalTailDownSideDone = true;
420
            _orientationCalTailDownSideRotate = false;
Don Gagne's avatar
Don Gagne committed
421 422 423 424 425 426 427 428 429 430
        }
        
        _orientationCalAreaHelpText->setProperty("text", "Place you vehicle into one of the orientations shown below and hold it still");

        emit orientationCalSidesInProgressChanged();
        emit orientationCalSidesDoneChanged();
        emit orientationCalSidesRotateChanged();
        return;
    }
    
431
    if (text.startsWith(QLatin1Literal("calibration done:"))) {
432 433 434 435
        _stopCalibration(StopCalibrationSuccess);
        return;
    }

436
    if (text.startsWith(QLatin1Literal("calibration cancelled"))) {
Don Gagne's avatar
Don Gagne committed
437 438 439 440
        _stopCalibration(_waitingForCancel ? StopCalibrationCancelled : StopCalibrationFailed);
        return;
    }

441
    if (text.startsWith(QLatin1Literal("calibration failed"))) {
442 443 444
        _stopCalibration(StopCalibrationFailed);
        return;
    }
Don Gagne's avatar
Don Gagne committed
445 446 447 448 449 450
}

void APMSensorsComponentController::_refreshParams(void)
{
    QStringList fastRefreshList;
    
451 452
    fastRefreshList << QStringLiteral("COMPASS_OFS_X") << QStringLiteral("COMPASS_OFS_X") << QStringLiteral("COMPASS_OFS_X")
                    << QStringLiteral("INS_ACCOFFS_X") << QStringLiteral("INS_ACCOFFS_Y") << QStringLiteral("INS_ACCOFFS_Z");
453
    foreach (const QString &paramName, fastRefreshList) {
454
        _vehicle->parameterManager()->refreshParameter(FactSystem::defaultComponentId, paramName);
Don Gagne's avatar
Don Gagne committed
455 456 457
    }
    
    // Now ask for all to refresh
458 459
    _vehicle->parameterManager()->refreshParametersPrefix(FactSystem::defaultComponentId, QStringLiteral("COMPASS_"));
    _vehicle->parameterManager()->refreshParametersPrefix(FactSystem::defaultComponentId, QStringLiteral("INS_"));
Don Gagne's avatar
Don Gagne committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
}

void APMSensorsComponentController::_updateAndEmitShowOrientationCalArea(bool show)
{
    _showOrientationCalArea = show;
    emit showOrientationCalAreaChanged();
}

void APMSensorsComponentController::_hideAllCalAreas(void)
{
    _updateAndEmitShowOrientationCalArea(false);
}

void APMSensorsComponentController::cancelCalibration(void)
{
    _waitingForCancel = true;
    emit waitingForCancelChanged();
    _cancelButton->setEnabled(false);
478 479 480 481 482 483 484 485

    if (_magCalInProgress) {
        _compassCal.cancelCalibration();
    } else {
        // The firmware doesn't always allow us to cancel calibration. The best we can do is wait
        // for it to timeout.
        _uas->stopCalibration();
    }
Don Gagne's avatar
Don Gagne committed
486 487 488 489 490 491 492 493 494
}

void APMSensorsComponentController::nextClicked(void)
{
    mavlink_message_t       msg;
    mavlink_command_ack_t   ack;

    ack.command = 0;
    ack.result = 1;
495 496 497 498 499
    mavlink_msg_command_ack_encode_chan(qgcApp()->toolbox()->mavlinkProtocol()->getSystemId(),
                                        qgcApp()->toolbox()->mavlinkProtocol()->getComponentId(),
                                        _vehicle->priorityLink()->mavlinkChannel(),
                                        &msg,
                                        &ack);
Don Gagne's avatar
Don Gagne committed
500

501
    _vehicle->sendMessageOnLink(_vehicle->priorityLink(), msg);
502 503 504 505

    if (_compassMotCalInProgress) {
        _stopCalibration(StopCalibrationSuccess);
    }
Don Gagne's avatar
Don Gagne committed
506
}
507 508 509 510 511 512 513 514 515 516

bool APMSensorsComponentController::compassSetupNeeded(void) const
{
    return _sensorsComponent->compassSetupNeeded();
}

bool APMSensorsComponentController::accelSetupNeeded(void) const
{
    return _sensorsComponent->accelSetupNeeded();
}
Don Gagne's avatar
Don Gagne committed
517 518 519 520 521

bool APMSensorsComponentController::usingUDPLink(void)
{
    return _vehicle->priorityLink()->getLinkConfiguration()->type() == LinkConfiguration::TypeUdp;
}
Don Gagne's avatar
Don Gagne committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

void APMSensorsComponentController::_mavlinkMessageReceived(LinkInterface* link, mavlink_message_t message)
{
    Q_UNUSED(link);

    if (message.sysid != _vehicle->id()) {
        return;
    }

    if (message.msgid == MAVLINK_MSG_ID_COMMAND_ACK && _levelInProgress) {
        mavlink_command_ack_t commandAck;
        mavlink_msg_command_ack_decode(&message, &commandAck);

        if (commandAck.command == MAV_CMD_PREFLIGHT_CALIBRATION) {
            switch (commandAck.result) {
            case MAV_RESULT_ACCEPTED:
                _appendStatusLog(tr("Level horizon complete"));
                _stopCalibration(StopCalibrationSuccessShowLog);
                break;
            default:
                _appendStatusLog(tr("Level horizon failed"));
                _stopCalibration(StopCalibrationFailed);
                break;
            }
        }
    }
}