文档

用mdlRTW例程编写完全内联的s函数

你可以使用s函数内联更复杂的s函数mdlRTW例行公事。的mdlRTW例程为代码生成过程提供了关于如何通过创建用于TLC文件的不可调参数的参数记录来内联s函数的更多信息。的mdlRTW例程将信息放在模型.rtw文件。的mdlRTW函数在文本文件中描述matlabroot/金宝app模型/ src /sfuntmpl_doc.c

使用mdlRTW函数,执行步骤创建直接索引查找s函数。查找表是函数中有序数据点的集合。通常,这些表使用一些插值方案来近似已知数据点之间相关函数的值。将示例查找表算法合并到Simulink中金宝app®模型,第一步是写一个s函数来执行算法mdlOutputs.为了生成最有效的代码,下一步是创建相应的TLC文件,以消除计算开销并提高查找计算的速度。

Simu金宝applink产品提供了两种通用查找1-D和2-金宝appD算法的支持。您可以按原样使用这些算法,也可以创建自定义查找表s -函数来满足您的需求。你可以创建一个一维查找s函数,sfun_directlook.c,其对应的内联sfun_directlook.tlc文件(见目标语言编译器(金宝appSimulink Coder)以获取更多详细信息)。您可以:

  • s函数参数的误差检查。

  • 缓存s函数的信息,这些信息在模型执行期间不会改变。

  • 使用mdlRTW函数自定义代码生成器,以为给定的块参数集生成最优代码。

  • 创建一个薄层色谱文件中的s函数要么完全内联查找表代码,要么调用查找表算法的包装器函数。

s函数RTWdata

RTWdata是blocks的属性,目标语言编译器可以在内联s函数时使用该属性。RTWdata是可以附加到块上的字符向量的结构。RTWdata与模型一起保存并放置在模型.rtw生成代码时文件。比如这一套MATLAB®命令:

mydata。field1 =字段1的信息;mydata。field2 =字段2的信息;set_param (gcb),“RTWdata”, mydata) get_param (gcb,“RTWdata”)

产生如下结果:

Ans = field1:字段1的信息field2:字段2的信息

控件中相关S-Function块的信息模型.rtw文件是:

