Technische Universität MünchenMATLAB and Simulink Basics TUM Graduate School Training Dipl.-Ing. Markus Hornauer Outline Day 1 Introduction to MATLAB Basics: 1) Introduction 2) MATLAB Basics 3) 2D and 3D Plots 4) Data Import and Export Advanced: 1) Programming with MATLAB 2) Graphical User Interfaces in MATLAB 2 Toolboxes: 1) Symbolic Math Toolbox 2) Control System Toolbox and Curve Fitting Toolbox TUM Graduate School Training on MATLAB and Simulink Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 3 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder, Simulink Coder, Embedded Coder TUM Graduate School Training on MATLAB and Simulink Oldenburg Verlag) . Stateflow and Code Generation based on slides by Todd Atkins (MathWorks) And on the book MATLAB – Simulink – Stateflow (Angermann. Rau. Wohlfarth.4 Technische Universität München Day 2 Introduction to Simulink. Beuschel. Simulink Coder.Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 5 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink . MATLAB toolboxes and blocksets 6 TUM Graduate School Training on MATLAB and Simulink .Simulink – Basics What is Simulink? Simulink is a software package for modeling. simulating. and analyzing dynamical systems • Graphical programming language • Block diagram editing • Nonlinear simulation • Hybrid (continuous and discrete) models • Asynchronous (non-uniform sampling) simulation • Fully integrated with MATLAB. and test: 7 • • • • • • Control systems Signal Processing systems Communications systems Embedded systems Physical systems other Dynamic systems TUM Graduate School Training on MATLAB and Simulink .Simulink – Basics What is Simulink? Accurately design. implement. Multi-Rate systems • Plant and Controller Design – – – – Rapidly model what-if scenarios Communicate design ideas Embody performance specifications Select/Optimize control architecture and parameters • Implementation – Automatic code generation – Rapid prototyping for HIL.Simulink – Basics What is Simulink? 8 • Modeling and simulation – Multidomain Dynamic Systems – Nonlinear Systems – Continuous-time . SIL – Verification and validation TUM Graduate School Training on MATLAB and Simulink . Discrete-time. Simulink – Basics Launching Simulink 9 Starting Simulink TUM Graduate School Training on MATLAB and Simulink . Simulink – Basics Simulink Library Browser 10 TUM Graduate School Training on MATLAB and Simulink . Simulink – Basics Finding Blocks 11 TUM Graduate School Training on MATLAB and Simulink . Simulink – Basics Getting Help 1 2 12 3 TUM Graduate School Training on MATLAB and Simulink . Simulink – Basics Demonstration of Model Elements 13 TUM Graduate School Training on MATLAB and Simulink . Embedded Coder TUM Graduate School Training on MATLAB and Simulink . Simulink Coder.Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 14 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. 𝑚 = 10. 𝑏 = 1. 𝑥 0 = 1. x 0 = 0.Simulink – Continuous Systems Modeling Continuous Systems • • • • Engine provides variable-step and fixed-step ODE solvers Block Diagram representation of dynamic systems Blocks define governing equations Signals are propagated between blocks over time 15 • Remeber MuPad: 𝑚𝑥 + b𝑥 + 𝑘𝑥 = 0. 𝑘 = 10 • Exercise: Create a mass-spring-damper system in Simulink TUM Graduate School Training on MATLAB and Simulink . : Gain. Sum/Subtract.g. Integrator or other history related block • Alternative. but bad solution: reduce diagnostics settings and leave solving up to Simulink engine (not recommended!!) TUM Graduate School Training on MATLAB and Simulink .Simulink – Continuous Systems Algebraic Loops • Error because of Direct Feedthrough – – Block input depends directly on its own output E. Product. Transfer Fcn 16 • Recommended solution: use Delay. Simulink – Continuous Systems Solver 17 • Solver? – – Determines solution at current time step Determines the next simulation time step • Solver options: Fixed-Step Ode1 Ode2 Ode3 Ode4 Ode5 Ode8 TUM Graduate School Training on MATLAB and Simulink Variable-Step Ode45 Ode23 Ode113 Ode15s Ode23s Ode23t Ode23tb . Simulink – Continuous Systems Fixed-step Solver 18 TUM Graduate School Training on MATLAB and Simulink Simulink – Continuous Systems Variable-step Solver 19 TUM Graduate School Training on MATLAB and Simulink Simulink – Continuous Systems Variable-step Size 20 mx _ step _ size t stop t start time _ steps Max step size is the largest time step that the solver can take (‘auto’ means 50 steps) TUM Graduate School Training on MATLAB and Simulink Simulink – Continuous Systems Demo: Importing and Exporting Data 21 TUM Graduate School Training on MATLAB and Simulink . u1…un ) defined a workspace variable FROM FILE • Requires vector of time along with input values input( t. u1…un ) defined configuration parameters CONSTANT • Changeable on the highest hierarchy level • Tunable with parameter objects FROM WORKSPACE • Requires vector of time along with input values input( t. u1…un ) defined in a given mat file 22 TUM Graduate School Training on MATLAB and Simulink .Simulink – Continuous Systems Importing Data into Simulink IN • Requires vector of time along with input values input( t. defined in the configuration parameters TO WORKSPACE • Saves the output data in a variable to the workspace. defined in the block parameters TO FILE • Saves the output data in a .mat file 23 TUM Graduate School Training on MATLAB and Simulink .Simulink – Continuous Systems Exporting Data to MATLAB Workspace OUT • Saves all outputs together in one variable. Simulink – Continuous Systems Exchanging Data with MATLAB Workspace For IN and OUT blocks variables must be defined in Configuration Parameters 24 TUM Graduate School Training on MATLAB and Simulink . Simulink – Continuous Systems MATLAB Embedded • • Subset of MATLAB for code generation Can be used for direct generation of source code out of MATLAB as well as in Simulink MATLAB Function blocks Enables user to reuse his MATLAB code in Simulink To call unsupported functions use eml.extrinsic (leads to significantly reduced performance!!!) 25 • • TUM Graduate School Training on MATLAB and Simulink .extrinsic or coder. Simulink – Continuous Systems Model and Block Callbacks 26 TUM Graduate School Training on MATLAB and Simulink . Simulink – Continuous Systems Callback Functions Common tasks you can achieve by using callback functions include: • Loading variables into the MATLAB workspace automatically when you open your Simulink model • Executing a MATLAB script by double-clicking on a block • Executing a series of commands before starting a simulation • Executing commands when a block diagram is closed Load Initialize Start Continue Stop Close 27 Help => Callback Functions TUM Graduate School Training on MATLAB and Simulink . Embedded Coder TUM Graduate School Training on MATLAB and Simulink .Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 28 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Simulink Coder. Simulink – Discrete Systems Demo: Creating a counter 29 Exercise: Create a Stop Watch • Combine counters to a stop watch • Show tenth seconds. seconds. minutes and hours • Reduce simulation speed to soft realtime TUM Graduate School Training on MATLAB and Simulink . Simulink – Discrete Systems Multirate Systems • Systems with signals that are sampled at different rates Use for discrete or hybrid systems To connect system use rate transition blocks Specify specific sampling rate by variable at each in and out port 30 • • • • Different sample times need to be an integer multiple of the highest (global) sampling rate • Sample Time Colors -> fastest discrete sampling time is displayed in red TUM Graduate School Training on MATLAB and Simulink . Embedded Coder TUM Graduate School Training on MATLAB and Simulink . Simulink Coder.Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 31 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Simulink – Subsystems Subsystems • Why? – – – Reduce blocks displayed in a model window Keep functionally related block together Establish hierarchical block diagram 32 TUM Graduate School Training on MATLAB and Simulink . Simulink – Subsystems Creating Subsystems 33 • • • Context menu -> Create Subsystem Subsystem ports Inside a subsystem TUM Graduate School Training on MATLAB and Simulink . Simulink – Subsystems Atomic Subsystems • • • • Represent nonvirtual systems within another system Have their own sampling rate Have their own code generating characteristics Have their own execution order number 34 TUM Graduate School Training on MATLAB and Simulink . Simulink – Subsystems Masking Subsystems • • Mask .Encapsulation with a UI Provides – Mask icon display – Block description – Parameter dialog prompt – Custom block help text mask masking 35 TUM Graduate School Training on MATLAB and Simulink . Simulink Coder.Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink 2) Stateflow 36 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink . Simulink – Libraries and Model Reference User Libraries • Collection of reusable blocks • Prototype block vs Reference block • Propagation of changes to Library – Discard – Push 37 • Library Links – Disable link – Restore link – Break link • Other features – Display in Simulink Library Browser – Add documentation TUM Graduate School Training on MATLAB and Simulink . parent and referenced model • Advantages: – Componentization/Modularization – IP protection – Multiple referencing – Acceleration 38 TUM Graduate School Training on MATLAB and Simulink .Simulink – Libraries and Model Reference Model Referencing • One model in another. Simulink Coder.Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 39 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink . Simulink – Signals Vectors • • • Matrix and Vector operations possible Mux block to compose vector Demux block to extract from signal 40 • Increase simulation performance TUM Graduate School Training on MATLAB and Simulink . Simulink – Signals Busses • • • Graphical grouping of signals to a hierarchical bus signal Bus creators to create a bus from signal and busses Bus selector to select single signals or whole sub-busses 41 • Bus Objects can be specified TUM Graduate School Training on MATLAB and Simulink . Simulink – Signals Simulink Model Explorer 42 TUM Graduate School Training on MATLAB and Simulink . Simulink – Signals Simulink Data Objects • Simulink Parameter Object >> Var = Simulink.Parameter 43 • Simulink Signal Object >> Var = Simulink.Signal • Simulink Bus Object – Use Bus editor TUM Graduate School Training on MATLAB and Simulink . Simulink – Signals Signal Logging 44 Configuration Parameters Signal Context Menu TUM Graduate School Training on MATLAB and Simulink . Simulink Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink .Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 45 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. then action {c= 0.Stateflow Demo: Flow Graph Soda Machine • Flow Graphs have no action or information in state. Everything is done on transitions. The machine puts out a can with the drink. First condition [a>b]. 46 • • TUM Graduate School Training on MATLAB and Simulink .} Demo: A soda machine provides coke. orange juice and water. The user enters the corresponding number. Stateflow Basic Elements Default Transition Condition Action Transitions 47 Terminating Junction Junction TUM Graduate School Training on MATLAB and Simulink . then Action! 48 • Double Click keeps buttons pressed TUM Graduate School Training on MATLAB and Simulink .Stateflow Hints • First Condition. Stateflow Defining Chart Data 49 TUM Graduate School Training on MATLAB and Simulink . Stateflow Guidelines for Creating Flow Charts 50 • The execution has only one entry point! • The execution has only one termination point! • The execution can always reach the termination point! • The flow never backtracks! TUM Graduate School Training on MATLAB and Simulink . during residence in the state and on exit A state can perform a self transition A state can be either active or passive 51 • • • TUM Graduate School Training on MATLAB and Simulink .Stateflow State Charts • State charts have a internal behavior and internal data Actions can be performed on entry. Stateflow Demo: Autopilot Mode Control 52 TUM Graduate School Training on MATLAB and Simulink . Stateflow Execution Order 53 true executes false does not execute TUM Graduate School Training on MATLAB and Simulink . Stateflow Parallel Charts and Hierarchical Charts 54 TUM Graduate School Training on MATLAB and Simulink . stop = 3 Height of each floor = 3 55 TUM Graduate School Training on MATLAB and Simulink . Request Outputs: Command. open = true Command: up = 1. Doors Doors: close = false. down = 2.Stateflow Exercise: Elevator • • • • • • • States: InitialState. Down Doors may only open when elevator is stopped Inputs: Height. Stopped. Up. Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 56 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Simulink Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink . Modeling Guidelines Increasing Model Quality 57 Help => Simulink => Modeling Guidelines TUM Graduate School Training on MATLAB and Simulink . Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 58 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Simulink Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink . Model Advisor Simulink Model Advisor 59 TUM Graduate School Training on MATLAB and Simulink . Simulink Coder.Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 60 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink . Report Generator and Model Comparison Automatic Report Generation 61 TUM Graduate School Training on MATLAB and Simulink . Report Generator and Model Comparison Compare XML Files 62 TUM Graduate School Training on MATLAB and Simulink . Simulink Coder.Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 63 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink . by S-Function Builder or they can be written by hand (template available) S-Functions always consist of two elements: A .Integrating C Code using the Legacy Code Tool Introducing S-Functions • S-Functions are used for: – Hiding information about a models content (IPR) – Speeding up simulation – Integrating external functions written in C 64 • S-Functions can be created by Block Context Menu.mexw32 file containing the compiled model A S-Function Block calling the .mexw32 file In most cases S-Function blocks are masked to increase usability • • • • TUM Graduate School Training on MATLAB and Simulink . by Legacy Code Tool. which will be removed at code generation Simple way to integrate C code in Simulink In MATLAB use ceval to integrate code 65 • • Help => „Integrating Existing C Functions into Simulink Models with the Legacy Code Tool” TUM Graduate School Training on MATLAB and Simulink .Integrating C Code using the Legacy Code Tool Demo: Legacy Code Tool • LCT only creates a wrapper. Simulink Coder. Embedded Coder TUM Graduate School Training on MATLAB and Simulink .Outline Day 2 Simulink und Stateflow: Basics: 1) Simulink • Basics • Continuous Models • Discrete Models • Subsystems • Signals 2) Stateflow 66 Advanced: 1) Libraries and Model Reference 2) Style Guidelines 3) Model Advisor 4) Report Generator and Model Comparison 5) Integrating C Code using the Legacy Code Tool 6) MATLAB Coder. Embedded Coder MATLAB Coder 67 TUM Graduate School Training on MATLAB and Simulink . Simulink Coder.MATLAB Coder. MATLAB Coder. Simulink Coder. Embedded Coder Simulink Coder and Embedded Coder 68 TUM Graduate School Training on MATLAB and Simulink . Simulink is a graphical programming language for embedded systems Simulink interacts with real hardware for Hardware In The Loop or Procesoor in the Loop setups. as well as for test beds and laboratory setups TUM Graduate School Training on MATLAB and Simulink .69 Summary • • • • • Simulink is a graphical modeling environment based on MATLAB Simulink is fully integrated in MATLAB environment Simulink can be used to model continuous. discrete and hybrid sytsems In addition. 15 85748 Garching. Markus Hornauer Institute of Flight Systems Dynamics Boltzmannstr.-Ing.70 Contact Contact for further information and feedback about this course: Dipl. Germany Tel: +49 (0)89 289 16047 Fax: +49 (0)89 289 16058 Email: markus.de TUM Graduate School Training on MATLAB and Simulink .hornauer@tum.