Documentation

万博1manbetx仿真软件的功能s

What Are Simulink Functions?

A Simulink®function is a computational unit that calculates a set of outputs when provided with a set of inputs. The function header uses a notation similar to programming languages sucmah as MATLAB®和C ++。您可以通过多种方式定义和实现Simulink函数:万博1manbetx

  • 万博1manbetx仿真软件的功能block- 使用Simulink功能块中的Simulink万博1manbetx块定义的函数。看万博1manbetx仿真软件的功能block reference.

  • 导出状态流®graphical function— Function defined with state transitions within a Stateflow chart, and then exported to a Simulink model.

  • 导出状态流MATLAB function- 在状态流图内使用MATLAB语言语句定义的函数,然后导出到Simulink模型。万博1manbetx

  • S-function— Function defined using andS-functionblock. For an example with an S-function, opensfcndemo_万博1manbetxsimulinkfunction_getset.

What Are Simulink Function Callers?

Si万博1manbetxmulink函数调用器调用模型或图表层次结构中任何地方的Simulink函数的执行。

  • 功能调用器块— In a Simulink model, calls a function defined in Simulink or exported from Stateflow. See功能呼叫者block reference.

  • Stateflow chart transition- 在状态流图中,调用在simulink中定义或从状态流导出的函数。万博1manbetx

  • MATLAB Function block— In a Simulink model, calls a function from a MATLAB language script.

Connect to Local Signals

In addition toArgument InportArgument Outportblocks, a万博1manbetx仿真软件的功能block can interface to signals in the local environment of the block through企业或者外港blocks. These signals are hidden from the caller. You can use port blocks to connect and communicate between two Simulink Function blocks or connect to root企业外港blocks that represent external I/O.

You can also connect the外港blocks to sink blocks that include logging (To File,To Workspace) and viewing (Scope,Display) blocks. However, these blocks execute last after all other blocks.

A万博1manbetx仿真软件的功能block can output a function-call signal to an外港block.

可重复使用的功能逻辑

当您需要跨模型层次结构可重复使用的逻辑时,请使用功能。考虑一个示例,其中在状态流图中定义了具有可重复使用逻辑的万博1manbetxsimulink函数。

You can move the reusable logic from inside the Stateflow chart to a Simulink Function block. The logic is then reusable by function callers in Simulink subsystems (Subsystem and Model blocks) and in Stateflow charts at any level in the model hierarchy.

The result is added flexibility for structuring your model for reuse.

不te

Input and output argument names (x2,y2) for calling a function from a Stateflow chart do not have to match the argument names in the function prototype (u,y) of a Simulink Function block.

输入/输出参数行为

功能原型万博1manbetx仿真软件的功能block can have identical input and output arguments. For example, a function that filters noise could input a signal and then return the signal after filtering.

mySignal =费尔ter(mySignal)

您可以用一个调用该功能功能呼叫者阻止并向测试信号添加噪声以验证函数算法。

When generating code for this model, the input argument for the万博1manbetx仿真软件的功能block passes a pointer to the signal, not a copy of the signal value.

void filter(real_T *rtuy_mySignal) { . . .
* rtuy_mysignal = model_p.discretefilter_numcoef * iNCETEFILTER_TMP;}

Shared Resources with Functions

Use functions when you model a shared resource, such as a printer. The modelslexPrinterExampleuses Simulink Function blocks as a common interface between multiple computers and a single Stateflow chart that models a printer process.

How a Function Caller Identifies a Function

The function interface uses MATLAB syntax to define the name of a function and its input and output arguments. The model hierarchy can contain only one function definition with the identified function name. Simulink verifies that:

  • The arguments in theFunction prototypeparameter for a Function Caller block matches the arguments specified in the function. For example, a function with two input arguments and one output argument appears as:

    y = myfunction(u1,u2)

  • The data type, dimension, and complexity of the arguments must agree. For a Function Caller block, you can set theInput argument specificationsOutput argument specifications参数,但通常你不需要指定these parameters manually. Simulink derives the specification from the function.

    The only case where you must specify the argument parameters is when the Function Caller block cannot find the function in the model or in any child model it references. This situation can happen when the Function Caller block and called function are in separate models that are referenced by a common parent model. See万博1manbetx仿真软件的功能s in Referenced ModelsArgument Specification for Simulink Function Blocks.

