主要内容

在 Stateflow 中调度子系统

此示例说明在 Stateflow® 中调度子系统执行的三种不同方法。此模型涵盖三种不同场景:

  1. 在一个时间步中调度多个子系统

  2. 在一个时间步中多次调度一个子系统

  3. 调度多个子系统在特定时间执行

对于每种场景,您可以使用不同类型的 Stateflow 逻辑来管理子系统的执行。要在一个时间步中调度多个子系统,请使用梯形逻辑。梯形逻辑调度器设计模式允许您指定多个 Simulink® 子系统在单个时间步中的执行顺序。要在一个时间步中多次调度一个子系统,请使用循环逻辑。循环调度器设计构型允许您在一个时间步内多次执行一个 Simulink 子系统。要调度多个子系统在特定时间执行,请使用时序逻辑。时序逻辑调度器设计模式允许您调度 Simulink 子系统在指定时间执行。

在所有三个模型中,Stateflow 调度器扩展了对 Simulink 模型中子系统执行的控制,它基于模块连接和采样时间传播隐式确定执行顺序。

在一个时间步中调度多个子系统

此示例部分显示如何在 Stateflow 中设计梯形逻辑调度器。

open_system("sf_ladder_logic_scheduler")

A model with two charts named Edge to Function and Ladder Logic Scheduler. The Ladder Logic Scheduler chart has two inputs named u1 and u2 and an output named y. Additionally, the Ladder Logic Scheduler chart triggers three subsystems named A1, A2, and A3 with output events.

Ladder Logic Scheduler 图广播一系列函数调用输出事件,以执行三个函数调用子系统(A1A2A3)。在每个时间步中:

  1. Simulink 模型在 1 毫秒脉冲发生器的上升沿激活 Edge to Function 图。

  2. Edge to Function 图广播函数调用输出事件 call 以激活 Ladder Logic Scheduler 图。

  3. Ladder Logic Scheduler 图使用顺序梯形逻辑,根据输入信号 u1u2 的值广播函数调用输出事件。

A chart that only contains junctions. If an input named u1 is greater than 0, the chart triggers the A1 subsystem with an event. If an input named u2 is greater than 1, the chart also triggers the A2 subsystem with an event. Finally, if u2 is greater than 2, the chart also triggers the A3 subsystem with an event.

该图按顺序计算每个条件。当条件有效时,该图调用 send 运算符来广播输出事件。对应的子系统计算其输出并将控制权交还给 Ladder Logic Scheduler 图。

当您对模型进行仿真时,示波器显示每个函数调用子系统的输入和输出。在每个时间步中,Ladder Logic Scheduler 图根据输入信号 u1u2 的值执行子系统:

  1. 如果 u1 为正,该图会发送函数调用输出事件以执行子系统 A1。此子系统将 u1 的值乘以增益 3,并将此值作为输入 u2 传回 Ladder Logic Scheduler 图。控制权返回到 Ladder Logic Scheduler 图中的下一个条件。

  2. 如果 u2 大于 1,该图会发送函数调用输出事件以执行子系统 A2。此子系统将 u2 的值减少 1。控制权返回到 Ladder Logic Scheduler 图中的最后一个条件。

  3. 如果 u2 小于 2,该图会发送函数调用输出事件以执行子系统 A3。此子系统将其输入乘以增益 2

在示波器中,水平段表示子系统未执行的时间步。

在一个时间步中多次调度一个子系统

此示例部分显示如何在 Stateflow 中设计循环调度器。

open_system("sf_loop_scheduler")

A model with two charts named Edge to Function 1 and Loop Scheduler. The Loop Scheduler chart uses output events to trigger a subsystem named A4.

Loop Scheduler 图广播函数调用输出事件,以在每个时间步多次执行函数调用子系统 A1。在每个时间步中:

  1. Simulink 模型在 1 毫秒脉冲发生器的上升沿激活 Edge to Function 图。

  2. Edge to Function 图广播函数调用输出事件 call 以激活 Loop Scheduler 图。

  3. Loop Scheduler 图调用 send 运算符以多次广播函数调用输出事件 A1

The Loop Scheduler chart, which only contains junctions. The junctions form a loop that, on each iteration, sends an event to the A1 subsystem and increments an output named y.

事件 A1 的每次广播都执行子系统 A1。子系统计算其输出,并将控制权交还给 Loop Scheduler 图。

当您仿真模型时,示波器显示画面会在每个时间步显示 y 的值。在每个时间步期间,y 的值增加 25,因为:

  • Loop Scheduler 图中的流程图实现一个进行 10 次迭代的 for 循环。

  • for 循环的每次迭代中,图将 y 递增 1(输入 u1 的常量值)。

  • 每次图向子系统 A1 广播输出事件时,子系统都会将 y 递增 1.5。

A Scope block graph that shows the value of the output named y increasing in a series of steps and plateaus.

调度子系统以在特定时间执行

此示例部分显示如何在 Stateflow 中设计时序逻辑调度器。

open_system("sf_temporal_logic_scheduler")

A model with two charts named Edge to Function 2 and Temporal Logic Scheduler. A 1-millisecond pulse block triggers the Edge to Function 2 block, which in turn uses a function-call input event to trigger the Temporal Logic Scheduler chart

在此示例中,Temporal Logic Scheduler 图包含两个状态,它们调度三个函数调用子系统(A1A2A3)以不同速率执行,具体由时序逻辑运算符 every 确定。

FastScheduler 状态被激活时,该图以输入事件 call 唤醒图的基本速率的分数倍来调度不同 Simulink 子系统的函数调用。

  • 该图以基本速率发送事件来执行子系统 A1

  • 该图以基本速率的一半发送事件来执行子系统 A2

  • 该图以基本速率的四分之一发送事件来执行子系统 A3

SlowScheduler 状态被激活时,该图以基本速率的 1/8、1/16 和 1/32 来调度 A1A2A3 的函数调用。

该图在 call 事件每调用 100 次后,在快速和慢速执行模式之间切换。

当您仿真模型时,示波器显示画面会在每个时间步显示 y 的值。值的变化说明执行速率的不同。

  • 当该图以慢速执行子系统时(例如,从 t=4.5t=4.6,从 t=4.7t=4.8,以及从 t=4.9t=5),值变化缓慢。

  • 当图以快速执行子系统时(例如,从 t=4.6t=4.7 以及从 t=4.8t=4.9),值变化迅速。

A Scope block graph that shows how the output named y changes over time.

另请参阅

主题