Using Simulink signal as a function variable (Real time Control System)
Ältere Kommentare anzeigen
I am working with an existing model of a brushless DC motor in order to simulate a control system I want to use on an actual motor. I have modeled a Fuzzy Logic controller that uses the error and change in error in order to actively tune the Proportional and Integral gains of the PI-controller, which is already built into the motor model I have. My problem is that the model I am using defines the P and I gains as constants within an S-function that runs as part of the simulation, whereas I would like input my tuned gains (which are constantly updating based on the simulation progression) in real-time.
I was originally saving the gain values to variables in the workspace, but these do not update until the end of the sim, and therefore are not helpful. I would like to be able to call these updated gains into the control function of the system in real time. Is this possible to do with the existing model? I realize that the 'best' approach would normally be to re-make my own model and use the gains as input signals in the system. However, as this is merely for simulation purposes, it is not worthwhile for me to spend time doing this - I merely would like it as a proof-of-concept.
Antworten (2)
You need to make sure the P and I gains are Tunable Parameters in the s-function. Based on what you have already written about your issue it sounds like they are not tunable.
7 Kommentare
Will
am 17 Jul. 2012
Ryan G
am 17 Jul. 2012
If the parameters are in fact tunable then you can use something like the following in a MATLAB function/Embedded MATLAB function block:
function y = fcn(u)
%#codegen
coder.extrinsic('assignin')
coder.extrinsic('set_param')
coder.extrinsic('num2str')
assignin('base','A',u);
set_param('untitled/Constant','Value','A')
y = u;
In this example I am updating a Constant block's value with the parameter in the base workspace. The input to the EML block for you would be the P and I gains and you would use set_param on your s-function block.
Ryan G
am 17 Jul. 2012
The first thing you should do is test the functionality with something simple like a constant block. If that works it may be an issue with the tunability of the parameter in the s-function. One way to test this is set the time in the simulation to inf and attempt to open and manually change this parameter. If that does not work it is not tunable.
Ryan G
am 17 Jul. 2012
If you look at the script for the EML I wrote you see I utilize assignin. This updates the base workspace parameter. After that the set_param function is essentially triggering the block, in this case an s-function, to determine that it has updated the parameter and will re-check the workspace for that parameter and update accordingly.
Will
am 17 Jul. 2012
4 Kommentare
Ryan G
am 17 Jul. 2012
I think what you really want is additional input into the s-function for your parameter. If you have control of the s-function you can make these parameters inputs instead of dialog parameters and attach the signals directly. This would greatly improve simulation speed versus updating the Dialog Parameter.
Will
am 17 Jul. 2012
Ryan G
am 17 Jul. 2012
If u is currently your input you can expand the mux with Kp and Ki and call it like:
Kp = u(5);
Ki = u(6);
This would of course depend on how big the input mux is and the position of those two values on the mux.
Will
am 18 Jul. 2012
Kategorien
Mehr zu C2000 Microcontroller Blockset finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!