Model initialization inputs vs RTW C code

Hello,
I have a model in Simulink and several constants of the model (e.g. gains) are computed in a dedicated initialization m-file. The computed values are based on a few values that should be tunable. Now a user can tune them using workspace variables. Then the m-file computes the desired constants and the model can be started.
The problem is I need to covert this model (including the initialization) into a corresponding C code using RTW. I know how to set a variable of a model to be tunable in a resulting C code, but I really don’t know, whether there is a way to make a variable of initialization m-file tunable.
Is there any other solution than to remove the m-file and put all its computations directly into the model? (That’s exactly the thing I’d like to avoid since it would mean lots of changes and thus lots of possible mistakes.)
Thank you very much!
BR, Ladi

4 Comments

Do the (tunable) constants in your model refer back to workspace variables, or do you directly perform a set_param to set the gain values? If it is the former, you should simply have to configure them as tunable parameters
The former is correct and the constants are tunable parameters – the constants are computed in the initialization m-file before the model is being run and I need to convert the whole thing (model + init m-file) into a C code. The problem is, the m-file computes the constants from several user defined variables. I need the resulting C code to enable the same initialization – compute the constants from user defined variables (e.g. global C variables). And it is not possible to make variables of an m-file tunable. I’m sorry if this is not completely clear, but it’s a bit difficult to describe :)
Even if you are defining the variables in the MATLAB-file, when you update the model (Ctrl+D), aren't these variables created in the base workspace? If yes, these should show up in the "Referenced workspace variables" list when you attempt to configure tunable parameters using the "Configure" button next to the "Inline parameters" checkbox in the Configuration Parameters window. Do you not see them?
Well, let's say that the init m-file computes a gain K based on two user defined constans X and Y (e.g. something like this: K = 2*X + six(Y)). K can be seen in the "Referenced workspace variables", because it's used directly in a simulink gain block. X and Y cannot be seen in this list (at least in 2007b), because they are not directly referenced in the model.

Sign in to comment.

Answers (1)

Let's say you have K1 and K2, two tunable constants for the gain value of two Gain blocks. You have K3=ComplexFunction(K1,K2) as the gain value for a third Gain block. You K3 value is updated manually every time K1 or K2 is changed for simulation.
I don't see a solution without putting ComplexFunction(K1,K2) in the model. Check out the "Embedded MATLAB Function" block. Maybe you can just copy your m-file over and it is supported by RTW.

5 Comments

Thanks for your answer. The main reason I don't want to put the ComplexFunction in the model is I want it to be called only once - in the beginning to initialize the gain. So I need something like parametrical constructor of the model.
If you want the calculation of ComplexFunction be executed only once, that is fine. There are ways to make it execute only once in Simulink as well as in C code. Let's say you've made your C code compiled and running, when K1 or K2 is changed (any time for calibration purpose, not just for initialization), does K2 need to be changed accordingly?
You wrote: "does K2 need to be changed accordingly?" Do you really mean K2, or it is a typo and you mean K3 - i.e. the result of the ComplexFunction? In any case, when K1 changes during the simulation, there is no need to change K2 nor K3.
You are right. I meant "K3".
Think of this way, if you were developing your algorithm by writing C code manually, the initialization of K3 needs to be written even though it is executed only once. Right now, your m-file could be completely separated from your model. You set the value of K1 and K2, run your m-file to calculate the value of K3 and then run your model file. I don't think you can expect that RTW can include your m-file.
Again, check out the "Embedded Matlab Function". It might be able to solve your problem.
OK. I'll give it a shot... Thank you!

Sign in to comment.

Categories

Products

Asked:

on 20 May 2011

Community Treasure Hunt

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

Start Hunting!