1 / 27

To days Outline

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

oliana
Download Presentation

To days Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. To days Outline • Callbacks • MATLAB And Simulink • S-functions • Project suggestions 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. Lecture 7

  3. Callbacks Lecture 7

  4. 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 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_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’,) Lecture 7

  6. MATLAB and Simulink Example: • Set the gain value in the model: Mymod.mdl Lecture 7

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

  8. 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. Lecture 7

  9. 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 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) 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 Lecture 7

  13. 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 Lecture 7

  14. S-functions mdlDerivatives • Return the derivatives for the continuous states. function sys=mdlDerivatives(t,x,u) sys = []; return Lecture 7

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

  16. S-functions mdlOutputs • Calculate the output function sys=mdlOutputs(t,x,u) sys = []; return Lecture 7

  17. S-functionsmdlGetTimeOfNextVarHit • 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 Lecture 7

  18. S-functionsmdlTerminate • Perform any end of simulation tasks. function sys=mdlTerminate(t,x,u) sys = []; return Lecture 7

  19. 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. 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) Lecture 7

  21. Exercises on this days topics • Work on the second laboration. • It’s time to start thinking about a project. 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. Lecture 7

  23. 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. Lecture 7

  24. Double Pendulum The position of the two masses The differential equation describing the movement Lecture 7

  25. 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. Lecture 7

  26. DC and AC motors • DC Motor • AC Motor • Change frequancy, magnetic field, view voltage, current and emc. 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 Lecture 7

More Related