1
//** INRIA / SCILAB / Roberto Bucher / Simone Mannori / Paolo Gai
4
// Modified by Roberto Bucher roberto.bucher@supsi.ch from the original
7
// Input editor function of Scicos code generator
10
//** 10 Set 2007 : cleaner startup code by Simone Mannori
12
function RTAICodeGen_()
14
//** ------------- Preliminary I/O section ___________________________________________________________________________
15
k = [] ; //** index of the CodeGen source superbloc candidate
17
xc = %pt(1); //** last valid click position
23
k = getobj(scs_m,[xc;yc]) ; //** look for a block
24
//** check if we have clicked near an object
27
//** check if we have clicked near a block
28
elseif typeof(scs_m.objs(k))<>"Block" then
32
//** If the clicked/selected block is really a superblock
34
if scs_m.objs(k).model.sim(1)=="super" then
36
XX = scs_m.objs(k); //** isolate the super block to use
38
//----------------------------------------------------> THE REAL CODE GEN IS HERE --------------------------------
39
//** the real code generator is here
40
[ok, XX, alreadyran, flgcdgen, szclkINTemp, freof] = do_compile_superblock42(XX, scs_m, k, alreadyran);
43
//**quick fix for sblock that contains scope
44
gh_curwin = scf(curwin)
47
//** the clicked/selected block is NOT a superblock
48
message("Generation Code only work for a Super Block ! ")
53
//==========================================================================
54
//BlockProto : generate prototype
58
//16/06/07 Author : A.Layec
61
// Modified for RT purposes by Roberto Bucher - RTAI Team
62
// roberto.bucher@supsi.ch
64
function [txt]=BlockProto(bk)
66
nin=inpptr(bk+1)-inpptr(bk); //* number of input ports */
67
nout=outptr(bk+1)-outptr(bk); //* number of output ports */
71
if ftyp>2000 then ftyp=ftyp-2000,end
72
if ftyp>1000 then ftyp=ftyp-1000,end
74
//** check function type
75
if ftyp < 0 then //** ifthenelse eselect blocks
79
if (ftyp<>0 & ftyp<>1 & ftyp<>2 & ftyp<>3 & ftyp<>4) then
80
disp("types other than 0,1,2,3 or 4 are not yet supported.")
87
txt=[get_comment('proto_blk',list(funs(bk),funtyp(bk),bk));]
93
//*********** prototype definition ***********//
94
txtp=['(int *, int *, double *, double *, double *, int *, double *, \';
95
' int *, double *, int *, double *, int *,int *, int *, \';
96
' double *, int *, double *, int *);'];
97
if (funtyp(bk)>2000 & funtyp(bk)<3000)
98
blank = get_blank('void '+funs(bk)+'(');
99
txtp(1) = 'void '+funs(bk)+txtp(1);
100
elseif (funtyp(bk)<2000)
101
txtp(1) = 'void C2F('+funs(bk)+')'+txtp(1);
102
blank = get_blank('void C2F('+funs(bk)+')');
104
txtp(2:$) = blank + txtp(2:$);
106
//*******************************************//
112
//*********** prototype definition ***********//
113
txtp=['(int *, int *, double *, double *, double *, int *, double *, \';
114
' int *, double *, int *, double *, int *,int *, int *';]
115
if (funtyp(bk)>2000 & funtyp(bk)<3000)
116
blank = get_blank('void '+funs(bk)+'(');
117
txtp(1) = 'void '+funs(bk)+txtp(1);
118
elseif (funtyp(bk)<2000)
119
txtp(1) = 'void C2F('+funs(bk)+')'+txtp(1);
120
blank = get_blank('void C2F('+funs(bk)+')');
122
if nin>=1 | nout>=1 then
123
txtp($)=txtp($)+', \'
127
txtp($)=txtp($)+' double *, int * ,'
129
txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
133
txtp($)=txtp($)+', \'
137
txtp($)=txtp($)+' double *, int * ,'
139
txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
144
txtp($)=txtp($)+', \'
145
txtp=[txtp;' double *,int *);'];
147
txtp($)=txtp($)+');';
150
txtp(2:$) = blank + txtp(2:$);
152
//*******************************************//
157
//*********** prototype definition ***********//
159
txtp=['void '+funs(bk)+...
160
'(int *, int *, double *, double *, double *, int *, double *, \';
161
' int *, double *, int *, double *, int *, int *, int *, \'
162
' double **, int *, int *, double **,int *, int *'];
164
txtp($)=txtp($)+');';
166
txtp($)=txtp($)+', \';
170
blank = get_blank('void '+funs(bk));
171
txtp(2:$) = blank + txtp(2:$);
173
//********************************************//
178
'void '+funs(bk)+'(scicos_block *, int );'];
183
//==========================================================================
184
//CallBlock : generate C calling sequence
187
//inputs : bk : bloc index
188
// pt : evt activation number
193
//16/06/07 Authors : Alan Layec
196
// Modified for RT purposes by Roberto Bucher - RTAI Team
197
// roberto.bucher@supsi.ch
199
function txt=call_block42(bk,pt,flag)
203
if flag==2 & ((zptr(bk+1)-zptr(bk))+..
204
(ozptr(bk+1)-ozptr(bk))+..
205
(xptr(bk+1)-xptr(bk)+..
206
with_work(bk))==0 |..
207
pt<=0) & ~(stalone & or(bk==actt(:,1))) then
208
return // block without state or continuously activated
210
if flag==0 & ((xptr(bk+1)-xptr(bk))==0) then
211
return // block without continuous state
213
if flag==9 & ((zcptr(bk+1)-zcptr(bk))==0) then
214
return // block without continuous state
216
if flag==3 & ((clkptr(bk+1)-clkptr(bk))==0) then
221
if ~(flag==3 & ((zcptr(bk+1)-zcptr(bk))<>0)) then
226
txt=[get_comment('call_blk',list(funs(bk),funtyp(bk),bk));]
228
//** set nevprt and flag for called block
230
'block_'+rdnom+'['+string(bk-1)+'].nevprt = '+string(pt)+';'
231
'local_flag = '+string(flag)+';']
233
//**see if its bidon, actuator or sensor
234
if funs(bk)=='bidon' then
237
elseif funs(bk)=='bidon2' then
240
elseif or(bk==actt(:,1)) then
241
ind=find(bk==actt(:,1))
247
'nport = '+string(ind)+';']
249
rdnom+'_actuator(&local_flag, &nport, &block_'+rdnom+'['+string(bk-1)+'].nevprt, \'
250
get_blank(rdnom+'_actuator')+' &t, ('+mat2scs_c_ptr(outtb(uk))+' *)'+rdnom+'_block_outtbptr['+string(uk-1)+'], \'
251
get_blank(rdnom+'_actuator')+' &nrd_'+string(nuk_1)+', &nrd_'+string(nuk_2)+', &nrd_'+string(uk_t)+',bbb);']
253
// 'if(local_flag < 0) return(5 - local_flag);']
255
elseif or(bk==capt(:,1)) then
256
ind=find(bk==capt(:,1))
262
'nport = '+string(ind)+';']
264
rdnom+'_sensor(&local_flag, &nport, &block_'+rdnom+'['+string(i-1)+'].nevprt, \'
265
get_blank(rdnom+'_sensor')+' &t, ('+mat2scs_c_ptr(outtb(yk))+' *)'+rdnom+'_block_outtbptr['+string(yk-1)+'], \'
266
get_blank(rdnom+'_sensor')+' &nrd_'+string(nyk_1)+', &nrd_'+string(nyk_2)+', &nrd_'+string(yk_t)+',aaa);']
268
// 'if(local_flag < 0) return(5 - local_flag);']
273
nx=xptr(bk+1)-xptr(bk);
274
nz=zptr(bk+1)-zptr(bk);
275
nrpar=rpptr(bk+1)-rpptr(bk);
276
nipar=ipptr(bk+1)-ipptr(bk);
277
nin=inpptr(bk+1)-inpptr(bk); //* number of input ports */
278
nout=outptr(bk+1)-outptr(bk); //* number of output ports */
281
//l'adresse du pointeur de ipar
282
if nipar<>0 then ipar=ipptr(bk), else ipar=1;end
283
//l'adresse du pointeur de rpar
284
if nrpar<>0 then rpar=rpptr(bk), else rpar=1; end
285
//l'adresse du pointeur de z attention -1 pas sur
286
if nz<>0 then z=zptr(bk)-1, else z=0;end
287
//l'adresse du pointeur de x
288
if nx<>0 then x=xptr(bk)-1, else x=0;end
292
if ftyp>2000 then ftyp=ftyp-2000,end
293
if ftyp>1000 then ftyp=ftyp-1000,end
295
//** check function type
296
if ftyp < 0 then //** ifthenelse eselect blocks
300
if (ftyp<>0 & ftyp<>1 & ftyp<>2 & ftyp<>3 & ftyp<>4) then
301
disp("types other than 0,1,2,3 or 4 are not supported.")
310
//**** input/output addresses definition ****//
313
uk=inplnk(inpptr(bk)-1+k);
314
nuk=size(outtb(uk),'*');
316
'rdouttb['+string(k-1)+']=(double *)'+rdnom+'_block_outtbptr['+string(uk-1)+'];']
319
'args[0]=&(rdouttb[0]);']
324
'args[0]=(double *)'+rdnom+'_block_outtbptr[0];']
326
uk=inplnk(inpptr(bk));
327
nuk=size(outtb(uk),'*');
329
'args[0]=(double *)'+rdnom+'_block_outtbptr['+string(uk-1)+'];']
334
yk=outlnk(outptr(bk)-1+k);
335
nyk=size(outtb(yk),'*');
337
'rdouttb['+string(k+nin-1)+']=(double *)'+rdnom+'_block_outtbptr['+string(yk-1)+'];'];
340
'args[1]=&(rdouttb['+string(nin)+']);'];
345
'args[1]=(double *)'+rdnom+'_block_outtbptr[0];'];
347
yk=outlnk(outptr(bk));
348
nyk=size(outtb(yk),'*'),;
350
'args[1]=(double *)'+rdnom+'_block_outtbptr['+string(yk-1)+'];'];
352
//*******************************************//
354
//*********** call seq definition ***********//
355
txtc=['(&local_flag,&block_'+rdnom+'['+string(bk-1)+'].nevprt,&t,block_'+rdnom+'['+string(bk-1)+'].xd, \';
356
'block_'+rdnom+'['+string(bk-1)+'].x,&block_'+rdnom+'['+string(bk-1)+'].nx, \';
357
'block_'+rdnom+'['+string(bk-1)+'].z,&block_'+rdnom+'['+string(bk-1)+'].nz,block_'+rdnom+'['+string(bk-1)+'].evout, \';
358
'&block_'+rdnom+'['+string(bk-1)+'].nevout,block_'+rdnom+'['+string(bk-1)+'].rpar,&block_'+rdnom+'['+string(bk-1)+'].nrpar, \';
359
'block_'+rdnom+'['+string(bk-1)+'].ipar,&block_'+rdnom+'['+string(bk-1)+'].nipar, \';
360
'(double *)args[0],&nrd_'+string(nuk)+',(double *)args[1],&nrd_'+string(nyk)+');'];
361
if (funtyp(bk)>2000 & funtyp(bk)<3000)
362
blank = get_blank(funs(bk)+'( ');
363
txtc(1) = funs(bk)+txtc(1);
364
elseif (funtyp(bk)<2000)
365
txtc(1) = 'C2F('+funs(bk)+')'+txtc(1);
366
blank = get_blank('C2F('+funs(bk)+') ');
368
txtc(2:$) = blank + txtc(2:$);
370
//*******************************************//
375
//*********** call seq definition ***********//
376
txtc=['(&local_flag,&block_'+rdnom+'['+string(bk-1)+'].nevprt,&t,block_'+rdnom+'['+string(bk-1)+'].xd, \';
377
'block_'+rdnom+'['+string(bk-1)+'].x,&block_'+rdnom+'['+string(bk-1)+'].nx, \';
378
'block_'+rdnom+'['+string(bk-1)+'].z,&block_'+rdnom+'['+string(bk-1)+'].nz,block_'+rdnom+'['+string(bk-1)+'].evout, \';
379
'&block_'+rdnom+'['+string(bk-1)+'].nevout,block_'+rdnom+'['+string(bk-1)+'].rpar,&block_'+rdnom+'['+string(bk-1)+'].nrpar, \';
380
'block_'+rdnom+'['+string(bk-1)+'].ipar,&block_'+rdnom+'['+string(bk-1)+'].nipar'];
381
if (funtyp(bk)>2000 & funtyp(bk)<3000)
382
blank = get_blank(funs(bk)+'( ');
383
txtc(1) = funs(bk)+txtc(1);
384
elseif (funtyp(bk)<2000)
385
txtc(1) = 'C2F('+funs(bk)+')'+txtc(1);
386
blank = get_blank('C2F('+funs(bk)+') ');
388
if nin>=1 | nout>=1 then
389
txtc($)=txtc($)+', \'
393
uk=inplnk(inpptr(bk)-1+k);
394
nuk=size(outtb(uk),'*');
395
txtc($)=txtc($)+'(double *)'+rdnom+'_block_outtbptr['+string(uk-1)+'],&nrd_'+string(nuk)+',';
397
txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
401
txtc($)=txtc($)+', \'
405
yk=outlnk(outptr(bk)-1+k);
406
nyk=size(outtb(yk),'*');
407
txtc($)=txtc($)+'(double *)'+rdnom+'_block_outtbptr['+string(yk-1)+'],&nrd_'+string(nyk)+',';
409
txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
414
txtc($)=txtc($)+', \'
416
'block_'+rdnom+'['+string(bk-1)+'].g,&block_'+rdnom+'['+string(bk-1)+'].ng);']
418
txtc($)=txtc($)+');';
421
txtc(2:$) = blank + txtc(2:$);
423
//*******************************************//
428
//*********** call seq definition ***********//
429
txtc=[funs(bk)+'(&local_flag,&block_'+rdnom+'['+string(bk-1)+'].nevprt,&t,block_'+rdnom+'['+string(bk-1)+'].xd, \';
430
'block_'+rdnom+'['+string(bk-1)+'].x,&block_'+rdnom+'['+string(bk-1)+'].nx, \';
431
'block_'+rdnom+'['+string(bk-1)+'].z,&block_'+rdnom+'['+string(bk-1)+'].nz,block_'+rdnom+'['+string(bk-1)+'].evout, \';
432
'&block_'+rdnom+'['+string(bk-1)+'].nevout,block_'+rdnom+'['+string(bk-1)+'].rpar,&block_'+rdnom+'['+string(bk-1)+'].nrpar, \';
433
'block_'+rdnom+'['+string(bk-1)+'].ipar,&block_'+rdnom+'['+string(bk-1)+'].nipar, \';
434
'(double **)block_'+rdnom+'['+string(bk-1)+'].inptr,block_'+rdnom+'['+string(bk-1)+'].insz,&block_'+rdnom+'['+string(bk-1)+'].nin, \';
435
'(double **)block_'+rdnom+'['+string(bk-1)+'].outptr,block_'+rdnom+'['+string(bk-1)+'].outsz, &block_'+rdnom+'['+string(bk-1)+'].nout'];
437
txtc($)=txtc($)+');';
439
txtc($)=txtc($)+', \';
441
'block_'+rdnom+'['+string(bk-1)+'].g,&block_'+rdnom+'['+string(bk-1)+'].ng);']
443
blank = get_blank(funs(bk)+'( ');
444
txtc(2:$) = blank + txtc(2:$);
446
//*******************************************//
451
funs(bk)+'(&block_'+rdnom+'['+string(bk-1)+'],local_flag);'];
455
// txt =[txt;'if(local_flag < 0) return(5 - local_flag);']
459
//==========================================================================
460
//utilitary fonction used to format long C instruction
461
//t : a string containing a C instruction
462
//l : max line length allowed
465
//Author : Rachid Djenidi
466
function t1=cformatline(t ,l)
474
if kw(1)==1 then // there is leading blanks
475
k1=find(kw(2:$)-kw(1:$-1)<>1)
476
if k1==[] then // there is a single blank
483
t=part(t,nw+1:length(t));
484
bl=part(' ',ones(1,nw))
487
if length(t)<=l then t1=[t1;bl+t],return,end
489
if k==[] then t1=[t1;bl+t],return,end
490
k($+1)=length(t)+1 // positions of the commas
491
i=find(k(1:$-1)<=l&k(2:$)>l) //nearest left comma (reltively to l)
492
if i==[] then i=1,end
493
t1=[t1;bl+part(t,1:k(i))]
494
t=part(t,k(i)+1:length(t))
495
if first then l1=l1-2;bl=bl+' ';first=%f;end
499
//==========================================================================
500
//used in do_compile_superblock
501
function vec=codebinaire(v,szclkIN)
511
//==========================================================================
513
// Modified for RT purposes by Roberto Bucher - RTAI Team
514
// roberto.bucher@supsi.ch
516
function ok = compile_standalone()
517
//compile rt standalone executable for standalone
519
//Author : Roberto Bucher (roberto.bucher@die.supsi.ch)
522
xinfo('Compiling standalone');
526
if getenv('WIN32','NO')=='OK' then
527
unix_w('nmake -f Makefile.mak');
535
//==========================================================================
536
// Transforms a given Scicos discrete and continuous SuperBlock into a C defined Block
540
// Original file from Project Metalau - INRIA
541
// Modified for RT purposes by Roberto Bucher - RTAI Team
542
// roberto.bucher@supsi.ch
544
function [ok,XX,alreadyran,flgcdgen,szclkINTemp,freof] = do_compile_superblock42(XX,all_scs_m,numk,alreadyran)
546
scs_m = XX.model.rpar ; //** isolate the super block scs_m data structure
549
//** SAFETY : terminate current simulation
551
//** terminate current simulation
556
hname = scs_m.props.title(1); //** super block name
558
//***********************************************************
559
//Check blocks properties and adapt them if necessary
560
//***********************************************************
563
//** These blocks are not ALLOWED for Emb code generation
564
vorbidden_items=["CLKOUT_f","activation (events) output ports";
565
"IN_f","input ports";
566
"OUT_f","output ports";
567
"CLKOUTV_f","activation outputs";
570
"SampleCLK","clocks";
571
"RFILE_f","Read block";
572
"READC_f","Read_block";
573
"WFILE_f","Write block";
574
"WRITEC_f","Write block"]
579
for i=1:size(scs_m.objs)
582
if typeof(scs_m.objs(i))=="Block" then
583
ind=find(vorbidden_items==scs_m.objs(i).gui);
587
message(vorbidden_items(ind(1),2)+" not allowed in Superblock");
588
return; // EXIT point
590
elseif scs_m.objs(i).gui=="CLKINV_f" then //** input clock from external diagram
591
//** replace event input ports by fictious block
592
scs_m.objs(i).gui="EVTGEN_f";
593
scs_m.objs(i).model.sim(1)="bidon"
599
message("Only one activation block allowed!");
600
return; // EXIT point
607
szclkIN = size(clkIN,2);
610
flgcdgen = szclkIN; //** pass the clock
613
//** BEWARE : the Scicos compiler is called here !
615
[bllst,connectmat,clkconnect,cor,corinv,ok,scs_m,flgcdgen,freof] = c_pass1(scs_m,flgcdgen);
618
message("Sorry: problem in the pre-compilation step.")
619
return ; //** EXIT point
622
szclkINTemp = szclkIN;
627
if (bllst(i).sim(1)=="bidon") then //** clock input
633
//** BEWARE : update to new graphics instructions !
634
%windo = xget('window') ;
636
cpr = c_pass2(bllst,connectmat,clkconnect,cor,corinv)
640
return ; //** exit point
643
//** Alan's patch 5/07/07: try to solve
644
// which blocks use work
646
funs_save = cpr.sim.funs ;
647
funtyp_save = cpr.sim.funtyp ;
648
with_work = zeros(cpr.sim.nblk,1) ;
651
//** ------ Windows cleaning for internal Scopes -------------------
654
//** retrieve all open Scilab windows with winsid()
657
BeforeCG_WinList = winsid();
659
ierr=execstr('[state,t]=scicosim(cpr.state,0,0,cpr.sim,'+..
660
'''start'',scs_m.props.tol)','errcatch')
663
if state.iz(i)<>0 then
667
ierr=execstr('[state,t]=scicosim(state,0,0,cpr.sim,'+..
668
'''finish'',scs_m.props.tol)','errcatch')
672
//** retrieve all open Scilab windows with winsid
673
//** and close the additional windows open since first
676
//** This code does not cover 100% of the possible situations because the user can
677
//** destroy one or more Scicos wins manually during this intermediate phase
678
//** This code is 100% functional if the the user does not close manually any win.
679
//** TO BE updated in Scilab 5.0
681
AfterCG_WinList = winsid();
683
AfterCG_size = size(AfterCG_WinList); //** matrix
684
AfterCG_size = AfterCG_size(2) ; //** vector lenght
686
BeforeCG_size = size(BeforeCG_WinList); //** matrix
687
BeforeCG_size = BeforeCG_size(2) ; //** vector lenght
689
if (or(AfterCG_WinList<>BeforeCG_WinList)) & (AfterCG_size>BeforeCG_size) then
690
//** means that a scope or other scicos object has created some
693
DiffCG_Winlist = AfterCG_WinList<>BeforeCG_WinList ; //** T/F mismatch
694
DiffCG_Index = find(DiffCG_Winlist); //** recover the mismatched indexes only
696
for win_idx = DiffCG_Index
697
delete( scf( AfterCG_WinList(win_idx) ) ) ; //** clear the spurious windows
701
//**------------- end of windows cleaning for internal scopes -------------------------------
703
cpr.sim.funs=funs_save;
704
cpr.sim.funtyp=funtyp_save;
706
//** BEWARE: replace this OLD graphics instruction !
707
xset('window',%windo) ;
713
outtb = cpr.state.outtb;
716
zcptr = cpr.sim.zcptr;
717
ozptr = cpr.sim.ozptr;
718
rpptr = cpr.sim.rpptr;
719
ipptr = cpr.sim.ipptr;
720
opptr = cpr.sim.opptr;
724
inpptr = cpr.sim.inpptr;
725
inplnk = cpr.sim.inplnk;
726
outptr = cpr.sim.outptr;
727
outlnk = cpr.sim.outlnk;
729
// @l@n lnkptr = cpr.sim.lnkptr; ???
731
ordclk = cpr.sim.ordclk;
732
funtyp = cpr.sim.funtyp;
734
ncord = size(cord,1);
737
clkptr = cpr.sim.clkptr
739
// taille totale de z : nztotal
742
//*******************************
743
//Checking if superblock is valid
744
//*******************************
747
for i=1:length(funs)-1
750
msg = [msg;"Type 3 block''s not allowed"] ;
753
elseif funtyp(i)==0 & funs(i)~="bidon" then
754
msg = [msg;"Type 0 block''s"+" ''"+funs(i)+"''"+" not allowed"] ;
757
elseif (clkptr(i+1)-clkptr(i))<>0 &funtyp(i)>-1 &funs(i)~="bidon" then
759
//Alan // msg=[msg;'Regular block generating activation not allowed yet']
772
//** Find the clock connected to the SuperBlock and retreive
773
//** the sampling time
775
if XX.graphics.pein==[] | XX.graphics.pein(1)==0 then
776
sTsamp="0.001"; //** default value is ONE millisecond
778
o_ev = XX.graphics.pein(1);
779
o_ev=all_scs_m.objs(o_ev).from(1);
781
while (all_scs_m.objs(o_ev).gui~='CLOCK_c' & ...
782
all_scs_m.objs(o_ev).gui~='CLOCK_f' & ...
783
all_scs_m.objs(o_ev).gui~='SampleCLK')
785
o_ev = all_scs_m.objs(o_ev).graphics.pein(1);
786
o_ev = all_scs_m.objs(o_ev).from(1);
790
if all_scs_m.objs(o_ev).gui=='SampleCLK' then
791
sTsamp=all_scs_m.objs(o_ev).model.rpar(1);
792
sTsamp=sci2exp(sTsamp);
793
Tsamp_delay=all_scs_m.objs(o_ev).model.rpar(2);
794
Tsamp_delay=sci2exp(Tsamp_delay);
796
sTsamp=all_scs_m.objs(o_ev).model.rpar.objs(2).graphics.exprs(1);
797
sTsamp=sci2exp(eval(sTsamp));
798
Tsamp_delay=all_scs_m.objs(o_ev).model.rpar.objs(2).graphics.exprs(2);
799
Tsamp_delay=sci2exp(eval(Tsamp_delay));
804
//***********************************
805
// Get the name of the file
806
//***********************************
807
foo = 3; //** probably this variable is never used ?
812
Tsamp = sci2exp(eval(sTsamp));
814
template = ''; //** default values for this version
816
if XX.model.rpar.props.void3 == [] then
817
target = 'rtai'; //** default compilation chain
818
odefun = 'ode4'; //** default solver
819
odestep = '10'; //** default continous step size
821
target = XX.model.rpar.props.void3(1); //** user defined parameters
822
odefun = XX.model.rpar.props.void3(2);
823
odestep = XX.model.rpar.props.void3(3);
828
//** dialog box default variables
829
label1=[hname;getcwd()+'/'+hname+"_scig";target;template];
830
label2=[hname;getcwd()+'/'+hname+"_scig";target;template;odefun;odestep];
832
ode_x=['ode1';'ode2';'ode4']; //** available continous solver
834
//** Open a dialog box
838
//** Pure discrete system NO CONTINOUS blocks
840
[okk, rdnom, rpat,target,template,label1] = getvalue(..
841
'Embedded Code Generation',..
842
['New block''s name :';
843
'Created files Path:';
846
list('str',1,'str',1,'str',1,'str',1),label1);
848
//** continous blocks are presents
849
[okk,rdnom,rpat,target,template,odefun,odestep,label2] = getvalue(..
850
"Embedded Code Generation",..
851
["New block''s name: " ;
852
"Created files Path: " ;
855
"ODE solver type: " ;
856
"ODE solver steps betw. samples: "],..
857
list('str',1,'str',1,'str',1,'str',1,'str',1,'str',1),label2);
862
return ; //** EXIT point
864
rpat = stripblanks(rpat);
867
//** I put a warning here in order to inform the user
868
//** that the name of the superblock will change
869
//** because the space char in name isn't allowed.
870
if grep(rdnom," ")<>[] then
871
message(['Superblock name cannot contains space characters.';
872
'space chars will be automatically substituted by ""_"" '])
874
rdnom = strsubst(rdnom,' ','_');
876
//** Put a warning here in order to inform the user
877
//** that the name of the superblock will change
878
//** because the "-" char could generate GCC problems
879
//** (the C functions contains the name of the superblock).
880
if grep(rdnom,"-")<>[] then
881
message(['For full C compiler compatibility ';
882
'Superblock name cannot contains ""-"" characters';
883
'""-"" chars will be automatically substituted by ""_"" '])
886
rdnom = strsubst(rdnom,'-','_');
888
dirinfo = fileinfo(rpat)
891
[pathrp, fnamerp, extensionrp] = fileparts(rpat);
892
ok = mkdir(pathrp, fnamerp+extensionrp) ;
894
message("Directory '+rpat+' cannot be created");
896
elseif filetype(dirinfo(2))<>'Directory' then
898
message(rpat+" is not a directory");
901
if stripblanks(rdnom)==emptystr() then
903
message("Sorry: C file name not defined");
907
//** This comments will be moved in the documentation
909
//** /contrib/RT_templates/pippo.gen
914
//** pippo.mak : scheletro del Makefile
915
//** - GNU/Linux : Makefile template
916
//** - Windows/Erika : conf.oil
919
//** pippo.cmd : sequenza di comandi Scilab
922
TARGETDIR = SCI+"/contrib/RTAI/RT_templates";
925
[fd,ierr] = mopen(TARGETDIR+'/'+target+'.gen','r');
931
message("Target not valid " + target + ".gen");
935
target_t = mgetl(TARGETDIR+'/'+target+'.gen');
936
makfil = target_t(1);
937
cmdfil = target_t(2);
939
[fd,ierr]=mopen(TARGETDIR+'/'+makfil,'r');
944
message("Makefile not valid " + makfil);
949
if grep(odefun,ode_x) == [] then
950
message("Ode function not valid");
958
//////////////////////////////////////////////////
959
maxnrpar=max(rpptr(2:$)-rpptr(1:$-1))
960
maxnipar=max(ipptr(2:$)-ipptr(1:$-1))
961
maxnx=max(xptr(2:$)-xptr(1:$-1))
962
maxnz=max(zptr(2:$)-zptr(1:$-1))
963
maxnin=max(inpptr(2:$)-inpptr(1:$-1))
964
maxnout=max(outptr(2:$)-outptr(1:$-1))
966
for i=1:lstsize(cpr.state.outtb)
967
maxdim=max(size(cpr.state.outtb(i)))
969
maxtotal=max([maxnrpar;maxnipar;maxnx;maxnz;maxnin;maxnout;maxdim]);
971
//------------------ The real code generation is here ------------------------------------
973
//************************************************************************
974
//generate the call to the blocks and blocs simulation function prototypes
975
//************************************************************************
987
//** scan the data structure and call the generating functions
988
//** Substitute previous code!!!!
991
ki= find(funs(i) == dfuns) ; //**
992
dfuns = [dfuns; funs(i)] ;
995
Protostalone=[Protostalone;'';BlockProto(i)];
1000
//***********************************
1001
// Scilab and C files generation
1002
//***********************************
1004
cmdseq = mgetl(TARGETDIR+'/' + cmdfil);
1005
n_cmd = size(cmdseq,1);
1009
if (cmdseq(i)~="") then
1010
disp("Executing " + """" +cmdseq(i)+ """" + '...');
1017
disp("----> Target generation terminated!");
1021
//==========================================================================
1022
function t=filetype(m)
1024
filetypes=['Directory','Character device','Block device',...
1025
'Regular file','FIFO','Symbolic link','Socket']
1026
bits=[16384,8192,24576,32768,4096,40960,49152]
1027
m=int32(m)&int32(61440)
1028
t=filetypes(find(m==int32(bits)))
1031
//==========================================================================
1032
//Generates Code for dynamically linked Fortran and C Blocks
1034
// Original file from Project Metalau - INRIA
1035
// Modified for RT purposes by Roberto Bucher - RTAI Team
1036
// roberto.bucher@supsi.ch
1038
function [CCode,FCode]=gen_blocks()
1043
kdyn=find(funtyp>1000) //dynamically linked blocs
1044
//100X : Fortran blocks
1047
if (size(kdyn,'*') >1)
1049
//get the block data structure in the initial scs_m structure
1050
if size(corinv(kdyn(1)),'*')==1 then
1051
O=scs_m.objs(corinv(kdyn(1)));
1054
for l=corinv(kdyn(1))(1:$-1)
1060
path($+1)=corinv(kdyn(1))($);
1063
if funtyp(kdyn(1))>2000 then
1065
CCode=[CCode;O.graphics.exprs(2)]
1067
FCode=[FCode;O.graphics.exprs(2)]
1069
kfuns=funs(kdyn(1));
1070
for i=2:size(kdyn,'*')
1071
//get the block data structure in the initial scs_m structure
1072
if size(corinv(kdyn(i)),'*')==1 then
1073
O=scs_m.objs(corinv(kdyn(i)));
1076
for l=corinv(kdyn(i))(1:$-1)
1082
path($+1)=corinv(kdyn(i))($);
1085
if (find(kfuns==funs(kdyn(i))) == [])
1086
kfuns=[kfuns;funs(kdyn(i))];
1087
if funtyp(kdyn(i))>2000 then
1089
CCode=[CCode;O.graphics.exprs(2)]
1091
FCode=[FCode;O.graphics.exprs(2)]
1095
elseif (size(kdyn,'*')==1)
1096
//get the block data structure in the initial scs_m structure
1097
if size(corinv(kdyn),'*')==1 then
1098
O=scs_m.objs(corinv(kdyn));
1101
for l=corinv(kdyn)(1:$-1)
1107
path($+1)=corinv(kdyn)($);
1110
if funtyp(kdyn)>2000 then
1112
CCode=[CCode;O.graphics.exprs(2)]
1114
FCode=[FCode;O.graphics.exprs(2)]
1118
CCode=['void no_ccode()'
1125
//==========================================================================
1126
//get_blank : return blanks with a length
1127
// of the given input string
1129
//input : str : a string
1131
//output : txt : blanks
1133
//16/06/07 Author : A.Layec
1135
function [txt] = get_blank(str)
1142
//==========================================================================
1143
// get_comment : return a C comment
1144
// for generated code
1146
//input : typ : a string
1149
//output : a C comment
1151
//16/06/07 Author : A.Layec
1153
function [txt]=get_comment(typ,param)
1160
txt = '/* Continuous state computation */'
1162
txt = '/* Output computation */'
1164
txt = '/* Discrete state computation */'
1166
txt = '/* Output Event computation */'
1168
txt = '/* Initialization */'
1170
txt = '/* Ending */'
1172
txt = '/* Update zero crossing surfaces */'
1174
//** blocks activated on event number
1176
txt = '/* Blocks activated on the event number '+string(param(1))+' */'
1178
//** blk calling sequence
1179
case 'call_blk' then
1180
txt = ['/* Call of '''+param(1) + ...
1181
''' (type '+string(param(2))+' - blk nb '+...
1183
if ztyp(param(3)) then
1184
txt=txt+' - with zcross) */';
1188
//** proto calling sequence
1189
case 'proto_blk' then
1190
txt = ['/* prototype of '''+param(1) + ...
1191
''' (type '+string(param(2))];
1192
if ztyp(param(3)) then
1193
txt=txt+' - with zcross) */';
1197
//** ifthenelse calling sequence
1198
case 'ifthenelse_blk' then
1199
txt = ['/* Call of ''if-then-else'' blk (blk nb '+...
1200
string(param(1))+') */']
1201
//** eventselect calling sequence
1202
case 'evtselect_blk' then
1203
txt = ['/* Call of ''event-select'' blk (blk nb '+...
1204
string(param(1))+') */']
1205
//** set block structure
1207
txt = ['/* set blk struc. of '''+param(1) + ...
1208
''' (type '+string(param(2))+' - blk nb '+...
1209
string(param(3))+') */'];
1210
//** Update xd vector ptr
1211
case 'update_xd' then
1212
txt = ['/* Update xd vector ptr */'];
1213
//** Update g vector ptr
1214
case 'update_g' then
1215
txt = ['/* Update g vector ptr */'];
1221
//==========================================================================
1222
//generates code of the standalone simulation procedure
1226
// rmq : La fonction zdoit n'est pas utilis�e pour le moment
1228
// Original file from Project Metalau - INRIA
1229
// Modified for RT purposes by Roberto Bucher - RTAI Team
1230
// roberto.bucher@supsi.ch
1232
function [Code,Code_common]=make_standalone42()
1235
modptr=cpr.sim.modptr;
1236
rpptr=cpr.sim.rpptr;
1237
ipptr=cpr.sim.ipptr;
1238
opptr=cpr.sim.opptr;
1243
ordptr=cpr.sim.ordptr;
1247
tevts=cpr.state.tevts;
1248
evtspt=cpr.state.evtspt;
1250
clkptr=cpr.sim.clkptr;
1251
ordptr=cpr.sim.ordptr;
1252
pointi=cpr.state.pointi;
1254
noord=size(cpr.sim.oord,1);
1255
nzord=size(cpr.sim.zord,1);
1256
niord=size(cpr.sim.iord,1);
1259
Indent2=Indent+Indent;
1263
Z=[z;zeros(lstsize(outtb),1);work]';
1269
Code=['/* Code prototype for standalone use */'
1270
'/* Generated by Code_Generation toolbox of Scicos with '+ ..
1272
'/* date : '+date()+' */'
1274
'/* ---- Headers ---- */'
1275
'#include <stdlib.h>'
1277
'#include <string.h>'
1278
'#include <memory.h>'
1279
'#include <scicos_block4.h>'
1280
'#include <machine.h>'
1285
'#define __CONST__ static const'
1288
'double '+rdnom+'_get_tsamp()'
1290
' return(' + string(Tsamp) + ');'
1293
'double '+rdnom+'_get_tsamp_delay()'
1296
' return(' + string(Tsamp_delay) + ');'
1299
'/* ---- Internals functions declaration ---- */'
1300
'int '+rdnom+'_init(void);'
1301
'int '+rdnom+'_isr(double);'
1302
'int '+rdnom+'_end(void);'
1308
'/* Code prototype for standalone use */'
1309
'int C2F('+rdnom+'simblk)(double , double *, double *);'
1310
'extern int C2F(dset)();'
1311
'static int ode1();'
1312
'static int ode2();'
1313
'static int ode4();'
1319
'/* Some general static variables */'
1320
'static double zero=0;'
1321
'static double w[1];'
1322
'void **'+rdnom+'_block_outtbptr;'] //** !!
1325
make_static_standalone42()]
1328
' /* Initial values */'
1330
' /* Note that z[]=[z_initial_condition;outtbptr;work]'
1331
cformatline(' z_initial_condition={'+...
1332
strcat(string(z),",")+'};',70)
1333
cformatline(' outtbptr={'+...
1334
strcat(string(zeros(lstsize(outtb),1)),"," )+'};',70)
1335
cformatline(' work= {'+...
1336
strcat(string(work),"," )+'};',70)
1339
cformatline(' static double z[]={'+strcat(string(Z),',')+'};',70)
1342
//** declaration of outtb
1344
for i=1:lstsize(outtb)
1345
if mat2scs_c_nb(outtb(i)) <> 11 then
1346
Code_outtb=[Code_outtb;
1347
cformatline(' static '+mat2c_typ(outtb(i))+...
1348
' outtb_'+string(i)+'[]={'+...
1349
strcat(string(outtb(i)(:)),',')+'};',70)]
1350
else //** cmplx test
1351
Code_outtb=[Code_outtb;
1352
cformatline(' static '+mat2c_typ(outtb(i))+...
1353
' outtb_'+string(i)+'[]={'+...
1354
strcat(string([real(outtb(i)(:));
1355
imag(outtb(i)(:))]),',')+'};',70)]
1367
'/*'+part('-',ones(1,40))+' Initialisation function */'
1368
'int '+rdnom+'_init()'
1373
// ' double *args[2];'
1378
if size(z,1) <> 0 then
1379
for i=1:(length(zptr)-1)
1380
if zptr(i+1)-zptr(i)>0 then
1381
if size(corinv(i),'*')==1 then
1382
OO=scs_m.objs(corinv(i))
1385
for l=cpr.corinv(i)(1:$-1)
1386
path($+1)=l;path($+1)='model'
1390
path($+1)=cpr.corinv(i)($)
1395
if and(aaa+bbb~=['INPUTPORTEVTS';'OUTPUTPORTEVTS';'EVTGEN_f']) then
1397
Code2($+1)=' /* Routine name of block: '+strcat(string(cpr.sim.funs(i)));
1398
Code2($+1)=' Gui name of block: '+strcat(string(OO.gui));
1399
//Code2($+1)='/* Name block: '+strcat(string(cpr.sim.funs(i)));
1400
//Code2($+1)='Object number in diagram: '+strcat(string(cpr.corinv(i)));
1401
Code2($+1)=' Compiled structure index: '+strcat(string(i));
1402
if stripblanks(OO.model.label)~=emptystr() then
1404
cformatline(' Label: '+strcat(string(OO.model.label)),70)]
1406
if stripblanks(OO.graphics.exprs(1))~=emptystr() then
1408
cformatline(' Exprs: '+strcat(OO.graphics.exprs(1),","),70)]
1410
if stripblanks(OO.graphics.id)~=emptystr() then
1412
cformatline(' Identification: '+..
1413
strcat(string(OO.graphics.id)),70)]
1416
cformatline(' z={'+...
1417
strcat(string(z(zptr(i):zptr(i+1)-1)),",")+'};',70)]
1424
//** declaration of oz
1427
if mat2scs_c_nb(oz(i)) <> 11 then
1429
cformatline(' '+mat2c_typ(oz(i))+...
1430
' oz_'+string(i)+'[]={'+...
1431
strcat(string(oz(i)(:)),',')+'};',70)]
1432
else //** cmplx test
1434
cformatline(' '+mat2c_typ(oz(i))+...
1435
' oz_'+string(i)+'[]={'+...
1436
strcat(string([real(oz(i)(:));
1437
imag(oz(i)(:))]),',')+'};',70)]
1441
if Code_oz <> [] then
1443
' /* oz declaration */'
1448
' /* Get work ptr of blocks */'
1450
' work = (void **)(z+'+string(size(z,'*')+lstsize(outtb))+');'
1454
if Code_outtb<>[] then
1456
' /* outtbptr declaration */'
1457
' '+rdnom+'_block_outtbptr = (void **)(z+'+string(nztotal)+');'
1462
for i=1:lstsize(outtb)
1463
Code_outtbptr=[Code_outtbptr;
1464
' '+rdnom+'_block_outtbptr['+...
1465
string(i-1)+'] = (void *) outtb_'+string(i)+';'];
1468
if Code_outtbptr<>[] then
1475
nx=xptr(kf+1)-xptr(kf); //** number of continuous state
1476
nin=inpptr(kf+1)-inpptr(kf); //** number of input ports
1477
nout=outptr(kf+1)-outptr(kf); //** number of output ports
1480
txt=[get_comment('set_blk',list(funs(kf),funtyp(kf),kf));]
1485
flex_ng = zcptr(kf+1)-zcptr(kf);
1486
flex_nz = zptr(kf+1)-zptr(kf);
1487
flex_noz = ozptr(kf+1)-ozptr(kf);
1488
flex_nin = inpptr(kf+1)-inpptr(kf);
1489
flex_nout = outptr(kf+1)-outptr(kf);
1490
flex_nevout = clkptr(kf+1)-clkptr(kf);
1491
flex_nopar = opptr(kf+1)-opptr(kf);
1494
' block_'+rdnom+'['+string(kf-1)+'].type = '+string(funtyp(kf))+';';
1495
' block_'+rdnom+'['+string(kf-1)+'].ztyp = '+string(ztyp(kf))+';';
1496
' block_'+rdnom+'['+string(kf-1)+'].ng = '+string(flex_ng)+';';
1497
' block_'+rdnom+'['+string(kf-1)+'].nz = '+string(flex_nz)+';';
1498
' block_'+rdnom+'['+string(kf-1)+'].noz = '+string(flex_noz)+';';
1499
' block_'+rdnom+'['+string(kf-1)+'].nrpar = '+string(rpptr(kf+1)-rpptr(kf))+';';
1500
' block_'+rdnom+'['+string(kf-1)+'].nopar = '+string(flex_nopar)+';';
1501
' block_'+rdnom+'['+string(kf-1)+'].nipar = '+string(ipptr(kf+1)-ipptr(kf))+';'
1502
' block_'+rdnom+'['+string(kf-1)+'].nin = '+string(flex_nin)+';';
1503
' block_'+rdnom+'['+string(kf-1)+'].nout = '+string(flex_nout)+';';
1504
' block_'+rdnom+'['+string(kf-1)+'].nevout = '+string(flex_nevout)+';';
1505
' block_'+rdnom+'['+string(kf-1)+'].nmode = '+string(modptr(kf+1)-modptr(kf))+';';]
1509
' block_'+rdnom+'['+string(kf-1)+'].nx = '+string(nx)+';';
1510
' block_'+rdnom+'['+string(kf-1)+'].x = &(x['+string(xptr(kf)-1)+']);'
1511
' block_'+rdnom+'['+string(kf-1)+'].xd = &(xd['+string(xptr(kf)-1)+']);']
1514
if flex_nevout <> 0 then
1515
flex_str = rdnom +'_'+string(kf-1)+'_evout'
1517
'double '+flex_str+'['+string(flex_nevout)+'];'
1520
' block_'+rdnom+'['+string(kf-1)+'].evout = '+flex_str+';';
1524
//***************************** input port *****************************//
1525
if flex_nin <> 0 then
1526
flex_str = rdnom +'_'+string(kf-1)+'_inptr'
1528
'double * '+flex_str+'['+string(flex_nin)+'];'
1531
' block_'+rdnom+'['+string(kf-1)+'].inptr = '+flex_str+';';
1533
flex_str = rdnom +'_'+string(kf-1)+'_insz'
1536
' block_'+rdnom+'['+string(kf-1)+'].insz = '+flex_str+';';
1541
lprt=inplnk(inpptr(kf)-1+k);
1543
' block_'+rdnom+'['+string(kf-1)+'].inptr['+string(k-1)+...
1544
'] = '+rdnom+'_block_outtbptr['+string(lprt-1)+'];']
1548
szCode='const int '+flex_str+'['+string(3*flex_nin)+']={'
1550
lprt=inplnk(inpptr(kf)-1+k);
1551
szCode=szCode+string(size(outtb(lprt),1))+','
1556
lprt=inplnk(inpptr(kf)-1+k);
1557
szCode=szCode+string(size(outtb(lprt),2))+','
1562
lprt=inplnk(inpptr(kf)-1+k);
1563
szCode=szCode+mat2scs_c_typ(outtb(lprt))+','
1565
szCode=part(szCode,1:length(szCode)-1)+'};'
1571
//***************************** output port *****************************//
1572
if flex_nout <> 0 then
1573
flex_str = rdnom +'_'+string(kf-1)+'_outptr'
1575
'double * '+flex_str+'['+string(flex_nout)+'];'
1578
' block_'+rdnom+'['+string(kf-1)+'].outptr = '+flex_str+';';
1580
flex_str = rdnom +'_'+string(kf-1)+'_outsz'
1582
' block_'+rdnom+'['+string(kf-1)+'].outsz = '+flex_str+';';
1587
lprt=outlnk(outptr(kf)-1+k);
1589
' block_'+rdnom+'['+string(kf-1)+'].outptr['+string(k-1)+...
1590
'] = '+rdnom+'_block_outtbptr['+string(lprt-1)+'];']
1594
szCode='const int '+flex_str+'['+string(3*flex_nout)+']={'
1596
lprt=outlnk(outptr(kf)-1+k);
1597
szCode=szCode+string(size(outtb(lprt),1))+','
1602
lprt=outlnk(outptr(kf)-1+k);
1603
szCode=szCode+string(size(outtb(lprt),2))+','
1608
lprt=outlnk(outptr(kf)-1+k);
1609
szCode=szCode+mat2scs_c_typ(outtb(lprt))+','
1611
szCode=part(szCode,1:length(szCode)-1)+'};'
1617
//**********************************************************************//
1619
' block_'+rdnom+'['+string(kf-1)+...
1620
'].z = &(z['+string(zptr(kf)-1)+']);']
1622
if (part(funs(kf),1:7) ~= 'capteur' &...
1623
part(funs(kf),1:10) ~= 'actionneur' &...
1624
funs(kf) ~= 'bidon') then
1626
if (rpptr(kf+1)-rpptr(kf)>0) then
1628
' block_'+rdnom+'['+string(kf-1)+...
1629
'].rpar=&(RPAR['+string(rpptr(kf)-1)+']);']
1632
if (ipptr(kf+1)-ipptr(kf)>0) then
1634
' block_'+rdnom+'['+string(kf-1)+...
1635
'].ipar=&(IPAR['+string(ipptr(kf)-1)+']);']
1638
//**********************************************************************//
1641
if flex_nopar<> 0 then
1642
flex_str = rdnom +'_'+string(kf-1)+'_oparptr'
1644
'void * '+flex_str+'['+string(flex_nopar)+'];'
1647
' block_'+rdnom+'['+string(kf-1)+'].oparptr = '+flex_str+';';
1650
flex_str = rdnom +'_'+string(kf-1)+'_oparsz'
1652
' block_'+rdnom+'['+string(kf-1)+'].oparsz = '+flex_str+';';
1655
flex_str = rdnom +'_'+string(kf-1)+'_opartyp'
1657
' block_'+rdnom+'['+string(kf-1)+'].opartyp = '+flex_str+';';
1664
' block_'+rdnom+'['+string(kf-1)+'].oparptr['+string(k-1)+...
1665
'] = (void *) OPAR_'+string(opptr(kf)-1+k)+';'];
1668
szCode='const int '+rdnom+'_'+string(kf-1)+'_oparsz['+string(2*flex_nopar)+']={';
1670
szCode=szCode+string(size(opar(opptr(kf)-1+k),1))+',';
1674
szCode=szCode+string(size(opar(opptr(kf)-1+k),2))+',';
1676
szCode=part(szCode,1:length(szCode)-1)+'};'
1682
szCode='const int '+rdnom+'_'+string(kf-1)+'_opartyp['+string(flex_nopar)+']={';
1684
szCode=szCode+mat2scs_c_typ(opar(opptr(kf)-1+k))+',';
1686
szCode=part(szCode,1:length(szCode)-1)+'};'
1692
//**********************************************************************//
1696
flex_str = rdnom +'_'+string(kf-1)+'_ozptr'
1698
'void * '+flex_str+'['+string(flex_noz)+'];'
1701
' block_'+rdnom+'['+string(kf-1)+'].ozptr = '+flex_str+';';
1704
flex_str = rdnom +'_'+string(kf-1)+'_ozsz'
1706
' block_'+rdnom+'['+string(kf-1)+'].ozsz = '+flex_str+';';
1709
flex_str = rdnom +'_'+string(kf-1)+'_oztyp'
1711
' block_'+rdnom+'['+string(kf-1)+'].oztyp = '+flex_str+';';
1717
' block_'+rdnom+'['+string(kf-1)+'].ozptr['+string(k-1)+...
1718
'] = (void *) oz_'+string(ozptr(kf)-1+k)+';'];
1721
szCode='const int '+rdnom+'_'+string(kf-1)+'_ozsz['+string(2*flex_noz)+']={';
1723
szCode=szCode+string(size(oz(ozptr(kf)-1+k),1))+',';
1727
szCode=szCode+string(size(oz(ozptr(kf)-1+k),2))+',';
1729
szCode=part(szCode,1:length(szCode)-1)+'};'
1735
szCode='int '+rdnom+'_'+string(kf-1)+'_oztyp['+string(flex_noz)+']={';
1737
szCode=szCode+mat2scs_c_typ(oz(ozptr(kf)-1+k))+',';
1739
szCode=part(szCode,1:length(szCode)-1)+'};'
1746
' block_'+rdnom+'['+string(kf-1)+'].work = '+...
1747
'(void **)(((double *)work)+'+string(kf-1)+');']
1754
' '+get_comment('flag',list(4))]
1757
// if or(kf==act) | or(kf==cap) then
1758
// txt = call_block42(kf,0,4);
1759
// if txt <> [] then
1765
txt = call_block42(kf,0,4);
1774
//** cst blocks and it's dep
1775
txt=write_code_idoit()
1780
' /* Initial blocks must be called with flag 1 */'
1784
' return(local_flag);'
1789
'/*'+part('-',ones(1,40))+' ISR function */'
1790
'int '+rdnom+'_isr(double t)'
1796
// ' double *args[2];'
1802
' double tout, dt, he, h;'
1806
//** find source activation number
1807
blks=find(funtyp>-1);
1811
for ev=clkptr(blk):clkptr(blk+1)-1
1812
if funs(blk)=='bidon' then
1813
if ev > clkptr(howclk) -1
1825
//** continuous time blocks must be activated
1828
txt = write_code_cdoit(flag);
1832
' '+get_comment('ev',list(0))
1838
//** blocks with input discrete event must be activated
1839
//** for flag 1, 2 and 3
1840
if size(evs,2)>=1 then
1844
txt2=write_code_doit(ev,flag);
1846
//** adjust event number because of bidon block
1847
new_ev=ev-(clkptr(howclk)-1)
1850
// Indent+['case '+string(new_ev)+' : '+...
1851
// get_comment('ev',list(new_ev))
1852
Indent+[get_comment('ev',list(new_ev))
1862
Indent+'/* Discrete activations */'
1863
// Indent+'switch (nevprt) {'
1873
' '+get_comment('flag',list(flag))
1882
' dt='+rdnom+'_get_tsamp();'
1883
' h=dt/'+odestep+';'
1884
' while (tout+h<t+dt){'
1885
' '+odefun+'(C2F('+rdnom+'simblk),tout,h);'
1890
' '+odefun+'(C2F('+rdnom+'simblk),tout,he);'
1894
//** fix bug provided by Roberto Bucher
1899
' /* update ptrs of continuous array */']
1901
nx=xptr(kf+1)-xptr(kf); //** number of continuous state
1904
' block_'+rdnom+'['+string(kf-1)+'].nx = '+...
1906
' block_'+rdnom+'['+string(kf-1)+'].x = '+...
1907
'&(x['+string(xptr(kf)-1)+']);'
1908
' block_'+rdnom+'['+string(kf-1)+'].xd = '+...
1909
'&(xd['+string(xptr(kf)-1)+']);']
1922
'/*'+part('-',ones(1,40))+' Termination function */'
1923
'int '+rdnom+'_end()'
1928
// ' double *args[2];'
1933
' '+get_comment('flag',list(5))]
1936
// if or(kf==act) | or(kf==cap) then
1937
// txt = call_block42(kf,0,5);
1938
// if txt <> [] then
1944
txt = call_block42(kf,0,5);
1958
Code_common=['/* Code prototype for common use */'
1959
'/* Generated by Code_Generation toolbox of Scicos with '+ ..
1961
'/* date : '+date()+' */'
1963
'/* ---- Headers ---- */'
1964
'#include <memory.h>'
1965
'#include '"machine.h'"'
1968
if(isempty(grep(SCI,'5.1.1'))) then
1969
Code_common=[Code_common
1970
'/*'+part('-',ones(1,40))+' Lapack messag function */';
1971
'void C2F(xerbla)(SRNAME,INFO,L)'
1979
Code_common=[Code_common
1980
'void set_block_error(int err)'
1985
'int get_phase_simulation()'
1990
'void * scicos_malloc(size_t size)'
1992
' return malloc(size);'
1995
'void scicos_free(void *p)'
2000
'void do_cold_restart()'
2005
'void sciprint (char *fmt)'
2013
'int C2F('+rdnom+'simblk)(t, xc, xdc)'
2015
' double t, *xc, *xdc;'
2019
' * compute state derivative of the continuous part'
2020
' * !calling sequence'
2021
' * neq : integer the size of the continuous state'
2022
' * t : current time'
2023
' * xc : double precision vector whose contains the continuous state'
2024
' * xdc : double precision vector, contain the computed derivative'
2033
// ' double *args[2];'
2035
' C2F(dset)(&neq, &c_b14,xd , &c__1);'
2039
' '+get_comment('update_xd',list())]
2042
if (xptr(kf+1)-xptr(kf)) > 0 then
2044
' block_'+rdnom+'['+string(kf-1)+'].x='+...
2045
'&(xc['+string(xptr(kf)-1)+']);'
2046
' block_'+rdnom+'['+string(kf-1)+'].xd='+...
2047
'&(xdc['+string(xptr(kf)-1)+']);']
2058
if (xptr(kf+1)-xptr(kf)) > 0 then
2060
' block_'+rdnom+'['+string(kf-1)+'].x='+...
2061
'&(x['+string(xptr(kf)-1)+']);'
2062
' block_'+rdnom+'['+string(kf-1)+'].xd='+...
2063
'&(xd['+string(xptr(kf)-1)+']);']
2072
'/* Euler''s Method */'
2073
'static int ode1(f,t,h)'
2082
' for (i=0;i<neq;i++) {'
2083
' x[i]=x[i]+h*xd[i];'
2089
'/* Heun''s Method */'
2090
'static int ode2(f,t,h)'
2095
' double y['+string(nX)+'],yh['+string(nX)+'],temp,f0['+string(nX)+'],th;'
2098
' memcpy(y,x,neq*sizeof(double));'
2099
' memcpy(f0,xd,neq*sizeof(double));'
2105
' for (i=0;i<neq;i++) {'
2106
' x[i]=y[i]+h*f0[i];'
2109
' for (i=0;i<neq;i++) {'
2110
' yh[i]=y[i]+h*f0[i];'
2116
' for (i=0;i<neq;i++) {'
2117
' x[i]=y[i]+temp*(f0[i]+xd[i]);'
2123
'/* Fourth-Order Runge-Kutta (RK4) Formula */'
2124
'static int ode4(f,t,h)'
2129
' double y['+string(nX)+'],yh['+string(nX)+'],'+...
2130
'temp,f0['+string(nX)+'],th,th2,'+...
2131
'f1['+string(nX)+'],f2['+string(nX)+'];'
2134
' memcpy(y,x,neq*sizeof(double));'
2135
' memcpy(f0,xd,neq*sizeof(double));'
2141
' for (i=0;i<neq;i++) {'
2142
' x[i]=y[i]+h*f0[i];'
2145
' for (i=0;i<neq;i++) {'
2146
' yh[i]=y[i]+(h/2)*f0[i];'
2148
' (*f)(th2,yh, f1);'
2152
' for (i=0;i<neq;i++) {'
2153
' x[i]=y[i]+temp*f1[i];'
2155
' for (i=0;i<neq;i++) {'
2156
' yh[i]=y[i]+(h/2)*f1[i];'
2158
' (*f)(th2,yh, f2);'
2161
' for (i=0;i<neq;i++) {'
2162
' x[i]=y[i]+h*f2[i];'
2165
' for (i=0;i<neq;i++) {'
2166
' yh[i]=y[i]+h*f2[i];'
2168
' (*f)(th2,yh, xd);'
2172
' for (i=0;i<neq;i++) {'
2173
' x[i]=y[i]+temp*(f0[i]+2.0*f1[i]+2.0*f2[i]+xd[i]);'
2181
//==========================================================================
2182
//generates static table definitions
2184
//Author : Rachid Djenidi, Alan Layec
2187
// Modified for RT purposes by Roberto Bucher - RTAI Team
2188
// roberto.bucher@supsi.ch
2190
function txt=make_static_standalone42()
2194
//*** Continuous state ***//
2197
'/* def continuous state */'
2198
cformatline('static double x[]={'+strcat(string(x),',')+'};',70)
2199
cformatline('static double xd[]={'+strcat(string(x),',')+'};',70)
2200
'static int c__1 = 1;'
2201
'static double c_b14 = 0.;'
2202
'static int neq='+string(nX)+';'
2205
//************************//
2208
'scicos_block block_'+rdnom+'['+string(nblk)+'];'
2211
//*** Real parameters ***//
2212
nbrpa=0;strRCode='';lenRCode=[];ntot_r=0;
2213
if size(rpar,1) <> 0 then
2215
'/* def real parameters */'
2216
'__CONST__ double RPAR[ ] = {'];
2218
for i=1:(length(rpptr)-1)
2219
if rpptr(i+1)-rpptr(i)>0 then
2221
if size(corinv(i),'*')==1 then
2222
OO=scs_m.objs(corinv(i));
2225
for l=cpr.corinv(i)(1:$-1)
2231
path($+1)=cpr.corinv(i)($);
2235
//** Add comments **//
2237
ntot_r = ntot_r + (rpptr(i+1)-rpptr(i));
2238
txt($+1)='/* Routine name of block: '+strcat(string(cpr.sim.funs(i)));
2239
txt($+1)=' * Gui name of block: '+strcat(string(OO.gui));
2240
txt($+1)=' * Compiled structure index: '+strcat(string(i));
2242
if stripblanks(OO.model.label)~=emptystr() then
2243
txt=[txt;cformatline(' * Label: '+strcat(string(OO.model.label)),70)];
2245
if stripblanks(OO.graphics.exprs(1))~=emptystr() then
2246
txt=[txt;cformatline(' * Exprs: '+strcat(OO.graphics.exprs(1),","),70)];
2248
if stripblanks(OO.graphics.id)~=emptystr() then
2249
str_id = string(OO.graphics.id);
2251
str_id = 'RPARAM[' + string(nbrpa) +']';
2254
cformatline(' * Identification: '+strcat(string(OO.graphics.id)),70)];
2255
txt=[txt;cformatline('rpar= {'+strcat(string(rpar(rpptr(i):rpptr(i+1)-1)),",")+'};',70)];
2257
//******************//
2260
cformatline(strcat(msprintf('%.16g,\n',rpar(rpptr(i):rpptr(i+1)-1))),70);
2262
strRCode = strRCode + '""' + str_id + '"",';
2263
lenRCode = lenRCode + string(rpptr(i+1)-rpptr(i)) + ',';
2270
txt($+1)='double RPAR[1];';
2277
txt($+1) = 'int NRPAR = '+string(nbrpa)+';';
2278
txt($+1) = 'int NTOTRPAR = '+string(ntot_r)+';';
2280
strRCode = 'char * strRPAR[' + string(nbrpa) + '] = {' + ..
2281
part(strRCode,[1:length(strRCode)-1]) + '};';
2284
txt($+1) = strRCode;
2285
lenRCode = 'int lenRPAR[' + string(nbrpa) + '] = {' + ..
2286
part(lenRCode,[1:length(lenRCode)-1]) + '};';
2288
txt($+1) = 'char * strRPAR;'
2289
lenRCode = 'int lenRPAR[1] = {0};'
2291
txt($+1) = lenRCode;
2297
//***********************//
2299
//*** Integer parameters ***//
2300
nbipa=0;strICode='';lenICode=[];ntot_i=0;
2301
if size(ipar,1) <> 0 then
2303
'/* def integer parameters */'
2304
'__CONST__ int IPAR[ ] = {'];
2306
for i=1:(length(ipptr)-1)
2307
if ipptr(i+1)-ipptr(i)>0 then
2308
if size(corinv(i),'*')==1 then
2309
OO=scs_m.objs(corinv(i));
2312
for l=cpr.corinv(i)(1:$-1)
2318
path($+1)=cpr.corinv(i)($);
2322
//** Add comments **//
2324
ntot_i = ntot_i + (ipptr(i+1)-ipptr(i));
2325
txt($+1)='/* Routine name of block: '+strcat(string(cpr.sim.funs(i)));
2326
txt($+1)=' * Gui name of block: '+strcat(string(OO.gui));
2327
txt($+1)=' * Compiled structure index: '+strcat(string(i));
2328
if stripblanks(OO.model.label)~=emptystr() then
2329
txt=[txt;cformatline(' * Label: '+strcat(string(OO.model.label)),70)];
2332
if stripblanks(OO.graphics.exprs(1))~=emptystr() then
2334
cformatline(' * Exprs: '+strcat(OO.graphics.exprs(1),","),70)];
2337
if stripblanks(OO.graphics.id)~=emptystr() then
2338
str_id = string(OO.graphics.id);
2340
str_id = 'IPARAM[' + string(nbipa) +']';
2344
cformatline(' * Identification: '+strcat(string(OO.graphics.id)),70)];
2345
txt=[txt;cformatline('ipar= {'+strcat(string(ipar(ipptr(i):ipptr(i+1)-1)),",")+'};',70)];
2348
//******************//
2350
txt=[txt;cformatline(strcat(string(ipar(ipptr(i):ipptr(i+1)-1))+','),70)];
2351
strICode = strICode + '""' + str_id + '"",';
2352
lenICode = lenICode + string(ipptr(i+1)-ipptr(i)) + ',';
2358
txt($+1)='int IPAR[1];';
2365
txt($+1) = 'int NIPAR = '+string(nbipa)+';';
2366
txt($+1) = 'int NTOTIPAR = '+string(ntot_i)+';';
2368
strICode = 'char * strIPAR[' + string(nbipa) + '] = {' + ..
2369
part(strICode,[1:length(strICode)-1]) + '};';
2372
txt($+1) = strICode;
2373
lenICode = 'int lenIPAR[' + string(nbipa) + '] = {' + ..
2374
part(lenICode,[1:length(lenICode)-1]) + '};';
2376
txt($+1) = 'char * strIPAR;'
2377
lenICode = 'int lenIPAR[1] = {0};'
2379
txt($+1) = lenICode;
2385
//**************************//
2387
//Alan added opar (27/06/07)
2388
//*** Object parameters ***//
2389
if lstsize(opar)<>0 then
2391
'/* def object parameters */']
2392
for i=1:(length(opptr)-1)
2393
if opptr(i+1)-opptr(i)>0 then
2395
if size(corinv(i),'*')==1 then
2396
OO=scs_m.objs(corinv(i));
2399
for l=cpr.corinv(i)(1:$-1)
2405
path($+1)=cpr.corinv(i)($);
2409
//** Add comments **//
2411
txt($+1)='/* Routine name of block: '+strcat(string(cpr.sim.funs(i)));
2412
txt($+1)=' * Gui name of block: '+strcat(string(OO.gui));
2413
txt($+1)=' * Compiled structure index: '+strcat(string(i));
2414
if stripblanks(OO.model.label)~=emptystr() then
2415
txt=[txt;cformatline(' * Label: '+strcat(string(OO.model.label)),70)];
2417
if stripblanks(OO.graphics.id)~=emptystr() then
2419
cformatline(' * Identification: '+strcat(string(OO.graphics.id)),70)];
2422
//******************//
2424
for j=1:opptr(i+1)-opptr(i)
2426
cformatline('static __CONST__ '+mat2c_typ(opar(opptr(i)+j-1)) +...
2427
' OPAR_'+string(opptr(i)+j-1) + '[] = {'+...
2428
strcat(string(opar(opptr(i)+j-1)),',')+'};',70)]
2433
//*************************//
2439
//==========================================================================
2440
//mat2c_typ : matrix to C type
2441
//sci2c_ttyp : get the C string of a scicos type
2443
//input : outtb : a matrix
2445
//output : txt : the string of the C scicos type
2446
// of the data of outtb
2448
//16/06/07 Author : A.Layec
2450
function [txt]=mat2c_typ(outtb)
2454
if isreal(outtb) then
2461
select typeof(outtb)
2469
txt = "unsigned long"
2471
txt = "unsigned short"
2473
txt = "unsigned char"
2481
//==========================================================================
2482
//mat2scs_c_nb matrix to scicos C number (sci2sci_n)
2484
//input : outtb : a matrix
2486
//output : c_nb : the scicos C number
2488
//16/06/07 Author : A.Layec
2490
function [c_nb]=mat2scs_c_nb(outtb)
2494
if isreal(outtb) then
2501
select typeof(outtb)
2520
//==========================================================================
2521
//mat2scs_c_ptr matrix to scicos C ptr (sci2c_typ)
2523
//input : outtb : a matrix
2525
//output : txt : the string of the C scicos type
2526
// of the data of outtb
2528
//16/06/07 Author : A.Layec
2530
function [txt]=mat2scs_c_ptr(outtb)
2534
if isreal(outtb) then
2537
txt = "SCSCOMPLEX_COP"
2541
select typeof(outtb)
2543
txt = "SCSINT32_COP"
2545
txt = "SCSINT16_COP"
2549
txt = "SCSUINT32_COP"
2551
txt = "SCSUINT16_COP"
2553
txt = "SCSUINT8_COP"
2560
//==========================================================================
2561
//mat2scs_c_typ matrix to scicos C type
2563
//input : outtb : a matrix
2565
//output : txt : the string of the C scicos type
2566
// of the data of outtb
2568
//16/06/07 Author : A.Layec
2570
function [txt]=mat2scs_c_typ(outtb)
2574
if isreal(outtb) then
2577
txt = "SCSCOMPLEX_N"
2581
select typeof(outtb)
2600
// Modified for RT purposes by Roberto Bucher - RTAI Team
2601
// roberto.bucher@supsi.ch
2603
function rename(folder,newname,ext)
2604
oldname=folder+'/Makefile';
2605
newname=folder+'/'+newname;
2607
T=strsubst(T,'.obj','.o');
2608
T=strsubst(T,'.o',ext);
2609
T=strsubst(T,SCI,WSCI);
2614
//==========================================================================
2616
// Modified for RT purposes by Roberto Bucher - RTAI Team
2617
// roberto.bucher@supsi.ch
2619
function Makename=rt_gen_make(name,files,libs)
2621
Makename=rpat+'/Makefile';
2623
T=mgetl(TARGETDIR+'/'+makfil);
2624
T=strsubst(T,'$$MODEL$$',name);
2625
T=strsubst(T,'$$OBJ$$',strcat(files+'.o',' '));
2626
T=strsubst(T,'$$SCILAB_DIR$$',SCI);
2633
//==========================================================================
2634
//scs_c_n2c_fmt : scicos C number to C format
2636
//input : c_nb : a C scicos type
2638
//output : txt : the string of the C format string
2639
// of the data of outtb
2641
//16/06/07 Author : A.Layec
2643
function [txt]=scs_c_n2c_fmt(c_nb)
2675
//==========================================================================
2676
//scs_c_n2c_typ scicos C number to C type
2678
//input : c_nb : a C scicos number
2680
//output : txt : the string of the C format string
2681
// of the data of outtb
2683
//16/06/07 Author : A.Layec
2685
function [txt]=scs_c_n2c_typ(c_nb)
2704
txt = 'unsigned char';
2707
txt = 'unsigned short';
2710
txt = 'unsigned long';
2717
//==========================================================================
2718
//scs_c_nb2scs_nb : scicos C number to scicos number
2720
//input : c_nb : the scicos C number type
2722
//output : scs_nb : the scilab number type
2724
//16/06/07 Author : A.Layec
2726
function [scs_nb]=scs_c_nb2scs_nb(c_nb)
2727
scs_nb=zeros(size(c_nb,1),size(c_nb,2));
2728
for i=1:size(c_nb,1)
2729
for j=1:size(c_nb,2)
2754
//==========================================================================
2755
//write_code_cdoit : generate body of the code for
2756
// for all time dependant blocks
2758
//input : flag : flag number for block's call
2760
//output : txt for cord blocks
2762
//12/07/07 Alan Layec
2765
// Modified for RT purposes by Roberto Bucher - RTAI Team
2766
// roberto.bucher@supsi.ch
2768
function [txt]=write_code_cdoit(flag)
2776
if funtyp(bk)>-1 then
2777
// if or(bk==act) | or(bk==cap) then
2779
// txt2=call_block42(bk,pt,flag);
2787
txt2=call_block42(bk,pt,flag);
2795
elseif funtyp(bk)==-1 then
2796
ix=-1+inplnk(inpptr(bk));
2797
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
2798
thentxt=write_code_doit(clkptr(bk),flag);
2799
elsetxt=write_code_doit(clkptr(bk)+1,flag);
2800
if thentxt<>[] | elsetxt<>[] then
2802
' '+get_comment('ifthenelse_blk',list(bk));]
2804
tmp_='*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
2806
' if('+tmp_+'>0) {']
2824
//** eventselect blk
2825
elseif funtyp(bk)==-2 then
2826
Noutport=clkptr(bk+1)-clkptr(bk);
2827
ix=-1+inplnk(inpptr(bk));
2828
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
2832
switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
2833
if switchtxt(i)<>[] then II=[II i];end
2837
' '+get_comment('evtselect_blk',list(bk));]
2839
tmp_='*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
2841
' i=max(min((int) '+...
2842
tmp_+',block_'+rdnom+'['+string(bk-1)+'].nevout),1);'
2849
' case '+string(i)+' :';]
2852
BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
2865
error('Unknown block type '+string(bk));
2871
//==========================================================================
2872
//write_code_doit : generate body of the code for
2873
// ordering calls of blocks during
2874
// flag 1,2 & flag 3
2876
//input : ev : evt number for block's call
2877
// flag : flag number for block's call
2879
//output : txt for flag 1 or 2, or flag 3
2881
//12/07/07 Alan Layec
2883
// Modified for RT purposes by Roberto Bucher - RTAI Team
2884
// roberto.bucher@supsi.ch
2886
function [txt]=write_code_doit(ev,flag)
2890
for j=ordptr(ev):ordptr(ev+1)-1
2894
if funtyp(bk)>-1 then
2895
// if or(bk==act) | or(bk==cap) then
2897
// txt2=call_block42(bk,pt,flag);
2906
if flag==1 | pt>0 then
2907
txt2=call_block42(bk,pt,flag);
2919
elseif funtyp(bk)==-1 then
2920
ix=-1+inplnk(inpptr(bk));
2921
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
2922
thentxt=write_code_doit(clkptr(bk),flag);
2923
elsetxt=write_code_doit(clkptr(bk)+1,flag);
2924
if thentxt<>[] | elsetxt<>[] then
2926
' '+get_comment('ifthenelse_blk',list(bk));]
2928
tmp_ = '*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
2930
' if('+tmp_+'>0) {']
2948
//** eventselect blk
2949
elseif funtyp(bk)==-2 then
2950
Noutport=clkptr(bk+1)-clkptr(bk);
2951
ix=-1+inplnk(inpptr(bk));
2952
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
2956
switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
2957
if switchtxt(i)<>[] then II=[II i];end
2961
' '+get_comment('evtselect_blk',list(bk));]
2962
tmp_='*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
2965
' i=max(min((int) '+...
2966
tmp_+',block_'+rdnom+'['+string(bk-1)+'].nevout),1);'
2973
' case '+string(i)+' :';]
2976
BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
2989
error('Unknown block type '+string(bk));
2995
//==========================================================================
2996
//write_code_idoit : generate body of the code for
2997
// ordering calls of initial
3000
//input : nothing (blocks are called with flag 1)
3002
//output : txt for iord
3004
//15/07/07 Alan Layec
3007
// Modified for RT purposes by Roberto Bucher - RTAI Team
3008
// roberto.bucher@supsi.ch
3010
function [txt]=write_code_idoit()
3018
if funtyp(bk)>-1 then
3019
// if or(bk==act) then
3021
// txt2=call_block42(bk,pt,1);
3029
txt2=call_block42(bk,pt,1);
3037
elseif funtyp(bk)==-1 then
3038
ix=-1+inplnk(inpptr(bk));
3039
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
3040
thentxt=write_code_doit(clkptr(bk),1);
3041
elsetxt=write_code_doit(clkptr(bk)+1,1);
3042
if thentxt<>[] | elsetxt<>[] then
3044
' '+get_comment('ifthenelse_blk',list(bk));]
3046
tmp_ = '*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
3048
' if('+tmp_+'>0) {']
3066
//** eventselect blk
3067
elseif funtyp(bk)==-2 then
3068
Noutport=clkptr(bk+1)-clkptr(bk);
3069
ix=-1+inplnk(inpptr(bk));
3070
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
3074
switchtxt(i)=write_code_doit(clkptr(bk)+i-1,1);
3075
if switchtxt(i)<>[] then II=[II i];end
3079
' '+get_comment('evtselect_blk',list(bk));]
3081
tmp_='*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
3083
' i=max(min((int) '+...
3084
tmp_+',block_'+rdnom+'['+string(bk-1)+'].nevout),1);']
3092
' case '+string(i)+' :';]
3095
BigIndent+write_code_doit(clkptr(bk)+i-1,1);]
3108
error('Unknown block type '+string(bk));
3114
//==========================================================================
3115
//write_code_odoit : generate body of the code for
3116
// ordering calls of blocks before
3117
// continuous time integration
3119
//input : flag : flag number for block's call
3121
//output : txt for flag 0
3123
//12/07/07 Alan Layec
3126
// Modified for RT purposes by Roberto Bucher - RTAI Team
3127
// roberto.bucher@supsi.ch
3130
function [txt]=write_code_odoit(flag)
3138
if funtyp(bk)>-1 then
3139
txt2=call_block42(bk,pt,flag);
3146
elseif funtyp(bk)==-1 then
3147
ix=-1+inplnk(inpptr(bk));
3148
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
3149
thentxt=write_code_ozdoit(clkptr(bk),flag);
3150
elsetxt=write_code_ozdoit(clkptr(bk)+1,flag);
3151
if thentxt<>[] | elsetxt<>[] then
3153
' '+get_comment('ifthenelse_blk',list(bk));]
3155
tmp_='*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
3157
' if ((block_'+rdnom+'['+string(bk-1)+'].nmode<0'+...
3158
' && '+tmp_+'>0)'+...
3160
' (block_'+rdnom+'['+string(bk-1)+'].nmode>0'+...
3161
' && block_'+rdnom+'['+string(bk-1)+'].mode[0]==1)) {']
3172
' else if ((block_'+rdnom+'['+string(bk-1)+'].nmode<0'+...
3173
' && '+tmp_+'<=0)'+...
3175
' (block_'+rdnom+'['+string(bk-1)+'].nmode>0'+...
3176
' && block_'+rdnom+'['+string(bk-1)+'].mode[0]==2)) {';]
3186
//** eventselect blk
3187
elseif funtyp(bk)==-2 then
3188
Noutport=clkptr(bk+1)-clkptr(bk);
3189
ix=-1+inplnk(inpptr(bk));
3190
TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
3194
switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,flag);
3195
if switchtxt(i)<>[] then II=[II i];end
3199
' '+get_comment('evtselect_blk',list(bk));]
3201
tmp_='*(('+TYPE+' *)'+rdnom+'_block_outtbptr['+string(ix)+'])'
3203
' if (block_'+rdnom+'['+string(bk-1)+'].nmode<0) {';
3204
' i=max(min((int) '+...
3205
tmp_+',block_'+rdnom+'['+string(bk-1)+'].evout),1);'
3208
' i=block_'+rdnom+'['+string(bk-1)+'].mode[0];'
3217
' case '+string(i)+' :';]
3220
BigIndent+write_code_ozdoit(clkptr(bk)+i-1,flag);]
3233
error('Unknown block type '+string(bk));
3239
function [files]=write_code(Code,CCode,FCode,Code_common)
3241
// Original file from Project Metalau - INRIA
3242
// Modified for RT purposes by Roberto Bucher - RTAI Team
3243
// roberto.bucher@supsi.ch
3245
ierr=execstr('mputl(Code,rpat+''/''+rdnom+''.c'')','errcatch')
3247
message(lasterror())
3252
ierr=execstr('mputl(Code_common,rpat+''/common.c'')','errcatch')
3254
message(lasterror())
3260
ierr=execstr('mputl(FCode,rpat+''/''+rdnom+''f.f'')','errcatch')
3262
message(lasterror())
3270
'#include <math.h>';
3271
'#include <stdlib.h>';
3272
'#include <scicos_block4.h>';
3275
ierr=execstr('mputl(CCode,rpat+''/''+rdnom+''_Cblocks.c'')','errcatch')
3277
message(lasterror())
3284
[fd,ierr]=mopen(rpat+'/'+rdnom+'f.f','r')
3285
if ierr==0 then mclose(fd),files=[files,rdnom+'f'],end
3286
[fd,ierr]=mopen(rpat+'/'+rdnom+'_Cblocks.c','r')
3287
if ierr==0 then mclose(fd),files=[files,rdnom+'_Cblocks'],end
3291
//==========================================================================