Documentation

将对话框参数传递给s功能

关于对话参数

You can pass parameters to an S-function at the start of and during the simulation, using theS功能参数field of the Block Parameters dialog box. Such parameters are called对话框参数将它们与S功能创建的运行时参数区分开来,以促进代码生成创建和更新S功能运行时参数)。

Note

您不能使用模型资源管理器,s功能块参数对话框或掩码来调整源s函数的参数,即,在运行仿真时,具有输出但没有输入的s函数。有关更多信息,请参阅调谐和实验块参数值

使用C S功能对话框参数

Simu金宝applink®引擎将对话框参数的值存储在s功能中SimStruct结构体。使用S功能回调方法和SimStructmacros to access and check the parameters and use them to compute the S-function output. To use dialog parameters in your C S-function, perform the following steps when you create the S-function:

  1. 确定在“块”对话框中指定参数的顺序。

  2. 在里面mdlinitialsizes功能,使用sssetnumsfcnparams宏告诉Simulink引擎S函数接受金宝app了多少个参数。指定Sas the first argument and the number of dialog box parameters as the second argument. If your S-function implements themdlcheckparameters方法,mdlinitialsizes例行应该打电话mdlcheckparameters检查参数的初始值的有效性。例如,mdlinitialsizes功能在sfun_runtime1.c从以下代码开始。

    sssetnumsfcnparams(s,nparams);/ *预期参数的数量 */ #if定义(matlab_mex_file)if(ssgetNumSfcnParams(s)== ssgetSfcnParamScount(s)){mdlCheckParameters(s);if(ssgetErrorStatus(s)!= null){return;}} else {return;/* simulink引擎报告的参数不匹配*/} #endif金宝app
  3. Access the dialog box parameters in the S-function using thessgetsfcnparammacro.

    指定Sas the first argument and the relative position of the parameter in the list entered on the dialog box (0 is the first position) as the second argument. Thessgetsfcnparam宏返回指针mxarray包含参数。您可以使用ssgetDtypeidFrommxArrayto get the data type of the parameter. Alternatively, you can useSSGETSFCNPARAMDATATYPEto get the data type of the parameter by specifying the index of the parameter.

    例如,在sfun_runtime1.c, the following#defineS函数开头的语句指定了三个对话框参数的顺序,并在块的对话框上访问其值。

    #define signs_idx 0 #define signs_param(s)ssgetsfcnparam(s,signs_idx) / *第一个参数 * / #define gain_idx 1 #define gain_param(s)ssgetSfcnParam(s)out_param(s)ssgetsfcnparam(s,out_idx) / *第三参数 * /

运行仿真时,您必须指定参数S功能参数field of the S-Function Block Parameters dialog box in the same order that you defined them in step 1. You can enter any valid MATLAB®expression as the value of a parameter, including literal values, names of workspace variables, function invocations, or arithmetic expressions. The Simulink engine evaluates the expression and passes its value to the S-function.

As another example, the following code is part of a device driver S-function. Four input parameters are used:base_address_prm,GAIN_RANGE_PRM,PROG_GAIN_PRM, 和num_of_channels_prm。代码使用#defineS函数顶部的语句将特定输入参数与参数名称相关联。

/* Input Parameters */ #define BASE_ADDRESS_PRM(S) ssGetSFcnParam(S, 0) #define GAIN_RANGE_PRM(S) ssGetSFcnParam(S, 1) #define PROG_GAIN_PRM(S) ssGetSFcnParam(S, 2) #define NUM_OF_CHANNELS_PRM(S) ssGetSFcnParam(S, 3)

运行仿真时,在“在”中输入四个变量名称或值S功能参数S功能块参数对话框的字段。第一个对应于第一个期望参数,BASE_ADDRESS_PRM (S)。第二个对应于下一个预期参数,依此类推。

Themdlinitialsizes功能包含此语句。

sssetnumsfcnparams(S,4);

可调参数

对话框参数可以可调谐或不可调能。可调参数是在模拟运行时用户可以更改的参数。

Note

对话框参数默认情况下是可调的。然而,设置每个参数的可调节性,即使是可调的,都是很好的编程实践。如果启用模拟诊断需要S功能升级,Simul金宝appink引擎每当遇到无法指定其所有参数可调性的S功能时就会发出诊断。

Themdlcheckparametersmethod enables you to validate changes to tunable parameters during a simulation. The engine invokes themdlcheckparameters每当您在模拟循环期间更改参数值时。此方法应选中“ S功能对话框”参数,以确保更改有效。

The optionalmdlprocessparameterscallback method allows an S-function to process changes to tunable parameters. The engine invokes this method only if valid parameter changes have occurred in the previous time step. A typical use of this method is to perform computations that depend only on the values of parameters and hence need to be computed only when parameter values change. The method can cache the results of the parameter computations in work vectors or, preferably, as run-time parameters (see创建和更新S功能运行时参数)。

Using Tunable Parameters in a C S-Function

在C函数中,使用宏ssetsetfcnparamtunablemdlinitialsizesto specify the tunability of each S-function dialog box parameter. The code below is taken from themdlinitialsizes示例中的功能sfun_runtime1.c。代码首先将S功能对话框参数的数量设置为三个之前mdlcheckparameters。If the parameter check passes, the tunability of the three S-function dialog box parameters is specified.

sssetnumsfcnparams(S, 3); /* Three dialog box parameters*/ #if defined(MATLAB_MEX_FILE) if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) { mdlCheckParameters(S); if (ssGetErrorStatus(S) != NULL) { return; } } else { return; /* Parameter mismatch reported by the Simulink engine*/ } #endif ssSetSFcnParamTunable(S,GAIN_IDX,true); /* Tunable */ ssSetSFcnParamTunable(S,SIGNS_IDX,false); /* Not tunable */ ssSetSFcnParamTunable(S,OUT_IDX,false); /* Not tunable */

Note

The S-functionmdlinitialsizes例行调用mdlcheckparametersmethod to ensure that the initial values of the parameters are valid.

Tuning Parameters in External Mode

当您在模拟过程中调整参数时,Simulink引擎会调用s功能金宝appmdlcheckparameters验证更改,然后验证s功能的方法'mdlprocessparametersmethod to give the S-function a chance to process the parameters in some way. The engine also invokes these methods when running in external mode, but it passes the unprocessed changes to the S-function target. Thus, if it is essential that your S-function process parameter changes, you need to create a Target Language Compiler (TLC) file that inlines the S-function, including its parameter processing code, during the code generation process. For information on inlining S-functions, see “Inlining S-Functions” in the金宝appSimulink Coder™Target Language Compiler documentation.

See Also

相关话题