Skip to content

mathworks/connect-simulations-to-an-external-environment

Repository files navigation

This example shows two methods to connect a deployed simulation with an external environment using callbacks. Restricting the callbacks to only run at communication steps reduces overhead and improves performance.

alt text

The examples require R2023a (or newer) version the following products:

  • MATLAB®
  • Simulink®
  • MATLAB Compiler™
  • Simulink Compiler™

Method 1

The first method uses PostSteFcn callback to get the latest simulation results (response) and update parameter values (stimuli). to use this method, the model should be setup as shown below, with stimuli coming into the model as parameter updates. When the value of the parameter, p, in the Constant block changes, its value is picked up by 'The Algorithm', block.

alt text

Method 2

The second method uses ExternalInputsFcn to get the latest simulation results (response) and specify new input signal values (stimuli). For this method the model should be setup as shown below, with the stimuli coming into the model as an input signal through the root inport block, In1.

alt text

Performance Comparison

The first method is easier to implement (and understand) but limits the stimuli to be constant between communication steps. The second method is more powerful because the input signal between communication steps can be a time series.

The plot below shows that as communication interval is increased, simulation pace increases (i.e., performace improves) due to the reduced callback overhead.

alt text

About

Connect simulations to an external environment using Simulink Compiler

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages