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

codecoderembeddedEmbedded Codergetsetoptimization

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?

Best Answer

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.