|On this page…|
An atomic box behaves in the same way as a regular Stateflow® box but with a few differences:
You can reuse an atomic box across multiple charts and models.
An atomic box cannot contain states, only functions (graphical, truth table, MATLAB, and Simulink).
Use an atomic box to reuse functions in the same way that you use an atomic subchart to reuse states. For more information about atomic subcharts, see What Is an Atomic Subchart?.
Suppose that you have a library model that contains a set of functions for use in multiple charts in a model. The functions reside in the library model to enable easier configuration management.
Models that use these functions can appear as referenced blocks in a top model. However, when the functions are exported functions of a Stateflow chart, you can use only one instance of that referenced block per top model. For a complete list of model referencing limitations, see Limitations on All Model Referencing in the Simulink® documentation.
With atomic boxes, you can avoid the limitation due to exported functions. You can reuse models with these functions multiple times as referenced blocks in a top model.
To reuse functions across multiple models:
Create a library model with an atomic box that contains the function you want to reuse.
Create a separate model with multiple charts.
In each chart that calls the function, add a linked atomic box.
Write each call to the function using the full path:
Using the full path for the function call has the following advantages:
Makes clear the dependency on the function in the linked atomic box
Avoids pollution of the global namespace
Does not affect efficiency of the generated code
Reuse that model multiple times as referenced blocks in a top model.
Because there are no exported functions in the charts, you can use more than one instance of that referenced block in the top model.
Suppose that you want to reuse a timer function that returns the simulation time. The following procedure shows how you can:
Call the timer function from multiple locations in a model.
Reuse that model multiple times in another model.
Store the timer function you want to reuse in a library model.
Create a new library named libTimerUtils.
Add a chart named TimerUtils to the library:
In your chart, add the following graphical function:
The function GetTime returns one output tout that corresponds to simulation time t. For more information about literal symbols you can use in your chart, see Supported Symbols in Actions.
Develop a separate model with multiple charts that use the timer function.
Create a new model named ex_timer_function_calls.
Add two charts, Chart1 and Chart2, to the model.
In each chart, add two states, two transitions, and a linked atomic box:
To add the linked atomic box, copy the TimerUtils library chart and paste it below state A. Name the linked atomic box as Time.
When you copy and paste a library chart that contains only functions and no states, you get a linked atomic box. If the library chart contains any states, you get a linked atomic subchart.
In Chart1, add the following state action and transition condition:
Upon entry to state A, the call to GetTime returns the simulation time. The transition from state A to B occurs when more than 5 seconds of simulation time passes.
In Chart2, add the following state action and transition condition:
Upon entry to state A, the call to GetTime returns the simulation time. The transition from state A to B occurs when more than 7 seconds of simulation time passes.
In each chart, add local data with the following properties:
In each chart, open the State properties dialog box for B and select Create Output port for monitoring:. Click OK.
This step adds an output data named B that is Boolean. The value is 1 when state B is active and 0 otherwise. For more information, see About Active State Output.
In your model, add two Outport blocks, Out1 and Out2. Then connect each block to the corresponding output of each chart.
Your model should look something like this:
Configure your model to meet referencing requirements:
Open the Model Configuration Parameters dialog box and navigate to the Optimization > Signals and Parameters pane.
Select Inline parameters.
For more information about model referencing requirements, see Configuration Parameter Requirements in the Simulink documentation.
Reuse the timer function in multiple referenced blocks of a top model.
Create a new model named ex_modelref_utility_functions.
Add two Model blocks that reference ex_timer_function_calls.
Add four Outport blocks and connect them as follows: