Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with...

38
Einführung in Eingebettete Systeme Vorlesung 5 Bernd Finkbeiner 19/11/2014 [email protected] Prof. Bernd Finkbeiner, Ph.D. [email protected] 1

Transcript of Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with...

Page 1: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Einführung in

Eingebettete Systeme

Vorlesung 5 Bernd Finkbeiner 19/11/2014 [email protected]

Prof. Bernd Finkbeiner, Ph.D. [email protected]

1

Page 2: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Hardware “in a loop”

actuators

2

Page 3: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Sensorsysteme

! Sensor: erkennt/misst Größe und übersetzt in elektrischen Strom/Spannung

! Verstärker: bringt das Signal in den Eingangsbereich für die Analog/Digital Wandlung

! Abtast-Halte Schaltung: hält analogen Spannungswert kurzzeitig auf einem definiterten Wert

! A/D Wandler: setzt analogen Wert in digitalen Wert um

Sensor Verstärker Abtast-Halte

Schaltung A/D

Wandler

3

Page 4: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Eingabemechanismen in Software

! Polling ! Die Eingabegeräte werden

periodisch abgefragt ! Wenn eine Eingabe bereit

ist initiert der Prozessor die nötige Kommunikation.

! Interrupts ! Externe Hardware sendet

Interrupt wenn Eingaben bereit sind.

! Das laufende Programm wird unterbrochen, der Prozessor ruft die Interrupt Service Routine (ISR) auf.Processor Setup Code

Processor checks I/O control register for status of peripheral 1 Processor services I/O 1

Processor checks I/O control register for status of peripheral 2

Processor checks I/O control register for status of peripheral 3

Processor services I/O 2

Processor services I/O 3

Ready

Ready

Ready

Not Ready

Not Ready

Not Ready

Processor Setup Code

Register the Interrupt Service Routine

Processor executes task code Run Interrupt Service Routine

Interrupt!

Context switch

Resume

4

Page 5: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Arduino: Polling

