Príkazom set_param sa nastavujú parametre simulácie modelu a jeho blokov.
set_param('model', 'parameter1', hodnota1, 'parameter2', hodnota2, ...)
kde 'model' je názov modelu a 'parameter1', 'parameter2' ... sú parametre a 'hodnota1', 'hodnota2' ... sú hodnoty zodpovedajúcich parametrov.
Príkaz set_param je možné použiť na spustenie, zastavenie, prerušenie/pokračovanie alebo zmenu parametrov simulácie. Jeho syntax bude vysvetlená na príklade nastavenia vlastnosti SimulationCommand:
set_param('sys', 'SimulationCommand', 'cmd')
kde 'sys' je názov modelu a 'cmd' je 'start', 'stop', 'pause', 'continue' alebo 'update'.
Naopak, príkazom get_param je možné skontrolovať stav simulácie.
get_param('sys', 'SimulationStatus')
Simulink vráti: 'stopped', 'initializing', 'running', 'paused', 'updating', 'terminating' a 'external' (Real-Time Workshop).
>> set_param('sys', 'SimulationCommand', 'start') >> set_param('sys', 'SimulationCommand', 'pause') >> get_param('sys', 'SimulationStatus') ans = paused >> set_param('sys', 'SimulationCommand', 'continue') >> get_param('sys', 'SimulationStatus') ans = running >> set_param('sys', 'SimulationCommand', 'stop') >> get_param('sys', 'SimulationStatus') ans = stopped
>> set_param('sys', 'Solver', 'ode15s', 'StopTime', '3000')
>> set_param('sys/Zosilnenie', 'Gain', '1000')
>> set_param('sys/Zero-Pole','Zeros','[2 4]','Poles','[1 2 3]')
% otvorenie modelu open_system('pidreg'); % nastavenie parametrov PID regulátora a prenosovej funkcie set_param('pidreg/PID Controller','P','1'); set_param('pidreg/PID Controller','I','0.5'); set_param('pidreg/Transfer Fcn','Numerator','[2]'); set_param('pidreg/Transfer Fcn','Denominator','[1 3 1]'); % spustenie simulácie [t,x] = sim('pidreg'); % iné nastavenie parametrov PID regulátora a prenosovej funkcie set_param('pidreg/PID Controller','P','0.5'); set_param('pidreg/PID Controller','I','2'); set_param('pidreg/Transfer Fcn','Numerator','[4 1]'); set_param('pidreg/Transfer Fcn','Denominator','[1 4 1]'); % spustenie simulácie [t1,x1] = sim('pidreg'); % zatvorenie všetkých modelov bdclose('all'); % vykreslenie plot(t,x(:,2),t1,x1(:,2))
Obr. Model pidreg.mdl
Kód modelu pidreg.mdl. Výsledný graf.
Callback je funkcia, ktorá sa spustí, ak užívateľ vykoná určitú akciu, napr. otvorí alebo zatvorí model, spustí simuláciu, vytvorí alebo vymaže blok v modeli.
Callback funkcie rozdelujeme na funkcie pre
Funkcia | Opis |
---|---|
CloseFcn | Before the block diagram is closed |
PostLoadFcn | After the model is loaded. Defining a callback routine for this parameter might be useful for generating an interface that requires that the model has already been loaded |
InitFcn | Called at start of model simulation |
PostSaveFcn | After the model is saved |
PreLoadFcn | Before the model is loaded. Defining a callback routine for this parameter might be useful for loading variables used by the model |
PreSaveFcn | Before the model is saved |
StartFcn | Before the simulation starts |
StopFcn | After the simulation stops. Output is written to workspace variables and files before the StopFcn is executed |
Použitie Callback funkcií pre model si ukážeme na príklade. Majme model ap11.mdl (obr. 7), ktorý nemá nastavené prenosy (LTI objekty) G1, G2, G3. V prípade, že otvoríme model ap11.mdl skôr ako definujeme prenosy G1, G2, G3, zobrazí sa chybové hlásenie (obr. 8). Bloky G1, G2, G3 majú zobrazené ???, pretože nie sú definované.
Obr. 7. Model ap11.mdl bez použitia Callback funkcií
Obr. 8. Chybové hlásenie
Aby sme sa vyhli takýmto hláseniam, môžeme použiť Callback funkciu, ktorá nám nastaví parametre pred otvorením modelu. V tomto prípade parametre nastavíme pomocou súboru ap11m.m, ktorý musí byť spustený pred otvorením modelu. Po uzatvorení modelu sa vypíše správa "koniec predstavenia", ktorá sa nachádza v súbore ap11mend.m.
G1=tf(1,1); G2=tf(1,1); G3=tf(1,1);
disp('koniec predstavenia')
Aby sme mohli uvedené nastavenia aplikovať, musíme nastaviť parametre (callback funkcie) modelu ap11.mdl príkazom set_param.
set_param('ap11n', 'PreLoadFcn', 'ap11m') set_param('ap11n', 'CloseFcn', 'ap11mend')
Výsledkom je model ap11n.mdl (kód, obr. 9)
Obr. 9. Model ap11n.mdl s použitím Callback funkcií
Funkcia | Opis |
---|---|
CloseFcn | When the block is closed using the close_system command |
CopyFcn | After a block is copied. The callback is recursive for Subsystem blocks (that is, if you copy a Subsystem block that contains a block for which the CopyFcn parameter is defined, the routine is also executed). The routine is also executed if an add_block command is used to copy the block |
DeleteFcn | Before a block is deleted. This callback is recursive for Subsystem blocks |
DestroyFcn | When the block has been destroyed |
InitFcn | Before the block diagram is compiled and before block parameters are evaluated |
LoadFcn | After the block diagram is loaded. This callback is recursive for Subsystem blocks |
ModelCloseFcn | Before the block diagram is closed. This callback is recursive for Subsystem blocks |
MoveFcn | When the block is moved or resized |
NameChangeFcn | After a block’s name and/or path changes. When a Subsystem block’s path is changed, it recursively calls this function for all blocks it contains after calling its own NameChangeFcn routine |
OpenFcn | When the block is opened. This parameter is generally used with Subsystem blocks. The routine is executed when you double-click the block or when an open_system command is called with the block as an argument. The OpenFcn parameter overrides the normal behavior associated with opening a block, which is to display the block’s dialog box or to open the subsystem |
ParentCloseFcn | Before closing a subsystem containing the block or when the block is made part of a new subsystem using the new_system command (see new_system in the “Model Creation Commands” section of the Simulink online help) |
PreSaveFcn | Before the block diagram is saved. This callback is recursive for Subsystem blocks |
PostSaveFcn | After the block diagram is saved. This callback is recursive for Subsystem blocks |
StartFcn | After the block diagram is compiled and before the simulation starts. In the case of an S-Function block, StartFcn executes immediately before the first execution of the block’s mdlProcessParameters function. See “S-Function Callback Methods” in Writing S-Functions for more information |
StopFcn | At any termination of the simulation. In the case of an S-Function block, StopFcn executes after the block’s mdlTerminate function executes. See “S-Function Callback Methods” in Writing S-Functions for more information |
UndoDeleteFcn | When a block delete is undone |
Majme nový model sys.mdl a m-súbor start.m, ktorý obsahuje definície premenných num a dem, ktoré sme použili v bloku prenosovej funkcie (Transfer function) modelu sys.mdl.
num = 1 den = [10 1] pocet = 0
Chceme, aby model pri otvorení načítal údaje zo súbora start.m
>> open_system('sys')
>> set_param('sys', 'PreLoadFcn', 'start') % uložiť model >> bdclose('sys') >> open_system('sys') num = 1 den = 10 1 pocet = 0
Vytvorme si m-súbor, ktorý bude vypisovať text, v ktorom sa bude meniť číslo zodpovedajúce počtu spustení súbora, t.j. koľkokrát sa presunie blok Scope v modeli sys.mdl.
pocet = pocet + 1; sprintf('Blok bol posunutý %d. krát',pocet)
>> get_param('sys/Scope','position') ans = 160 34 190 66 >> set_param('sys/Scope','MoveFcn','move')
ans = Blok bol posunutý 1. krát >> get_param('sys/Scope','position') ans = 160 54 190 86
ans = Blok bol posunutý 2. krát >> get_param('sys/Scope','position') ans = 175 54 205 86
Poznámka:
Niektoré Callback funkcie (Model pre-load, Model initialization, Simulation start, Simulation stop, Model pre-save a Model close) môžu byť nastavené pomcou menu File » Model properties » Callback.