27
MATLAB and Simul ink Lecture 7 1 To days Outline Callbacks MATLAB And Simulink S-functions Project suggestions

To days Outline

  • Upload
    oliana

  • View
    17

  • Download
    0

Embed Size (px)

DESCRIPTION

To days Outline. Callbacks MATLAB And Simulink S-functions Project suggestions. Callbacks. A callback is a MATLAB command that executes when a certain event occurs. Opening a model Double-click, moving etc of Simulink blocks - PowerPoint PPT Presentation

Citation preview

Page 1: To days Outline

MATLAB and Simulink

Lecture 7 1

To days Outline Callbacks MATLAB And Simulink S-functions Project suggestions

Page 2: To days Outline

MATLAB and Simulink

Lecture 7 2

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.

Page 3: To days Outline

MATLAB and Simulink

Lecture 7 3

Callbacks

Page 4: To days Outline

MATLAB and Simulink

Lecture 7 4

CallbacksExample 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

Page 5: To days Outline

MATLAB and Simulink

Lecture 7 5

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_paramSet_param(’Object’, ’parameter’, value’)

All parameter values in the different simulink blocks can be viewed with the command: get_param

get_param(’Object’, ’parameter’,)

Page 6: To days Outline

MATLAB and Simulink

Lecture 7 6

MATLAB and Simulink

Example: Set the gain value

in the model:Mymod.mdl

Page 7: To days Outline

MATLAB and Simulink

Lecture 7 7

MATLAB and Simulink

How can we set parameters in a closed model?

What if the model has several levels of subsystems?

Page 8: To days Outline

MATLAB and Simulink

Lecture 7 8

MATLAB and SimulinkExample: 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.

Page 9: To days Outline

MATLAB and Simulink

Lecture 7 9

MATLAB and Simulink

Page 10: To days Outline

MATLAB and Simulink

Lecture 7 10

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

Page 11: To days Outline

MATLAB and Simulink

Lecture 7 11

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 Set sample time if there is a discrete model.

S-function must compute the output 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)

Page 12: To days Outline

MATLAB and Simulink

Lecture 7 12

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

Page 13: To days Outline

MATLAB and Simulink

Lecture 7 13

S-functions mdlInitializeSizes Return the sizes, initial conditions, and sample times for the S-

function.function [sys,x0,str,ts]=mdlInitializeSizessizes = 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

neededsys = simsizes(sizes);x0 = []; % initialize the initial conditionsstr = []; % str is always an empty matrixts = [0 0]; % initialize the array of sample timesreturn

Page 14: To days Outline

MATLAB and Simulink

Lecture 7 14

S-functions mdlDerivatives Return the derivatives for the continuous states.

function sys=mdlDerivatives(t,x,u)sys = [];return

Page 15: To days Outline

MATLAB and Simulink

Lecture 7 15

S-functions mdlUpdate Handle discrete state updates, sample time hits, and

major time stepfunction sys=mdlUpdate(t,x,u)sys = [];return

Page 16: To days Outline

MATLAB and Simulink

Lecture 7 16

S-functions mdlOutputs Calculate the outputfunction sys=mdlOutputs(t,x,u)sys = [];return

Page 17: To days Outline

MATLAB and Simulink

Lecture 7 17

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

Page 18: To days Outline

MATLAB and Simulink

Lecture 7 18

S-functions mdlTerminate Perform any end of simulation tasks.function sys=mdlTerminate(t,x,u)sys = [];return

Page 19: To days Outline

MATLAB and Simulink

Lecture 7 19

S-functions

Example: Create a continuous s-

function that describes the double tanksystem.

Initial condition Upper 0.1m Lower 0.2m

Output should be the difference between the

two tanks.

Page 20: To days Outline

MATLAB and Simulink

Lecture 7 20

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)

Page 21: To days Outline

MATLAB and Simulink

Lecture 7 21

Exercises on this days topics Work on the second laboration. It’s time to start thinking about a

project.

Page 22: To days Outline

MATLAB and Simulink

Lecture 7 22

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.

Page 23: To days Outline

MATLAB and Simulink

Lecture 7 23

Design of a Water ClockA 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.

Page 24: To days Outline

MATLAB and Simulink

Lecture 7 24

Double Pendulum

)2cos(2)1cos(12

)2sin(2)1sin(12

)1cos(11

)1sin(11

llyllx

lylx

)2sin(2)21sin(2112)21cos(1122220

)1sin()21()21sin(2222)21cos(22211)21(0

gmlmlmlm

mmglmlmlmm

The position of the two masses

The differential equation describing the movement

Page 25: To days Outline

MATLAB and Simulink

Lecture 7 25

Two Salty TanksConsider 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.

Page 26: To days Outline

MATLAB and Simulink

Lecture 7 26

DC and AC motors DC Motor AC Motor

Change frequancy, magnetic field, view voltage, current and emc.

Page 27: To days Outline

MATLAB and Simulink

Lecture 7 27

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