unable to modify Simulink model workspace variable through APP Designer codes when using Simscape-Mechanical blocks in Simulink model

11 views (last 30 days)
  1. I constructed a simple spring model with matlab 2021b simulink by using very basic blocks from Simscape-Mechanical. the model is very simple which I just combined several physical translational springs together in some way, then use a translational velocity source to drive it and the corresponding displacement will generate the force from springs. it really worked. The force value is exactly correct which has been verified byhmy hand calculation in paper. Simscape translational spring blocks used(picture):
  2. After that, I made a brief interface with matlab GUI tool APP Designer. with this interface, I want to modify and override some variables during each run, those variables listed in APP interface are parameters used in previous spring model. All the variables are pre-defined in model workspace to prevent the warning. actually variables are basically some kind of stiffness "k" of used springs, (as we know k*displacement = force). again, like I mentioned, very simple model with correct output, very simple APP interface with only some button pushed callbacks to modify variables used in spring model.
  3. at last, I connect the model together with APP interface by coding in APP Designer, later i even wanna pack them together into a standalonge application for sharing purpose. In order to do that, I read many articles in the community. include: https://ww2.mathworks.cn/help/slcompiler/ug/deploy-a-simulation-with-simulink-compiler.html
Problems came: I am not able to modify the variables in model workspace. Even if I can, the model is not accepting its own model workspace updated variable values. this 2 problems can both cause wrong ploting curve in APP interface.
Some details and my try:
  1. Method 1: Use "assignin" to directly put the variables in base workspace, it was not working, failed. I believe the model workspace priority is higher than base workspace. after this, my direction is guided to how can I modify the model workspace variable itself rather than letting the model go to read base workspace data.
  2. Method 2: Use “ Simulink.SimulationInput('model name')” to create an object “simIn” for my model and configure(override) the model workspace variables by using "simIn = simIn.setVariable('variable name',some Value)", it failed, output is staying with origianl values, meanwhile, I didnt see any update of model workspace variables(maybe not visible?).
  3. Method 3: similar to above, but I used "simIn = simIn.setVariable('variable name',some Value,'workspace','model name')”, it failed.
  4. Method 4: Use “hws = get_param(bdroot, 'modelworkspace')” and then "hws.assignin('variable name',some Value)", I observed that the model workspace variables were updated. it is quite good, although mathworks webpage is not recommending this method. However, the output data is still not reflecting the updated variables.
  5. at last, I tried to modify my model settings, even model blocks themself. Surprisingly! I found once I put variable in Simscape-Physical blocks, the variable will stick alaways permanently there and can not be override by APP designer codes.
  6. Finally, until I use ONLY common signal blocks and defined some variables in them, the APP Designer coding can work and can pass the value to model, then successfully recieving model output.
I’m a little frustrated. Some one please correct me if i draw wrong conclusion. otherwise I have to think: what is the meaning to have those physical blocks? to only use them inside of simulink and only for observation or have one time run?
my call back codes:
cla(app.UIAxes)
simIn = Simulink.SimulationInput('rg01');
simIn = simIn.setModelParameter('StopTime', num2str(app.StopTimeSpinner.Value));
simIn = simIn.setVariable('k1',app.k1.Value,'Workspace','rg01');
simIn = simIn.setVariable('k2',app.k2.Value,'Workspace','rg01');
simIn = simIn.setVariable('k3',app.k3.Value,'Workspace','rg01');
out = sim(simIn);
t = out.y.time;
F = out.y.signals.values;
plot(app.UIAxes,t,F);
  1 Comment
逸龙
逸龙 on 23 Jan 2024
Encountered the same issue as you did. This is killing me.
Could anyone provide alternative ways to manipulate variables inside of Simscape physical blocks from some syntax or APP Designer codes. So I dont have to open simulink evertime and double click those blocks(or model workspace) one by one ...
I doubt, is there such kind of way by this year by 2024?

Sign in to comment.

Accepted Answer

Reid Spence
Reid Spence on 23 Jan 2024
Make sure the parameters you are trying to tune are set to run-time tunable
Simscape paremeters must be run-time tunable inorder to be tuned in Simulink Compiler. Then use setVariable or setBlockParameter on the SimulationInput object.
Some enhancments have been made im more recent releases to make parameter tunability for simscape more visible and provide more diagnostics.
  1 Comment
Jeff
Jeff on 25 Jan 2024
Edited: Jeff on 27 Jan 2024
Hello Reid Spence,
It worked, "Setting Simscape physical block with run-time Parameter" is a correct way for future modification in APP designer. Again, thanks!

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!