ExecOmlScript

This block executes a user-defined command or script in OpenMatrix Language.

    ExecOmlScript

Library

Activate/CustomBlocks

Description

This block runs an OML script. The script is executed in the base environment interactively through the block GUI, but is not active during the simulation. The execution of the script follows the execution of the Model Initialization script and the corresponding diagram Context scripts following the usual Super Block scoping rules.

Unlike the Initialization script, which is executed at every simulation, the OML script in ExecOMLScript is executed on demand. Such scripts can also be executed directly in OML prior to running simulations. The advantage of using a ExecOMLScript block is that the model is self contained (the script is included in the model).

The main usage is to allow users to initialize the environment, for example by running a script to linearize a plant and construct a controller before running a simulation to validate the controller.

Parameters

ExecOmlScript_0

NameLabelDescriptionData TypeValid Values

script

OML Script

The OML script to be executed using the Execute button

String

Example

1. Controller design based on linearization

The demo model 'Invers pendulum lin execOML.scm' (Demo browser/Activate/Custom blocks/ExecScript) contains the classical inverted pendulum on a cart system with a linear controller modeled by a state-space block. The controller (At, Bt, Ct, Dt) matrices are defined by the structure _syslin, if defined in the base environment. Otherwise they are defined so that the associated transfer function is zero (so no control).

This is done in the top diagram Context script as follows:

_syslin=GetFromBase('_syslin',[]);
if isempty(_syslin)
  At=[];
  Bt=zeros(0,2);
  Ct=zeros(1,0);
  Dt=zeros(1,2);
else
  At=_syslin.a;
  Bt=_syslin.b;
  Ct=_syslin.c;
  Dt=_syslin.d;
end

The construction of the _syslin structure is done by linearizing the Super Block containing the model of the cart-pendulum system and using the resulting linear system to construct an observer-based controller based on pole placement. This is done by the OML script associated with the ExecOMLScript block. By executing this script, the controller is computed, placed in the _syslin structure and saved in the base environment (the OML script runs in the base environment).

clear('_syslin');
% vector of input port indices considered for linearization
inps = 1;
% vector of output port indices considered for linearization
outs = [1,2];
% Modified context
ctx=struct; ctx.z0=0; ctx.th0=0; ctx.phi=0;

model=bdeGetCurrentModel;
% Selected Super block to linearize
superblock = 'pendulum';

[A,B,C,D]=vssLinearizeSuperBlock(model,superblock,inps,outs,0,ctx);
n=size(A,1);
Kc=-place(A,B,-1*ones(1,n));
Kf=-place(A',C',-2*ones(1,n)); Kf=Kf';

At=A+B*Kc+Kf*C+Kf*D*Kc; Bt=-Kf; Ct=Kc;

_syslin.a=At; _syslin.b=Bt; _syslin.c=Ct; _syslin.d=[];

If the model is simulated without running the script, the cart pendulum system is simulated without control and as expected the pendulum is not stabilized. On the other hand, if the script is run before, the pendulum is stabilized.

2. Controller design based on optimization

The demo model 'Car min time opt execOML.scm' (Demo browser/Activate/Custom blocks/ExecScript) contains the model of a vehicle. The objective is to find the best gear ratios so that the vehicle can travel 1 Km in least amount of time. The optimization is performed over the gear ratios gear and the gear changing times T.

The script performing the optimization is placed inside an ExecOMLScript block. The script finds the optimal values of gear and T. They are used during simulation is already computed, otherwise default values are used. This is done in the Initialization script of the model as follows:

T = GetFromBase('T',4*ones(4,1));
gear = GetFromBase('gear',[3.2;1.8;1.3;1;.8]);

3. Connecting a documentation

A document like a PDF can be connected to the model using vssOpenDocument('PathToDocument') as script, see demo model 'Open PDF execOML.scm' (Demo browser/Activate/Custom blocks/ExecScript).

vssOpenDocument('PathToDocument')

If the PDF is in the same path as the model you can use

f = [fileparts(bdeGetModelFilePath(bdeGetCurrentModel)),'/'];
vssOpenDocument([f, 'Document.pdf']);

Diagram

ExecOmlScript

See Also