To days Outline

advertisement
To days Outline




Callbacks
MATLAB And Simulink
S-functions
Project suggestions
MATLAB and Simulink
Lecture 7
1
Callbacks

A callback is a MATLAB command that executes
when a certain event occurs.



Opening a model
Double-click, moving etc of Simulink blocks
Callback are installed using the MATLAB
command Set_param(object, parameter, value)



object: A MATLAB string containing the name of the model
or a path to the block
parameter: A MATLAB string containing the name given to
the event of interest.
value: A MATLAB string containing the callback.
MATLAB and Simulink
Lecture 7
2
Callbacks
MATLAB and Simulink
Lecture 7
3
Callbacks
Example
 Create a simple model in Simulink that
contains




a gain
a sine wave
a scope.
When a user tries to run the model he
should be prompted for a gain value
MATLAB and Simulink
Lecture 7
4
MATLAB and Simulink



When running a simulink model we have access to
variables in the MATLAB workspace
What if we want to set variables from a function
All parameter values in the different simulink blocks
can be set with the command: set_param
Set_param(’Object’, ’parameter’, value’)

All parameter values in the different simulink blocks
can be viewed with the command: get_param
get_param(’Object’, ’parameter’,)
MATLAB and Simulink
Lecture 7
5
MATLAB and Simulink
Example:
 Set the gain value in
the model:
Mymod.mdl
MATLAB and Simulink
Lecture 7
6
MATLAB and Simulink


How can we set
parameters in a
closed model?
What if the model
has several levels
of subsystems?
MATLAB and Simulink
Lecture 7
7
MATLAB and Simulink
Example:
 Create a Graphical user interface and a
simulink model of the double tank system.
 A user should be able to set process
parameters:




Bottom area
Area of the bottom hole
Pump constant.
The user should also be able to set gain
values in the controller.
MATLAB and Simulink
Lecture 7
8
MATLAB and Simulink
MATLAB and Simulink
Lecture 7
9
S-functions


Allows us to define custom Simulink
blocks
S-function represents a general
Simulink block with:



input vector u
output vector y
state vector x


Continuous states xc
Discrete states xd
MATLAB and Simulink
Lecture 7
10
S-functions

We must define:






S-function must compute the output


Initial values of each state
Define the size of the state vector
Define the size of the output vector
Define the size of the input vector
Set sample time if there is a discrete model.
y=g(x,u,t,p)
Update the discrete states


xd(k+1)=fd(x,u,t,p)
Compute the derivatives

xc’=fc(x,u,t,p)
MATLAB and Simulink
Lecture 7
11
S-functions / m-file
sfunc_name(t,x,u,flag,p1..pn)
function [sys,X0,str,ts]=sfunc_name(t,x,u,flag,p1..pn)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
MATLAB and Simulink
Lecture 7
12
S-functions mdlInitializeSizes
Return the sizes, initial conditions, and sample times for the Sfunction.
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs
= 0;
sizes.NumInputs
= 0;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
x0 = []; % initialize the initial conditions
str = []; % str is always an empty matrix
ts = [0 0]; % initialize the array of sample times
return

MATLAB and Simulink
Lecture 7
13
S-functions mdlDerivatives

Return the derivatives for the continuous states.
function sys=mdlDerivatives(t,x,u)
sys = [];
return
MATLAB and Simulink
Lecture 7
14
S-functions mdlUpdate
Handle discrete state updates, sample time hits, and major time
step
function sys=mdlUpdate(t,x,u)
sys = [];
return

MATLAB and Simulink
Lecture 7
15
S-functions mdlOutputs
Calculate the output
function sys=mdlOutputs(t,x,u)
sys = [];
return

MATLAB and Simulink
Lecture 7
16
S-functions mdlGetTimeOfNextVarHit
Return the time of the next hit for this block.

Note that the result is absolute time.

Note that this function is only used when you specify
 variable discrete-time sample time [-2 0] in the sample time
array in mdlInitializeSizes.
 Example, set the next hit to be one second later.
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; sys = t + sampleTime;
return

MATLAB and Simulink
Lecture 7
17
S-functions mdlTerminate
Perform any end of simulation tasks.
function sys=mdlTerminate(t,x,u)
sys = [];
return

MATLAB and Simulink
Lecture 7
18
S-functions
Example:


Create a continuous s-function
that describes the double tank
system.
Initial condition



Upper 0.1m
Lower 0.2m
Output should be the

difference between the two
tanks.
MATLAB and Simulink
Lecture 7
19
S-functions

We must define:





Initial values of each state
Define the size of the state vector
Define the size of the output vector
Define the size of the input vector
Compute the derivatives


xc’=fc(x,u,t,p)
S-function must compute the output

y=g(x,u,t,p)
MATLAB and Simulink
Lecture 7
20
Exercises on this days topics


Work on the second laboration.
It’s time to start thinking about a
project.
MATLAB and Simulink
Lecture 7
21
Project suggestions






Design of a Water Clock
Double Pendulum
DC and AC motors
Two Salty Tanks
Animate a bouncing ball
Search the internet and library for
interesting projects.
MATLAB and Simulink
Lecture 7
22
Design of a Water Clock
A 12-hour water clock is to be
designed with the dimensions
shown in the sketch. The shape
of the clock is obtained by
revolving the curve y = f(x)
around the y axis. Define the
shape function, f(x), and the
radius of the circular hole at the
bottom that gives a constant
water level decrease of 4 in/hr.
MATLAB and Simulink
Lecture 7
23
Double Pendulum
The position of the two masses
x1  l1  sin(1)
y1  l1  cos(1)
x2  l1  sin(1)  l2  sin( 2 )
y2  l1  cos(1)  l2  cos( 2 )
The differential equation describing the movement
0  (m1  m2 )l11  m2l22 cos(1  2 )  m2l222 sin(1  2 )  g (m1  m2 ) sin(1)
0  m2l22  m2l11 cos(1  2 )  m2l112 sin(1  2 )  gm2 sin(2 )
MATLAB and Simulink
Lecture 7
24
Two Salty Tanks
Consider the cascade of two tanks as
shown in the figure. Assume that the
volumetric flow rate throughout the
system is constant with q = 5 gal/s.
With a constant flow rate the volumes
of both Tank 1 and Tank 2 are also
constant with V1 = 100 gal and V2 =
200 gal. If the inlet to Tank 1 is pure
water and the initial masses of the salt
dissolved in the tanks are m1 = m2 =
50 lbm, determine the amount of salt
in each tank versus time. Also
determine the time and magnitude of
the mass in Tank 2 when m2 is at its
highest value.
MATLAB and Simulink
Lecture 7
25
DC and AC motors


DC Motor
AC Motor

Change frequancy, magnetic field, view
voltage, current and emc.
MATLAB and Simulink
Lecture 7
26
Animate a bouncing ball


Throw or drop an elastic ball with some
initial velocity and angel
Simulate how the ball will bounce

A user should be able to set different
parameters as



Elastic constant
Initial angle
Initial velocity
MATLAB and Simulink
Lecture 7
27
Download