Functions in Stateflow(Stateflow中的函数)
Stateflow supports the creation of various types of functions that can be called multiple times within a chart. As with textual programming, using functions reduces redundancy and improves readability. In this course, you will learn about two types of Stateflow functions: graphical functions and MATLAB functions. You can learn about other types of functions by browsing the documentation page, Reusable Functions in Charts. Functions can be called as state actions and condition actions. If a function returns a Boolean value, it can also be called as a transition condition.
(Stateflow支持创建各种类型的函数,这些函数可以在图表中多次调用。与文本编程一样,使用函数可以减少冗余并提高可读性。在本课程中,您将了解两种类型的Stateflow函数:图形函数和MATLAB函数。您可以通过浏览文档页面“图表中的可重用函数”来了解其他类型的函数。函数可以称为状态操作和条件操作。如果函数返回布尔值,也可以将其称为转换条件。)
Graphical Functions(图形功能)
Graphical functions are reusable chart elements that contain flow charts. As you saw previously, a flow chart can be inserted directly into a Stateflow chart. However, you can reuse this logic by encapsulating it into a graphical function. To add a graphical function, use the left mouse button to click and drag from the icon on the object palette (fx). This will create a function container for you to name and populate.
(图形函数是包含流程图的可重用图表元素。正如您之前看到的,流程图可以直接插入到状态流程图中。但是,您可以通过将此逻辑封装到图形函数中来重用它。要添加图形功能,请使用鼠标左键单击对象调色板上的图标并进行拖动(fx)。这将创建一个函数容器,供您命名和填充。)
MATLAB Functions(MATLAB函数)
It can also be helpful to incorporate textual MATLAB functions into a Stateflow chart. For example, it may be easier to express parts of your algorithm as code, or you may have preexisting MATLAB code that you wish to utilize in your Stateflow chart. To create a MATLAB function box in Stateflow, click and drag from the icon on the object palette .
(将文本MATLAB函数合并到状态流程图中也很有帮助。例如,将算法的部分表示为代码可能更容易,或者您可能有预先存在的MATLAB代码,希望在状态流程图中使用。要在Stateflow中创建MATLAB函数框,请单击对象选项板上的图标并进行拖动。)
Graphical Functions in Stateflow(Stateflow中的图形函数)
TASK1(任务1)
At right is a state machine that sorts a stream of characters into 5- and 6- letter words, which are printed on separate displays(右边是一个状态机,它将字符流分类为5个字母和6个字母的单词,这些单词分别打印在不同的显示器上)
The chart has two inputs: a stream of characters and a second stream with the associated display for each character. If the character stream contains a period (.), the display pauses.
(图表有两个输入:一个是字符流,另一个是每个字符的关联显示流。如果字符流包含句点(.),则显示将暂停。)
To display the messages, the chart needs to increment the index being displayed, and wrap when the index exceeds the word length. You modeled this architecture in the Flow Charts lesson. Here, you will turn the flow chart into a graphical function and use it in both Display states.
(要显示消息,图表需要增加显示的索引,并在索引超过单词长度时换行。您在“流程图”课程中对此体系结构进行了建模。在这里,您将把流程图变成一个图形函数,并在两种显示状态下使用它。)
Create a graphical function box by dragging from the graphical function icon on the object palette.Define the function signature as(通过从对象调色板上的图形功能图标拖动来创建图形功能框。将功能签名定义为)
idx = increment(currIdx,wordLength).
(You can use the shortcut Ctrl+Shift+A to automatically arrange your model and resize elements to fit text.)(可以使用快捷键Ctrl+Shift+A自动排列模型并调整元素大小以适应文本。)
TASK2(任务2)
Graphical functions have their own variable scope, just like functions in other programming contexts. Information is exchanged with the Stateflow chart via the graphical function’s inputs and outputs. The Data Scope is automatically defined for you based on the function signature.
(图形函数有自己的可变范围,就像其他编程环境中的函数一样。通过图形功能的输入和输出与状态流程图交换信息。数据范围是根据函数签名自动为您定义的。)
Your graphical function will contain the same flow chart that you created previously. However, now you will use the function Symbols: idx, currIdx, and wordLength.
(您的图形函数将包含与您之前创建的流程图相同的流程图。但是,现在您将使用函数Symbols:idx、currIdx和wordLength。)
Delete the automatically added transition and junction. Click on the graphical function box to make it active, then use the Pattern Wizard, as before, to insert an if statement in the graphical function.
(删除自动添加的过渡和连接。单击图形功能框使其处于活动状态,然后像以前一样使用模式向导在图形功能中插入if语句。)
Fill out the fields in the dialog box as follows.(按如下方式填写对话框中的字段。)
Description increment and loop(描述增量和循环)
If condition idx > wordLength(如果条件idx>wordLength)
If action idx = 1;(如果动作idx=1;)
Press Ok to insert the pattern. Adjust the size and positioning of the flow chart and the box so the flow chart is completely within the graphical function box.
(按“确定”插入图案。调整流程图和方框的大小和位置,使流程图完全位于图形功能框内。)
TASK3(任务3)
Add the action {idx = currIdx + 1;} on the graphical function’s default transition, prior to the if statement.
TASK4(任务4)
Replace the %TODO comment in the DisplayShort state with a call to the graphical function. The inputs to the function are idx1 and the word length, 5. Assign the output of the function back to the variable idx1.
(将DisplayShort状态下的**%TODO** 注释替换为对图形函数的调用。该函数的输入为idx1,字长为5。将函数的输出重新分配给变量idx1。)
TASK5(任务5)
Replace the %TODO comment in the DisplayLong state with a similar call to the graphical function. In this state,you would like to increment the variable idx2, and the word length is now 6.
(将DisplayLong状态下的**%TODO** 注释替换为对图形函数的类似调用。在这种状态下,您希望增加变量idx2,现在单词长度为6。)
MATLAB Functions in Stateflow(Stateflow中的MATLAB函数)
TASK1(任务1)
In this lesson, you will use a Stateflow chart with a MATLAB function to detect outliers in a noisy signal. The Simulink model provides the last five data points of the signal to the chart.
(在本课中,您将使用带有MATLAB函数的状态流程图来检测噪声信号中的异常值。Simulink模型将信号的最后五个数据点提供给图表。)
Your criteria for identifying an outlier will be: “Does the current point differ from the mean of the last five points by more than 2?”
(您识别异常值的标准是:“当前点与最后五个点的平均值相差2以上吗?”)
Create a new MATLAB function in your chart by using the left mouse button to drag from the icon on the left toolbar.
(使用鼠标左键从左侧工具栏上的图标拖动,在图表中创建一个新的MATLAB函数。)
Define the function signature as
isOutlier = detectOutlier(input).
TASK2(任务2)
The MATLAB code to identify outliers is(识别异常值的MATLAB代码是)
dist = abs(input(end) - mean(input));
isOutlier = dist > 2;
Double-click on the MATLAB Function in the chart to edit in the MATLAB Editor. The function signature is populated from the chart. Enter the algorithm in the body of the function.
(双击图表中的MATLAB函数,在MATLAB编辑器中进行编辑。函数签名是从图表中填充的。在函数体中输入算法。)
TASK3(任务3)
The transition from NotOutlier to Outlier should occur if detectOutlier(data) retums true.
(如果detectOutlier(data)返回true,则应发生从NotOutlier到Outlier的转换。)
Conversely, the transition from Outlier to NotOutlier should occur if detectOutlier returns false.
(相反,如果detectOutlier返回false,则应该发生从Outlier到NotOutlier的转换。)
Use calls to the MATLAB function to create the transition conditions between the two states.
(使用对MATLAB函数的调用来创建两种状态之间的转换条件。)
版权归原作者 疯狂的麦克斯韦 所有, 如有侵权,请联系我们删除。