11QGC_LOGGING_CATEGORY(SensorsComponentControllerLog,
"AutoPilotPlugins.SensorsComponentController")
19void px4SensorsCancelCalibrationResultHandler(
void* ,
int ,
24 QGC::showAppMessage(SensorsComponentController::tr(
"Vehicle did not respond to the calibration cancel"));
27 if (ack.result == MAV_RESULT_ACCEPTED || ack.result == MAV_RESULT_TEMPORARILY_REJECTED) {
36 : _showOrientationCalArea(false)
37 , _gyroCalInProgress(false)
38 , _magCalInProgress(false)
39 , _accelCalInProgress(false)
40 , _airspeedCalInProgress(false)
41 , _levelCalInProgress(false)
42 , _baroCalInProgress(false)
43 , _orientationCalDownSideVisible(false)
44 , _orientationCalUpsideDownSideVisible(false)
45 , _orientationCalLeftSideVisible(false)
46 , _orientationCalRightSideVisible(false)
47 , _orientationCalNoseDownSideVisible(false)
48 , _orientationCalTailDownSideVisible(false)
55 , _unknownFirmwareVersion(false)
56 , _waitingForCancel(false)
60 _largeVehicleCalibrationTimer.setSingleShot(
true);
61 _largeVehicleCalibrationTimer.setInterval(_largeVehicleCalibrationTimeoutMs);
62 connect(&_largeVehicleCalibrationTimer, &QTimer::timeout,
this,
63 &SensorsComponentController::_largeVehicleCalibrationTimedOut);
77void SensorsComponentController::_appendStatusLog(
const QString& text)
79 if (!_calibrationLog.isEmpty()) {
80 _calibrationLog += QLatin1Char(
'\n');
82 _calibrationLog += text;
88 _calibrationResult = result;
92void SensorsComponentController::_startLogCalibration(
void)
94 _unknownFirmwareVersion =
false;
95 _calibrationFailureText.clear();
96 _calibrationDetail.clear();
97 _calibrationLog.clear();
98 _sideAlreadyDone =
false;
101 _setCalibrationProgress(0);
105 Qt::UniqueConnection);
108void SensorsComponentController::_setCalibrationProgress(
double progress)
110 if (!qFuzzyIsNull(_calibrationProgress - progress)) {
111 _calibrationProgress = progress;
116void SensorsComponentController::_startVisualCalibration(
void)
119 _setCalibrationProgress(0);
122void SensorsComponentController::_setAllSidesState(SideCalState state)
124 _orientationCalDownSideState = state;
125 _orientationCalUpsideDownSideState = state;
126 _orientationCalLeftSideState = state;
127 _orientationCalRightSideState = state;
128 _orientationCalTailDownSideState = state;
129 _orientationCalNoseDownSideState = state;
134void SensorsComponentController::_stopCalibration(SensorsComponentController::StopCalibrationCode code)
139 const bool inlineCalibration = _baroCalInProgress || _levelCalInProgress || _gyroCalInProgress;
141 if (code == StopCalibrationSuccess) {
143 _setCalibrationProgress(1);
148 _setCalibrationProgress(0);
151 _waitingForCancel =
false;
157 case StopCalibrationSuccess:
159 if (_magCalInProgress) {
165 case StopCalibrationCancelled:
176 if (inlineCalibration) {
180 _magCalInProgress =
false;
181 _accelCalInProgress =
false;
182 _gyroCalInProgress =
false;
183 _airspeedCalInProgress =
false;
184 _levelCalInProgress =
false;
185 _baroCalInProgress =
false;
192 _startLogCalibration();
198 _startLogCalibration();
204 _startLogCalibration();
210 _startLogCalibration();
216 _startLogCalibration();
222 _startLogCalibration();
230 _watchLargeVehicleCalibration();
233 _vehicle->sendMavCommand(
_vehicle->defaultComponentId(), MAV_CMD_FIXED_MAG_CAL_YAW,
235 static_cast<float>(headingDegrees),
245 if (text.contains(QStringLiteral(
"Mag quick calibration finished"), Qt::CaseInsensitive)) {
249 static const QStringList failureTexts = {
250 QStringLiteral(
"mag quick cal"),
251 QStringLiteral(
"calibration failed"),
252 QStringLiteral(
"calibration save failed"),
253 QStringLiteral(
"cannot open calibration storage"),
255 for (
const QString& failureText : failureTexts) {
256 if (text.contains(failureText, Qt::CaseInsensitive)) {
264void SensorsComponentController::_watchLargeVehicleCalibration(
void)
267 &SensorsComponentController::_handleLargeVehicleCalibrationMessage, Qt::UniqueConnection);
269 Qt::UniqueConnection);
270 _largeVehicleCalibrationTimer.start();
271 _setLargeVehicleCalibrationPending(
true);
274void SensorsComponentController::_stopWatchingLargeVehicleCalibration(
void)
276 _largeVehicleCalibrationTimer.stop();
278 &SensorsComponentController::_handleLargeVehicleCalibrationMessage);
280 &SensorsComponentController::_handleLargeVehicleCommandResult);
281 _setLargeVehicleCalibrationPending(
false);
284void SensorsComponentController::_setLargeVehicleCalibrationPending(
bool pending)
286 if (_largeVehicleCalibrationPending != pending) {
287 _largeVehicleCalibrationPending = pending;
294 switch (failureCode) {
296 return tr(
"The vehicle did not respond.");
298 return tr(
"A calibration request is already waiting for the vehicle.");
304 "The vehicle did not accept the calibration. It may be armed or busy, or its firmware may not support it.");
307void SensorsComponentController::_handleLargeVehicleCommandResult(quint32 vehicleId,
int targetComponent,
int command,
308 int ackResult,
int failureCode)
310 Q_UNUSED(targetComponent);
312 if (vehicleId !=
_vehicle->
id() || command != MAV_CMD_FIXED_MAG_CAL_YAW) {
316 if (ackResult == MAV_RESULT_ACCEPTED || ackResult == MAV_RESULT_IN_PROGRESS) {
320 _stopWatchingLargeVehicleCalibration();
324void SensorsComponentController::_handleLargeVehicleCalibrationMessage(quint32 uasId,
int compId,
int severity,
325 QString text,
const QString& description)
329 Q_UNUSED(description);
340 _stopWatchingLargeVehicleCalibration();
345void SensorsComponentController::_largeVehicleCalibrationTimedOut(
void)
348 _stopWatchingLargeVehicleCalibration();
351 tr(
"No result from the vehicle. Check the compass offsets, or try again."));
361 if (_magCalInProgress) {
364 if (_gyroCalInProgress) {
367 if (_accelCalInProgress) {
370 if (_levelCalInProgress) {
373 if (_airspeedCalInProgress) {
376 if (_baroCalInProgress) {
382void SensorsComponentController::_handleUASTextMessage(quint32 uasId,
int compId,
int severity, QString text,
383 const QString& description)
387 Q_UNUSED(description);
394 text.replace(
"<",
"<");
395 text.replace(
">",
">");
397 if (text.contains(
"progress <")) {
398 QString percent = text.split(
"<").last().split(
">").first();
400 int p = percent.toInt(&ok);
402 _setCalibrationProgress(p / 100.0);
407 _appendStatusLog(text);
408 qCDebug(SensorsComponentControllerLog) << text;
410 if (_unknownFirmwareVersion) {
416 QString calPrefix(
"[cal] ");
417 if (!text.startsWith(calPrefix)) {
420 _calibrationDetail = text;
423 text = text.right(text.length() - calPrefix.length());
425 QString calStartPrefix(
"calibration started: ");
426 if (text.startsWith(calStartPrefix)) {
427 text = text.right(text.length() - calStartPrefix.length());
430 QStringList parts = text.split(
" ");
431 if (parts.count() != 2 && parts[0].toInt() != _supportedFirmwareCalVersion) {
432 _unknownFirmwareVersion =
true;
433 QString msg = tr(
"Unsupported calibration firmware version, using log");
434 _appendStatusLog(msg);
439 _startVisualCalibration();
442 if (text ==
"accel" || text ==
"mag" || text ==
"gyro") {
446 _orientationCalDownSideVisible =
false;
447 _orientationCalUpsideDownSideVisible =
false;
448 _orientationCalLeftSideVisible =
false;
449 _orientationCalRightSideVisible =
false;
450 _orientationCalTailDownSideVisible =
false;
451 _orientationCalNoseDownSideVisible =
false;
453 if (text ==
"accel") {
454 _accelCalInProgress =
true;
455 _orientationCalDownSideVisible =
true;
456 _orientationCalUpsideDownSideVisible =
true;
457 _orientationCalLeftSideVisible =
true;
458 _orientationCalRightSideVisible =
true;
459 _orientationCalTailDownSideVisible =
true;
460 _orientationCalNoseDownSideVisible =
true;
461 }
else if (text ==
"mag") {
465 const QString sidesParamName =
_vehicle->parameterManager()->parameterExists(
467 ? QStringLiteral(
"SENS_MAG_SIDES")
468 : QStringLiteral(
"CAL_MAG_SIDES");
473 Fact* const calMagSides = _vehicle->parameterManager()->getParameter(
474 ParameterManager::defaultComponentId, sidesParamName);
476 sides = calMagSides->rawValue().toFloat();
478 qCWarning(SensorsComponentControllerLog)
479 << sidesParamName <<
"disappeared while starting calibration";
480 sides = (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
484 sides = (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
487 _magCalInProgress =
true;
488 _orientationCalTailDownSideVisible = ((sides & (1 << 0)) > 0);
489 _orientationCalNoseDownSideVisible = ((sides & (1 << 1)) > 0);
490 _orientationCalLeftSideVisible = ((sides & (1 << 2)) > 0);
491 _orientationCalRightSideVisible = ((sides & (1 << 3)) > 0);
492 _orientationCalUpsideDownSideVisible = ((sides & (1 << 4)) > 0);
493 _orientationCalDownSideVisible = ((sides & (1 << 5)) > 0);
494 }
else if (text ==
"gyro") {
495 _gyroCalInProgress =
true;
496 _orientationCalDownSideVisible =
true;
498 qWarning() <<
"Unknown calibration message type" << text;
501 _updateAndEmitShowOrientationCalArea(
true);
502 }
else if (text ==
"airspeed") {
503 _airspeedCalInProgress =
true;
504 }
else if (text ==
"level") {
505 _levelCalInProgress =
true;
506 }
else if (text ==
"baro") {
507 _baroCalInProgress =
true;
513 if (text.endsWith(
"orientation detected")) {
514 QString side = text.section(
" ", 0, 0);
515 qCDebug(SensorsComponentControllerLog) <<
"Side started" << side;
517 if (side ==
"down") {
518 _orientationCalDownSideState = SideCalStateInProgress;
519 }
else if (side ==
"up") {
520 _orientationCalUpsideDownSideState = SideCalStateInProgress;
521 }
else if (side ==
"left") {
522 _orientationCalLeftSideState = SideCalStateInProgress;
523 }
else if (side ==
"right") {
524 _orientationCalRightSideState = SideCalStateInProgress;
525 }
else if (side ==
"front") {
526 _orientationCalNoseDownSideState = SideCalStateInProgress;
527 }
else if (side ==
"back") {
528 _orientationCalTailDownSideState = SideCalStateInProgress;
531 _sideAlreadyDone =
false;
533 emit orientationCalSidesStateChanged();
537 if (text.endsWith(
"side done, rotate to a different side")) {
538 QString side = text.section(
" ", 0, 0);
539 qCDebug(SensorsComponentControllerLog) <<
"Side finished" << side;
541 if (side ==
"down") {
542 _orientationCalDownSideState = SideCalStateCompleted;
543 }
else if (side ==
"up") {
544 _orientationCalUpsideDownSideState = SideCalStateCompleted;
545 }
else if (side ==
"left") {
546 _orientationCalLeftSideState = SideCalStateCompleted;
547 }
else if (side ==
"right") {
548 _orientationCalRightSideState = SideCalStateCompleted;
549 }
else if (side ==
"front") {
550 _orientationCalNoseDownSideState = SideCalStateCompleted;
551 }
else if (side ==
"back") {
552 _orientationCalTailDownSideState = SideCalStateCompleted;
555 _sideAlreadyDone =
false;
557 emit orientationCalSidesStateChanged();
561 if (text.endsWith(
"side already completed")) {
562 _sideAlreadyDone =
true;
563 emit orientationCalSidesStateChanged();
567 QString calCompletePrefix(
"calibration done:");
568 if (text.startsWith(calCompletePrefix)) {
569 _stopCalibration(StopCalibrationSuccess);
573 if (text.startsWith(
"calibration cancelled")) {
574 _stopCalibration(_waitingForCancel ? StopCalibrationCancelled : StopCalibrationFailed);
578 if (text.startsWith(
"calibration failed")) {
579 _calibrationFailureText =
580 _calibrationDetail.isEmpty() ? text : _calibrationDetail + QStringLiteral(
": ") + text;
581 _stopCalibration(StopCalibrationFailed);
586void SensorsComponentController::_refreshParams(
void)
589 QStringLiteral(
"CAL_MAG0_ID"), QStringLiteral(
"CAL_MAG1_ID"), QStringLiteral(
"CAL_MAG2_ID"),
590 QStringLiteral(
"CAL_MAG0_ROT"), QStringLiteral(
"CAL_MAG1_ROT"), QStringLiteral(
"CAL_MAG2_ROT"),
591 QStringLiteral(
"CAL_*"),
592 QStringLiteral(
"SENS_*"),
596void SensorsComponentController::_updateAndEmitShowOrientationCalArea(
bool show)
598 _showOrientationCalArea = show;
602void SensorsComponentController::_hideAllCalAreas(
void)
604 _updateAndEmitShowOrientationCalArea(
false);
609 _waitingForCancel =
true;
613 .resultHandler = px4SensorsCancelCalibrationResultHandler,
614 .resultHandlerData =
nullptr,
615 .progressHandler =
nullptr,
616 .progressHandlerData =
nullptr,
619 _vehicle->sendMavCommandWithHandler(&handlerInfo,
_vehicle->defaultComponentId(), MAV_CMD_PREFLIGHT_CALIBRATION);
622void SensorsComponentController::_handleParametersReset(
bool success)
627 QTimer::singleShot(1000,
this, [
this]() {
638 auto compId =
_vehicle->defaultComponentId();
641 MAV_CMD_PREFLIGHT_STORAGE,
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
#define QGC_LOGGING_CATEGORY(name, categoryStr)
struct __mavlink_command_ack_t mavlink_command_ack_t
static QString description(quint32 deviceId)
Chip name and bus for display, such as "ICM42688P ยท SPI". Empty for the unset device ID 0.
static constexpr int defaultComponentId
SensorsComponentController(void)
Q_INVOKABLE void calibrateCompassLargeVehicle(double headingDegrees)
Q_INVOKABLE void calibrateGyro(void)
Q_INVOKABLE void calibrateAccel(void)
void orientationCalSidesVisibleChanged(void)
ActiveCalibration
Calibration which is running through the firmware's [cal] message protocol.
@ ActiveCalibrationCompass
@ ActiveCalibrationAirspeed
void magCalComplete(void)
Q_INVOKABLE void calibrateLevel(void)
static QString largeVehicleCommandFailureText(int failureCode)
Why the large vehicle compass calibration command failed, from the failureCode of Vehicle::mavCommand...
void calibrationProgressChanged(void)
@ CalibrationResultNone
Running, cancelled, or none has run.
@ CalibrationResultFailed
@ CalibrationResultSuccess
void inlineCalibrationFinished(bool success, const QString &message)
void waitingForCancelChanged(void)
LargeVehicleCalibrationResult
@ Pending
Not a result message.
ActiveCalibration activeCalibration() const
static LargeVehicleCalibrationResult largeVehicleCalibrationResult(const QString &text)
What a status text from the vehicle says about the outcome of a large vehicle compass calibration.
@ SideCalStateIncomplete
Calibration running, side not yet calibrated.
@ SideCalStateIdle
No calibration data to show (neutral preview).
@ SideCalStateCompleted
Side calibration complete.
void largeVehicleCalibrationPendingChanged(void)
void orientationCalSidesStateChanged(void)
Q_INVOKABLE QString deviceDescription(int deviceId) const
Chip and bus of a CAL_*_ID device ID parameter value, empty when the ID is not set.
void largeVehicleCalibrationFinished(bool success, const QString &message)
success is false when the firmware reported a failure, message is its text
Q_INVOKABLE void calibrateAirspeed(void)
Q_INVOKABLE void calibrateCompass(void)
What the firmware has logged during the calibration, one message per line.
Q_INVOKABLE void calibrateBaro(void)
Q_INVOKABLE void cancelCalibration(void)
void calibrationResultChanged(void)
void calibrationActiveChanged(void)
void calibrationLogChanged(void)
Q_INVOKABLE bool usingUDPLink(void)
Q_INVOKABLE void resetFactoryParameters()
void showOrientationCalAreaChanged(void)
static void showCommandAckError(const mavlink_command_ack_t &ack)
void sensorsParametersResetAck(bool success)
void textMessageReceived(quint32 sysid, int componentid, int severity, QString text, QString description)
void mavCommandResult(quint32 vehicleId, int targetComponent, int command, int ackResult, int failureCode)
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Result of timestamp-based calibration/matching.
struct VehicleTypes::MavCmdAckHandlerInfo_s MavCmdAckHandlerInfo_t
Callback info bundle for sendMavCommandWithHandler.
MavCmdResultFailureCode_t
@ MavCmdResultFailureDuplicateCommand
Unable to send command since duplicate is already being waited on for response.
@ MavCmdResultFailureNoResponseToCommand
No response from vehicle to command.