From 96e5674dd8b20317e7a22963fea2a35317f86ca2 Mon Sep 17 00:00:00 2001
From: Don Gagne <don@thegagnes.com>
Date: Tue, 10 Feb 2015 13:12:29 -0800
Subject: [PATCH] Support two param sets for calibration

The parameters for calibration were changed in the latest master. QGC
needs to work with both. Hacked in for now, waiting on a true parameter
re-mapping implementation.
---
 src/AutoPilotPlugins/PX4/SensorsComponent.cc  | 15 +++++++++---
 src/AutoPilotPlugins/PX4/SensorsComponent.h   |  1 +
 .../PX4/SensorsComponentSummary.qml           |  9 +++++---
 .../QGCPX4SensorCalibration.cc                | 23 ++++++++++++-------
 4 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/src/AutoPilotPlugins/PX4/SensorsComponent.cc b/src/AutoPilotPlugins/PX4/SensorsComponent.cc
index 29b6282dc9..b0d5bbb974 100644
--- a/src/AutoPilotPlugins/PX4/SensorsComponent.cc
+++ b/src/AutoPilotPlugins/PX4/SensorsComponent.cc
@@ -31,12 +31,17 @@
 // These two list must be kept in sync
 
 /// @brief Parameters which signal a change in setupComplete state
-static const char* triggerParams[] = {  "SENS_MAG_XOFF", "SENS_GYRO_XOFF", "SENS_ACC_XOFF", "SENS_DPRES_OFF", NULL };
+static const char* triggerParamsV1[] = {  "SENS_MAG_XOFF", "SENS_GYRO_XOFF", "SENS_ACC_XOFF", "SENS_DPRES_OFF", NULL };
+static const char* triggerParamsV2[] = {  "CAL_MAG0_ID", "CAL_GYRO0_ID", "CAL_ACC0_ID", "SENS_DPRES_OFF", NULL };
 
 SensorsComponent::SensorsComponent(UASInterface* uas, AutoPilotPlugin* autopilot, QObject* parent) :
     PX4Component(uas, autopilot, parent),
     _name(tr("Sensors"))
 {
+    // Determine what set of parameters are available. This is a temporary hack for now. Will need real parameter
+    // mapping in the future.
+    QVariant value;
+    _paramsV1 = _paramMgr->getParameterValue(_paramMgr->getDefaultComponentId(), "SENS_MAG_XOFF", value);
 }
 
 QString SensorsComponent::name(void) const
@@ -97,14 +102,18 @@ QString SensorsComponent::setupStateDescription(void) const
 
 const char** SensorsComponent::setupCompleteChangedTriggerList(void) const
 {
-    return triggerParams;
+    return _paramsV1 ? triggerParamsV1 : triggerParamsV2;
 }
 
 QStringList SensorsComponent::paramFilterList(void) const
 {
     QStringList list;
     
-    list << "SENS_*";
+    if (_paramsV1) {
+        list << "SENS_*";
+    } else {
+        list << "CAL_*";
+    }
     
     return list;
 }
diff --git a/src/AutoPilotPlugins/PX4/SensorsComponent.h b/src/AutoPilotPlugins/PX4/SensorsComponent.h
index a58874986a..992a25f5ce 100644
--- a/src/AutoPilotPlugins/PX4/SensorsComponent.h
+++ b/src/AutoPilotPlugins/PX4/SensorsComponent.h
@@ -55,6 +55,7 @@ public:
 private:
     const QString   _name;
     QVariantList    _summaryItems;
+    bool            _paramsV1;
 };
 
 #endif
diff --git a/src/AutoPilotPlugins/PX4/SensorsComponentSummary.qml b/src/AutoPilotPlugins/PX4/SensorsComponentSummary.qml
index be7b52d238..25f237cbd8 100644
--- a/src/AutoPilotPlugins/PX4/SensorsComponentSummary.qml
+++ b/src/AutoPilotPlugins/PX4/SensorsComponentSummary.qml
@@ -15,7 +15,8 @@ Column {
         Text {
             horizontalAlignment: Text.AlignRight;
             width: parent.width - compass.contentWidth;
-            text: autopilot.parameters["SENS_MAG_XOFF"].value == 0.0 ? "Setup required" : "Ready"
+            property bool setupRequiredValue: autopilot.parameters["SENS_MAG_XOFF"] ? autopilot.parameters["SENS_MAG_XOFF"].value : autopilot.parameters["CAL_MAG0_ID"].value
+            text: setupRequiredValue  == 0 ? "Setup required" : "Ready"
         }
     }
 
@@ -26,7 +27,8 @@ Column {
         Text {
             horizontalAlignment: Text.AlignRight;
             width: parent.width - gyro.contentWidth;
-            text: autopilot.parameters["SENS_GYRO_XOFF"].value == 0.0 ? "Setup required" : "Ready"
+            property bool setupRequiredValue: autopilot.parameters["SENS_GYRO_XOFF"] ? autopilot.parameters["SENS_GYRO_XOFF"].value : autopilot.parameters["CAL_GYRO0_ID"].value
+            text: setupRequiredValue  == 0 ? "Setup required" : "Ready"
         }
     }
 
