ReQuestion: passing information/parameters from Simulink to embedded code generated

4 views (last 30 days)
Hi,
I'm developing inline s-function driver block. I have created the .c .tlc and .mex32 files.
These blocks have a mask with 4 parameters, and I want to pass parameters to the files generated by RTW tool, but when I try to generate the code I have this error:
Error using ==> rtwgen Error in mdlRTW of S-function myModel/model_DI'. This function wrote 0 run-time parameters where as it has registered 4 run-time parameters.
I try to do so:
In my driver, at the top of my .c I have this:
#define param1(S) (mxGetScalar(ssGetSFcnParam(S,1)))
#define param2(S) (mxGetScalar(ssGetSFcnParam(S,2)))
#define param3(S) (mxGetScalar(ssGetSFcnParam(S,3)))
Then this is the MdlRTW function:
#define MDL_RTW
static void mdlRTW(SimStruct *S) {
uint8_T Numparam1, Numparam2, Numparam3;
Numparam1=(uint8_T)param1(S);
if( Numparam1<0 Numparam1>15 )
{
sprintf(msg,"Número incorrecto de Numparam1 %d", Numparam1);
ssSetErrorStatus(S,msg); return;
}
Numparam2=(uint8_T)param2(S);
if( Numparam2<0 Numparam2>15 )
{
sprintf(msg,"Número incorrecto de Numparam2 %d", Numparam2);
ssSetErrorStatus(S,msg); return;
}
Numparam3=(uint8_T)param3(S);
if( Numparam3<0 Numparam3>1 )
{
sprintf(msg,"Número incorrecto de Numparam3 %d", Numparam3);
ssSetErrorStatus(S,msg); return; }
// Write out the parameters for this block.
if (!ssWriteRTWParamSettings(S, 3, SSWRITE_VALUE_DTYPE_NUM, "Numparam1", &Numparam1, DTINFO (SS_UINT8, COMPLEX_NO), SSWRITE_VALUE_DTYPE_NUM, "Numparam2", &Numparam2, DTINFO (SS_UINT8, COMPLEX_NO), SSWRITE_VALUE_DTYPE_NUM, "Numparam3", &Numparam3, DTINFO (SS_UINT8, COMPLEX_NO)))
{
sprintf(msg,"Error al pasar parametros de la mascara a model.rtw");
ssSetErrorStatus(S,msg);
return; // An error occurred which will be reported by SL } }
And then, to pick in my .tlc I have:
%%Function: mdlOutputs ========================================================
%function Outputs(block, system) Output /* %<Type> Block: %<Name> */
{
%assign Numparam1= CAST( "Number",SFcnParamSettings.Numparam1)
%assign Numparam2= CAST( "Number",SFcnParamSettings.Numparam2)
%assign Numparam3= CAST( "Number",SFcnParamSettings.Numparam3)
...

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 14 Nov 2011
As the documentation suggests, ssWriteRTWParamSettings is needed to write the values of nontunable parameters to the model.rtw file. Since you seem to have already registered your parameters as tunable, you don't have to define the mdlRTW() method at all, because these are automatically written to model.rtw for you. Examine the model.rtw file (ensure that "Retain .rtw file" under Code Generation>Debug is checked before you attempt to generate code from the model - the model.rtw file will be generated to the same folder where the model.c files are generated). The parameters values are written against the name you registered for each of them in the 4th argument to ssRegDlgParamAsRunTimeParam.
  3 Comments
Carlos
Carlos on 15 Nov 2011
For example in this piece of code is posible to see that I have two blocks with the same source code but different mask parameters. And I need to dynamically acces by the same fucntion "Outputs":
Block {
Type "S-Function"
InMask yes
MaskType ""
BlockIdx [0, 0, 0]
SL_BlockIdx 0
GrSrc [0, 8]
ExprCommentInfo {
}
ExprCommentSrcIdx {
}
Name "<Root>/wce_DI1"
Identifier wce_DI1
TID 0
RollRegions [0]
NumDataOutputPorts 2
DataOutputPort {
SignalSrc [b0]
DataTypeIdx 4
}
DataOutputPort {
SignalSrc [b4]
DataTypeIdx 8
}
Connections {
InputPortContiguous []
InputPortConnected []
OutputPortConnected [yes, yes]
OutputPortBeingMerged [no, no]
DirectSrcConn []
DirectDstConn [no, yes]
DataOutputPort {
NumConnPoints 2
ConnPoint {
SrcSignal [0, 1]
DstBlockAndPortEl [0, 2, 2, 0]
}
ConnPoint {
SrcSignal [0, 1]
DstBlockAndPortEl [0, 3, 0, 0]
}
}
DataOutputPort {
NumConnPoints 1
ConnPoint {
SrcSignal [0, 1]
DstBlockAndPortEl [0, 4, 0, 0]
}
}
}
ParamSettings {
FunctionName wce_DI
FunctionType "C-MEX"
Inlined yes
ExplicitFCSSCtrl no
Asynchronous no
FunctionLevel 2
DirectFeedthrough []
UsingUPtrs []
InputContiguous []
DataStoreSource []
DataStoreGlobalDSM []
SampleTimesToSet Matrix(1,2)
[[0, 0];]
DynamicallySizedVectors ["NonsampledZCs"]
SFcnmdlRoutines Vector(4)
["mdlInitializeSizes", "mdlInitializeSampleTimes", "mdlOutputs", "mdlTerminate"]
AliasDataTypeCompliant 0
}
NumSFcnSysOutputCalls 0
Parameters [4, 4]
Parameter {
Name "P1"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 0
}
Value [0.01]
OriginalDataTypeIdx -10
String "sample_time"
StringType "Expression"
}
Parameter {
Name "P2"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 1
}
Value [1.0]
OriginalDataTypeIdx -10
String "backplane"
StringType "Expression"
}
Parameter {
Name "P3"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 2
}
Value [2.0]
OriginalDataTypeIdx -10
String "slot"
StringType "Expression"
}
Parameter {
Name "P4"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 3
}
Value [1.0]
OriginalDataTypeIdx -10
String "word"
StringType "Expression"
}
P1 Parameter[0]
P2 Parameter[1]
P3 Parameter[2]
P4 Parameter[3]
}
Block {
Type "S-Function"
InMask yes
MaskType ""
BlockIdx [0, 0, 1]
SL_BlockIdx 1
GrSrc [0, 9]
ExprCommentInfo {
}
ExprCommentSrcIdx {
}
Name "<Root>/wce_DI2"
Identifier wce_DI2
TID 1
RollRegions [0]
NumDataOutputPorts 2
DataOutputPort {
SignalSrc [b1]
DataTypeIdx 4
}
DataOutputPort {
SignalSrc [b5]
DataTypeIdx 8
}
Connections {
InputPortContiguous []
InputPortConnected []
OutputPortConnected [yes, yes]
OutputPortBeingMerged [no, no]
DirectSrcConn []
DirectDstConn [no, yes]
DataOutputPort {
NumConnPoints 2
ConnPoint {
SrcSignal [0, 1]
DstBlockAndPortEl [0, 9, 2, 0]
}
ConnPoint {
SrcSignal [0, 1]
DstBlockAndPortEl [0, 10, 0, 0]
}
}
DataOutputPort {
NumConnPoints 1
ConnPoint {
SrcSignal [0, 1]
DstBlockAndPortEl [0, 11, 0, 0]
}
}
}
ParamSettings {
FunctionName wce_DI
FunctionType "C-MEX"
Inlined yes
ExplicitFCSSCtrl no
Asynchronous no
FunctionLevel 2
DirectFeedthrough []
UsingUPtrs []
InputContiguous []
DataStoreSource []
DataStoreGlobalDSM []
SampleTimesToSet Matrix(1,2)
[[0, 1];]
DynamicallySizedVectors ["NonsampledZCs"]
SFcnmdlRoutines Vector(4)
["mdlInitializeSizes", "mdlInitializeSampleTimes", "mdlOutputs", "mdlTerminate"]
AliasDataTypeCompliant 0
}
NumSFcnSysOutputCalls 0
Parameters [4, 4]
Parameter {
Name "P1"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 4
}
Value [0.050000000000000003]
OriginalDataTypeIdx -10
String "sample_time"
StringType "Expression"
}
Parameter {
Name "P2"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 5
}
Value [5.0]
OriginalDataTypeIdx -10
String "backplane"
StringType "Expression"
}
Parameter {
Name "P3"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 6
}
Value [7.0]
OriginalDataTypeIdx -10
String "slot"
StringType "Expression"
}
Parameter {
Name "P4"
ASTNode {
IsNonTerminal 0
Op SL_NOT_INLINED
ModelParameterIdx 7
}
Value [2.0]
OriginalDataTypeIdx -10
String "word"
StringType "Expression"
}
P1 Parameter[0]
P2 Parameter[1]
P3 Parameter[2]
P4 Parameter[3]
}
Kaustubha Govind
Kaustubha Govind on 16 Nov 2011
Please type "sfcndemo_runtime" at the MATLAB prompt to see an example of S-function with run-time parameters and their corresponding TLCs. Hopefully this will help you achieve what you need.

Sign in to comment.

More Answers (0)

Categories

Find more on Embedded Coder in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!