Unlike many, this post is Simulink centric and deals with the question of global signal data within Simulink models. So first what is “signal data?” Broadly speaking within a Simulink model data elements are broken into parameters (fixed) and signals (things that change). Signals are either calculated or come in from the root level.
Within the model, the signal data is “scoped” to the line it is attached to, or in the case of a Stateflow chart or MATLAB function block, the scope of the chart/function.
Within Simulink, the exception to the rule is the Data Store. With Data Store (read and write blocks) data can be shared in different parts of a model without the use of connecting signal lines. Further, the data stores can be shared with Stateflow Charts and MATLAB functions.
In addition to acting as global data, Data Stores have the unique ability to be written to in multiple locations within a Simulink diagram. Because of this ability, they must be fully defined with the data type, dimensions, and complexity when they are first created.
Global data bad……
Global data is easy to work with, allows you to quickly share information between functions and to reduce interfaces. At the same time, it makes debugging code more difficult (where was X set?) and reduces reusability of code by expanding the dependencies of a function. But… there are times when global data is the correct solution.
When to use global data
So with these downsides when should global data be used? As a general rule of thumb, I advocate for 3 uses
- Error/Fault detection: By their nature error flags can be set by multiple causes. Because of this, the ability to write to an error flag in multiple locations is a valid rationale. Additionally, since the error flags may be needed in multiple places in the model (more so than normal data) the ability to pass this without routing is important.
- Mode data: A system should respond to mode changes all within the same execution step. Like error flags, Mode Data is shared across the full scope of a model.
- Reset flags: Reset flags are used to reset state behavior of integrators and transfer functions.
As a final note, the global property of data in Simulink models should not be confused with the scope of the data in the generated code. The scope of the data in the generated code (for both parameters and signals) can either be determined automatically by Embedded Coder or controlled through Data Objects. This will be covered in a future post.