Skip to content

Commit b0fa32b

Browse files
committed
Macro example
1 parent a1c2c00 commit b0fa32b

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

Makro_Beispiel.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<!--
2+
3+
author: Sebastian Zug
4+
5+
version: 1.0.1
6+
language: de
7+
comment: In dieser Vorlesungen werden die Schichten einer Roboterarchitektur adressiert.
8+
narrator: Deutsch Female
9+
10+
import: https://raw.githubusercontent.com/LiaTemplates/Rextester/master/README.md
11+
12+
attribute: Danke an Andre Dietrich für seinen Kurs "Einführung Regelungstechnik" aus dem Teile übernommen wurden.
13+
14+
script: https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js
15+
https://d3js.org/d3-random.v2.min.js
16+
https://d3js.org/d3.v4.min.js
17+
https://cdn.plot.ly/plotly-latest.min.js
18+
19+
link: https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css
20+
21+
@eval
22+
@Rextester._eval_(@uid, @Python, , , ,```
23+
var lines = data.Result.replace(/\n/g, ' ')
24+
.replace(/\s{2,}/g, ' ')
25+
.match(/(?<=\[).+?(?=\])/g);
26+
var outcome = [];
27+
for (var i=0; i<lines.length; i++){
28+
outcome[i] = lines[i].split(' ').map(function(item) {
29+
return parseFloat(item);
30+
});
31+
}
32+
@input(1);
33+
Plotly.newPlot(span_id, traces, layout);
34+
console.log("Aus Maus");
35+
```)
36+
@end
37+
-->
38+
39+
# Example for Macro usage in LiaScript
40+
41+
Let us assume that we intend to explain a PT1 behavior and its simulation in a lecture, based on LiaScript. The implementation has to be done in python while the visualization needs to be realized in js. Hence, we need a parser that extracts the actual values and provides the diagrams. The macro `@eval` calls another macro definition `@Rextester._eval_` that coordinates these activities.
42+
43+
<!--
44+
style="width: 60%; min-width: 420px; max-width: 720px;"
45+
-->
46+
```ascii
47+
+-------------------------------------------------------+
48+
| +--------------------------------------+ |
49+
| | Rextester Python Implementation | @input(0) |
50+
| +--------------------------------------+ |
51+
| | print(u, v) |
52+
| v |
53+
| +--------------------------------------+ |
54+
| | JavaScript Snippet for visualization | @input(1) |
55+
| +--------------------------------------+ |
56+
+-------------------------------------------------------+
57+
@Rextester._eval_(@uid, @Python, , , , code)
58+
```
59+
60+
The macro `@eval` is contained in the header. `_input(0)_` is called automatically. The first ten lines of the macro code include the parsing and extraction process based on `_input(0)_` output. The next step executes the visualization script by its reference `_input(1)_`. Extract the code by clicking on the corresponding blue bar in the code section. The specific implementation is hidden by default. The last step initiates the actual drawing process.
61+
62+
@@eval
63+
64+
The example visualizes a PT1-element. Test different parameter settings for an evaluation of their effect!
65+
66+
```python System.py
67+
import numpy as np
68+
69+
# Parameters of the system
70+
T = 0.01 # time constant of the system
71+
deltaT = 0.001 # sample frequency
72+
K = 1 # Final value
73+
74+
# Input values
75+
u = np.zeros(50); u[10:20]=1; u[30:-1]=1
76+
T_star = 1 / ((T / deltaT) + 1)
77+
78+
# Simulation of the system
79+
y = np.zeros(len(u))
80+
for index, entry in enumerate(u):
81+
if index>0:
82+
y[index] = T_star*(K*u[index]-y[index-1])+y[index-1]
83+
84+
print(u)
85+
print(y)
86+
```
87+
```js -Visualization
88+
var traces = [
89+
{
90+
x: d3.range(0, 100),
91+
y: outcome[0],
92+
mode: 'lines',
93+
line: {shape: 'vh'},
94+
type: 'scatter',
95+
name: 'Activation u',
96+
},
97+
{
98+
x: d3.range(0, 100),
99+
y: outcome[1],
100+
type: 'scatter',
101+
name: 'System response y',
102+
}
103+
];
104+
105+
var layout = {
106+
height : 300,
107+
width : 650,
108+
yaxis: {
109+
range: [0, 1],
110+
title: {
111+
text: 'System value',
112+
},
113+
},
114+
xaxis: {
115+
title: {
116+
text: 'Samples',
117+
},
118+
},
119+
margin: { l: 60, r: 10, b: 35, t: 10, pad: 4},
120+
showlegend: true,
121+
legend: { x: 1, xanchor: 'right', y: 0},
122+
tracetoggle: false
123+
};
124+
```
125+
@eval

0 commit comments

Comments
 (0)