diff options
author | Julian T <julian@jtle.dk> | 2020-06-07 23:40:14 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-06-07 23:40:14 +0200 |
commit | 6075c41624efa8260344dfd4b0c7fa563f4cfe5e (patch) | |
tree | 0ee0942543935c70ddd108f71562612cdd72daee /sem4/embedded | |
parent | 6cb9bec1e0241fe64afdad9a520f026a9c25ea91 (diff) |
Added solutions for many of the first exercises
Diffstat (limited to 'sem4/embedded')
-rw-r--r-- | sem4/embedded/eksamnen/M1f1.png | bin | 0 -> 289274 bytes | |||
-rw-r--r-- | sem4/embedded/eksamnen/M7opg.adoc | 17 | ||||
-rw-r--r-- | sem4/embedded/eksamnen/notes.adoc | 181 | ||||
-rw-r--r-- | sem4/embedded/emb_m2/emb_m2.ino | 59 | ||||
-rw-r--r-- | sem4/embedded/emb_m4/emb_m4.ino | 7 | ||||
-rw-r--r-- | sem4/embedded/m1/int/int.ino | 22 | ||||
-rw-r--r-- | sem4/embedded/m1/router/router.ino | 47 | ||||
-rw-r--r-- | sem4/embedded/m5/m5.ino | 65 | ||||
-rw-r--r-- | sem4/embedded/m6/m6.ino | 67 |
9 files changed, 417 insertions, 48 deletions
diff --git a/sem4/embedded/eksamnen/M1f1.png b/sem4/embedded/eksamnen/M1f1.png Binary files differnew file mode 100644 index 0000000..ef8238d --- /dev/null +++ b/sem4/embedded/eksamnen/M1f1.png diff --git a/sem4/embedded/eksamnen/M7opg.adoc b/sem4/embedded/eksamnen/M7opg.adoc index c4b32f6..4be491a 100644 --- a/sem4/embedded/eksamnen/M7opg.adoc +++ b/sem4/embedded/eksamnen/M7opg.adoc @@ -16,21 +16,8 @@ ____ _Does utilization guarantee feasibility of an RMA schedule?_ ____ - -Først regner jeg utilization ud. - ----- -U = 20/50 + 20/80 = 0.65 ----- - -Derefter kan man regne utilization critereon ud. - ----- -\overline{U} = 2 * (2^(1/2) - 1) = 0.82 ----- - -Her kan man garantere at RMA er schedulable. - +Nej det kan man ikke. +Hvis deadlines havde været samme som periode kunne man regne stem:[\overline{U}] ud. ____ _Validate an RMA schedule with the exact schedulability criterion._ ____ diff --git a/sem4/embedded/eksamnen/notes.adoc b/sem4/embedded/eksamnen/notes.adoc index 5ff6874..736c275 100644 --- a/sem4/embedded/eksamnen/notes.adoc +++ b/sem4/embedded/eksamnen/notes.adoc @@ -9,6 +9,69 @@ ____ TODO med arduino. Og debouncer. +== Opgave løsning + +____ +_Design and implement an interrupt service routine which acts on digital pin 2._ +____ + +Digital pin 2 i arduino er mapped til atmega328p pin PD2, og er koblet på INT0. +Derfor starter vi med at enable INT0. + +Se for kode ../m1/int/int.ino + +---- +EIMSK |= 1 << INT0; +---- + +Derefter kan man lave en interrupt der trigger på rising edge, hvilket man ud +fra datasheet kan se er ISC01 og ISC00 i EICRAfor INT0. + +---- +EICRA |= (1 << ISC01) | (1 << ISC00); +---- + +Hvis man har sat den rigtige pinmode på 2, vil interrupten nu være aktiveret. + +---- +ISR(INT0_vect) { + do_the_thing(); +} +---- + +Her bruger jeg en normal ISR, hvor gcc håndtere det med at push registre på +stakken og sætte dem tilbage igen efter. + +Dette kan man også se i den compilede fil. + +image::M1f1.png[] + +____ +_Or add a timed decision in ISR to prevent more ISR response for the nex xx mseconds._ +____ + +Dette er gjort ved at gemme tiden for hver gang den kører med millis. +Derefter kan man check om det er over 100 ms siden at man sidst kørte. + +____ +_Design a tick routing which can run a function on given times (and explain code below_ +____ + +Har lavet en function man kalder for hver tasks man vil tick route. +Denne bruger millis() funktionen som bagved bruger timer0. + +____ +_What is limitations ? like timewise starvation,..._ +____ + +Man kan tydelig se problemet hvis man sætter task0 til at have et 10000 ms langt +delay. + +Her vil man se starvation i at det hele bliver langsomere. + +Dette er fordi det ikke er preimtive hvilket betyder at hvis et job bare tager lang +tid må de andre bare vente. + === Noter Findes forskellige slags realtime afhængig af hvor vigtigt det er at man @@ -26,9 +89,56 @@ ____ Exercise 1-11 ____ +=== Løsning + See ../emb_m2/emb_m2.ino -TODO med arduino +____ +_Compute the mean relative error between da and mda_ +____ + +0.0052564806 hvilket er okay for at den kun er 16 bit. + +____ +_Compute the mean relative error between da2 and mda2_ +____ + +0.1998619556 her kan man se at fejlen stiger en del. + +____ +_Iterate "a*=da[i];" over the length of da and measure the execution time (micros())_ +____ + +Exe time: 1272 + +____ +_Iterate "mult_float(&f1,&mda[i],&f); memcpy(&f1,&f,2);" over the length of mda and measure the execution time_ +____ + +Exe time: 828 + +____ +_Compute the relative difference between results from (7) and (9)_ +____ + +---- +Abs diff: 444 +Rel diff: 0.35 +---- + +____ +_Implement the fastest version of the trigornometric function "sine" that has below 2% relative error._ +____ + +Her brugte jeg tayler til at exporimere sin(x). +Dette blev ret præcis men var meget langsomere end sin(x). + +---- +TOTAL STATS +rel err : 0.0005467962 +own time: 2420 +ard time: 134 +---- === Noter @@ -56,7 +166,48 @@ ____ Exercise 4-7 ____ -TODO lav det her på arduino. +=== Løsning + +Her har jeg lavet noget test data med to beskeder, dette løser den ret godt. + +Jeg bruger bare uint8_t til de to først typer. + +Se ../m3/m3.ino + +---- +DONE +Type :1 +Recvid :514 +Transducerlevel :88 +Quiettime :50 +Masters :1 + +MASTER: 0 + rssi :90 + txid :5 + ixidtime :600 +DONE +Type :65 +Recvid :1 +Transducerlevel :136 +Quiettime :16 +Masters :3 + +MASTER: 0 + rssi :23 + txid :68 + ixidtime :65535 + +MASTER: 1 + rssi :11 + txid :171 + ixidtime :65534 + +MASTER: 2 + rssi :3 + txid :64 + ixidtime :65535 +---- === Noter @@ -91,10 +242,15 @@ ____ Show LED13 blink code and explain program. ____ +Se ../emb_m4/emb_m4.ino + ____ Explain interrupt ____ +Se ../emb_m4/jrnl/jrnl.ino +Se ../m1/int/int.ino + ____ Lack of control maybe bq of non-interruptable ISRs ____ @@ -103,10 +259,27 @@ ____ Procesmodel: running,ready,blocked ____ +Man kan se at processorne bruger k_sleep, hvilket gør dem blocked. +Når de er færdige med at sleep kommer de i ready, og når de så bliver scheduled +kommer de i running. + ____ semaphore wait and signal ____ +Dette kan man bruge til forskellige ting. + +Critical region:: + Man har noget kode der tilgår noget data. + Dette kan man låse ved at lave en semafor med en start værdi på 1. + Når nogen låser den tager de den værdi. +Periodisk timing:: + Man kan sætte os til at signal ens semafor hvert x sekundt. + Her giver man start værdi 0. +Timing og beskeder:: + Det er en god måde at synkronisere forskellige task. + For eksempel kan en ISR sætte en semafor for at signalere at der er en ny besked. + == J3 module 5 ____ @@ -114,12 +287,16 @@ What is a semaphore, show code from above and eplaing critical region setup ____ +Se ../m5/m5.ino + == J4 module 6 ____ Show code for and explain message queues as a buffering tool. ____ +Se ../m6/m6.ino + == J5 module 6.5 ____ diff --git a/sem4/embedded/emb_m2/emb_m2.ino b/sem4/embedded/emb_m2/emb_m2.ino index c6a6a07..b90e7f0 100644 --- a/sem4/embedded/emb_m2/emb_m2.ino +++ b/sem4/embedded/emb_m2/emb_m2.ino @@ -55,15 +55,19 @@ double sine(double x) { // Works by finding value in specific -pi/2 to pi/2 period. int period = x / TAU; + // Remember to multiply with sign + int sign = x < 0 ? -1 : 1; + // One cycle of sin takes 4 periods. // If 0 just return the sine approx x // If 1 do negative sine approx on x - TAU // If 2 do negative sine approx x // If 3 do sine approx on period x - TAU - x = abs(x - TAU*period); + x = abs(x) - abs(TAU*period); double res = 12; + Serial.print("Period "); Serial.println(abs(period % 4)); switch( abs(period % 4) ) { case 0: res = sineTayler(x, TAYLORN); @@ -78,8 +82,6 @@ double sine(double x) { res = sineTayler(x - TAU, TAYLORN); break; } - // Remember to multiply with sign - int sign = period < 0 ? -1 : 1; // Should never return because all cases are handled return res * sign; @@ -210,21 +212,42 @@ void setup() { NEXT_OPG(opg_count); // OPG11. Impl sine // Hardest value should be TAU as it's the furthest from center. - unsigned long begin = micros(); - double res = sine(TAU); - unsigned long time1 = micros() - begin; - Serial.print("own sin : "); Serial.println(res, PRINT_PRECISION); - - begin = micros(); - double correct = sin(TAU); - unsigned long time2 = micros() - begin; - Serial.print("ard sin : "); Serial.println(correct, PRINT_PRECISION); - - double error = correct ? abs(correct - res) / abs(correct) : 0; - Serial.print("rel err : "); Serial.println(error, PRINT_PRECISION); - - Serial.print("\nown time: "); Serial.println(time1); - Serial.print("ard time: "); Serial.println(time2); +#define STEPS 100 + double start = -100; + double end = 100; + double step = (end - start) / (STEPS-1); + unsigned long owntotaltime = 0; + unsigned long ardtotaltime = 0; + double totalerror = 0; + for (int i = 0; i < STEPS; i++) { + Serial.print("\n\nInput: "); Serial.println(start, PRINT_PRECISION); + + unsigned long begin = micros(); + double res = sine(start); + unsigned long time1 = micros() - begin; + Serial.print("own sin : "); Serial.println(res, PRINT_PRECISION); + + begin = micros(); + double correct = sin(start); + unsigned long time2 = micros() - begin; + Serial.print("ard sin : "); Serial.println(correct, PRINT_PRECISION); + + double error = correct ? abs(correct - res) / abs(correct) : 0; + Serial.print("rel err : "); Serial.println(error, PRINT_PRECISION); + + Serial.print("own time: "); Serial.println(time1); + Serial.print("ard time: "); Serial.println(time2); + + owntotaltime += time1; + ardtotaltime += time2; + totalerror += error; + + start += step; + } + Serial.println("\n\nTOTAL STATS"); + Serial.print("rel err : "); Serial.println(totalerror / STEPS, PRINT_PRECISION); + Serial.print("own time: "); Serial.println(owntotaltime / STEPS); + Serial.print("ard time: "); Serial.println(ardtotaltime / STEPS); } diff --git a/sem4/embedded/emb_m4/emb_m4.ino b/sem4/embedded/emb_m4/emb_m4.ino index 179f2b6..7e5ff48 100644 --- a/sem4/embedded/emb_m4/emb_m4.ino +++ b/sem4/embedded/emb_m4/emb_m4.ino @@ -7,8 +7,9 @@ char stk2[STACK]; struct k_t *tsk1, *tsk2; void f1() { - for(;;) { - Serial.println("Haha"); + for(int i = 0;;i++) { + Serial.println(i); + k_sleep(400); } } void f2() { @@ -16,7 +17,7 @@ void f2() { for(;;) { digitalWrite(13, state); state = !state; - k_sleep(1000); + k_sleep(100); } } diff --git a/sem4/embedded/m1/int/int.ino b/sem4/embedded/m1/int/int.ino index 8978be7..c4a2c31 100644 --- a/sem4/embedded/m1/int/int.ino +++ b/sem4/embedded/m1/int/int.ino @@ -4,19 +4,21 @@ #define DEBOUNCE 1 ISR(INT0_vect) { - static bool state = 0; + static unsigned long last = 0; - // Toggle LEDPIN - digitalWrite(LEDPIN, state); - state = !state; + unsigned long now = millis(); + if (now - last < 100) { + return; + } + last = now; + + static unsigned char state = 0; - /* - for(long i = 0; i < 1000; i++) { - Serial.print("Hej "); - Serial.println(i); + // Toggle LEDPIN + digitalWrite(LEDPIN, state % 2 == 0); + state++; + Serial.print("Hej "); Serial.println(state); - } - */ } void setup() { diff --git a/sem4/embedded/m1/router/router.ino b/sem4/embedded/m1/router/router.ino new file mode 100644 index 0000000..ba0b375 --- /dev/null +++ b/sem4/embedded/m1/router/router.ino @@ -0,0 +1,47 @@ +#define LEDPIN 13 + +int task1(void) { + static bool state = 0; + + digitalWrite(LEDPIN, state); + state = !state; + return 0; +} + +int task2(void) { + Serial.print("l"); + return 0; +} +int task3(void) { + Serial.print("h"); + return 0; +} + +int runevery(int (*f)(), unsigned long *last, unsigned long period) { + unsigned long now = millis(); + if (*last + period > now) { + return -1; + } + + *last = now; + return (*f)(); +} + + +#define TASKS 3 +unsigned long lasts[TASKS]; + +void setup() { + Serial.begin(115200); + pinMode(LEDPIN, OUTPUT); + for (int i = 0; i < TASKS; i++) { + lasts[i] = 0; + } +} + +void loop() { + unsigned i = 0; + runevery(task1, &lasts[i++], 1000); + runevery(task2, &lasts[i++], 100); + runevery(task3, &lasts[i++], 10); +} diff --git a/sem4/embedded/m5/m5.ino b/sem4/embedded/m5/m5.ino new file mode 100644 index 0000000..496e33b --- /dev/null +++ b/sem4/embedded/m5/m5.ino @@ -0,0 +1,65 @@ +#include <krnl.h> +#define STACK 200 + +char stk1[STACK]; +char stk2[STACK]; +struct k_t *tsk1, *tsk2, *sem; + +unsigned long shared1 = 0; +unsigned long shared2 = 0; + +#define DELAY 100000 +void f1() { + for(;;) { + for (int i = 0; i < DELAY/4; i++); + unsigned long s1 = shared1; + + // Lock + k_wait(sem, 0); + unsigned long s2 = shared2; + // Unlock + k_signal(sem); + + if (s1 != 0) { + Serial.print("S1: "); Serial.println(s1); + } + if (s2 != 0) { + Serial.print("S2: "); Serial.println(s2); + } + } +} + +void f2() { + for(;;) { + shared1 += 1; + for (int i = 0; i < DELAY; i++); + shared1 -= 1; + + // Lock + k_wait(sem, 0); + shared2 += 1; + for (int i = 0; i < DELAY; i++); + shared2 -= 1; + // Unlock + k_signal(sem); + } +} + +void setup() { + pinMode(13, OUTPUT); + Serial.begin(115200); + Serial.println("Starting"); + + k_init(2, 1, 0); + + sem = k_crt_sem(1, 1); + + tsk1 = k_crt_task(f1, 10, stk1, STACK); + tsk2 = k_crt_task(f2, 10, stk2, STACK); + + k_start(1); +} + +void loop() { + +} diff --git a/sem4/embedded/m6/m6.ino b/sem4/embedded/m6/m6.ino new file mode 100644 index 0000000..949c3d4 --- /dev/null +++ b/sem4/embedded/m6/m6.ino @@ -0,0 +1,67 @@ +#include <krnl.h> +#define STACK 100 +#define BUTTONPIN 2 +#define LEDPIN 13 + +char stk1[STACK]; +struct k_t *tsk1; +struct k_msg_t *pmsg; +char msgbuff[10]; + +ISR(INT0_vect) { + static unsigned long last = 0; + + unsigned long now = millis(); + if (now - last < 100) { + return; + } + last = now; + + static char state = 0; + state = !state; + + ki_send(pmsg, (void *)&state); + +} + +void f1() { + while(true) { + char state; + int lost; + if (k_receive(pmsg, &state, 0, &lost) < 0) { + continue; + } + + //k_eat_msec_time(1000); + + Serial.print("Lost: "); Serial.println(lost); + + digitalWrite(LEDPIN, state); + } +} + +void setup() +{ + pinMode(LEDPIN, OUTPUT); + + pinMode(BUTTONPIN, INPUT_PULLUP); + // Enable interrupt 0 + EIMSK |= 1 << INT0; + // Eable on rising edge + EICRA |= (1 << ISC01); + + Serial.begin(115200); + Serial.println("Starting"); + + k_init(1, 0, 1); + + pmsg = k_crt_send_Q(10, 1, msgbuff); + tsk1 = k_crt_task(f1, 10, stk1, STACK); + + k_start(1); +} + +void loop() +{ + +} |