Rapid Prototyping Model Functions
Rapid prototyping code defines the following functions that interface with the main program (main.c
ormain.cpp
):
Model()
:这model registration function. This function initializes the work areas (for example, allocating and setting pointers to various data structures) used by the model. The model registration function calls theMdlInitializeSizes
andMdlInitializeSampleTimes
功能。这两个功能与S功能非常相似mdlInitializeSizes
andmdlInitializeSampleTimes
methods.mdlstart((void)
:在模型注册功能之后MdlInitializeSizes
andMdlInitializeSampleTimes
执行,主程序通过调用开始执行mdlstart
。此例程在启动时被称为一次。这F你nction
mdlstart
有四个基本部分:Code to initialize the states for each block in the root model that has states. A subroutine call is made to the “initialize states” routines of conditionally executed subsystems.
由模型中每个块的一次性初始化(启动)函数生成的代码。
代码,使街区根模型中have enable methods, and the blocks inside triggered or function-call subsystems residing in the root model. Simulink®块可以具有启用和禁用方法。启用方法在块开始执行之前就调用,并且在块停止执行之后,该禁用方法被调用。
Code for each block in the model whose output value is constant. The block code appears in the
mdlstart
F你nction only if the block parameters are not tunable in the generated code and if the code generator cannot eliminate the block code through constant folding.
MdlOutputs(int_T tid)
:MdlOutputs
更新块的输出。这tid
(任务标识符)参数标识了根据其示例时间执行块何时执行块的任务。在主要时间和次要步骤中,主计划调用了此例程。主要时间步骤是主程序正在采取实际时间步骤(也就是说,是时候执行特定任务了)。如果您的模型包含连续状态,则将采取次要时间步骤。次要时间步骤是求解器生成集成阶段的时间,即主要输出之间的点。这些集成阶段用于计算用于推进连续状态的衍生物。MdlUpdate(int_T tid)
:MdlUpdate
更新在工作向量中保存的状态和工作向量状态信息(即既不是连续也不是离散的状态)。这tid
(任务标识符)参数标识了一个任务,该任务依次指示哪些样本时间处于活动状态,从而使您只能有条件地更新活动块的状态。大专业之后,该界面调用了此例程MdlOutputs
已执行。求解器也被称为
is called in minor steps by the solver during its integration stages. All blocks that have continuous states have an identical number of derivatives. These blocks are required to compute the derivatives so that the solvers can integrate the states.model
_DerivativesMdlTerminate(void)
:MdlTerminate
Contains any block shutdown code.MdlTerminate
作为实时程序终止的一部分,由接口调用。
这Contents of the above functions are directly related to the blocks in your model. A Simulink block can be generalized to the following set of equations.
输出y是连续状态的函数XC,离散状态Xd和输入你。每个块在一个部分中编写其特定方程式MdlOutputs
。
离散状态Xdare a function of the current state and input. Each block that has a discrete state updates its state inMdlUpdate
。
衍生物Xare a function of the current input. Each block that has continuous states provides its derivatives to the solver (for example,ode5
) 在
。衍生物are used by the solver to integrate the continuous state to produce the next value.model
_Derivatives
输出,y,,,,is generally written to the block I/O structure. Root-level Outport blocks write to the external outputs structure. The continuous and discrete states are stored in the states structure. The input,你,可以源自另一个块的输出,该输出位于块I/O结构中,外部输入(位于外部输入结构)或状态。这些结构在
归档Simulink Coder™软件生成。model
。h
这next example shows the general contents of the rapid prototyping style of C code written to the
File.model
。C
该图显示了描述快速原型生成代码的执行的流程图。
快速原型执行流程图
Each block places code in specificMdl
routines according to the algorithm that it is implementing. Blocks have input, output, parameters, and states, as well as other general items. For example, in general, block inputs and outputs are written to a block I/O structure (
)。Block inputs can also come from the external input structure (model
_B
)or the state structure when connected to a state port of an integrator (model
_U
),或地面(model
_Xrtground
)如果没有连接或接地。块输出也可以转到外部输出结构(
)。该图显示了这些项目之间的一般映射。model
_Y
生成代码的数据视图
Block I/O structure (
):该结构由持续的块输出信号组成。块输出信号的数量是模型中非虚拟块的数据输出端口的宽度之和。如果激活块I/O优化,则Simulink和金宝appSimulink Coderproducts reduce the size of themodel
_B
结构model
_BReusing the entries in the
structuremodel
_BMaking other entries local variables
看How Generated Code Stores Internal Signal, State, and Parameter DataFor more information on these optimizations.
Structure field names are determined either by the block's output signal name (when present) or by the block name and port number when the output signal is left unlabeled.
Block states structures: The continuous states structure (
)Contains the continuous state information for blocks in your model that have continuous states. Discrete states are stored in a data structure called themodel
_X
。dwork矢量
((model
_ dwork)块参数结构(
):参数结构包含可以在执行过程中更改的块参数(例如,增益块的参数)。model
_P外部输入结构(
):这external inputs structure consists of the root-level Inport block signals. Field names are determined by either the block's output signal name, when present, or by the Inport block's name when the output signal is left unlabeled.model
_UExternal outputs structure (
):外部输出结构由根级外口块组成。字段名称由模型中的根级输出块名称确定。model
_YReal work, integer work, and pointer work structures (
,,,,model
_RWork
,,,,model
_IWork
):块可能需要真实,整数或指针工作区域。例如,内存块为每个信号使用一个真实的工作元素。这些领域用于保存内部状态或类似信息。model
_pwork