主要内容

使用状态流图设计人机接口逻辑

此示例显示了如何在独立stateflow®图表中建模图形用户界面的逻辑。独立图表以MATLAB®作为动作语言实现经典图表语义。您可以使用MATLAB的完整功能来对图表进行编程,包括在Simulink®中限制代码生成的功能。万博1manbetx有关更多信息,请参阅Create Stateflow Charts for Execution as MATLAB Objects

您可以通过调用其输入事件并使用临时操作员来执行独立的状态流图。事件和计时器驱动的执行工作流程适合设计逻辑人机接口(HMIS)和图形用户界面(UIS)。

  • 当您使用MATLAB应用程序设计器时,从接口窗口小部件中调用了图表中的事件。

  • 在里面Stateflow chart, temporal operators and local data control the properties of the user interface.

For more information on how to use MATLAB to create graphical user interfaces, seeDevelop Apps Using App Designer

控制应用程序设计师用户界面

该用户界面包含一个控制灯的开关开关。当开关处于ON位置时,灯以固体或闪烁的两种模式之一亮起,具体取决于模式选项按钮的位置。您可以通过移动眨眼率滑块来控制眨眼的速度。要启动应用程序,在应用程序设计器工具条中,请单击Run

The filesf_lamp_logic.sfxdefines a standalone Stateflow chart that implements the logic for the user interface. The chart has input events (,OFF,BLINKING, 和SOLID) and local data (delayapp). The actions in the chart control which widgets are accessible from each state. For instance, the actions in theOffstate cause the Lamp widget, the Mode option buttons, and the Blink Rate slider in the user interface to appear dimmed.

在里面state, the substatesSolid眨眼denote the two modes of operation. To implement a blinking lamp, the chart relies on the temporal logic operatorafter。When the chart enters the state眨眼。Off, the expressionafter(delay,sec)在传出过渡上,会创建一个MATLAB计时器对象,该对象在数秒后执行图表。然后图表过渡到状态眨眼并创建另一个计时器对象,以触发过渡回到眨眼。Off。虽然图表在两种状态之间不断过渡,但您可以通过调用输入事件来更改本地数据延迟或闪烁模式的过渡来调整闪烁的速率SOLID或者OFF

历史交界处状态保留有关最近活动的替代的信息,以便在打开灯时,用户界面返回前面的操作模式。

Execute Standalone Chart by Using Events

You can execute the standalone chart by calling its input event functions in the MATLAB Command Window. The Stateflow Editor shows the effects of each of these commands by highlighting active states and transitions through chart animation.

1. Create the chart objectL并初始化的值delay至0.5。该值对应于每秒一个闪光灯的闪烁速率(持续0.5秒,关闭0.5秒)。

L = sf_lamp_logic('delay',0.5);

2. Turn on the lamp.

上(L)

3. Switch to blinking mode.

眨眼(L)

4. Set the value ofdelay至0.25。该值对应于每秒两次闪光的闪烁速率(持续0.25秒,关闭0.25秒)。

L.delay = 0.25;

5. Switch to solid mode.

固体(L)

6. Turn off the lamp.

OFF(L)

7. Delete the chart objectL从MATLAB工作区。

delete(L)

Connect Standalone Chart to User Interface

要在用户界面和独立状态流图之间建立双向连接,请打开App Designer窗口并选择Code View

1. In the App Designer window, create a private propertylampLogicto store the handle to the Stateflow chart object.

属性(Access =私有)Lamplogicend

2. Create astartupfcncallback function that creates the chart object and sets its local dataappto the user interface handle. Assign the chart object handle to thelampLogicprivate property.

% Code that executes after component creation功能startupfcn(app) app.lampLogic = sf_lamp_logic('delay',0.5,'app',app);end

3. Create aCloseRequestFcncallback function that deletes the chart object when you close the user interface.

% Close request function: UIFigure功能uifigurecloserequest(app,event)delete(app.lamplogic);删除(app);end

4. For each one of the user interface widgets, add a callback function that invokes the appropriate event on the standalone chart.

  • ValueChangedFcncallback function for Switch widget:

功能SwitchValueChanged(app,event) value = app.Switch.Value;转变lower(value)case'off'OFF(app.lampLogic);case'on'上(app.lampLogic);endend
  • SelectionChangedFcncallback function for Mode Button widget:

功能ModeButtonGroupSelectionChanged(app,event) selectedButton = app.ModeButtonGroup.SelectedObject;ifapp.SolidButton == selectedButton SOLID(app.lampLogic);else眨眼(app.lamplogic);endend
  • ValueChangedFcncallback function for Blink Rate Slider widget:

功能blinkrateslidervaluechanged(app,event)app.lamplogic.delay = round(0.5/app.blinkrateslider.value,2);end

运行用户界面时,您可以观察到在图表画布和灯具上调整控制小部件的效果。

See Also

相关话题