Presentation on theme: "MATLAB and SimulinkLecture 71 To days Outline Callbacks MATLAB And Simulink S-functions Project suggestions."— Presentation transcript:
MATLAB and SimulinkLecture 71 To days Outline Callbacks MATLAB And Simulink S-functions Project suggestions
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.
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
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’,)
MATLAB and SimulinkLecture 76 MATLAB and Simulink Example: Set the gain value in the model: Mymod.mdl
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?
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.
MATLAB and SimulinkLecture 79 MATLAB and Simulink
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
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)
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
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
MATLAB and SimulinkLecture 714 S-functions mdlDerivatives Return the derivatives for the continuous states. function sys=mdlDerivatives(t,x,u) sys = ; return
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
MATLAB and SimulinkLecture 716 S-functions mdlOutputs Calculate the output function sys=mdlOutputs(t,x,u) sys = ; return
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
MATLAB and SimulinkLecture 718 S-functions mdlTerminate Perform any end of simulation tasks. function sys=mdlTerminate(t,x,u) sys = ; return
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.
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)
MATLAB and SimulinkLecture 721 Exercises on this days topics Work on the second laboration. It’s time to start thinking about a project.
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.
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.
MATLAB and SimulinkLecture 724 Double Pendulum The position of the two masses The differential equation describing the movement
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.
MATLAB and SimulinkLecture 726 DC and AC motors DC Motor AC Motor Change frequancy, magnetic field, view voltage, current and emc.
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
Your consent to our cookies if you continue to use this website.