主要内容

本页采用了机器翻译。点击此处可查看英文原文。

仿真车辆头灯管理的异步服务

此示例展示了如何在 System Composer™ 软件架构模型中使用异步服务来仿真车辆前大灯。

在此示例中,模型 HeadlightArch 由一个照明管理器组件 LightingManager、两个前照灯组件 LeftHeadlightRightHeadlight 以及一个记录组件 Logging 组成。前大灯组件被建模为同一引用模型 HeadLight 的两个不同实例。

引用的模型定义了两个 Simulink® 函数:

  • setMode 函数接受 lightMode 变量作为输入,并返回一个输出值,指示前照灯是否损坏。

  • getMode,该函数返回 lightMode 变量。

在照明管理器和两个头灯实例之间指定了一个单一的服务接口,这允许管理器调用 setModegetMode 来获取引用头灯组件的特定实例。

打开模型。

model = systemcomposer.openModel("HeadlightArch");

Architecture model showing LightingManager component connected to three components, LeftHeadlight, RightHeadlight, and Logging.

使用接口编辑器定义异步服务

要查看 接口编辑器,请在工具栏上导航至建模 > 接口编辑器。请注意,代表函数 getModesetMode 的函数元素的异步复选框已选中。

The Interface Editor showing defined interfaces of the HeadlightArch model. The LightInterface interface has two elements, setMode(lightMode) and getMode().

Simulink 行为模型的模块参数已预先配置,以支持异步仿真。

在服务器模型中,选择 Simulink Function 模块中的 Trigger 模块参数异步执行函数调用,用于 setModegetMode。在客户端模型上,选择模块参数 Function Caller 异步执行函数调用

用于车辆前大灯仿真的异步函数调用

对于异步执行,当客户端向服务器发出请求时,服务器根据 函数编辑器 中定义的优先级顺序进行响应,而不是按照接收请求的顺序进行响应。要启动函数编辑器工具,请在工具栏上依次点击建模 > 函数编辑器

使用函数编辑器工具更改函数的执行顺序,以便在同时调用这些函数时,优先级较高的函数先执行。

如果列表中的一个函数调用另一个函数:

  • 如果优先级较低的函数已经在运行,则优先级较高的函数运行。完成后,优先级较低的函数继续运行。

  • 如果已经运行了一个优先级较高的函数,则优先级较低的函数在优先级较高的函数之后运行。

Functions Editor of the HeadlightArch model showing ten functions and their associated software components.

对于异步函数调用,Function Caller 模块具有与输出参量数一致的消息输出端口。此消息输出端口连接到 Message Triggered Subsystem 模块以处理消息。LightingManager 组件引用了由两个异步函数调用组成的 LightingManager Simulink 模型。changelLightMode Function-Call Subsystem 模块使用 setMode 函数,确定每个前大灯应如何更改照明模式。checkLight Function-Call Subsystem 模块使用 getMode 函数检查每个前大灯是否损坏,并返回其状态。

Simulink model showing two function-call subsystems for controlling and checking light status. The top subsystem, labeled changeLightMode, receives inputs Left.setMode and Right.setMode and outputs to LightStatus.left and LightStatus.right through blocks onLeftChange and onRightChange. The bottom subsystem, labeled checkLights, receives inputs Left.getMode and Right.getMode and outputs to LightStatus.leftCheck and LightStatus.rightCheck through blocks onCheckStatusLeft and onCheckStatusRight. Both subsystems include internal signals statusLeft and statusRight.

仿真模型。

sim("HeadlightArch");

使用 仿真数据检查器,您可以在仿真后可视化记录的信号。在工具栏上,依次点击仿真 > 数据检查器

Simulink Data Inspector showing four signal plots: LeftGetModeR (blue, top left), LeftSetModeR (yellow, top right), RightGetModeR (orange, bottom left), and RightSetModeR (purple, bottom right). Each plot displays step changes over a time range of 0 to 10 seconds.

要查看函数调用的执行顺序,在工具栏上,导航至仿真 > 序列查看器,启动 序列查看器。再次仿真模型,查看序列查看器上记录的消息以及消息的执行顺序。由于函数 setMode函数编辑器上的优先级更高,因此这些服务器调用将首先被接收。

Sequence viewer showing function-call interactions between LightingManager and headlight components, including calls like onLeftChange, onRightChange, and setMode.

您可以在函数编辑器中更改函数的优先级顺序,并在序列查看器中查看结果。

另请参阅

模块

函数

主题