From 0ad459e58f5bacaa60edaf72e026c8462904cefa Mon Sep 17 00:00:00 2001
From: DonLakeFlyer <don@thegagnes.com>
Date: Thu, 20 Aug 2020 16:19:22 -0700
Subject: [PATCH 1/3] Remove logging

---
 src/QmlControls/HorizontalFactValueGrid.qml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/QmlControls/HorizontalFactValueGrid.qml b/src/QmlControls/HorizontalFactValueGrid.qml
index 28ed1f5..93460f3 100644
--- a/src/QmlControls/HorizontalFactValueGrid.qml
+++ b/src/QmlControls/HorizontalFactValueGrid.qml
@@ -109,7 +109,6 @@ T.HorizontalFactValueGrid {
                             for (var i=0; i<valueRepeater.count; i++) {
                                 newMaxWidth = Math.max(newMaxWidth, valueRepeater.itemAt(0).contentWidth)
                             }
-                            console.log("recalcWidth", newMaxWidth, maxWidth)
                             maxWidth = Math.min(maxWidth, newMaxWidth)
                         }
 

From eb5e395498e6ac503f3304dda6071aa8ba38d944 Mon Sep 17 00:00:00 2001
From: DonLakeFlyer <don@thegagnes.com>
Date: Thu, 20 Aug 2020 16:19:31 -0700
Subject: [PATCH 2/3] Fix logging

---
 src/AutoPilotPlugins/PX4/SensorsComponentController.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/AutoPilotPlugins/PX4/SensorsComponentController.cc b/src/AutoPilotPlugins/PX4/SensorsComponentController.cc
