
stateflow是一个基于 有限状态机 和 流程图,来构建 组合逻辑 和 时序逻辑 决策模型 并进行 仿真 的环境,该模块位于Simulink库中的 根目录库 中。
1.what is "有限状态机"?
有限状态机(英语:finite-state machine,缩写:FSM)又称有限状态自动状态机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型。
Finite state machine (FSM) is also called finite state automatic state machine (FSM), which is a mathematical model to represent the finite states and the behavior of transition and action between these states.
FSM可以把模型的多状态、多状态间的转换条件解耦。
可以使维护变得容易;
代码更加具有可读性;
更加艺术;
FSM can decouple the multi-state and transition conditions of the model.
Can make maintenance easy;
The code is more readable;
More art;
理解有限状态机首先需要理解整个模型
To understand FSM, we need to understand the whole model first.
FSM关键点:
1.状态
2.动作
3.状态过渡
4.转换事件
5.FSM
FSM key points:
1. Status
2. Action
3. State transition
4. Conversion event
5.FSM
简化:
1.状态(现态-动作,次态-动作)
2.状态过渡(状态转换条件-转换事件)
3.状态机
simplify:
1. State (state action)
2. State transition (state transition transition event)
3. State machine
由状态机管理和驱动“状态”,然后由“状态”和“状态过渡”确定每个状态之间的转换。
The state machine manages and drives the state, and then the state and state transition determine the transition between each state.
2.what is "组合逻辑 时序逻辑"?
组合逻辑电路,在逻辑功能上的特点:是任意时刻的输出仅仅取决于该时刻的输入,与电路原来的状态无关。
时序逻辑电路,在逻辑功能上的特点:是任意时刻的输出不仅取决于当时的输入信号,而且还取决于电路原来的状态,或者说,还与以前的输入有关。
组合逻辑:组合逻辑相当于组合电路,与或非门组成的电路,其输出只与当前状态有关,与其他输入状态的函数无关,不涉及信号调变处理(组合逻辑竞争冒险:只要输入信号同时变化,组合逻辑就必然产生毛刺)。
时序逻辑:其输出不仅仅与当前状态有关,只有在时钟跳变的时候才会变化。
通过设置simulink生成代码语言为C语言,利用Custom Code功能添加需要测试的C代码函数,然后利用stateflow,实现测试代码与simulink的输入,输出的整合。
Import custom code :指定是否解析可用的自定义代码变量和函数,并将自定义代码编译到自己的仿真目标中。此选项影响C调用程序块、MATLAB功能块、MATLAB系统块和Stateflow图表。
Specify whether or not to parse available custom code variables and functions and compile custom code into its own simulation target. This option affects the C Caller block, the MATLAB Function block, the MATLAB System block, and Stateflow® charts.
创建模型:新建空白simulink,添加stateflow模块.
Simulink生成代码类型选择生成C语言。

Simulink生成代码类型选择生成C语言。


来添加要测试的自己写的C代码


stateflow输入输出变量设置,打开设置界面,通过点击stateflow模块内model explorer。


在stateflow模块内添加输入变量赋值,调用被测函数,输出变量赋值。

设置模型运行时间,解算器选择Fixed-step,discrete(no continuous states),Fixed-step size选择自己写的测试代买被调用的周期。

时序型信号输入构建器



一切准备就序,开会仿真

验证 c代码逻辑OK 无语法错误
结束

我的WX zero001___,有问题,欢迎与我交流。