Commit bb3679d7 authored by DonLakeFlyer's avatar DonLakeFlyer

Support load/save/default of NaN for param

parent 8192004c
...@@ -102,6 +102,10 @@ bool JsonHelper::validateKeyTypes(const QJsonObject& jsonObject, const QStringLi ...@@ -102,6 +102,10 @@ bool JsonHelper::validateKeyTypes(const QJsonObject& jsonObject, const QStringLi
QString valueKey = keys[i]; QString valueKey = keys[i];
if (jsonObject.contains(valueKey)) { if (jsonObject.contains(valueKey)) {
const QJsonValue& jsonValue = jsonObject[valueKey]; const QJsonValue& jsonValue = jsonObject[valueKey];
if (types[i] == QJsonValue::Null && jsonValue.type() == QJsonValue::Double) {
// Null type signals a NaN on a double value
continue;
}
if (jsonValue.type() != types[i]) { if (jsonValue.type() != types[i]) {
errorString = QObject::tr("Incorrect value type - key:type:expected %1:%2:%3").arg(valueKey).arg(_jsonValueTypeToString(jsonValue.type())).arg(_jsonValueTypeToString(types[i])); errorString = QObject::tr("Incorrect value type - key:type:expected %1:%2:%3").arg(valueKey).arg(_jsonValueTypeToString(jsonValue.type())).arg(_jsonValueTypeToString(types[i]));
return false; return false;
...@@ -342,3 +346,12 @@ void JsonHelper::savePolygon(QmlObjectListModel& list, QJsonArray& polygonArray) ...@@ -342,3 +346,12 @@ void JsonHelper::savePolygon(QmlObjectListModel& list, QJsonArray& polygonArray)
polygonArray.append(jsonValue); polygonArray.append(jsonValue);
} }
} }
double JsonHelper::possibleNaNJsonValue(const QJsonValue& value)
{
if (value.type() == QJsonValue::Null) {
return std::numeric_limits<double>::quiet_NaN();
} else {
return value.toDouble();
}
}
...@@ -34,20 +34,30 @@ public: ...@@ -34,20 +34,30 @@ public:
/// jsonFileTypeKey - Required and checked to be equal to expectedFileType /// jsonFileTypeKey - Required and checked to be equal to expectedFileType
/// jsonVersionKey - Required and checked to be below supportedMajorVersion, supportedMinorVersion /// jsonVersionKey - Required and checked to be below supportedMajorVersion, supportedMinorVersion
/// jsonGroundStationKey - Required and checked to be string type /// jsonGroundStationKey - Required and checked to be string type
/// @return false: validation failed /// @return false: validation failed, errorString set
static bool validateQGCJsonFile(const QJsonObject& jsonObject, ///< root json object static bool validateQGCJsonFile(const QJsonObject& jsonObject, ///< json object to validate
const QString& expectedFileType, ///< correct file type for file const QString& expectedFileType, ///< correct file type for file
int minSupportedVersion, ///< minimum supported version int minSupportedVersion, ///< minimum supported version
int maxSupportedVersion, ///< maximum supported major version int maxSupportedVersion, ///< maximum supported major version
int &version, ///< returned file version int &version, ///< returned file version
QString& errorString); ///< returned error string if validation fails QString& errorString); ///< returned error string if validation fails
static bool validateRequiredKeys(const QJsonObject& jsonObject, const QStringList& keys, QString& errorString); /// Validates that the specified keys are in the object
static bool validateKeyTypes(const QJsonObject& jsonObject, const QStringList& keys, const QList<QJsonValue::Type>& types, QString& errorString); /// @return false: validation failed, errorString set
static bool validateRequiredKeys(const QJsonObject& jsonObject, ///< json object to validate
const QStringList& keys, ///< keys which are required to be present
QString& errorString); ///< returned error string if validation fails
/// Validates the types of specified keys are in the object
/// @return false: validation failed, errorString set
static bool validateKeyTypes(const QJsonObject& jsonObject, ///< json object to validate
const QStringList& keys, ///< keys to validate
const QList<QJsonValue::Type>& types, ///< required type for each key, QJsonValue::Null specifies double with possible NaN
QString& errorString); ///< returned error string if validation fails
typedef struct { typedef struct {
const char* key; ///< json key name const char* key; ///< json key name
QJsonValue::Type type; ///< type of key QJsonValue::Type type; ///< required type for key, QJsonValue::Null specifies double with possible NaN
bool required; ///< true: key must be present bool required; ///< true: key must be present
} KeyValidateInfo; } KeyValidateInfo;
...@@ -96,6 +106,8 @@ public: ...@@ -96,6 +106,8 @@ public:
static bool parseEnum(const QJsonObject& jsonObject, QStringList& enumStrings, QStringList& enumValues, QString& errorString); static bool parseEnum(const QJsonObject& jsonObject, QStringList& enumStrings, QStringList& enumValues, QString& errorString);
/// Returns NaN if the value is null, or it not the double value
static double possibleNaNJsonValue(const QJsonValue& value);
static const char* jsonVersionKey; static const char* jsonVersionKey;
static const char* jsonGroundStationKey; static const char* jsonGroundStationKey;
......
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
"label": "Heading", "label": "Heading",
"units": "radians", "units": "radians",
"nanUnchanged": true, "nanUnchanged": true,
"default": null,
"decimalPlaces": 2 "decimalPlaces": 2
} }
}, },
...@@ -75,6 +76,7 @@ ...@@ -75,6 +76,7 @@
"label": "Heading", "label": "Heading",
"units": "radians", "units": "radians",
"nanUnchanged": true, "nanUnchanged": true,
"default": null,
"decimalPlaces": 2 "decimalPlaces": 2
} }
}, },
...@@ -101,6 +103,7 @@ ...@@ -101,6 +103,7 @@
"label": "Heading", "label": "Heading",
"units": "radians", "units": "radians",
"nanUnchanged": true, "nanUnchanged": true,
"default": null,
"decimalPlaces": 2 "decimalPlaces": 2
} }
}, },
...@@ -157,6 +160,7 @@ ...@@ -157,6 +160,7 @@
"label": "Heading", "label": "Heading",
"units": "radians", "units": "radians",
"nanUnchanged": true, "nanUnchanged": true,
"default": null,
"decimalPlaces": 2 "decimalPlaces": 2
} }
}, },
...@@ -178,6 +182,7 @@ ...@@ -178,6 +182,7 @@
"label": "Heading", "label": "Heading",
"units": "radians", "units": "radians",
"nanUnchanged": true, "nanUnchanged": true,
"default": null,
"decimalPlaces": 2 "decimalPlaces": 2
} }
}, },
...@@ -316,6 +321,7 @@ ...@@ -316,6 +321,7 @@
"label": "Heading", "label": "Heading",
"units": "deg", "units": "deg",
"nanUnchanged": true, "nanUnchanged": true,
"default": null,
"decimalPlaces": 2 "decimalPlaces": 2
} }
}, },
...@@ -331,6 +337,7 @@ ...@@ -331,6 +337,7 @@
"label": "Heading", "label": "Heading",
"units": "deg", "units": "deg",
"nanUnchanged": true, "nanUnchanged": true,
"default": null,
"decimalPlaces": 2 "decimalPlaces": 2
} }
}, },
......
...@@ -334,7 +334,7 @@ bool MissionCommandUIInfo::loadJsonInfo(const QJsonObject& jsonObject, bool requ ...@@ -334,7 +334,7 @@ bool MissionCommandUIInfo::loadJsonInfo(const QJsonObject& jsonObject, bool requ
// Validate key types // Validate key types
QList<QJsonValue::Type> types; QList<QJsonValue::Type> types;
types << QJsonValue::Double << QJsonValue::Double << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::Bool; types << QJsonValue::Null << QJsonValue::Double << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::Bool;
if (!JsonHelper::validateKeyTypes(jsonObject, allParamKeys, types, internalError)) { if (!JsonHelper::validateKeyTypes(jsonObject, allParamKeys, types, internalError)) {
errorString = _loadErrorString(internalError); errorString = _loadErrorString(internalError);
return false; return false;
...@@ -358,7 +358,15 @@ bool MissionCommandUIInfo::loadJsonInfo(const QJsonObject& jsonObject, bool requ ...@@ -358,7 +358,15 @@ bool MissionCommandUIInfo::loadJsonInfo(const QJsonObject& jsonObject, bool requ
paramInfo->_nanUnchanged = paramObject.value(_nanUnchangedJsonKey).toBool(false); paramInfo->_nanUnchanged = paramObject.value(_nanUnchangedJsonKey).toBool(false);
if (paramObject.contains(_defaultJsonKey)) { if (paramObject.contains(_defaultJsonKey)) {
paramInfo->_defaultValue = paramObject.value(_defaultJsonKey).toDouble(0.0); if (paramInfo->_nanUnchanged) {
paramInfo->_defaultValue = JsonHelper::possibleNaNJsonValue(paramObject[_defaultJsonKey]);
} else {
if (paramObject[_defaultJsonKey].type() == QJsonValue::Null) {
errorString = QString("Param %1 default value was null/NaN but NaN is not allowed");
return false;
}
paramInfo->_defaultValue = paramObject.value(_defaultJsonKey).toDouble(0.0);
}
} else { } else {
paramInfo->_defaultValue = paramInfo->_nanUnchanged ? std::numeric_limits<double>::quiet_NaN() : 0; paramInfo->_defaultValue = paramInfo->_nanUnchanged ? std::numeric_limits<double>::quiet_NaN() : 0;
} }
......
...@@ -260,6 +260,13 @@ bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& err ...@@ -260,6 +260,13 @@ bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& err
return false; return false;
} }
for (int i=0; i<4; i++) {
if (rgParams[i].type() != QJsonValue::Double && rgParams[i].type() != QJsonValue::Null) {
errorString = tr("Param %1 incorrect type %2, must be double or null").arg(i+1).arg(rgParams[i].type());
return false;
}
}
// Make sure to set these first since they can signal other changes // Make sure to set these first since they can signal other changes
setFrame((MAV_FRAME)v2Json[_jsonFrameKey].toInt()); setFrame((MAV_FRAME)v2Json[_jsonFrameKey].toInt());
setCommand((MAV_CMD)v2Json[_jsonCommandKey].toInt()); setCommand((MAV_CMD)v2Json[_jsonCommandKey].toInt());
...@@ -280,10 +287,10 @@ bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& err ...@@ -280,10 +287,10 @@ bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& err
setSequenceNumber(sequenceNumber); setSequenceNumber(sequenceNumber);
setAutoContinue(v2Json[_jsonAutoContinueKey].toBool()); setAutoContinue(v2Json[_jsonAutoContinueKey].toBool());
setParam1(rgParams[0].toDouble()); setParam1(JsonHelper::possibleNaNJsonValue(rgParams[0]));
setParam2(rgParams[1].toDouble()); setParam2(JsonHelper::possibleNaNJsonValue(rgParams[1]));
setParam3(rgParams[2].toDouble()); setParam3(JsonHelper::possibleNaNJsonValue(rgParams[2]));
setParam4(rgParams[3].toDouble()); setParam4(JsonHelper::possibleNaNJsonValue(rgParams[3]));
return true; return true;
} }
......
...@@ -633,6 +633,7 @@ void SimpleMissionItem::setDefaultsForCommand(void) ...@@ -633,6 +633,7 @@ void SimpleMissionItem::setDefaultsForCommand(void)
_missionItem.setParam2(0); _missionItem.setParam2(0);
break; break;
case MAV_CMD_NAV_VTOL_LAND:
case MAV_CMD_NAV_LAND: case MAV_CMD_NAV_LAND:
_missionItem.setParam7(0); _missionItem.setParam7(0);
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment