⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtaicodegen_.sci

📁 rtai-3.1-test3的源代码(Real-Time Application Interface )
💻 SCI
📖 第 1 页 / 共 5 页
字号:
//t : a string containing a C instruction//l : max line length allowed// Copyright INRIA//Author : Rachid Djenidi    sep=[',','+']  l1=l-2  t1=[]  kw=strindex(t,' ')  nw=0  if kw<>[] then    if kw(1)==1 then // there is leading blanks      k1=find(kw(2:$)-kw(1:$-1)<>1)      if k1==[] then // there is a single blank	nw=1      else	nw=kw(k1(1))      end    end  end  t=part(t,nw+1:length(t));  bl=part(' ',ones(1,nw))  l1=l-nw;first=%t  while %t     if length(t)<=l then t1=[t1;bl+t],return,end    k=strindex(t,sep);    if k==[] then t1=[t1;bl+t],return,end    k($+1)=length(t)+1 // positions of the commas    i=find(k(1:$-1)<=l&k(2:$)>l) //nearest left comma (reltively to l)    if i==[] then i=1,end    t1=[t1;bl+part(t,1:k(i))]    t=part(t,k(i)+1:length(t))    if first then l1=l1-2;bl=bl+'  ';first=%f;end  endendfunctionfunction  [ok,XX]=do_compile_superblock(XX)// Transforms a given Scicos discrete SuperBlock into a C defined Block// Copyright INRIA//Author : Rachid Djenidi  scs_m=XX.model.rpar  par=scs_m.props;  if alreadyran then     //terminate current simulation    do_terminate()  end  hname=scs_m.props.title(1) //superblock name  //***********************************************************  //Check blocks properties and adapt them if necessary  //***********************************************************  IN=[];OUT=[];clkIN=[];numa=[];numc=[];  for i=1:size(scs_m.objs)     if typeof(scs_m.objs(i))=='Block' then        if scs_m.objs(i).gui=='CLKOUT_f' then	        ok=%f;%cpr=list()	message('Superblock should not have any activation output port.')	return      elseif scs_m.objs(i).gui=='IN_f' then	//replace input ports by sensor blocks	numc=numc+1	scs_m.objs(i).gui='INPUTPORTEVTS';	scs_m.objs(i).model.evtin=1	scs_m.objs(i).model.sim(1)='capteur'+string(numc)	IN=[IN scs_m.objs(i).model.ipar]      elseif scs_m.objs(i).gui=='OUT_f' then	//replace output ports by actuator blocks	numa=numa+1	scs_m.objs(i).gui='OUTPUTPORTEVTS';	scs_m.objs(i).model.sim(1)='actionneur'+string(numa)	OUT=[OUT  scs_m.objs(i).model.ipar]      elseif scs_m.objs(i).gui=='CLKINV_f' then	//replace event input ports by  fictious block	scs_m.objs(i).gui='EVTGEN_f';	scs_m.objs(i).model.sim(1)='bidon'	clkIN=[clkIN scs_m.objs(i).model.ipar];      elseif scs_m.objs(i).model.dep_ut(2)==%t then	//check for time dependency PAS IICI	ok=%f;%cpr=list()	message('a block have time dependence.')	return      end    end  end  //Check if input/output ports are numered properly  IN=-sort(-IN);  if or(IN<>[1:size(IN,'*')]) then     ok=%f;%cpr=list()    message('Input ports are not numbered properly.')    return  end  OUT=-sort(-OUT);  if or(OUT<>[1:size(OUT,'*')]) then     ok=%f;%cpr=list()    message('Output ports are not numbered properly.')    return  end  clkIN=-sort(-clkIN);  if or(clkIN<>[1:size(clkIN,'*')]) then     ok=%f;%cpr=list()    message('Event input ports are not numbered properly.')    return  end  //Check if there is more than one clock in the diagram  szclkIN=size(clkIN,2);  if szclkIN==0 then    szclkIN=[]  end  [bllst,connectmat,clkconnect,cor,corinv,ok]=c_pass1(scs_m);if ok==%f then message('Sorry: problem in the pre-compilation step.'),return, end  a=[];b=[];tt=[];howclk=[];allhowclk=[];cap=[];act=[];  ///**********************************  for i=1:size(bllst)      for j=1:size(bllst)             if (bllst(i).sim(1)=='actionneur'+string(j)) then	if tt<>i then	  act=[act;i];	  tt=i;	end      elseif (bllst(i).sim(1)=='capteur'+string(j)) then	if tt<>i then	  cap=[cap;i];	  tt=i;	end	      elseif (bllst(i).sim(1)=='bidon') then	if tt<>i then	  allhowclk=[allhowclk;i];	  tt=i;	end      end    end    ///**********************************  end    if szclkIN>1 then    //replace the N Event inputs by a fictious block with 2^N as many event    //outputs    output=ones((2^szclkIN)-1,1)    bllst($+1)=scicos_model(sim=list('bidon',1),evtout=output,..			    blocktype='d',..			    firing=-output',dep_ut=[%f %f])    corinv(size(bllst))=size(bllst)+1;    howclk=size(bllst)    // adjust the links accordingly    for i=1:(2^szclkIN)-1      vec=codebinaire(i,szclkIN)      for j=1:szclkIN	if vec(j)*allhowclk(j)>=1 then	  for k=1:size(clkconnect,1)	    if clkconnect(k,1)==allhowclk(j) then	      clkconnect=[clkconnect;[howclk i clkconnect(k,3:4)]]	    end	  end	end	      end    end  elseif szclkIN==[]  then    //superblock has no Event input, add a fictious clock    bllst($+1)=scicos_model(sim=list('bidon',1),evtout=1,..			    firing=0,blocktype='d',dep_ut=[%f %f])    howclk=size(bllst);  elseif szclkIN==1  then    howclk=allhowclk;  end    //mise en ordre de clkconnect  if szclkIN>1 then    newclkconnect=clkconnect;    clkconnect=[];    for i=1:size(newclkconnect,1)-1      if  or(newclkconnect(i,:)<>newclkconnect(i+1,:)) then	clkconnect=[clkconnect;newclkconnect(i,:)]      end    end    if  or(newclkconnect($-1,:)<>newclkconnect($,:)) then      clkconnect=[clkconnect;newclkconnect($,:)]    end    //suppression des blocs bidons    newclkconnect=clkconnect;nkt=[];    for i=1:szclkIN      for k=1:size(newclkconnect,1)	if newclkconnect(k,1)~=allhowclk(i) then	  nkt=[nkt;newclkconnect(k,:)];	end      end      newclkconnect=nkt;      nkt=[];    end    clkconnect=newclkconnect;  end  //**************************************************  // nouveau clkconnect avec liaisons sur les capteurs  //**************************************************  for i=1:size(cap,1)    if szclkIN>1 then      for j=1:(2^szclkIN)-1	clkconnect=[clkconnect;[howclk j cap(i) 1]];      end	    else      clkconnect=[clkconnect;[howclk 1 cap(i) 1]];    end  endcpr=newc_pass2(bllst,connectmat,clkconnect,cor,corinv)  if cpr==list() then ok=%f,return, end  ///////////////////  //les pointeurs de cpr :  x=cpr.state.x;z=cpr.state.z;tevts=cpr.state.tevts;  evtspt=cpr.state.evtspt;  pointi=cpr.state.pointi;outtb=cpr.state.outtb;    rpptr=cpr.sim.rpptr;  ipptr=cpr.sim.ipptr;funs=cpr.sim.funs;xptr=cpr.sim.xptr;  zptr=cpr.sim.zptr;inpptr=cpr.sim.inpptr;inplnk=cpr.sim.inplnk;  outptr=cpr.sim.outptr;outlnk=cpr.sim.outlnk;  lnkptr=cpr.sim.lnkptr;  ordclk=cpr.sim.ordclk;nordcl=size(ordclk,1);  funtyp=cpr.sim.funtyp;cord=cpr.sim.cord;ncord=size(cord,1);  nblk=cpr.sim.nblk;ztyp=cpr.sim.ztyp;  clkptr=cpr.sim.clkptr  //taille totale de z : nztotal  nztotal=size(z,1);    //*******************************  //Checking if superblock is valid  //*******************************  msg=[]  for i=1:length(funs)-1    if funtyp(i)==3 then      msg=[msg;'Type 3 block''s not allowed']    elseif ztyp(i)<>0 then      msg=[msg;'Zero crossing block''s not allowed']    elseif (xptr(i+1)-xptr(i))<>0 then      msg=[msg;'Continuous state block''s not allowed']    elseif (clkptr(i+1)-clkptr(i))<>0 &funtyp(i)>-1 &funs(i)~='bidon' then      msg=[msg;'Regular block generating activation not allowed yet']    end    if msg<>[] then message(msg),ok=%f,return,end  end    //********************************************************  // Change logical units for readf and writef blocks if any ???  //********************************************************  lunit=0  for d=1:length(funs)    if funs(d)=='readf'  then      z(zptr(d)+2)=lunit      lunit=lunit+1;    elseif funs(d)=='writef'      z(zptr(d)+1)=lunit      lunit=lunit+1;    end  end      //***********************************  // Get the name of the file  //***********************************  foo=3;okk=%f;rdnom='foo';rpat=getcwd();archname='';  label1=[hname;getcwd()+'/'+hname;'config';'0.001'];  while %t do    [okk,rdnom,rpat,cffile,Tsamp,label1]=getvalue(..			'PLEASE, GIVE US SOME INFORMATION. ',..		        ['New block''s name: ';		        'Created files Path: ';			'Config file: ';			'Sampling Time: '],..		        list('str',1,'str',1,'str',1,'str',1),label1);    if okk==%f then ok=%f;return; end      dirinfo=fileinfo(rpat)    if dirinfo==[] then      ok=mkdir(rpat)          if ~ok then 	x_message('Directory '+rpat+' cannot be created');      end    elseif filetype(dirinfo(2))<>'Directory' then      ok=%f;x_message(rpat+' is not a directory');    end    if stripblanks(rdnom)==emptystr() then       ok=%f;x_message('sorry C file name not defined');    end    if ok then break,end  end    //////////////////////////////////////////////////    maxnrpar=max(rpptr(2:$)-rpptr(1:$-1))  maxnipar=max(ipptr(2:$)-ipptr(1:$-1))  maxnx=max(xptr(2:$)-xptr(1:$-1))  maxnz=max(zptr(2:$)-zptr(1:$-1))  maxnin=max(inpptr(2:$)-inpptr(1:$-1))  maxnout=max(outptr(2:$)-outptr(1:$-1))    maxtotal=max([maxnrpar;maxnipar;maxnx;maxnz;maxnin;maxnout]);  if maxtotal< length(cpr(2).outtb) then    maxtotal=length(cpr(2).outtb)  end    //************************************************************************  //generate the call to the blocks and blocs simulation function prototypes  //************************************************************************  wfunclist=list();  nbcap=0;nbact=0;capt=[];actt=[];Protos=[];  dfuns=[]  for i=1:length(funs)    if or(i==act) then //block is an actuator      nbact=nbact+1;      [Code,actti,protoi]=call_actuator(i)      wfunclist($+1)=[Code;'   if(flag < 0 ) return(5 - flag); ']      if nbact==1 then Protos=[Protos;'';protoi],end         actt=[actt;actti]    elseif or(i==cap) then //block is a sensor       nbcap=nbcap+1;      [Code,capti,protoi]=call_sensor(i)      wfunclist($+1)=[Code;'   if(flag < 0 ) return(5 - flag); ']      if nbcap==1 then Protos=[Protos;'';protoi] ,end      capt=[capt;capti]    elseif funs(i)=='bidon'      wfunclist($+1)=' '    else      ki=find(funs(i)==dfuns)      [Code,protoi]=call_block(i)      if Code<>[] then	wfunclist($+1)=[Code;'   if(flag < 0 ) return(5 - flag); ']      else	wfunclist($+1)=' ';      end      if ki==[] then Protos=[Protos;'';protoi],end            dfuns=[dfuns;funs(i)]    end  end  //**************************************  //on prend en compte l'ordre des numerotation graphique des  //capteur et des actionneur   [junk,index]=sort(-actt(:,$));  actt=actt(index,1:$) ;  [junk,index]=sort(-capt(:,$));  capt=capt(index,1:$) ;      //***************************************************  //Compute the initial state and outtb (links)  values  //***************************************************  //petite modification pour uniformiser les fichiers capteurs   //et actionneurs ???  rdcpr=cpr.sim.funs;  for r=1:length(cap),rdcpr(cap(r))='bidon';end  for r=1:length(act),rdcpr(act(r))='bidon';end  Total_rdcpr=cpr.sim;Total_rdcpr.funs=rdcpr;  //  tcur=0;  tf=scs_m.props.tf;  tolerances=scs_m.props.tol;  //[state,t]=scicosim(cpr.state,tcur,tf,Total_rdcpr,'start',tolerances); // cpr.state=state;  z=cpr.state.z;outtb=cpr.state.outtb;  //[junk_state,t]=scicosim(cpr.state,tcur,tf,Total_rdcpr,'finish',tolerances);    //***********************************  // Scilab and C files generation  //***********************************  ok=gen_gui();  if ok then ok=gen_ccode(),end    //*****************  //Compile and link  //*****************  if ok then [ok,Makename]=buildnewblock(),end  if ok then ok=gen_loader(),end  [ok] = compile_rt_standalone();endfunctionfunction vec=codebinaire(v,szclkIN)  vec=zeros(1,szclkIN)  for i=1:szclkIN    w=v/2;    vec(i)=v-2*int(w);    v=int(w);  endendfunctionfunction [CCode,FCode]=gen_blocks()//Generates Code for dynamically linked Fortran and C Blocks    CCode=[]  FCode=[]    kdyn=find(funtyp>1000) //dynamically linked blocs			 //100X : Fortran blocks			 //200X : C blocks			   if kdyn==[] then    return  end  for i=1:size(kdyn,'*')    //get the block data structure in the initial scs_m structure    if size(corinv(kdyn(i)),'*')==1 then      O=scs_m.objs(corinv(kdyn(i)));    else      path=list('objs');      for l=corinv(kdyn(i))(1:$-1),path($+1)=l;path($+1)='model';path($+1)='rpar';path($+1)='objs';end      path($+1)=corinv(kdyn(i))($);      O=scs_m(path);    end    if funtyp(kdyn(i))>2000 then      //C block      CCode=[CCode;strsubst(O.graphics.exprs(2),"#include <machine.h>","")]    else      FCode=[FCode;O.graphics.exprs(2)]    end  endendfunctionfunction ok=gen_ccode();//Generates the C code for new block simulation  //Copyright INRIA//Author : Rachid Djenidi// Modified for RTAI by Paolo Mantegazza (mantegazza@aero.polimi.it// and Roberto Bucher (roberto.bucher@die.supsi.ch)  [CCode,FCode]=gen_blocks()  Code=[make_decl();	Protos;	make_static()	make_computational() 	make_main1();	make_main2();	make_init();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -