From 3960ed42ff2e00f63679e82f2d245bdf4a98835c Mon Sep 17 00:00:00 2001 From: Diego Ferigo Date: Mon, 7 May 2018 11:13:34 +0200 Subject: [PATCH] Improved how dynamic-sized parameters are handled - The Coder implementation should get the real size from the RTW - The Simulink implementation should get the real size from the parameter parsed from the mask (this value actually is what is written to the RTW file later in the pipeline) --- toolbox/src/base/CoderBlockInformation.cpp | 24 ++++++++++--- toolbox/src/base/SimulinkBlockInformation.cpp | 36 +++++++++++++++---- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/toolbox/src/base/CoderBlockInformation.cpp b/toolbox/src/base/CoderBlockInformation.cpp index aefeed0f0..e39bf0701 100644 --- a/toolbox/src/base/CoderBlockInformation.cpp +++ b/toolbox/src/base/CoderBlockInformation.cpp @@ -205,17 +205,31 @@ bool CoderBlockInformation::parseParameters(wbt::Parameters& parameters) return false; } - for (wbt::ParameterMetadata md : pImpl->paramsMetadata) { + for (wbt::ParameterMetadata& md : pImpl->paramsMetadata) { // Check that all the parameters that are parsed have already been stored from the coder if (!pImpl->parametersFromRTW.existName(md.name)) { wbtError << "Trying to get a parameter value for " << md.name << ", but its value has never been stored."; return false; } - // Check if the parameters are not dynamically sized - if (!(md.rows == -1 || md.cols == -1) - && md != pImpl->parametersFromRTW.getParameterMetadata(md.name)) { - wbtError << "Dynamically sized parameters are not supported."; + + // Handle the case of dynamically sized columns. In this case the metadata passed + // from the Block (containing DynamicSize) is modified with the length of the + // vector that is going to be stored. + if (md.cols == ParameterMetadata::DynamicSize) { + const auto colsFromRTW = pImpl->parametersFromRTW.getParameterMetadata(md.name).cols; + if (colsFromRTW == ParameterMetadata::DynamicSize) { + wbtError << "Trying to store the cols of a dynamically sized parameters, but the " + << "metadata does not specify a valid size. Probably the block didn't " + << "updat the size in its initialization phase."; + return false; + } + md.cols = colsFromRTW; + } + + if (md != pImpl->parametersFromRTW.getParameterMetadata(md.name)) { + wbtError << "Trying to parse a parameter which metadata differs from the metadata " + << "stored by Simulink Coder."; return false; } } diff --git a/toolbox/src/base/SimulinkBlockInformation.cpp b/toolbox/src/base/SimulinkBlockInformation.cpp index a856ec14e..fa088d20c 100644 --- a/toolbox/src/base/SimulinkBlockInformation.cpp +++ b/toolbox/src/base/SimulinkBlockInformation.cpp @@ -489,7 +489,19 @@ bool SimulinkBlockInformation::parseParameters(wbt::Parameters& parameters) // Handle the case of dynamically sized columns. In this case the metadata passed // from the Block (containing DynamicSize) is modified with the length of the // vector that is going to be stored. + // This is necessary in the pipeline for storing the metadata in the RTW file, which should + // not have any dynamic size. const bool hasDynSizeColumns = (paramMD.cols == ParameterMetadata::DynamicSize); + auto handleDynSizeColumns = [](int& sizeToUpdate, const int& realSize) -> const bool { + if (realSize == ParameterMetadata::DynamicSize) { + wbtError << "Trying to store the cols of a dynamically sized parameters, but the " + << "metadata does not specify a valid size. Probably the block didn't " + << "updat the size in its initialization phase."; + return false; + } + sizeToUpdate = realSize; + return true; + }; switch (paramMD.type) { // SCALAR / VECTOR PARAMETERS @@ -523,7 +535,9 @@ bool SimulinkBlockInformation::parseParameters(wbt::Parameters& parameters) return false; } if (hasDynSizeColumns) { - paramMD.cols = paramVector.size(); + if (!handleDynSizeColumns(paramMD.cols, paramVector.size())) { + return false; + } } ok = parameters.storeParameter(paramVector, paramMD); } @@ -566,7 +580,9 @@ bool SimulinkBlockInformation::parseParameters(wbt::Parameters& parameters) paramVector.push_back(value); } if (hasDynSizeColumns) { - paramMD.cols = paramVector.size(); + if (!handleDynSizeColumns(paramMD.cols, paramVector.size())) { + return false; + } } ok = parameters.storeParameter(paramVector, paramMD); break; @@ -588,7 +604,9 @@ bool SimulinkBlockInformation::parseParameters(wbt::Parameters& parameters) paramVector.push_back(value); } if (hasDynSizeColumns) { - paramMD.cols = paramVector.size(); + if (!handleDynSizeColumns(paramMD.cols, paramVector.size())) { + return false; + } } ok = parameters.storeParameter(paramVector, paramMD); break; @@ -615,7 +633,9 @@ bool SimulinkBlockInformation::parseParameters(wbt::Parameters& parameters) return false; } if (hasDynSizeColumns) { - paramMD.cols = paramVector.size(); + if (!handleDynSizeColumns(paramMD.cols, paramVector.size())) { + return false; + } } ok = parameters.storeParameter(paramVector, paramMD); } @@ -658,7 +678,9 @@ bool SimulinkBlockInformation::parseParameters(wbt::Parameters& parameters) paramVector.push_back(value); } if (hasDynSizeColumns) { - paramMD.cols = paramVector.size(); + if (!handleDynSizeColumns(paramMD.cols, paramVector.size())) { + return false; + } } ok = parameters.storeParameter(paramVector, paramMD); break; @@ -682,7 +704,9 @@ bool SimulinkBlockInformation::parseParameters(wbt::Parameters& parameters) paramVector.push_back(value); } if (hasDynSizeColumns) { - paramMD.cols = paramVector.size(); + if (!handleDynSizeColumns(paramMD.cols, paramVector.size())) { + return false; + } } ok = parameters.storeParameter(paramVector, paramMD); break;