Skip to content

Commit

Permalink
Improved how dynamic-sized parameters are handled
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
diegoferigo committed May 7, 2018
1 parent 44a5cfc commit 3960ed4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
24 changes: 19 additions & 5 deletions toolbox/src/base/CoderBlockInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
36 changes: 30 additions & 6 deletions toolbox/src/base/SimulinkBlockInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<double>(paramVector, paramMD);
}
Expand Down Expand Up @@ -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<double>(paramVector, paramMD);
break;
Expand All @@ -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<std::string>(paramVector, paramMD);
break;
Expand All @@ -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<double>(paramVector, paramMD);
}
Expand Down Expand Up @@ -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<double>(paramVector, paramMD);
break;
Expand All @@ -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<std::string>(paramVector, paramMD);
break;
Expand Down

0 comments on commit 3960ed4

Please sign in to comment.