当前位置:网站首页>Simulink code generation: simple state machine and its code
Simulink code generation: simple state machine and its code
2022-06-13 03:34:00 【chhttty】
List of articles
This paper studies simple state machine modeling and its generated code .
1 Simulink Medium Chart modular
Chart The module is used to describe the jump relationship between states , It is often used in the process of automotive software development . You need to define the state in the state machine 、 Jump condition between States 、 I / O, local variables, etc . This article will use a simple example to study state machine modeling , And understand the principle through the generated code .
2 State machine modeling
If there is a need now , When the vehicle speed is higher than a certain value , The output variable is the motion state , Below this value, it is at rest . The first reaction to this problem is to use if-else Conditional judgment implementation , But it can also be implemented by state machine modeling . The state machine modeling process will be demonstrated later .
1) First pull out one Chart modular , At this time, there are no input / output interfaces on both sides of it ;
2) Double click in Chart, Open it up Model Explorer, Add input interface as VehicleSpeed, The output interface is MotionState, Respectively, the input signals are vehicle speed information , The output signal is the vehicle motion state ;

here ,Chart The module will be refreshed to have an I / O port ;
3) next , Need to be in Chart Internally defined state 、 State jump condition ;
The explanation is as follows :
- The two rectangular boxes represent Stop and Move Two states , In the upper left corner is the name of the state ;
- In the status box en and du Indicates when entering a state or in a state , Executed statements , Here is the time to enter the state MotionState Assign the corresponding constant ;
- The arrows between states indicate a jump , The statements in square brackets are bar conditions , Here is when the vehicle speed is greater than a certain value Stop Jump to Move, Less than this value from Move Jump meeting Stop;
- Stop The arrow above indicates that the default jump to at the initial time Stop in ;
4) In the last step STOP/MOVE/P_VehStopThres These three variables have not been defined , The state machine doesn't know what it is , So first Matlab Workspace definition ;
Define... In the workspace MOVE and STOP by boolen type , The values are 1 and 0;P_VehStopThres Defined as single type , Values for 0.5, It means that the vehicle speed exceeds 0.5 It is judged to be in motion , On the contrary, it is in a static state ;
next , stay Chart The three variables defined in the module are Parameter Parameters , Inherit Simulink The type of ;
5) Last , Set up Chart The working language in is C Language , The default is Matlab Language ; Check Execute (enter) Chart At Initialization Indicates that the state machine is initialized at zero time ; These two are bloggers' working habits , It can be configured according to your own needs ;
thus ,Chart The modeling of the module is completed
3 Model simulation
stay Chart Add a... Outside the module Signal Builder Input module , One Scope output module .
Signal Builder The vehicle speed is defined in the input module , from 0 To 0.9 Back to 0, The expected result should be a static state first , Re movement state , And then rest .

Run the model , The results are in line with expectations ;
4 Code generation
Replace the input and output of the model with port mouth ,Ctrl + B The generated code .
1) First , In addition to the global variables corresponding to the input and output ports , Another global variable is generated demo_DW, It stores the current status .
This state can take two enumerators :demo_IN_Move and demo_IN_Stop.
2) stay Step Function , Generated a lot of conditional judgment nesting . The basic idea is the same as that in the state machine , Will judge demo_DW, The current state , And state jump conditions CAN_VehicleSpeed, Thus different... Are given in the branch MotionState Value of output .
This code is pretty good to understand , You can have a look and study for yourself .
In this case , Use one... Directly Switch The module judges the vehicle speed , The output status is more convenient , The code will also be simpler . But if the execution statement in the state is more complex , Or the jump between States is more complicated , Or should we use Stateflow State machine for modeling . This not only ensures the readability of the model , Policy modeling in the form of state machine will also greatly reduce the possibility of logic errors .
5 summary
This paper studies the modeling and code generation of state machine . Note the configuration , And global variables representing states in the generated code .
边栏推荐
- Age anxiety? How to view the 35 year old programmer career crisis?
- [JVM series 4] common JVM commands
- C simple understanding - arrays and sets
- Simulink代码生成: 简单状态机及其代码
- 2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)
- Solution of Kitti data set unable to download
- MySQL learning summary 12: system variables, user variables, definition conditions and handlers
- [JVM Series 2] runtime data area
- The latest collation of the number of years of education per capita in the country and provinces -1989-2020- includes the annual original data, calculation process and result summary
- Doris data backup and recovery
猜你喜欢
![[JVM Series 5] JVM tuning instance](/img/29/271fa25a338ee1268f7bce58673e11.jpg)
[JVM Series 5] JVM tuning instance

A data modeling optimization solution for graph data super nodes

Neil eifrem, CEO of neo4j, interprets the chart data platform and leads the development of database in the next decade

(9) Explain broadcasting mechanism in detail

To resolve project conflicts, first-class project managers do so
![[azure data platform] ETL tool (1) -- Introduction to azure data factory](/img/0c/cd054c65aee6db5ae690f104db58a3.jpg)
[azure data platform] ETL tool (1) -- Introduction to azure data factory
![[JVM Series 2] runtime data area](/img/6c/82cc0ff9e14507f57492ed3f67a7f6.jpg)
[JVM Series 2] runtime data area

MASA Auth - SSO与Identity设计

Time processing class in PHP
![[azure data platform] ETL tool (3) - azure data factory copy from local data source to azure](/img/c3/e4b118a378ce8d884163aa1709a7f5.jpg)
[azure data platform] ETL tool (3) - azure data factory copy from local data source to azure
随机推荐
C method parameter: out
China Civil Aviation Statistical Yearbook (1996-2020)
Domestic zynq standalone pl-ps interrupt commissioning
Application scenarios of large arrows in Scala
MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string
2016. maximum difference between incremental elements
[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
MySQL learning summary 7: create and manage databases, create tables, modify tables, and delete tables
PHP uses the header function to download files
Feign based remote service invocation
C method parameter: ref
How to Draw Useful Technical Architecture Diagrams
2000-2019 enterprise registration data of all provinces, cities and counties in China (including longitude and latitude, registration number and other multi indicator information)
Isolation level, unreal read, gap lock, next key lock
This article takes you to learn DDD, basic introduction
Age anxiety? How to view the 35 year old programmer career crisis?
Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
MySQL learning summary 8: addition, deletion and modification of data processing
2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)
C # simple understanding - method overloading and rewriting