Sunteți pe pagina 1din 70

Technische Universitt Mnchen

MATLAB 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

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

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

4
Technische Universitt Mnchen

Day 2 Introduction to Simulink, Stateflow and Code Generation


based on slides by Todd Atkins (MathWorks) And on the book MATLAB Simulink Stateflow (Angermann, Beuschel, Rau, Wohlfarth, Oldenburg Verlag)

Outline Day 2 Simulink und Stateflow:


Basics: 1) Simulink Basics Continuous Models Discrete Models Subsystems Signals 2) Stateflow

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

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, MATLAB toolboxes and blocksets

TUM Graduate School Training on MATLAB and Simulink

Simulink Basics What is Simulink?


Accurately design, implement, and test:

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?

Modeling and simulation


Multidomain Dynamic Systems Nonlinear Systems Continuous-time , Discrete-time, 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, SIL Verification and validation

TUM Graduate School Training on MATLAB and Simulink

Simulink Basics Launching Simulink

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

TUM Graduate School Training on MATLAB and Simulink

Simulink Basics Demonstration of Model Elements

13

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

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, Simulink Coder, Embedded Coder

TUM Graduate School Training on MATLAB and Simulink

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, x 0 = 0, 0 = 1, = 10, = 1, = 10

Exercise: Create a mass-spring-damper system in Simulink

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.g.: Gain, Product, Sum/Subtract, Transfer Fcn

16

Recommended solution: use Delay, 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 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

Simulink Continuous Systems Importing Data into Simulink


IN Requires vector of time along with input values input( t, u1un ) 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, u1un ) defined a workspace variable FROM FILE Requires vector of time along with input values input( t, u1un ) defined in a given mat file

22

TUM Graduate School Training on MATLAB and Simulink

Simulink Continuous Systems Exporting Data to MATLAB Workspace


OUT Saves all outputs together in one variable, 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 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 or coder.extrinsic (leads to significantly reduced performance!!!)

25

TUM Graduate School Training on MATLAB and Simulink

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

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, Embedded Coder

TUM Graduate School Training on MATLAB and Simulink

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

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 Coder, Embedded Coder

TUM Graduate School Training on MATLAB and Simulink

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

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, Simulink 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

Simulink Libraries and Model Reference Model Referencing One model in another- parent and referenced model Advantages:
Componentization/Modularization IP protection Multiple referencing Acceleration

38

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

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, Simulink 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

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, Simulink Coder, Embedded Coder

TUM Graduate School Training on MATLAB and Simulink

Stateflow Demo: Flow Graph Soda Machine


Flow Graphs have no action or information in state. Everything is done on transitions. First condition [a>b], then action {c= 0;} Demo: A soda machine provides coke, orange juice and water. The user enters the corresponding number. The machine puts out a can with the drink.

46

TUM Graduate School Training on MATLAB and Simulink

Stateflow Basic Elements


Default Transition Condition Action Transitions

47

Terminating Junction

Junction

TUM Graduate School Training on MATLAB and Simulink

Stateflow Hints
First Condition, then Action!

48

Double Click keeps buttons pressed

TUM Graduate School Training on MATLAB and Simulink

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

Stateflow State Charts


State charts have a internal behavior and internal data Actions can be performed on entry, 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 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

Stateflow Exercise: Elevator


States: InitialState, Stopped, Up, Down Doors may only open when elevator is stopped Inputs: Height, Request Outputs: Command, Doors Doors: close = false, open = true Command: up = 1, down = 2, stop = 3 Height of each floor = 3

55

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

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

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, Simulink 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

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, Simulink Coder, Embedded Coder

TUM Graduate School Training on MATLAB and Simulink

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, by Legacy Code Tool, by S-Function Builder or they can be written by hand (template available) S-Functions always consist of two elements: A .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

Integrating C Code using the Legacy Code Tool Demo: Legacy Code Tool
LCT only creates a wrapper, 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

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, Simulink Coder, Embedded Coder

TUM Graduate School Training on MATLAB and Simulink

MATLAB Coder, Simulink Coder, Embedded Coder MATLAB Coder

67

TUM Graduate School Training on MATLAB and Simulink

MATLAB Coder, Simulink Coder, Embedded Coder Simulink Coder and Embedded Coder

68

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, 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

70

Contact

Contact for further information and feedback about this course:


Dipl.-Ing. Markus Hornauer Institute of Flight Systems Dynamics Boltzmannstr. 15 85748 Garching, Germany Tel: +49 (0)89 289 16047 Fax: +49 (0)89 289 16058 Email: markus.hornauer@tum.de

TUM Graduate School Training on MATLAB and Simulink

S-ar putea să vă placă și