5[http://www.dave-auld.net]

int pbIn = 2; // Digital input on pin 2 int ledOut = 4; // The output LED pin int state = LOW; // The input state void setup() { // Set up the digital Pin 2 to an Input and Pin 4 to an Output pinMode(pbIn, INPUT); pinMode(ledOut, OUTPUT); } void loop() { state = digitalRead(pbIn); //Read the button digitalWrite(ledOut, state); //write the LED state //Simulate a long running process or complex task for (int i = 0; i < 100; i++) { // do nothing but waste some time delay(10); } }

Page 6: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Arduino: Interrupts

6[http://www.dave-auld.net]

int pbIn = 0; // Interrupt 0 is on DIGITAL PIN 2! int ledOut = 4; // The output LED pin volatile int state = LOW; // The input state toggle

void setup() { //Set up the digital pin 2 to an Interrupt and Pin 4 to an Output pinMode(ledOut, OUTPUT); //Attach the interrupt to the input pin and monitor for ANY Change attachInterrupt(pbIn, stateChange, CHANGE); } void loop() { //Simulate a long running process or complex task for (int i = 0; i < 100; i++) { delay(10); } } void stateChange() { state = !state; digitalWrite(ledOut, state); }

Page 7: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Arduino: Aktivieren und Deaktivieren von Interrupts

7[http://arduino.cc/en/Reference/Interrupts]

void setup() {}

void loop() { noInterrupts(); // critical, time-sensitive code here interrupts(); // other code here }

Page 8: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Timer Interrrupt

Timer

Update Tick / Sample

When timer expires, interrupt processor

Reset timer

Processor jumps to ISR

Resumes

Processor Setup

Register Interrupt Service Routine

Initialize Timer

Execute Task Code

8

Page 9: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Arduino: Timer Interrupts

9http://playground.arduino.cc/Code/Timer1

#include "TimerOne.h"

void setup() { pinMode(10, OUTPUT); Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt } void callback() { digitalWrite(10, digitalRead(10) ^ 1); }

void loop() { // your program here... }

Page 10: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Kommunikation: Serielles Interface

10

Page 11: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Arduino: Serial

11[http://arduino.cc/en/Tutorial/ReadASCIIString]

void setup() { Serial.begin(9600); ... }

void loop() { while (Serial.available() > 0) { // look for the next valid integer in the incoming serial stream: int red = Serial.parseInt(); int green = Serial.parseInt(); // look for the newline. if (Serial.read() == '\n') { // constrain the values to 0 - 255 and invert red = 255 - constrain(red, 0, 255); green = 255 - constrain(green, 0, 255); // fade the red, green, and blue legs of the LED: analogWrite(redPin, red); analogWrite(greenPin, green); // print the numbers in one string as hexadecimal: Serial.print(red, HEX); Serial.print(green, HEX); } } }

Page 12: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Kommunikation: I2C

12

Page 13: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Arduino: I2C (Sender)

13[http://arduino.cc/en/Tutorial/MasterWriter]

#include <Wire.h>

void setup() { Wire.begin(); // join i2c bus (address optional for master) }

byte x = 0;

void loop() { Wire.beginTransmission(4); // transmit to device #4 Wire.write("x is "); // sends five bytes Wire.write(x); // sends one byte Wire.endTransmission(); // stop transmitting

x++; delay(500); }

Page 14: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Arduino: I2C (Empfänger)

14[http://arduino.cc/en/Tutorial/MasterWriter]

#include <Wire.h>

void setup() { Wire.begin(4); // join i2c bus with address #4 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output }

void loop() { delay(100); }

void receiveEvent(int howMany) { while(1 < Wire.available()) // loop through all but the last { char c = Wire.read(); // receive byte as a character Serial.print(c); // print the character } int x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer }

Page 15: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

6. Zeitkontinuierliche Modelle

Ziel:

! Modellierung kontinuierlicher Systeme mit Differenzialgleichungen und Signalflußplänen

Page 16: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Differenzialgleichungen als Modelle kontinuierlicher Systeme

! Zeit ist kontinuierlich:

! Ein Signal ist eine Funktion, die Zeit in Werte abbildet:

! Ein System übersetzt ein Eingangssignal in ein Ausgangssignal:

! Wir beschreiben ein System durch Gleichungen über seine Eingabesignale, Ausgabesignale, und Ableitungen der Signale. 16

R�0

x : R�0 ! Rn

S : (R�0 ! Rn) ! (R�0 ! Rm)

Page 17: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Beispiel: Freier Fall

17[http://www.physicsclassroom.com/mmedia/newtlaws/efff.html]

Position: y(t)Geschwindigkeit: v(t) = y(t)Beschleunigung: a(t) = v(t) = 9.81

Anfangswerte: y(0) = 100

v(0) = 0

y(t) = 100 +

R t0 v(t)

v(t) = 0 +

R t0 9.81

Page 18: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Einfache Blöcke

! Algebraische Blöcke die Ausgabe ist eine zeitinvariante Funktion der Eingabe (d.h. von der Zeit unabhängige Funktion, z.B. ein Produkt mit einer Konstanten)

! Integratoren

18

out(t) = f(in(t))

out(t) = init +

Z t

0in(u) du

Page 19: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

19

Beispiel: Freier Fall

Page 20: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

20

Page 21: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

.y (t) = v(t)00

.v (t) = F (t)

m

F (t) = k(l(t)� l0)

l(t) = u(t)� y(t)

Beispiel: Masse-Feder System

21

Example: spring-mass system w. disturbance

m

u(t)

y(t)

• Basic model:··y (t) = F(t)

m

F(t) = k (l(t) − l0)

l(t) = u(t) − y(t)

• Replace higher-order derivatives:

Add v(t) =·y (t).

Gives·y (t) = v(t)·v (t) = k

m(u(t) − y(t) − l0)

ES I —- WS 2005/06: Co-modelling – p.16/33

Page 22: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Beispiel: Masse-Feder System

! Differenzialgleichung:

! Startwert:

! Nach Integration:

22

.y (t) = v(t).v (t) = k

m (u(t)� y(t)� l0)

y(t) = 1 +R t0 v(z)dz

v(t) = 0 +R t0

km (u(z)� y(z)� l0)dz

y(0) = 1v(t) = 0

Page 23: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Blockschaltbild

23

y(t) = 1 +R t0 v(z)dz

v(t) = 0 +R t0

km (u(z)� y(z)� l0)dz

Page 24: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

24

Page 25: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Aufgabe

! Modellieren Sie die Flugbahn eines Balls der im 45 Grad Winkel nach oben mit einer Initialgeschwindigkeit von vx(0)= 10 m/s und vy(0)= 10 m/s geworfen wird.

! Vernachlässigen Sie den Luftwiderstand, aber berücksichtigen Sie die Schwerkraft.

25

Page 26: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

26

Page 27: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

27

Page 28: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Alfa Romeo 147 GTA

28

Page 29: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

29[Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing]

Page 30: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

30

Page 31: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Numerische Integration

! Wir analysieren ein kontinuierliches Signal:

! Ziel der numerischen Integration is es, den (approximativen) Wert des kontinuierlichen Signals an genügend vielen Zeitpunkten zu berechnen um die Form des Signals zu bestimmen.

! Es gibt zahlreiche verschiedene Verfahren, die sich in Präzision und Berechnungsaufwand stark unterscheiden.

31

x(t) = x0 +

Z t

0w(z)dz

Page 32: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Trapezverfahren

32[Ptolemaeus, System Design, 2013]

Page 33: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Trapezverfahren

! Fixed step size solver: x wird in festen Intervallen h berechnet (bzw. approximiert): x(0), x(h), x(2h), x(3h),...

! x wird wie folgt approximiert:

! Problem:Zur Berechnung von x(nh) wird sowohl w((n-1)h) als auch w(nh) benötigt; in Systemen mit feedback ergibt sich eine zirkuläre Abhängigkeit!(Solche Solver heissen auch implicit method solvers.)

33

x(nh) =

⇢x0 fur n = 0x((n� 1)h) + h(w((n� 1)h) + w(nh))/2 fur n � 1

Page 34: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Euler Methode

34[Ptolemaeus, System Design, 2013]

Page 35: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Euler Methode

! Explicit-method solver: x(nh) hängt von w((n-1)h) aber nicht von w(nh) ab.

! x wird wie folgt approximiert:

! Problem:Genauigkeit hängt von h ab.

35

x(nh) =

⇢x0 fur n = 0x((n� 1)h) + h(w((n� 1)h)) fur n � 1

Page 36: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

Variable-step-size Euler

! Variable-step-size Methoden passen die Schrittweite an die Variabilität des Signals an

! Der Algorithmus wählt zunächst eine Schrittweite, führt die numerische Integration durch, und schätzt dann den Fehler ab; wenn die Abschätzung des Fehlers zu groß ausfällt, wird die numerische Integration mit kleinerer Schrittweite wiederholt.

! Variable-step-size forward Euler:

36

x(tn) =

⇢x0 fur n = 0x(tn�1) + hn(w(tn�1)) fur n � 1

Page 37: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

37

0,2

Page 38: Einführung in Eingebettete Systeme...Alfa Romeo 147 GTA 28 [Luca Zamboni: Getting Started with Simulink, 2013, Packt Publishing] 29. 30. Numerische Integration! Wir analysieren ein

38

0,2

1,5

2,0