Thursday, November 7, 2019

Input / output code generated from Simulink model

When you want to generate code from a Simulink model and drive that code from another C++ file, Simulink has a mechanism that can help you setting model inputs and getting model outputs.

For outputs, in Simulink you right click on the signal, give it a name and select storage class. For inputs that are normally read from Matlab workspace and have to be set from external C++ function, you configure them as inlined parameters:



When you define constants loaded from workspace in Simulink as Model Default/Simulink Global (Auto) or for signals Auto, and if you forget to define init structure in C++, you will get "LNK2001 unresolved external symbol _aa_P", which is a good thing because the compiler won't let you forget initialization.

If you define parameters as extern Global, Simulink sets initial values to the constants in Simulink and if you forget to provide your own initial conditions, C++ will happily run with embedded inits. Additionally, auto puts parameters into a structure while extern global makes them separate global variables which increases probability of naming conflicts.