Top Banner
Embedded Systems MATLAB Tutorial Hans-J ¨ org Peter October 28 th , 2008
22

Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Feb 15, 2019

Download

Documents

duongkiet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Embedded SystemsMATLAB Tutorial

Hans-Jorg Peter

October 28th, 2008

Page 2: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Assignments and Tutorials

AssignmentsHandout / return: Thursdays (before the lecture)Teams are allowed (at most 3 students / team)Box will be available

First assignmentHandout: Today afternoon, online availableReturn: By Monday, 8:00

TutorialsMonday, 16:00 - 18:00, SR 015 / E1.3Wednesday, 14:00 - 16:00, SR 5 (215) / E2.4Friday, 10:00 - 12:00, SR 015 / E1.3

Page 3: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

MATLAB - Matrix Laboratory

Produced by The MathworksUsed for simulation and numerical computationNo (Maple-like) symbolical solvingStandard tool for developing embedded systems

Page 4: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

MATLAB Structure

MATLAB core: IDE for the MATLAB languageSimulink: Graphical environment for continuous simulationStateflow: Statecharts for SimulinkMany other add-ons available...

Page 5: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Numerical Computing

Some problems cannot be solved preciselyApproximative numerical solutionsSimulation of the physical world

Page 6: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Starting MATLAB

1 http://sunray1.studcs.uni-sb.de

2 Log in3 Click on MatLab

alternatively:1 Log onto a cip, bio, or sunray workstation2 ssh -Y appsrv{1,2}.studcs.uni-sb.de

3 Execute /usr/local/matlab/bin/matlab

Page 7: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

MATLAB IDE

1

2

3

4

1 Current directory2 Workspace3 Command history4 Command window

Page 8: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

The MATLAB Language

Simplified C-like syntaxCase sensitiveInteractive shell: command windowUser defined functions: m-filesMany built-in commands:

lookfor <keyword>help <function>sprintf (<format str>, v1, v2, ...)disp (<object>)plot (Y)plot (X, Y)...

Page 9: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Variables

Each numerical variable is a matrixScalars = 1× 1 matricesNo explicit declarations / dynamic typingPolymorphismRemoving variables:

clear <variable>clear

Page 10: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Working with Matrices

a = 4

b = [4 8 15 ; 16 23 42 ; 1 2 3]

c = b’

d = 0:10

e = 0:0.01:2*pi

f = ones(4)

g = eye(3)

h = b*b

i = b.*b

Page 11: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Script Files

So called m-filesMust be located in

the current directory orthe global search path

Can be executed from the command windowCan also define functions

Page 12: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Control Structures

Conditionalif <cond><statements>

[else<statements>]

end

While loopwhile <cond><statements>

end

For loopfor v = <from>:[<step>:]<to><statements>

end

Page 13: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Example: Computing π

Monte Carlo method for computing π

points insidepoints total

≈ π

4

Page 14: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Simulink

Page 15: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Harmonic Oscillator

m = mass constantk = spring constanty0 = initial displacementy = current displacementv = y = current velocity

my + ky = 0⇔ mv + ky = 0

Page 16: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Harmonic Oscillator in Simulink

Page 17: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Damped Harmonic Oscillator

m = mass constantR = damper constantk = spring constanty0 = initial displacementy = current displacementv = y = current velocity

my + Ry + ky = 0⇔ mv + Rv + ky = 0

Page 18: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Damped Harmonic Oscillator in Simulink

Page 19: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Stateflow

Page 20: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Semantics: Statemate vs. Stateflow

Standard (Statemate)Any finite number ofactive events.

Emitted events arecollected and thenpassed to the entire chart.

StateflowAt most one activeevent.

Emitted events areimmediately passed tothe receiver.

Page 21: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Semantics: Statemate vs. Stateflow (2)

Standard (Statemate)Non-determinism isallowed.

Synchronous execution ofAND-states.

Variable changes at theend of the step.

StateflowNon-determinism is notallowed.

Sequential execution ofAND-states.

Immediate variablechanges.

Page 22: Embedded Systems MATLAB Tutorial - uni-saarland.de · Standard tool for developing embedded systems. MATLAB Structure MATLAB core: ... 1 Log onto a cip, bio, or sunray workstation

Stateflow Development