Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Reuse Functions with an Atomic Box

What Is an Atomic Box?

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?.

Rationale for Using an Atomic Box

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.

How to Reuse Functions with an Atomic Box

To reuse functions across multiple models:

  1. Create a library model with an atomic box that contains the function you want to reuse.

  2. Create a separate model with multiple charts.

    1. In each chart that calls the function, add a linked atomic box.

    2. Write each call to the function using the full path:

      linked_box_name.function_name

      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

  3. 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.

Example of Reusing a Timer Function Multiple Times

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.

  1. Store the timer function you want to reuse in a library model.

    1. Create a new library named libTimerUtils.

    2. Add a chart named TimerUtils to the library:

    3. 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.

    4. Save libTimerUtils.

  2. Develop a separate model with multiple charts that use the timer function.

    1. Create a new model named ex_timer_function_calls.

    2. Add two charts, Chart1 and Chart2, to the model.

    3. 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.

    4. 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.

    5. 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.

    6. In each chart, add local data with the following properties:

      PropertyValue
      Namet0
      ScopeLocal
      Typedouble

    7. 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.

    8. 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:

    9. Configure your model to meet referencing requirements:

      1. Open the Model Configuration Parameters dialog box and navigate to the Optimization > Signals and Parameters pane.

      2. Select Inline parameters.

      For more information about model referencing requirements, see Configuration Parameter Requirements in the Simulink documentation.

    10. Save ex_timer_function_calls.

  3. Reuse the timer function in multiple referenced blocks of a top model.

    1. Create a new model named ex_modelref_utility_functions.

    2. Add two Model blocks that reference ex_timer_function_calls.

    3. Add four Outport blocks and connect them as follows:

    4. Save ex_modelref_utility_functions.

Was this topic helpful?