Scicos tests: fix sciblk2 language
[scilab.git] / scilab / modules / scicos / tests / unit_tests / sciblk2.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2015 - Scilab Enterprises - Paul Bignier
4 //
5 //  This file is distributed under the same license as the Scilab package.
6 // =============================================================================
7 //
8 // <-- XCOS TEST -->
9 // <-- ENGLISH IMPOSED -->
10 //
11 // Load and run a diagram that calls a Scilab macro (tkscaleblk.sci)
12 exec("SCI/modules/scicos/tests/unit_tests/bug_8348.cosf", -1);
13 // Call sciblk2 instead of sciblk4
14 scs_m.objs(1).model.sim = list("tkscaleblk", 3);
15 scs_m.props.tf = 100;
16
17 // Rewrite tkscaleblk so it minds sciblk2's syntax
18 wMode = warning("query");
19 warning("off")
20 function [xd, tvec, z, x, outptr] = tkscaleblk(flag, nevprt, t, x, z, rpar, ipar, inptr)
21     outptr = list();
22     if flag == 1 then
23         // Output update
24         slider = get("-38f07e57:12bd41b596e:-7f2b#slider");
25
26         if slider <> [] then
27             // Calculate real value
28             value = get(slider,"value") / rpar(3);
29
30             w = get("-38f07e57:12bd41b596e:-7f2b");
31             if w <> [] then
32                 set(w, "info_message", string(value));
33             end
34
35             outptr = list(value);
36         end
37     elseif flag == 4 then
38         // Initialization
39
40         // If already exists (stopped) then reuse
41         f = get("-38f07e57:12bd41b596e:-7f2b");
42         if f <> [] then
43             return;
44         end
45
46         f = figure("Figure_name", "TK Source: " + "", ...
47         "dockable", "off", ...
48         "infobar_visible" , "on", ...
49         "toolbar", "none", ...
50         "menubar_visible", "off", ...
51         "menubar", "none", ...
52         "backgroundcolor", [1 1 1], ...
53         "default_axes", "off", ...
54         "figure_size", [180 350], ...
55         "layout", "border", ...
56         "figure_position", [40 40], ...
57         "Tag", "-38f07e57:12bd41b596e:-7f2b");
58
59         frame_slider = uicontrol(f, ...
60         "style", "frame", ...
61         "constraints", createConstraints("border", "left", [180, 0]), ...
62         "border", createBorder("line", "lightGray", 1), ...
63         "backgroundcolor", [1 1 1], ...
64         "layout", "gridbag");
65
66         // Slider
67         bounds = rpar(1:2);
68         initial = mean(bounds);
69         uicontrol(frame_slider, ...
70         "Style", "slider", ...
71         "Tag", "-38f07e57:12bd41b596e:-7f2b#slider", ...
72         "Min", bounds(1), ...
73         "Max", bounds(2), ...
74         "Value", initial, ...
75         "Position", [0 0 10 20], ...
76         "SliderStep", [rpar(3) 2*rpar(3)]);
77
78         frame_label = uicontrol(frame_slider, ...
79         "style", "frame", ...
80         "constraints", createConstraints("border", "right"), ...
81         "backgroundcolor", [1 1 1], ...
82         "layout", "gridbag");
83
84         // Labels
85         labels = string([bounds(2) ; ...
86         mean([bounds(2) initial])  ; ...
87         initial                    ; ...
88         mean([bounds(1) initial])  ; ...
89         bounds(1)]);
90         labels = "<html>" + strcat(labels, "<br /><br /><br />") + "</html>";
91
92         uicontrol(frame_label, ...
93         "Style", "text", ...
94         "String", labels(1), ...
95         "FontWeight", "bold", ...
96         "backgroundcolor", [1 1 1]);
97
98         // Update default value
99         outptr = list(initial / rpar(3));
100     elseif flag == 5 then
101         // Ending
102         f = get("-38f07e57:12bd41b596e:-7f2b");
103         if f <> [] then
104             close(f);
105         end
106     end
107     xd = 0;
108     tvec = [];
109 endfunction
110 warning(wMode);
111
112 cpr = scicos_simulate(scs_m);
113
114 cpr229 = [ ...
115 1;
116 5.5;
117 5.5;
118 5.5];
119
120 // Check the sensitive value of the continuous state
121 assert_checkalmostequal(list2vec(cpr(2)(2)(9)), cpr229);
122 close(gcf());
123
124 // Load and run a diagram that calls a Scilab macro (anim_pen.sci)
125 exec("SCI/modules/scicos/tests/unit_tests/pendulum_anim5.cosf", -1);
126 // Call sciblk2 instead of sciblk4
127 scs_m.objs(8).model.sim = list("anim_pen", 3);
128
129 // Rewrite anim_pen so it minds sciblk2's syntax
130 wMode = warning("query");
131 warning("off")
132 function [xd, tvec, z, x, outptr] = anim_pen(flag, nevprt, t, x, z, rpar, ipar, inptr)
133     win=20000+curblock()
134     if flag<>4 then
135         H=scf(win)
136     end
137     xold=z
138     plen=rpar(1)*1.6;
139     csiz=rpar(2)/4;
140     phi=rpar(3);
141     rcirc=csiz/3;
142     if flag==4 then
143         //** INIT
144         scf(win);
145         set("figure_style","new")
146         H=scf(win)
147         clf(H)
148         Axe=H.children
149         Axe.data_bounds=rpar(4:7)
150         Axe.isoview="on"
151
152         S=[cos(phi),-sin(phi);sin(phi),cos(phi)]
153         XY=S*[rpar(4),rpar(5);-csiz,-csiz]
154         gca().foreground = 3;
155         xsegs(XY(1,:),XY(2,:)-rcirc)
156
157         xTemp=0;
158         theta=0;
159         x1=xTemp-csiz;
160         x2=xTemp+csiz;
161         y1=-csiz;
162         y2=csiz
163         XY=S*[x1 x2 x2 x1 x1;y1,y1,y2,y2,y1]
164         gca().foreground = 5;
165         xfpoly(XY(1,:),XY(2,:))// cart
166         gca().foreground = 2;
167         xfarc(XY(1,1),XY(2,1),rcirc,rcirc,0,360*64) //wheel
168         xfarc(XY(1,2),XY(2,2),rcirc,rcirc,0,360*64) //wheel
169
170         XY=S*[xTemp,xTemp+plen*sin(theta);0,0+plen*cos(theta)]//pendulum
171         xsegs(XY(1,:),XY(2,:))
172
173     elseif flag==2 then
174         //** UPDATE
175         Axe=H.children
176         xTemp=inptr(1)(1)
177         theta=inptr(2)(1)
178         drawlater();
179         XY=Axe.children(4).data'+ [cos(phi)*(xTemp-xold);sin(phi)*(xTemp-xold)]*ones(1,5)
180         Axe.children(4).data=XY'
181
182         Axe.children(3).data(1)=XY(1,1)
183         Axe.children(3).data(2)=XY(2,1)
184         XY=Axe.children(4).data'+ [cos(phi)*(xTemp-xold-rcirc);sin(phi)*(xTemp-xold-rcirc)]*ones(1,5)
185         Axe.children(2).data(1)=XY(1,2)
186         Axe.children(2).data(2)=XY(2,2)
187         x1=xTemp*cos(phi);
188         y1=xTemp*sin(phi)
189         XY=[x1,x1+plen*sin(theta);y1,y1+plen*cos(theta)]
190         Axe.children(1).data=XY'
191         drawnow();
192         z=xTemp
193     end
194     xd = 0;
195     tvec = [];
196     outptr = list();
197 endfunction
198 warning(wMode);
199
200 cpr = scicos_simulate(scs_m);
201
202 cpr229 = [ ...
203 -0.00000010072892435; ...
204 -0.0000000093251865; ...
205 12.731754622278423; ...
206 0.00000000775393652; ...
207 -0.00000007274473145; ...
208 0.00000008598899216; ...
209 -0.00000000636625628; ...
210 -0.00000010072892435;
211 -0.0000000093251865; ...
212 -0.00000100269157242; ...
213 12.7317556249699955; ];
214
215 // Check the sensitive value of the continuous state
216 assert_checkalmostequal(list2vec(cpr(2)(2)(9)), cpr229, [], 1e9);