Block {Type "S-Function" RTWdata {field1" information for field1" " field2" information for field2"}

请注意

RTWdata保存在没有链接到库的s函数的模型文件中。然而,RTWdata不持久用于链接到库的S-Function块。

直接索引查找表算法

Simulink库中提供的1-D查找表块在计算输出时使用插值或外推。金宝app在本例中,您创建了一个查找表,它直接为输出向量(y-data vector)基于当前输入(x拼点。

这个直接1-D查找示例计算一个近似解p (x)一个部分已知的函数f (x)x = x0,给定数据点对(x, y)以an的形式x-data vector和ay拼向量。对于给定的数据对(例如i两人),Y_i = f(x_i).这是假定的x-data的值单调递增。如果x0的范围之外x-data vector,返回第一个或最后一个点。

s函数的参数为:

XData, YData, XEvenlySpaced

XData而且YData是相等长度的两个向量,表示未知函数的值。XDataEvenlySpaced是标量,0.0对于虚假和1.0为真实的。如果XData向量是等距的,XDataEvenlySpaced1.0生成更高效的代码。

该图显示了参数XData = [1:6]而且YData =[1、2、7、4、5、9)处理。例如,如果输入(x-value)到S-Function块为3,则输出(y-value)是7。

直接索引查找表示例

通过将直接索引s函数与TLC文件内联来改进查找表。这个直接索引查找表s函数不需要TLC文件。该示例将TLC文件用于直接索引查找表s函数,以减少代码大小并提高生成代码的效率。

使用内联TLC文件实现直接索引算法需要s函数主模块,sfun_directlook.c一个对应的lookup_index.c模块。的lookup_index.c模块包含GetDirectLookupIndex对象中的索引XData对于电流x输入值时,XData是间隔不均匀的。的GetDirectLookupIndex例程从s函数和生成的代码中调用。该示例使用包装器概念在Simulink mexs文件和生成的代码之间共享C/ c++代码。金宝app

如果XData,则s函数主模块和生成的代码都包含查找算法来计算y-给定值的值x-value,因为算法很短。

内联TLC文件为sfun_directlook.tlc,用于执行包装器调用或为s函数嵌入最佳的C/ c++代码。(参见mdlRTW用法(金宝app仿真软件编码器))。

错误处理

sfun_directlook.tlc,mdlCheckParameters例程验证:

  • 修改后的参数有效。

  • XData而且YData是包含有限实数的相同长度的向量。

  • XDataEvenlySpaced是标量。

  • XData向量是一个单调递增且间隔均匀的向量。

mdlInitializeSizes函数显式调用mdlCheckParameters在它验证传递给s函数的参数数量之后。在Simulink引金宝app擎调用之后mdlInitializeSizes,然后调用mdlCheckParameters当你改变参数或重新评估它们时。

用户数据缓存

sfun_directlook.tlc,mdlStart例程显示如何缓存在模拟期间或在执行生成的代码时不会更改的信息。的值进行缓存XDataEvenlySpaced参数用户数据,一片田野的SimStruct.下面这行mdlInitializeSizes指示Simulink引擎禁止金宝app对XDataEvenlySpaced

ssSetSFcnParamTunable(S, iParam, SS_PRM_NOT_TUNABLE);

在执行期间,mdlOutputs的值。XDataEvenlySpaced用户数据而不是调用mxGetPrMATLAB API函数。

mdlRTW用法

代码生成器调用mdlRTW例程,同时生成模型.rtw文件。要为Simulink模型生成最佳代码,可以将信息添加到金宝app模型.rtw关于S-Function块运行模式的文件。

属性的参数设置模型.rtw文件。参数设置在执行过程中不会改变。在这种情况下,XDataEvenlySpaceds函数参数在执行过程中不能改变(ssSetSFcnParamTunable被指定为false (0)mdlInitializeSizes).参数设置(XSpacing)使用该函数ssWriteRTWParamSettings

因为xData而且yData注册为运行时参数mdlSetWorkWidths时,代码生成器将写入模型.rtw自动文件。

在检查s函数和内联TLC文件之前,请考虑为该模型生成的代码。

模型采用等间距XData在顶部的s功能块和不均匀的间隔XData在底部的S-Function块中。在创建此模型时,为每个S-Function块指定以下命令。

set_param (“sfun_directlook_ex / s函数”“SFunctionModules”“lookup_index”) set_param (“sfun_directlook_ex / S-Function1”“SFunctionModules”“lookup_index”)

构建过程使用该模块lookup_index.c当创建可执行文件时。

在为这个模型生成代码时,代码生成器使用s函数mdlRTW方法来生成模型.rtw文件的值为EvenlySpacedXSpacing参数为顶部S-Function块和值UnEvenlySpacedXSpacing参数为底部S-Function块。tlc文件使用值XSpacing以确定在生成的代码中包含什么算法。时生成的代码包含查找算法XData是等距的,但是调用GetDirectLookupIndex例行公事时XData是间隔不均匀的。生成的模型.c模型. cpp查找表示例模型的代码类似于下面的代码:

/* * sfun_directlook_ex.c * * Simulink模型的代码生成* "sf金宝appun_directlook_ex.slx"*……*/ #include "sfun_directlook_ex.h" #include "sfun_directlook_ex_private.h" /*外部输出(由自动存储信号提供的根输出)*/ ExtY_sfun_directlook_ex_T sfun_directlook_ex_Y;/*实时模型*/ RT_MODEL_sfun_directlook_ex_T sfun_directlook_ex_M_;RT_MODEL_sfun_directlook_ex_T *const sfun_directlook_ex_M = &sfun_directlook_ex_M_;/*模型输出函数*/ void sfun_directlook_ex_output(void){/*本地块i/o变量*/ real_T rtb_SFunction;real_T rtb_SFunction1;/* Sin: '<根>/正弦波' */ rtb_SFunction1 = Sin (sfun_directlook_ex_M->Timing.t[0]);/*在* sfun_directlook_ex模型中为顶部s函数块内联的代码/* S-Function (sfun_directlook): '/S-Function' */ {const real_T *xData = sfun_directlook_ex_ConstP.SFunction_XData;const real_T *yData = sfun_directlook_ex_ConstP.SFunction_YData;real_T spacing = xData[1] - xData[0];if (rtb_SFunction1 <= xData[0]) {rtb_SFunction = yData[0];} else if (rtb_SFunction1 >= yData[20]) {rtb_SFunction = yData[20];} else {int_T idx = (int_T)((rtb_SFunction1 - xData[0]) / spacing);rtb_SFunction = yData[idx];}} /* Outport: '/Out1' */ sfun_directlook_ex_Y. Outport: '/Out1'Out1 = rtb_SFunction;/*在* sfun_directlook_ex模型中为底部s函数块内联的代码/* S-Function (sfun_directlook): '/S-Function1' */ {const real_T *xData = sfun_directlook_ex_ConstP.SFunction1_XData;const real_T *yData = sfun_directlook_ex_ConstP.SFunction1_YData;int_T idx;idx = GetDirectLookupIndex(xData, 5, rtb_SFunction1);rtb_SFunction1 = yData[idx];} /* Outport: '/Out2' */ sfun_directlook_ex_Y. Outport: '/Out2'Out2 = rtb_SFunction1;} /*模型更新函数*/ void sfun_directlook_ex_update(void){/*信号主停止模拟*/{/*采样时间:[0.0s, 0.0s] */ if ((rtmGetTFinal(sfun_directlook_ex_M)!=-1) && !((rtmGetTFinal(sfun_directlook_ex_M)-sfun_directlook_ex_M->Timing.t[0]) > sfun_directlook_ex_M->Timing.t[0])t[0] * (DBL_EPSILON))) {rtmSetErrorStatus(sfun_directlook_ex_M, "模拟完成");}} /*更新基本速率的绝对时间*/ /*“clockTick0”计数此任务的代码已*执行的次数。绝对时间是“clockTick0”*和“Timing.stepSize0”的乘积。“clockTick0”的大小确保计时器在所选择的应用程序生命周期内不会溢出。 * Timer of this task consists of two 32 bit unsigned integers. * The two integers represent the low bits Timing.clockTick0 and the high bits * Timing.clockTickH0. When the low bit overflows to 0, the high bits increment. */ if (!(++sfun_directlook_ex_M->Timing.clockTick0)) { ++sfun_directlook_ex_M->Timing.clockTickH0; } sfun_directlook_ex_M->Timing.t[0] = sfun_directlook_ex_M->Timing.clockTick0 * sfun_directlook_ex_M->Timing.stepSize0 + sfun_directlook_ex_M->Timing.clockTickH0 * sfun_directlook_ex_M->Timing.stepSize0 * 4294967296.0; } ...

相关的话题