Download presentation

Presentation is loading. Please wait.

Published byMia miah Swallow Modified about 1 year ago

1
MATLAB and SimulinkLecture 71 To days Outline Callbacks MATLAB And Simulink S-functions Project suggestions

2
MATLAB and SimulinkLecture 72 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.

3
MATLAB and SimulinkLecture 73 Callbacks

4
MATLAB and SimulinkLecture 74 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

5
MATLAB and SimulinkLecture 75 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’,)

6
MATLAB and SimulinkLecture 76 MATLAB and Simulink Example: Set the gain value in the model: Mymod.mdl

7
MATLAB and SimulinkLecture 77 MATLAB and Simulink How can we set parameters in a closed model? What if the model has several levels of subsystems?

8
MATLAB and SimulinkLecture 78 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.

9
MATLAB and SimulinkLecture 79 MATLAB and Simulink

10
Lecture 710 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

11
MATLAB and SimulinkLecture 711 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)

12
MATLAB and SimulinkLecture 712 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

13
MATLAB and SimulinkLecture 713 S-functions mdlInitializeSizes Return the sizes, initial conditions, and sample times for the S- function. 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

14
MATLAB and SimulinkLecture 714 S-functions mdlDerivatives Return the derivatives for the continuous states. function sys=mdlDerivatives(t,x,u) sys = []; return

15
MATLAB and SimulinkLecture 715 S-functions mdlUpdate Handle discrete state updates, sample time hits, and major time step function sys=mdlUpdate(t,x,u) sys = []; return

16
MATLAB and SimulinkLecture 716 S-functions mdlOutputs Calculate the output function sys=mdlOutputs(t,x,u) sys = []; return

17
MATLAB and SimulinkLecture 717 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

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

19
MATLAB and SimulinkLecture 719 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.

20
MATLAB and SimulinkLecture 720 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)

21
MATLAB and SimulinkLecture 721 Exercises on this days topics Work on the second laboration. It’s time to start thinking about a project.

22
MATLAB and SimulinkLecture 722 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.

23
MATLAB and SimulinkLecture 723 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.

24
MATLAB and SimulinkLecture 724 Double Pendulum The position of the two masses The differential equation describing the movement

25
MATLAB and SimulinkLecture 725 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.

26
MATLAB and SimulinkLecture 726 DC and AC motors DC Motor AC Motor Change frequancy, magnetic field, view voltage, current and emc.

27
MATLAB and SimulinkLecture 727 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google