Reasons to Use a Simulink Function Block

Function-Call Subsystem blocks with direct signal connections for triggering provide better signal traceability than Simulink Function blocks, but Simulink Function blocks have other advantages.

  • Eliminate routing of signal lines.The Function Caller block allows you to execute functions defined with a Simulink Function block without a connecting signal line. In addition, functions and their callers can reside in different models or subsystems. This approach eliminates signal routing problems through a hierarchal model structure and allows greater reuse of model components.

  • Use multiple callers to the same function.Multiple Function Caller blocks or Stateflow charts can call the same function. If the function contains state (e.g., a Unit Delay block), the state is shared between the different callers.

  • 与函数定义的单独函数接口。Functions separate their interface (input and output arguments) from their implementation. Therefore, you can define a function using a Simulink Function block, an exported graphical function from Stateflow, or an exported MATLAB function from Stateflow. The caller does not need to know how or where the function was implemented.

Choose a Simulink Function or Reusable Subsystem

考虑使用万博1manbetx仿真软件的功能block or aSubsystemblock has to do with shared state between function calls. A万博1manbetx仿真软件的功能block has shared state while aSubsystem即使指定为可重复使用的函数,块也没有。

  • For a万博1manbetx仿真软件的功能块,当一个块具有多个呼叫者时,始终为一个函数生成代码。如果Simul万博1manbetxink函数块包含具有状态的块(例如,延迟或内存),则状态是持久的,并且在函数调用器之间共享。在这种情况下,呼叫顺序是一个重要的考虑因素。

  • For aSubsystem块,当一个块具有多个实例并将其配置为可重复使用的函数时,通常将代码用于一个函数作为优化。如果是Subsystemblock contains blocks with state, code is still generated for one function, but a different state variable is passed to the function. State is not shared between the instances.

When Not to Use a Simulink Function Block

万博1manbetx仿真软件的功能blocks allow you to implement functions graphically, but sometimes using a Simulink Function block is not the best solution.

For example, when modeling a PID controller or a digital filter and you have to model the equations defining the dynamic system. Use an S-Function, Subsystem, or Model block to implement systems of equations, but do not use a Simulink Function block, because these conditions can occur:

  • Persistence of state between function calls.If a Simulink Function block contains any blocks with state (for example, Unit Delay or Memory), then their state values are persistent between calls to the function. If there are multiple calls to that function, the state values are also persistent between the calls originating from different callers. For more on this topic, seeCall a Simulink Function Block from Multiple Sites.

  • Inheriting continuous sample time.A Simulink Function block cannot inherit a continuous sample time. Therefore, do not use this block in systems that use continuous sample times to model continuous system equations.

Tracing Simulink Functions

Visually display connections between a Simulink function and their callers with lines that connect callers to functions:

  • Turning on/off tracing lines — From the Simulink Editor menu, selectDisplay>Function Connectors.

  • Direction of tracing lines — Lines connected at the bottom of a block are from a function caller. Lines connected at the top of a block are to a Simulink function or a subsystem containing the function.

  • Navigation to functions — A function caller can be within a subsystem.

    Navigate from a caller in a subsystem to a function by first opening the subsystem, and then clicking a link to the function.

    如果是function is at the root level of a model, the function opens. If the function is within a subsystem, the subsystem containing the function opens.

Monitor Ink Status on a Shared Printer Using Simulink Functions

After selectingDisplay>Function Connectors, the modelslexPrinterExampleshows the relationships between callers and functions.

In this example, the Function Caller in the Simulink Function blockaddprintjob, calls the exported Stateflow functionqueuePrintJob. The subchartBusy调用simulink万博1manbetx功能块打印机墨水. Tracing lines are drawn into and out of the Stateflow chart.

看Also

Blocks

相关话题

这个话题有帮助吗?