12#include <QtCore/QVariant>
14QGC_LOGGING_CATEGORY(APMSensorsComponentControllerLog,
"AutoPilotPlugins.APMSensorsComponentController")
15QGC_LOGGING_CATEGORY(APMSensorsComponentControllerVerboseLog,
"AutoPilotPlugins.APMSensorsComponentController:verbose")
24 _sensorsComponent = qobject_cast<APMSensorsComponent*>(varVehicleComponent.value<
VehicleComponent*>());
25 if (_sensorsComponent) {
30 if (_sensorsComponent) {
33 qCWarning(APMSensorsComponentControllerLog) <<
"Sensors component is missing";
40 _restorePreviousCompassCalFitness();
43void APMSensorsComponentController::_appendStatusLog(
const QString &text)
46 qCWarning(APMSensorsComponentControllerLog) <<
"Status log is not available";
50 const QString varText = text;
51 (void) QMetaObject::invokeMethod(_statusLog,
"append", varText);
54void APMSensorsComponentController::_startLogCalibration()
62 _nextButton->setEnabled(
true);
70void APMSensorsComponentController::_startVisualCalibration()
73 _cancelButton->setEnabled(
true);
74 _nextButton->setEnabled(
false);
76 _resetInternalState();
78 (void) _progressBar->setProperty(
"value", 0);
84void APMSensorsComponentController::_resetInternalState()
86 _orientationCalDownSideDone =
true;
87 _orientationCalUpsideDownSideDone =
true;
88 _orientationCalLeftSideDone =
true;
89 _orientationCalRightSideDone =
true;
90 _orientationCalTailDownSideDone =
true;
91 _orientationCalNoseDownSideDone =
true;
92 _orientationCalDownSideInProgress =
false;
93 _orientationCalUpsideDownSideInProgress =
false;
94 _orientationCalLeftSideInProgress =
false;
95 _orientationCalRightSideInProgress =
false;
96 _orientationCalNoseDownSideInProgress =
false;
97 _orientationCalTailDownSideInProgress =
false;
98 _orientationCalDownSideRotate =
false;
99 _orientationCalUpsideDownSideRotate =
false;
100 _orientationCalLeftSideRotate =
false;
101 _orientationCalRightSideRotate =
false;
102 _orientationCalNoseDownSideRotate =
false;
103 _orientationCalTailDownSideRotate =
false;
110void APMSensorsComponentController::_stopCalibration(APMSensorsComponentController::StopCalibrationCode code)
113 _magCalStartAccepted =
false;
114 _magCalCancelBeforeStartPending =
false;
117 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(
true);
122 _nextButton->setEnabled(
false);
123 _cancelButton->setEnabled(
false);
126 _restorePreviousCompassCalFitness();
127 if (code == StopCalibrationFailed) {
129 _vehicle->sendMavCommand(
_vehicle->defaultComponentId(), MAV_CMD_DO_CANCEL_MAG_CAL,
false );
133 if (code == StopCalibrationSuccess) {
134 _resetInternalState();
135 (void) _progressBar->setProperty(
"value", 1);
140 (void) _progressBar->setProperty(
"value", 0);
143 _waitingForCancel =
false;
148 const bool succeeded = (code == StopCalibrationSuccess) || (code == StopCalibrationSuccessShowLog);
155 case StopCalibrationSuccess:
156 (void) _orientationCalAreaHelpText->setProperty(
"text", tr(
"Calibration complete"));
160 case StopCalibrationSuccessShowLog:
163 case StopCalibrationCancelled:
179void APMSensorsComponentController::_mavCommandResult(quint32 vehicleId,
int component,
int command,
int result,
182 Q_UNUSED(component); Q_UNUSED(failureCode);
189 case MAV_CMD_DO_CANCEL_MAG_CAL:
190 if (_magCalCancelBeforeStartPending) {
193 _magCalCancelBeforeStartPending =
false;
197 case MAV_CMD_DO_START_MAG_CAL:
198 if (result == MAV_RESULT_ACCEPTED) {
199 _magCalStartAccepted =
true;
201 _appendStatusLog(tr(
"Failed to start compass calibration"));
202 _stopCalibration(StopCalibrationFailed);
205 case MAV_CMD_FIXED_MAG_CAL_YAW:
206 if (result == MAV_RESULT_ACCEPTED) {
207 _appendStatusLog(tr(
"Successfully completed"));
208 _stopCalibration(StopCalibrationSuccessShowLog);
210 _appendStatusLog(tr(
"Failed"));
211 _stopCalibration(StopCalibrationFailed);
222 _magCalStartAccepted =
false;
223 _rgCompassCalProgress[0] = 0;
224 _rgCompassCalProgress[1] = 0;
225 _rgCompassCalProgress[2] = 0;
226 _rgCompassCalComplete[0] =
false;
227 _rgCompassCalComplete[1] =
false;
228 _rgCompassCalComplete[2] =
false;
230 _startLogCalibration();
231 uint8_t compassBits = 0;
234 compassBits |= 1 << 0;
235 qCDebug(APMSensorsComponentControllerLog) <<
"Performing onboard compass cal for compass 1";
237 _rgCompassCalComplete[0] =
true;
238 _rgCompassCalSucceeded[0] =
true;
239 _rgCompassCalFitness[0] = 0;
243 compassBits |= 1 << 1;
244 qCDebug(APMSensorsComponentControllerLog) <<
"Performing onboard compass cal for compass 2";
246 _rgCompassCalComplete[1] =
true;
247 _rgCompassCalSucceeded[1] =
true;
248 _rgCompassCalFitness[1] = 0;
252 compassBits |= 1 << 2;
253 qCDebug(APMSensorsComponentControllerLog) <<
"Performing onboard compass cal for compass 3";
255 _rgCompassCalComplete[2] =
true;
256 _rgCompassCalSucceeded[2] =
true;
257 _rgCompassCalFitness[2] = 0;
262 _restoreCompassCalFitness =
true;
263 _previousCompassCalFitness = compassCalFitness->
rawValue().toFloat();
266 _appendStatusLog(tr(
"Rotate the vehicle randomly around all axes until the progress bar fills all the way to the right."));
272 _magCalCompassBits = compassBits;
273 _magCalCancelBeforeStartPending =
true;
274 _vehicle->sendMavCommand(
_vehicle->defaultComponentId(), MAV_CMD_DO_CANCEL_MAG_CAL,
false );
277void APMSensorsComponentController::_sendStartMagCal()
281 MAV_CMD_DO_START_MAG_CAL,
294 _startLogCalibration();
296 _vehicle->sendMavCommand(
_vehicle->defaultComponentId(), MAV_CMD_FIXED_MAG_CAL_YAW,
true , 0 , mask, lat, lon);
302 if (doSimpleAccelCal) {
303 _startLogCalibration();
305 _vehicle->startCalibration(_calTypeInProgress);
308 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(
false);
309 _startVisualCalibration();
310 _cancelButton->setEnabled(
false);
311 (void) _orientationCalAreaHelpText->setProperty(
"text", tr(
"Hold still in the current orientation and press Next when ready"));
314 _orientationCalDownSideDone =
false;
315 _orientationCalUpsideDownSideDone =
false;
316 _orientationCalLeftSideDone =
false;
317 _orientationCalRightSideDone =
false;
318 _orientationCalTailDownSideDone =
false;
319 _orientationCalNoseDownSideDone =
false;
320 _orientationCalDownSideInProgress =
false;
321 _orientationCalUpsideDownSideInProgress =
false;
322 _orientationCalLeftSideInProgress =
false;
323 _orientationCalRightSideInProgress =
false;
324 _orientationCalNoseDownSideInProgress =
false;
325 _orientationCalTailDownSideInProgress =
false;
328 _orientationCalDownSideVisible =
false;
329 _orientationCalUpsideDownSideVisible =
false;
330 _orientationCalLeftSideVisible =
false;
331 _orientationCalRightSideVisible =
false;
332 _orientationCalTailDownSideVisible =
false;
333 _orientationCalNoseDownSideVisible =
false;
336 _orientationCalDownSideVisible =
true;
337 _orientationCalUpsideDownSideVisible =
true;
338 _orientationCalLeftSideVisible =
true;
339 _orientationCalRightSideVisible =
true;
340 _orientationCalTailDownSideVisible =
true;
341 _orientationCalNoseDownSideVisible =
true;
346 _updateAndEmitShowOrientationCalArea(
true);
348 _vehicle->startCalibration(_calTypeInProgress);
354 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(
false);
355 _startLogCalibration();
356 _appendStatusLog(tr(
"Raise the throttle slowly to between 50% ~ 75% (the props will spin!) for 5 ~ 10 seconds."));
357 _appendStatusLog(tr(
"Quickly bring the throttle back down to zero"));
358 _appendStatusLog(tr(
"Press the Next button to complete the calibration"));
359 _vehicle->startCalibration(_calTypeInProgress);
365 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(
false);
366 _startLogCalibration();
367 _appendStatusLog(tr(
"Hold the vehicle in its level flight position."));
368 _vehicle->startCalibration(_calTypeInProgress);
374 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(
false);
375 _startLogCalibration();
376 _appendStatusLog(tr(
"Requesting pressure calibration..."));
377 _vehicle->startCalibration(_calTypeInProgress);
383 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(
false);
384 _startLogCalibration();
385 _appendStatusLog(tr(
"Requesting gyro calibration..."));
386 _vehicle->startCalibration(_calTypeInProgress);
389void APMSensorsComponentController::_handleTextMessage(quint32 sysid,
int componentid,
int severity,
390 const QString& text,
const QString& description)
392 Q_UNUSED(componentid); Q_UNUSED(severity); Q_UNUSED(description);
398 const QString originalMessageText = text;
399 const QString messageText = text.toLower();
401 const QStringList hidePrefixList = { QStringLiteral(
"prearm:"), QStringLiteral(
"ekf"), QStringLiteral(
"arm"), QStringLiteral(
"initialising") };
402 for (
const QString &hidePrefix : hidePrefixList) {
403 if (messageText.startsWith(hidePrefix)) {
408 _appendStatusLog(originalMessageText);
409 qCDebug(APMSensorsComponentControllerLog) << originalMessageText << severity;
412void APMSensorsComponentController::_refreshParams()
415 QStringLiteral(
"COMPASS_OFS_X"), QStringLiteral(
"COMPASS_OFS_Y"), QStringLiteral(
"COMPASS_OFS_Z"),
416 QStringLiteral(
"INS_ACCOFFS_X"), QStringLiteral(
"INS_ACCOFFS_Y"), QStringLiteral(
"INS_ACCOFFS_Z"),
417 QStringLiteral(
"COMPASS_*"),
418 QStringLiteral(
"INS_*"),
422void APMSensorsComponentController::_updateAndEmitShowOrientationCalArea(
bool show)
424 _showOrientationCalArea = show;
428void APMSensorsComponentController::_hideAllCalAreas()
430 _updateAndEmitShowOrientationCalArea(
false);
435 _cancelButton->setEnabled(
false);
439 _magCalCancelBeforeStartPending =
false;
440 _vehicle->sendMavCommand(
_vehicle->defaultComponentId(), MAV_CMD_DO_CANCEL_MAG_CAL,
true );
441 _stopCalibration(StopCalibrationCancelled);
443 _waitingForCancel =
true;
457 (void) mavlink_msg_command_ack_pack_chan(
460 sharedLink->mavlinkChannel(),
470 (void)
_vehicle->sendMessageOnLinkThreadSafe(sharedLink.get(), msg);
473 _stopCalibration(StopCalibrationSuccess);
480 return _sensorsComponent->compassSetupNeeded();
485 return _sensorsComponent->accelSetupNeeded();
498void APMSensorsComponentController::_handleCommandAck(
const mavlink_message_t &message)
502 mavlink_msg_command_ack_decode(&message, &commandAck);
504 if (commandAck.command == MAV_CMD_PREFLIGHT_CALIBRATION) {
505 switch (commandAck.result) {
506 case MAV_RESULT_IN_PROGRESS:
507 _appendStatusLog(tr(
"In progress"));
509 case MAV_RESULT_ACCEPTED:
510 _appendStatusLog(tr(
"Successfully completed"));
511 _stopCalibration(StopCalibrationSuccessShowLog);
514 _appendStatusLog(tr(
"Failed"));
515 _stopCalibration(StopCalibrationFailed);
522void APMSensorsComponentController::_handleMagCalProgress(
const mavlink_message_t &message)
528 mavlink_mag_cal_progress_t magCalProgress{};
529 mavlink_msg_mag_cal_progress_decode(&message, &magCalProgress);
531 qCDebug(APMSensorsComponentControllerVerboseLog) <<
"id:mask:pct"
532 << magCalProgress.compass_id
533 << magCalProgress.cal_mask
534 << magCalProgress.completion_pct;
537 int compassCalCount = 0;
538 for (
int i = 0; i < 3; i++) {
539 if (magCalProgress.cal_mask & (1 << i)) {
544 if ((magCalProgress.compass_id < 3) && (compassCalCount != 0)) {
546 _rgCompassCalProgress[magCalProgress.compass_id] = magCalProgress.completion_pct / compassCalCount;
550 (void) _progressBar->setProperty(
"value",
static_cast<float>(_rgCompassCalProgress[0] + _rgCompassCalProgress[1] + _rgCompassCalProgress[2]) / 100.0);
554void APMSensorsComponentController::_handleMagCalReport(
const mavlink_message_t &message)
560 mavlink_mag_cal_report_t magCalReport{};
561 mavlink_msg_mag_cal_report_decode(&message, &magCalReport);
563 qCDebug(APMSensorsComponentControllerVerboseLog) <<
"id:mask:status:fitness"
564 << magCalReport.compass_id << magCalReport.cal_mask << magCalReport.cal_status << magCalReport.fitness;
566 bool additionalCompassCompleted =
false;
567 if ((magCalReport.compass_id < 3) && !_rgCompassCalComplete[magCalReport.compass_id]) {
568 if (magCalReport.cal_status == MAG_CAL_SUCCESS) {
569 _appendStatusLog(tr(
"Compass %1 calibration complete").arg(magCalReport.compass_id));
571 _appendStatusLog(tr(
"Compass %1 calibration below quality threshold").arg(magCalReport.compass_id));
573 _rgCompassCalComplete[magCalReport.compass_id] =
true;
574 _rgCompassCalSucceeded[magCalReport.compass_id] = (magCalReport.cal_status == MAG_CAL_SUCCESS);
575 _rgCompassCalFitness[magCalReport.compass_id] = magCalReport.fitness;
576 additionalCompassCompleted =
true;
579 if (_rgCompassCalComplete[0] && _rgCompassCalComplete[1] &&_rgCompassCalComplete[2]) {
580 for (
int i = 0; i < 3; i++) {
581 qCDebug(APMSensorsComponentControllerLog) << QString(
"Onboard compass call report #%1: succeed:fitness %2:%3").arg(i).arg(_rgCompassCalSucceeded[i]).arg(_rgCompassCalFitness[i]);
589 if (_rgCompassCalSucceeded[0] && _rgCompassCalSucceeded[1] && _rgCompassCalSucceeded[2]) {
590 _appendStatusLog(tr(
"All compasses calibrated successfully"));
591 _appendStatusLog(tr(
"YOU MUST REBOOT YOUR VEHICLE NOW FOR NEW SETTINGS TO TAKE AFFECT"));
592 _stopCalibration(StopCalibrationSuccessShowLog);
594 _appendStatusLog(tr(
"Compass calibration failed"));
595 _appendStatusLog(tr(
"YOU MUST REBOOT YOUR VEHICLE NOW AND RETRY COMPASS CALIBRATION PRIOR TO FLIGHT"));
597 _stopCalibration(StopCalibrationFailed);
599 }
else if (additionalCompassCompleted) {
600 _appendStatusLog(tr(
"Continue rotating..."));
604bool APMSensorsComponentController::_handleCmdLongAccelcalVehiclePos(
const mavlink_command_long_t &commandLong)
606 bool updateImages =
false;
608 switch (
static_cast<ACCELCAL_VEHICLE_POS
>(
static_cast<int>(commandLong.param1))) {
609 case ACCELCAL_VEHICLE_POS_LEVEL:
610 if (!_orientationCalDownSideInProgress) {
612 _orientationCalDownSideInProgress =
true;
613 _nextButton->setEnabled(
true);
616 case ACCELCAL_VEHICLE_POS_LEFT:
617 if (!_orientationCalLeftSideInProgress) {
619 _orientationCalDownSideDone =
true;
620 _orientationCalDownSideInProgress =
false;
621 _orientationCalLeftSideInProgress =
true;
622 (void) _progressBar->setProperty(
"value",
static_cast<qreal
>(17 / 100.0));
625 case ACCELCAL_VEHICLE_POS_RIGHT:
626 if (!_orientationCalRightSideInProgress) {
628 _orientationCalLeftSideDone =
true;
629 _orientationCalLeftSideInProgress =
false;
630 _orientationCalRightSideInProgress =
true;
631 (void) _progressBar->setProperty(
"value",
static_cast<qreal
>(34 / 100.0));
634 case ACCELCAL_VEHICLE_POS_NOSEDOWN:
635 if (!_orientationCalNoseDownSideInProgress) {
637 _orientationCalRightSideDone =
true;
638 _orientationCalRightSideInProgress =
false;
639 _orientationCalNoseDownSideInProgress =
true;
640 (void) _progressBar->setProperty(
"value",
static_cast<qreal
>(51 / 100.0));
643 case ACCELCAL_VEHICLE_POS_NOSEUP:
644 if (!_orientationCalTailDownSideInProgress) {
646 _orientationCalNoseDownSideDone =
true;
647 _orientationCalNoseDownSideInProgress =
false;
648 _orientationCalTailDownSideInProgress =
true;
649 (void) _progressBar->setProperty(
"value",
static_cast<qreal
>(68 / 100.0));
652 case ACCELCAL_VEHICLE_POS_BACK:
653 if (!_orientationCalUpsideDownSideInProgress) {
655 _orientationCalTailDownSideDone =
true;
656 _orientationCalTailDownSideInProgress =
false;
657 _orientationCalUpsideDownSideInProgress =
true;
658 (void) _progressBar->setProperty(
"value",
static_cast<qreal
>(85 / 100.0));
661 case ACCELCAL_VEHICLE_POS_SUCCESS:
662 _stopCalibration(StopCalibrationSuccess);
664 case ACCELCAL_VEHICLE_POS_FAILED:
665 _stopCalibration(StopCalibrationFailed);
667 case ACCELCAL_VEHICLE_POS_ENUM_END:
675void APMSensorsComponentController::_handleCommandLong(
const mavlink_message_t &message)
677 bool updateImages =
false;
680 mavlink_msg_command_long_decode(&message, &commandLong);
682 if (commandLong.command == MAV_CMD_ACCELCAL_VEHICLE_POS) {
683 updateImages = _handleCmdLongAccelcalVehiclePos(commandLong);
697 if (message.sysid !=
_vehicle->id()) {
701 switch (message.msgid) {
702 case MAVLINK_MSG_ID_COMMAND_ACK:
703 _handleCommandAck(message);
705 case MAVLINK_MSG_ID_MAG_CAL_PROGRESS:
706 _handleMagCalProgress(message);
708 case MAVLINK_MSG_ID_MAG_CAL_REPORT:
709 _handleMagCalReport(message);
711 case MAVLINK_MSG_ID_COMMAND_LONG:
712 _handleCommandLong(message);
717void APMSensorsComponentController::_restorePreviousCompassCalFitness()
719 if (_restoreCompassCalFitness) {
720 _restoreCompassCalFitness =
false;
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
struct __mavlink_message mavlink_message_t
#define QGC_LOGGING_CATEGORY(name, categoryStr)
struct __mavlink_command_ack_t mavlink_command_ack_t
struct __mavlink_command_long_t mavlink_command_long_t
This is the AutoPilotPlugin implementation for the MAV_AUTOPILOT_ARDUPILOT type.
const QVariantList & vehicleComponents() override
Q_INVOKABLE void calibrateCompassNorth(float lat, float lon, int mask)
void waitingForCancelChanged()
void compass3CalFitnessChanged(double compass3CalFitness)
void compass1CalFitnessChanged(double compass1CalFitness)
Q_INVOKABLE void nextClicked()
void showOrientationCalAreaChanged()
void compass2CalFitnessChanged(double compass2CalFitness)
Q_INVOKABLE void calibrateAccel(bool doSimpleAccelCal)
void calibrationActiveChanged()
Q_INVOKABLE bool usingUDPLink() const
void compass2CalSucceededChanged(bool compass2CalSucceeded)
Q_INVOKABLE void cancelCalibration()
Q_INVOKABLE void calibrateMotorInterference()
~APMSensorsComponentController()
bool accelSetupNeeded() const
Q_INVOKABLE void calibrateCompass()
Q_INVOKABLE void levelHorizon()
void orientationCalSidesInProgressChanged()
void setAllCalButtonsEnabled(bool enabled)
void compass3CalSucceededChanged(bool compass3CalSucceeded)
void setupNeededChanged()
void compass1CalSucceededChanged(bool compass1CalSucceeded)
Q_INVOKABLE void calibrateGyro()
Q_INVOKABLE void calibratePressure()
void calibrationComplete(QGCMAVLink::CalibrationType calType)
void orientationCalSidesDoneChanged()
void resetStatusTextArea()
void orientationCalSidesRotateChanged()
APMSensorsComponentController(QObject *parent=nullptr)
bool compassSetupNeeded() const
void orientationCalSidesVisibleChanged()
Q_INVOKABLE Fact * getParameterFact(int componentId, const QString &name, bool reportMissing=true) const
FactPanelController(QObject *parent=nullptr)
Q_INVOKABLE bool parameterExists(int componentId, const QString &name) const
A Fact is used to hold a single value within the system.
void setRawValue(const QVariant &value)
QVariant rawValue() const
The link interface defines the interface for all links used to communicate with the ground station ap...
static int getComponentId()
void messageReceived(LinkInterface *link, const mavlink_message_t &message)
static MAVLinkProtocol * instance()
static constexpr int defaultComponentId
@ CalibrationAPMPressureAirspeed
@ CalibrationAPMAccelSimple
@ CalibrationAPMCompassMot
A vehicle component is an object which abstracts the physical portion of a vehicle into a set of conf...
void setupCompleteChanged()
void sendMavCommand(int compId, MAV_CMD command, bool showError, float param1=0.0f, float param2=0.0f, float param3=0.0f, float param4=0.0f, float param5=0.0f, float param6=0.0f, float param7=0.0f)
int defaultComponentId() const
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.