c++ 代码_用stateflow测试C代码

本文介绍了Stateflow作为Simulink中的一个模块,用于构建基于有限状态机的组合逻辑与时序逻辑模型。有限状态机是一种数学模型,可以解耦模型的状态和状态间的转换条件。Stateflow还支持组合逻辑和时序逻辑,前者输出仅依赖于当前输入,后者则考虑了历史状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

da508b5d792a410ea792deaf6e5f50a1.png

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语言。

a48b330781ed5a3d19d9364dc2000257.png

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

976c5dd14934453d506de6ae2bf6e998.png

e36c53d6010c5cf3087dc1dbac614da9.png

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

8bc725590c10928e4b7b8a7141edad49.png

26e5aa026aa4eaf838af964a4edad2a1.png

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

d805de7daf1a58ad3a87317256895d0c.png

13f5fc890f50e2751647ffdd00d6e2ab.png

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

817bd7c3bf4d203e40f6190dcde015d1.png

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

7ae6f04aa062853db707109f80649b45.png

时序型信号输入构建器

a24bf347c48aafda2b4f77e8e9168eba.png

0400b2942146447013c81274a1cf5eeb.png

ce50eec8369d124bd005fd5d7e6d185c.png

一切准备就序,开会仿真

845d074f408a4a349240d53961c184af.png

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

结束

9b7667b74d1d011cdd55860330a19906.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值