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