@@ -37,7 +39,8 @@ Column {
         Text {
             horizontalAlignment: Text.AlignRight;
             width: parent.width - accel.contentWidth;
-            text: autopilot.parameters["SENS_ACC_XOFF"].value == 0.0 ? "Setup required" : "Ready"
+            property bool setupRequiredValue: autopilot.parameters["SENS_ACC_XOFF"] ? autopilot.parameters["SENS_ACC_XOFF"].value : autopilot.parameters["CAL_ACC0_ID"].value
+            text: setupRequiredValue  == 0 ? "Setup required" : "Ready"
         }
     }
 
diff --git a/src/ui/px4_configuration/QGCPX4SensorCalibration.cc b/src/ui/px4_configuration/QGCPX4SensorCalibration.cc
index 2107f1dc22..515ec2b16d 100644
--- a/src/ui/px4_configuration/QGCPX4SensorCalibration.cc
+++ b/src/ui/px4_configuration/QGCPX4SensorCalibration.cc
@@ -142,7 +142,7 @@ void QGCPX4SensorCalibration::parameterChanged(int uas, int component, QString p
     }
 
     // Check mag calibration naively
-    if (parameterName.contains("SENS_MAG_XOFF")) {
+    if (parameterName.contains("SENS_MAG_XOFF") || parameterName.contains("CAL_MAG0_ID")) {
         float offset = value.toFloat();
         if (offset < 0.000001f && offset > -0.000001f) {
             // Must be zero, not good
@@ -153,7 +153,7 @@ void QGCPX4SensorCalibration::parameterChanged(int uas, int component, QString p
     }
 
     // Check gyro calibration naively
-    if (parameterName.contains("SENS_GYRO_XOFF")) {
+    if (parameterName.contains("SENS_GYRO_XOFF") || parameterName.contains("CAL_GYRO0_ID")) {
         float offset = value.toFloat();
         if (offset < 0.000001f && offset > -0.000001f) {
             // Must be zero, not good
@@ -164,7 +164,7 @@ void QGCPX4SensorCalibration::parameterChanged(int uas, int component, QString p
     }
 
     // Check accel calibration naively
-    if (parameterName.contains("SENS_ACC_XOFF")) {
+    if (parameterName.contains("SENS_ACC_XOFF") || parameterName.contains("CAL_ACC0_ID")) {
         float offset = value.toFloat();
         if (offset < 0.000001f && offset > -0.000001f) {
             // Must be zero, not good
@@ -354,17 +354,24 @@ void QGCPX4SensorCalibration::setActiveUAS(UASInterface* uas)
     // If the parameters are ready, we aren't going to get paramterChanged signals. So fake them in order to make the UI work.
     if (uas->getParamManager()->parametersReady()) {
         QVariant value;
-        static const char* rgParams[] = { "SENS_BOARD_ROT", "SENS_EXT_MAG_ROT", "SENS_MAG_XOFF", "SENS_GYRO_XOFF", "SENS_ACC_XOFF", "SENS_DPRES_OFF" };
+        static const char* rgParamsV1[] = { "SENS_BOARD_ROT", "SENS_EXT_MAG_ROT", "SENS_MAG_XOFF", "SENS_GYRO_XOFF", "SENS_ACC_XOFF", "SENS_DPRES_OFF" };
+        static const char* rgParamsV2[] = { "SENS_BOARD_ROT", "SENS_EXT_MAG_ROT", "CAL_MAG0_XOFF", "CAL_GYRO0_XOFF", "CAL_ACC0_XOFF", "SENS_DPRES_OFF" };
         
         QGCUASParamManagerInterface* paramMgr = uas->getParamManager();
         
-        for (size_t i=0; i<sizeof(rgParams)/sizeof(rgParams[0]); i++) {
+        // Temp hack for parameter mapping
+        bool paramsV1 = paramMgr->getComponentForParam("SENS_MAG_XOFF").count();
+        static const char** prgParamList = paramsV1 ? rgParamsV1 : rgParamsV2;
+        
+        for (size_t i=0; i<sizeof(rgParamsV1)/sizeof(rgParamsV1[0]); i++) {
             QVariant value;
             
-            QList<int> compIds = paramMgr->getComponentForParam(rgParams[i]);
+            QList<int> compIds = paramMgr->getComponentForParam(prgParamList[i]);
             Q_ASSERT(compIds.count() == 1);
-            paramMgr->getParameterValue(compIds[0], rgParams[i], value);
-            parameterChanged(uas->getUASID(), compIds[0], rgParams[i], value);
+            bool found = paramMgr->getParameterValue(compIds[0], prgParamList[i], value);
+            Q_ASSERT(found);
+            Q_UNUSED(found);
+            parameterChanged(uas->getUASID(), compIds[0], prgParamList[i], value);
         }
     }
 }
-- 
GitLab