index 590196f..d8124fb 100644
--- a/src/AutoPilotPlugins/PX4/SensorsComponentController.cc
+++ b/src/AutoPilotPlugins/PX4/SensorsComponentController.cc
@@ -347,7 +347,7 @@ void SensorsComponentController::_handleUASTextMessage(int uasId, int compId, in
     
     if (text.endsWith("orientation detected")) {
         QString side = text.section(" ", 0, 0);
-        qDebug() << "Side started" << side;
+        qCDebug(SensorsComponentControllerLog) << "Side started" << side;
         
         if (side == "down") {
             _orientationCalDownSideInProgress = true;
@@ -394,7 +394,7 @@ void SensorsComponentController::_handleUASTextMessage(int uasId, int compId, in
     
     if (text.endsWith("side done, rotate to a different side")) {
         QString side = text.section(" ", 0, 0);
-        qDebug() << "Side finished" << side;
+        qCDebug(SensorsComponentControllerLog) << "Side finished" << side;
         
         if (side == "down") {
             _orientationCalDownSideInProgress = false;

From b0e79c0b7d94682cdc4281ada89216cf84ef921d Mon Sep 17 00:00:00 2001
From: DonLakeFlyer <don@thegagnes.com>
Date: Thu, 20 Aug 2020 16:20:35 -0700
Subject: [PATCH 3/3] Support unlimited compasses, plus all sort of other ui
 fixup

---
 src/AutoPilotPlugins/PX4/SensorsSetup.qml | 249 ++++++++++++++++--------------
 1 file changed, 132 insertions(+), 117 deletions(-)

diff --git a/src/AutoPilotPlugins/PX4/SensorsSetup.qml b/src/AutoPilotPlugins/PX4/SensorsSetup.qml
index 817f53f..f834d16 100644
--- a/src/AutoPilotPlugins/PX4/SensorsSetup.qml
+++ b/src/AutoPilotPlugins/PX4/SensorsSetup.qml
@@ -41,10 +41,10 @@ Item {
     readonly property string boardRotationText: qsTr("If the orientation is in the direction of flight, select ROTATION_NONE.")
     readonly property string compassRotationText: qsTr("If the orientation is in the direction of flight, select ROTATION_NONE.")
 
-    readonly property string compassHelp:   qsTr("For Compass calibration you will need to rotate your vehicle through a number of positions.\n\nClick Ok to start calibration.")
-    readonly property string gyroHelp:      qsTr("For Gyroscope calibration you will need to place your vehicle on a surface and leave it still.\n\nClick Ok to start calibration.")
-    readonly property string accelHelp:     qsTr("For Accelerometer calibration you will need to place your vehicle on all six sides on a perfectly level surface and hold it still in each orientation for a few seconds.\n\nClick Ok to start calibration.")
-    readonly property string levelHelp:     qsTr("To level the horizon you need to place the vehicle in its level flight position and press OK.")
+    readonly property string compassHelp:   qsTr("For Compass calibration you will need to rotate your vehicle through a number of positions.")
+    readonly property string gyroHelp:      qsTr("For Gyroscope calibration you will need to place your vehicle on a surface and leave it still.")
+    readonly property string accelHelp:     qsTr("For Accelerometer calibration you will need to place your vehicle on all six sides on a perfectly level surface and hold it still in each orientation for a few seconds.")
+    readonly property string levelHelp:     qsTr("To level the horizon you need to place the vehicle in its level flight position and leave still.")
     readonly property string airspeedHelp:  qsTr("For Airspeed calibration you will need to keep your airspeed sensor out of any wind and then blow across the sensor. Do not touch the sensor or obstruct any holes during the calibration.")
 
     readonly property string statusTextAreaDefaultText: qsTr("Start the individual calibration steps by clicking one of the buttons to the left.")
@@ -55,7 +55,6 @@ Item {
     // Used to pass help text to the preCalibrationDialog dialog
     property string preCalibrationDialogHelp
 
-    readonly property int rotationColumnWidth: ScreenTools.defaultFontPixelWidth * 30
     readonly property var rotations: [
         "ROTATION_NONE",
         "ROTATION_YAW_45",
@@ -110,9 +109,75 @@ Item {
     property bool showCompass1Rot: cal_mag1_id.value > 0 && cal_mag1_rot.value >= 0
     property bool showCompass2Rot: cal_mag2_id.value > 0 && cal_mag2_rot.value >= 0
 
-    property bool   _sensorsHaveFixedOrientation:   QGroundControl.corePlugin.options.sensorsHaveFixedOrientation
-    property bool   _wifiReliableForCalibration:    QGroundControl.corePlugin.options.wifiReliableForCalibration
-    property int    _buttonWidth:                   ScreenTools.defaultFontPixelWidth * 15
+    property bool   _sensorsHaveFixedOrientation:       QGroundControl.corePlugin.options.sensorsHaveFixedOrientation
+    property bool   _wifiReliableForCalibration:        QGroundControl.corePlugin.options.wifiReliableForCalibration
+    property int    _buttonWidth:                       ScreenTools.defaultFontPixelWidth * 15
+    property string _calMagIdParamFormat:               "CAL_MAG#_ID"
+    property string _calMagRotParamFormat:              "CAL_MAG#_ROT"
+    property bool 	_allMagsDisabled:                   controller.parameterExists(-1, "SYS_HAS_MAG") ? controller.getParameterFact(-1, "SYS_HAS_MAG").value === 0 : false
+    property bool   _boardOrientationChangeAllowed:     !_sensorsHaveFixedOrientation && setOrientationsDialogShowBoardOrientation
+    property bool   _compassOrientationChangeAllowed:   !_sensorsHaveFixedOrientation
+    property int    _arbitrarilyLargeMaxMagIndex:       50
+
+    function currentMagParamCount() {
+        if (_allMagsDisabled) {
+            return 0
+        } else {
+            for (var index=0; index<_arbitrarilyLargeMaxMagIndex; index++) {
+                var magIdParam = _calMagIdParamFormat.replace("#", index)
+                if (!controller.parameterExists(-1, magIdParam)) {
+                    return index
+                }
+            }
+            console.warn("SensorSetup.qml:currentMagParamCount internal error")
+            return -1
+        }
+    }
+
+    function currentExternalMagCount() {
+        if (_allMagsDisabled) {
+            return 0
+        } else {
+            var externalMagCount = 0
+            for (var index=0; index<_arbitrarilyLargeMaxMagIndex; index++) {
+                var magIdParam = _calMagIdParamFormat.replace("#", index)
+                if (controller.parameterExists(-1, magIdParam)) {
+                    var calMagIdFact = controller.getParameterFact(-1, magIdParam)
+                    var calMagRotFact = controller.getParameterFact(-1, _calMagRotParamFormat.replace("#", index))
+                    if (calMagIdFact.value > 0 && calMagRotFact.value >= 0) {
+                        externalMagCount++
+                    }
+                } else {
+                    return externalMagCount
+                }
+            }
+            console.warn("SensorSetup.qml:currentExternalMagCount internal error")
+            return 0
+        }
+    }
+
+    function orientationsButtonVisible() {
+        if (_sensorsHaveFixedOrientation || !showSetOrientations) {
+            return false
+        } else if (_boardOrientationChangeAllowed) {
+            return true
+        } else if (_compassOrientationChangeAllowed && !_allMagsDisabled) {
+            for (var index=0; index<_arbitrarilyLargeMaxMagIndex; index++) {
+                var magIdParam = _calMagIdParamFormat.replace("#", index)
+                if (controller.parameterExists(-1, magIdParam)) {
+                    var calMagIdFact = controller.parameterExists(-1, magIdParam)
+                    var calMagRotFact = controller.parameterExists(-1, _calMagRotParamFormat.replace("#", index))
+                    if (calMagIdFact.value > 0 && calMagRotFact.value >= 0) {
+                        // Only external compasses can set orientation
+                        return true
+                    }
+                }
+            }
+            return false
+        } else {
+            return false
+        }
+    }
 
     SensorsComponentController {
         id:                         controller
@@ -130,7 +195,9 @@ Item {
         onResetStatusTextArea: statusLog.text = statusTextAreaDefaultText
 
         onMagCalComplete: {
-            setOrientationsDialogShowBoardOrientation = false
+            setOrientationsButton.visible               = orientationsButtonVisible()
+            setOrientationsDialogShowBoardOrientation   = false
+            setOrientationsDialogShowReboot             = true
             mainWindow.showComponentDialog(setOrientationsDialogComponent, qsTr("Compass Calibration Complete"), mainWindow.showDialogDefaultWidth, StandardButton.Ok)
         }
 
@@ -191,43 +258,49 @@ Item {
                 anchors.fill:   parent
                 spacing:        ScreenTools.defaultFontPixelHeight
 
+                QGCLabel {
+                    width:      parent.width
+                    wrapMode:   Text.WordWrap
+                    text:       preCalibrationDialogHelp
+                }
+
+                QGCLabel {
+                    id:         boardRotationHelp
+                    width:      parent.width
+                    wrapMode:   Text.WordWrap
+                    visible:    !_sensorsHaveFixedOrientation && (preCalibrationDialogType == "accel" || preCalibrationDialogType == "compass")
+                    text:       qsTr("Set autopilot orientation before calibrating.")
+                }
+
                 Column {
-                    width:          parent.width
-                    spacing:        5
-                    visible:        !_sensorsHaveFixedOrientation
+                    width:      parent.width
+                    visible:    boardRotationHelp.visible
+                    QGCLabel { text: qsTr("Autopilot Orientation") }
+
+                    FactComboBox {
+                        sizeToContents: true
+                        model:          rotations
+                        fact:           sens_board_rot
+                    }
 
                     QGCLabel {
-                        id:         boardRotationHelp
                         width:      parent.width
                         wrapMode:   Text.WordWrap
-                        visible:    (preCalibrationDialogType != "airspeed") && (preCalibrationDialogType != "gyro")
-                        text:       qsTr("Set autopilot orientation before calibrating.")
-                    }
-
-                    Column {
-                        visible:    boardRotationHelp.visible
-
-                        QGCLabel { text: qsTr("Autopilot Orientation:") }
-
-                        FactComboBox {
-                            id:     boardRotationCombo
-                            width:  rotationColumnWidth;
-                            model:  rotations
-                            fact:   sens_board_rot
-                        }
+                        text:       qsTr("ROTATION_NONE indicates component points in direction of flight.")
                     }
                 }
 
                 QGCLabel {
                     width:      parent.width
                     wrapMode:   Text.WordWrap
-                    text:       preCalibrationDialogHelp
+                    text:       qsTr("Click Ok to start calibration.")
                 }
             }
         }
     }
 
-    property bool setOrientationsDialogShowBoardOrientation: true
+    property bool setOrientationsDialogShowBoardOrientation:    true
+    property bool setOrientationsDialogShowReboot:              true
 
     Component {
         id: setOrientationsDialogComponent
@@ -251,13 +324,13 @@ Item {
                     QGCLabel {
                         width:      parent.width
                         wrapMode:   Text.WordWrap
-                        text:       _sensorsHaveFixedOrientation ?
-                                        qsTr("Make sure to reboot the vehicle prior to flight.") :
-                                        qsTr("Set your compass orientations below and the make sure to reboot the vehicle prior to flight.")
+                        text:       qsTr("Reboot the vehicle prior to flight.")
+                        visible:    setOrientationsDialogShowReboot
                     }
 
                     QGCButton {
-                        text: qsTr("Reboot Vehicle")
+                        text:       qsTr("Reboot Vehicle")
+                        visible:    setOrientationsDialogShowReboot
                         onClicked: {
                             controller.vehicle.rebootVehicle()
                             hideDialog()
@@ -267,104 +340,46 @@ Item {
                     QGCLabel {
                         width:      parent.width
                         wrapMode:   Text.WordWrap
-                        text:       boardRotationText
-                        visible:    !_sensorsHaveFixedOrientation
+                        text:       qsTr("Adjust orientations as needed.\n\nROTATION_NONE indicates component points in direction of flight.")
+                        visible:    _boardOrientationChangeAllowed || (_compassOrientationChangeAllowed && currentExternalMagCount() !== 0)
+
+                        Component.onCompleted: console.log(_boardOrientationChangeAllowed, _compassOrientationChangeAllowed, currentExternalMagCount())
                     }
 
                     Column {
-                        visible: setOrientationsDialogShowBoardOrientation
+                        visible: _boardOrientationChangeAllowed
 
                         QGCLabel {
-                            text: qsTr("Autopilot Orientation:")
+                            text: qsTr("Autopilot Orientation")
                         }
 
                         FactComboBox {
-                            id:     boardRotationCombo
-                            width:  rotationColumnWidth;
-                            model:  rotations
-                            fact:   sens_board_rot
-                        }
-                    }
-
-                    // Compass 0 rotation
-                    Column {
-                        visible: !_sensorsHaveFixedOrientation
-
-                        Component {
-                            id: compass0ComponentLabel2
-
-                            QGCLabel {
-                                text: qsTr("External Compass Orientation:")
-                            }
-                        }
-
-                        Component {
-                            id: compass0ComponentCombo2
-
-                            FactComboBox {
-                                id:     compass0RotationCombo
-                                width:  rotationColumnWidth
-                                model:  rotations
-                                fact:   cal_mag0_rot
-                            }
+                            sizeToContents: true
+                            model:          rotations
+                            fact:           sens_board_rot
                         }
-
-                        Loader { sourceComponent: showCompass0Rot ? compass0ComponentLabel2 : null }
-                        Loader { sourceComponent: showCompass0Rot ? compass0ComponentCombo2 : null }
                     }
 
-                    // Compass 1 rotation
-                    Column {
-                        visible: !_sensorsHaveFixedOrientation
+                    Repeater {
+                        model: _compassOrientationChangeAllowed ? currentMagParamCount() : 0
 
-                        Component {
-                            id: compass1ComponentLabel2
+                        Column {
+                            // id > = signals compass available, rot < 0 signals internal compass
+                            visible: calMagIdFact.value > 0 && calMagRotFact.value >= 0
 
-                            QGCLabel {
-                                text: qsTr("External Compass 1 Orientation:")
-                            }
-                        }
-
-                        Component {
-                            id: compass1ComponentCombo2
-
-                            FactComboBox {
-                                id:     compass1RotationCombo
-                                width:  rotationColumnWidth
-                                model:  rotations
-                                fact:   cal_mag1_rot
-                            }
-                        }
-
-                        Loader { sourceComponent: showCompass1Rot ? compass1ComponentLabel2 : null }
-                        Loader { sourceComponent: showCompass1Rot ? compass1ComponentCombo2 : null }
-                    }
-
-                    // Compass 2 rotation
-                    Column {
-                        visible: !_sensorsHaveFixedOrientation
-                        spacing: ScreenTools.defaultFontPixelWidth
-
-                        Component {
-                            id: compass2ComponentLabel2
+                            property Fact calMagIdFact:     controller.getParameterFact(-1, _calMagIdParamFormat.replace("#", index))
+                            property Fact calMagRotFact:    controller.getParameterFact(-1, _calMagRotParamFormat.replace("#", index))
 
                             QGCLabel {
-                                text: qsTr("Compass 2 Orientation")
+                                text: qsTr("Mag %1 Orientation").arg(index)
                             }
-                        }
-
-                        Component {
-                            id: compass2ComponentCombo2
 
                             FactComboBox {
-                                id:     compass1RotationCombo
-                                width:  rotationColumnWidth
-                                model:  rotations
-                                fact:   cal_mag2_rot
+                                sizeToContents: true
+                                model:          rotations
+                                fact:           parent.calMagRotFact
                             }
                         }
-                        Loader { sourceComponent: showCompass2Rot ? compass2ComponentLabel2 : null }
-                        Loader { sourceComponent: showCompass2Rot ? compass2ComponentCombo2 : null }
                     }
                 } // Column
             } // QGCFlickable
@@ -383,12 +398,11 @@ Item {
             spacing:    ScreenTools.defaultFontPixelHeight / 2
 
             IndicatorButton {
-                property bool 	_hasMag: controller.parameterExists(-1, "SYS_HAS_MAG") ? controller.getParameterFact(-1, "SYS_HAS_MAG").value !== 0 : true
                 id:             compassButton
                 width:          _buttonWidth
                 text:           qsTr("Compass")
                 indicatorGreen: cal_mag0_id.value !== 0
-                visible:        _hasMag && QGroundControl.corePlugin.options.showSensorCalibrationCompass && showSensorCalibrationCompass
+                visible:        !_allMagsDisabled && QGroundControl.corePlugin.options.showSensorCalibrationCompass && showSensorCalibrationCompass
 
                 onClicked: {
                     preCalibrationDialogType = "compass"
@@ -478,11 +492,12 @@ Item {
             QGCButton {
                 id:         setOrientationsButton
                 width:      _buttonWidth
-                text:       qsTr("Set Orientations")
-                visible:    !_sensorsHaveFixedOrientation && showSetOrientations
+                text:       qsTr("Orientations")
+                visible:    orientationsButtonVisible()
 
                 onClicked:  {
-                    setOrientationsDialogShowBoardOrientation = true
+                    setOrientationsDialogShowBoardOrientation   = true
+                    setOrientationsDialogShowReboot             = false
                     mainWindow.showComponentDialog(setOrientationsDialogComponent, qsTr("Set Orientations"), mainWindow.showDialogDefaultWidth, StandardButton.Ok)
                 }
             }