Documente Academic
Documente Profesional
Documente Cultură
This work is licensed under the Creative Commons Attribution-Share Alike !0 "nited States #icense! To view a co$% o& this license, visit htt$'((creativecommons!or)(licenses(b%-sa( !0(us( or send a letter to Creative Commons, *7* Second Street, Suite 00, San +rancisco, Cali&ornia, 9,*0-, "SA!
Table of Contents
* /ntroduction!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2 0e&inin) the Model!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2!* 12uations &or The 13am$le 4lock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2!2 Modes, Sur&aces and 5ero-Crossin)s!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!, Creatin) the Com$utational +unction!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!, !* The Code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2 Commentar% on the Code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*6 6!2 Commentar% on the Code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*? 7 Testin) the /nter&acin) +unction!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*9 ? 0ebu))in)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2* ?!* +irst Ste$s!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2* ?!2 Startin) a 0ebu))in) Session!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2 9 Conclusion!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2-
1 Introduction
This document is a tutorial that describes the $rocess o& creatin) a custom Scicos &unction block in C! Scicos is a )ra$hical d%namic s%stem modelin) tool that is contained within Scilab, a Matlab-like numerical anal%sis and simulation $acka)e! / have created this tutorial &or a cou$le reasons! +irst, this is m% wa% to leave breadcrumbs &or m%sel&, so that whenever / create m% own &unction blocks, / have an e3am$le to &ollow! Second, / &ound that the available Scicos documentation is sorel% lackin) in com$lete e3am$les with thorou)h descri$tions o& the ste$s to take! / s$ent man% hours $orin) over what documentation / could &ind, $ostin) 2uestions to the Scilab news)rou$, and en)a)in) in $lain trial-and-error, be&ore / was able to success&ull% build m% own &unction blocks! / ho$e that b% creatin) this tutorial, / can com$ress %our learnin) curve and hel$ %ou to 2uickl% become $roductive! <e)ardin) the develo$ment environment, / have used Scicos#ab ,! on "buntu #inu3 to create the e3am$le $resented in this tutorial! /& %ou are usin) @indows, / canAt sa% whether this stu&& will work e3actl% the same, thou)h most o& it should! So, letAs )et startedB
C*D
where u, gp and gn are in$uts to the block and #hi and #lo are block $arameters!
Tutorial: Creating a C Function Block in Scicos Phil Schmidt, email'$hil&arm.&astmail!&m Pa)e
/n addition to de&inin) the blockAs state, we will also want to de&ine its out$ut! @e will choose the state variable as one out$ut, and the blockAs current )ain C gp, gn or EeroD as a second out$ut! 7otice in e2uations C*D the care&ul de&inition o& the discontinuities in the derivative at #hi and #lo! This care&ul de&inition is im$ortant &or the numerical inte)rator in Scicos, which needs to know where the discontinuities are in order to $ro$erl% simulate the s%stem! This leads us to the ne3t section!!!
C2D
$2 $3 $4 $ $$ $' $( $) '* '1 '2 '3 '4 ' '$ '' '( ') (* (1 (2 (3 (4 ( ($ (' (( () )* )1
// compute the outputs of the block if -9 &: 8hi DD 9 %: 8lo/ +gain : *A else if -9 & */ +gain : gainpA else +gain : gainnA out : 9A breakA case )B // compute =ero crossing surfaces and set modes surf* : 9 - 8hiA surf1 : 9A surf2 : 9 - 8loA if -get#phase#simulation-/ :: 1/ @ if -surf* &: */ mode* : mode#>h=lA else if -surf2 %: */ mode* : mode#h=l>A else if -surf1 & */ mode* : mode#h>=lA else mode* : mode#h=>lA E breakA
Lines 1-
name the &our mode values that the mode variable ma% hold!
Lines %-&1 is the &unction that im$lements the behavior o& the new block! Line %' The &unction name, lim_int, will be used later when linkin) the code into Scicos! Line 5"-5&: @hen &la) is Eero, the Scicos inte)rator is re2uestin) the block to com$ute its derivative! The derivative e2uation to be used de$ends on the mode, which can be seen in lines -7 and -9! Com$are the e2uations in this section o& the code to the e2uations )iven in sections 2 and ! Lines %1-$0: @hen &la) is one, the Scicos inte)rator is re2uestin) the block to com$ute its out$uts! As %ou can see, the mode is not used to determine which )ain value to use as out$ut! / could not &ind documentation e3$lainin) whether mode should be used, but in all the e3am$le code / looked at, it was not used, so m% assum$tion is that it should not be used here! Lines $"-!&: @hen &la) is nine, the Scicos inte)rator is re2uestin) in&ormation about modes and Eero crossin)s! Sur&ace in&ormation is needed at ever% call, while mode in&ormation is onl% needed durin) simulation $hase *! Lines $ -$%: The values o& the three sur&aces are assi)ned here! Line $!: This is the test &or simulation $hase one! Lines !0-!$: Modes are calculated here based on the $ositions o& the sur&ace values!
This is kind o& a scar% lookin) command, but itAs $rett% sim$le once %ou break it down Cand note that %ou can )et hel$ on this command &rom ScilabAs built-in hel$ browser in the #tilities cate)or%D!
The &irst $arameter, Flim#intF, is the name o& the &unction we want linked into Scilab! The 2nd $arameter, Flim#int#comp.oF, is the name o& the obMect &ile that will be created when our source code is com$iled! The rd $arameter, 45, is a list o& e3tra libraries needed &or linkin)! 9ur e3am$le does not need an%, so an em$t% matri3 is $assed! The ,th $arameter, FcF, tells Scilab that this is a C &unction Cas o$$osed to a +ortran &unctionD! Parameters - and 6, F<akelibF and Floader.sceF, are the names o& the make&ile and loader &ile res$ectivel%! 4oth o& the values / used are also the de&ault values! Parameters 7 and ? are the libname and ld&la)s! +or this e3am$le, / Must $ass em$t% strin)s, which also ha$$en to be the de&aults! +inall%, $arameter ? is a list o& the c&la)s to $ass to the C com$iler! / $ass in a F-gF to make the com$iler )enerate debu))in) in&ormation! This is absolutel% necessar% i& %our C code has runtime errors, so that %ou can use )db to debu) it!
@hen %ou run the command, %ou should )et somethin) that looks like the &ollowin) $icture'
+unction t%$e' A C block is t%$e ,! /n$ut $orts siEes' The block has three *3* CscalarD in$uts! /n$ut $orts t%$e' The three in$uts are o& t%$e real! 9ut$ut $orts siEes' The block has two *3* CscalarD out$uts! 9ut$ut $orts t%$e' The two out$uts are o& t%$e real! The block has no in$ut or out$ut events! The initial condition &or the blckAs state is 0!2-! There are no discrete states, thus no initial condition!
Tutorial: Creating a C Function Block in Scicos
Pa)e *,
/nitial obMect state' I ha$e not %igured out &hat this is 'et( The blockAs $arameters Cu$$er and lower limitD are real $arameters! There are no inte)er $arameters! There is one mode variable! There are three Eero crossin) sur&aces! The block has direct &eedthrou)h, i!e!, it can &orm $art o& an al)ebraic loo$! The block is time de$endent, as it has a continuous state!
Then run the simulation CSimulate N <unD, to )et the &ollowin) $lot'
And there %ou have it, a custom block written in C! This is all )reat, but it would be nice to have a customiEed )ra$hical block to )o with the custom com$utational &unction! The ne3t section shows how to do this!
$ ' ( ) 1* 11 12 13 14 1 1$ 1' 1( 1) 2* 21 22 23 24 2 2$ 2' 2( 2) 3* 31 32 33 34 3 3$ 3' 3( 3) 4* 41 42 43 44 4 4$ 4' 4( 4) * 1 2 3 4 $ ' ( ) $* $1 $2 $3 $4 $ $$ $' $( $) '* '1 '2 '3 '4
//disp-Hob/ select Hob case FplotF then standard#draw-arg1/ case FgetinputsF then 4>.G.tGp5 : standard#inputs-arg1/ //disp-sci2e>p->// case FgetoutputsF then 4>.G.tGp5 : standard#outputs-arg1/ //disp-sci2e>p->// case FgetoriginF then 4>.G5 : standard#origin-arg1/ //disp-sci2e>p->// case FsetF then //message-sci2e>p-arg1// > : arg1 graphics : arg1.graphics e>prs : graphics.e>prs model : arg1.model while It do 4ok.8hi.8lo.9initial.e>prs5 : getvalue-FSet funnG integrator parametersF... 4Fhigh limitFAFlow limitFAF9initialF5... list-FvecF.1.FvecF.1.FvecF.1/... e>prs/ if Jok then break.end model.state : 49initial5 model.rpar : 48hiA8lo5 graphics.e>prs : e>prs >.graphics : graphics >.model : model break end case FdefineF then //message-Fin defineF/ 8hi : 1.* 8lo : -1.* 9initial : * model : scicos#model-/ model.sim : list-Flim#intF.4/ model.in : 41A1A15 model.out : 41A15 model.state : 49initial5 model.dstate : 4*5 model.rpar : 48hiA8lo5 model.blocktGpe : FcF model.nmode : 1 model.n=cross : 3 model.dep#ut : 4It It5 e>prs : 4string-48hiA8loA9initial5/5 gr#i : 4F>:orig-1/.G:orig-2/.w:s=-1/.h:s=-2/FA Ft>t:4FFKunnGFFAFF+ntegratorFF5FA F>stringb->3*.2 ?w. G3*.2*?h. t>t. *. *?w. *.$*?h. FFfillFF/FA Ft>t:4FFinFFAFFFFAFFgainpFFAFFFFAFFgainnFF5FA F>stringb->3*.*2?w. G3*.*(?h. t>t. *.2 ?w. *.(*?h. FFfillFF/FA Ft>t:4FFFFAFFoutFFAFFFFAFFgainFFAFFFF5FA F>stringb->3*.'3?w. G3*.*(?h. t>t. *.2 ?w. *.(*?h. FFfillFF/FA 5 > : standard#define-44 25.model.e>prs.gr#i/ end
'
endfunction
#ine -* corres$onds to Simulation /unction and Function t45e! #ine -2 corres$onds to 6n5ut 5orts si7es! #ine - corres$onds to 'ut5ut 5orts si7es! #ine -, corres$onds to 6nitial continuous state! #ine -- corres$onds to 6nitial discrete state! / &ound that this cannot be an em$t% vector, thus / assi)ned a value Cwhich is unused b% the com$utational &unctionD to make Scilab ha$$%! #ine -6 corres$onds to .eal 5arameters 8ector! #ine -? corres$onds to ,um0er o/ modes! #ine -9 corres$onds to ,um0er o/ 7ero crossings! #ine 60 corres$onds to 9irect /eedthrough and Time de5endence!
Line %" de&ines the initial values that will be dis$la%ed in the blockAs con&i)uration dialo)! Lines %#-$0 de&ine the Scilab code that will be e3ecuted to draw the blockAs )ra$hical and te3t elements! The code must be ca$tured as a list o& strin)s! 4e care&ul about 2uotin)B Line $1 creates the block, includin) both its )ra$hical and model elements! The siEe o& the block is s$eci&ied b% the &irst $arameter o& the standard;de&ineCD &unction!
Tutorial: Creating a C Function Block in Scicos Phil Schmidt, email'$hil&arm.&astmail!&m Pa)e *?
9nce a)ain, run the model! Lou should )et the &ollowin) $lot'
As e3$ected, the out$uts o& the two blocks are the same because the% use the same com$utational &unction! CoolB
8 Debugging
All the above is well and )ood, however, when %ouAre writin) %our own &unctions, it is likel% that %ou will have errors, and will need to debu) them! / showed in the inter&acin) &unction how to dro$ in sim$le debu))in) lines, so / wonAt discuss that an% &urther! 8owever, &or C code, s$rinklin) $rint&CD calls into the code ma% not be su&&icient or ma% not work at all, es$eciall% i& %our Scilab instance is runnin) as a se$arate $rocess, as stdout ma% not )o to a console! So, %ou will need to use )db! /n this section, /All )ive %ou a Cver%D hi)h level overview o& the $rocess! And remember, what / describe here is what / do on m% "buntu s%stem! /t will likel% work the same on most Oni3 s%stems, but is liable not to work the same on @indows s%stems!
end
while Li % Larg* -& ng printf "HIi : Ig ". Li. Larg* -& Hroot4Li5 set Li : Li 3 1 end printf "Mn" printf "<odeB " set Li : * while Li % Larg* -& nmode printf "mIi : Ii ". Li. Larg* -& mode4Li5 set Li : Li 3 1 end printf "Mn"
define Show show+nputs-blk/ show6utputs-blk/ show2arms-blk/ show<N-blk/ showState-blk/ end define hook-stop Show end break lim#int continue
Pust a 2uick comment on the scri$t above' 7otice that the ho& &unction $asses the name blk to the various sho&))) &unctionsH blk is the ver% same blk $arameter in the lim_int*+ C com$utational &unction!
8.!
At this $oint, Scicos has been sto$$ed at a break$oint at the com$utational &unction lim;intCD! This whole interaction will look like the &ollowin) screenshots'
7otice in the above screenshot that the values o& the block data structure elements are dis$la%ed! The% will a$$ear each time that )db breaks $ro)ram e3ecution! +rom here, %ou will use the standard )db commands to ins$ect code and ste$ throu)h it! The hel5, ste5 and continue commands will be ver% use&ul! +or &urther details on )db debu))in), use its online hel$ or consult the co$ious documentation that is available on the web!
" Conclusion
This concludes this tutorial! / ho$e it has been hel$&ulB