How to reduce calls to get function of Simulink.Signal object when using the GetSet storage class

1 view (last 30 days)
I'm using the GetSet storage class in my model on a signal which is of type Simulink.Signal. This signal is a control input that is fed to a Discrete PID Controller block. When I proceed to build the model, I observe the following code:
void feedback_control_step(void)
{
/* First calculation */
a= f(get_control_input())
/* Second calculation */
b= f(get_control_input())
/* Third calculation */
c= f(get_control_input())
}
This means that if the step function execution is interrupted, and then resumed, I might end up with different values of the input "control_input". What I want is to have the following behavior:
void feedback_control_step(void)
{
local_control_input = get_control_input();
/* First calculation */
a= f(local_control_input)
/* Second calculation */
b= f(local_control_input)
/* Third calculation */
c= f(local_control_input)
}
How do I achieve this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 Dec 2018
Making a simple change to the model will bring about the desired behavior in the code. To illustrate the change, please find attached a modified version of the model 'feedback_control.slx' and the 'pid' data dictionary. I've summarized the changes below:
  • Insert Signal conversion block in between the inport and the PID block. In the signal conversion block, select the Signal Copy option and check 'Exclude this block from 'Block reduction' optimization'.
  • For the output signal from the signal conversion block, resolve it to a Simulink.Signal object with the storage class set to ExportedGlobal.
When you generate code for the modified model, you will observe that 'get_control_signal' is called only once and its output is assigned to a local variable inside the step function.

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!