Tutorial Creating a C Function Block in Scicos

download Tutorial Creating a C Function Block in Scicos

of 25

Transcript of Tutorial Creating a C Function Block in Scicos

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    1/25

    Tutorial

    Creating a C Function Block in ScicosPhil Schmidt

    March 7, 2009

    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!

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e *

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    2/25

    Table of Contents

    * /ntroduction!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2 e&inin) the Model!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    2!* 1uations &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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!6

    Com$ilin) and #inkin)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!7!* Startin) Scilab!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!7!2 /nvokin) the Com$iler and #inker!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!9

    Testin) the ew 4lock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*0!* 4lock ia)ram!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*0!2 4lock Con&i)urations!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!**

    !2!* #e&t8and C#C:;c 4lock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!**!2!2 < 4lock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*2!2! 4ottom =1;S>< 4lock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*2!2!6 CMSCP1 4lock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*!2!7 )eneric;block 4lock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*

    !

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    3/25

    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) uestions 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 uickl% become $roductive!

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    4/25

    /n addition to de&inin) the blocks state, we will also want to de&ine its out$ut! @e will choose the

    state variable as one out$ut, and the blocks current )ain gp, gnor EeroD as a second out$ut!

    otice in euations *D the care&ul de&inition o& the discontinuities in the derivative at #hiand #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!!!

    2.2 Modes! "urfaces and #ero$Crossings

    =enerall%, Scicos needs to know where there are discontinuities in the derivatives o& the s%stem in

    order to $ro$erl% simulate the s%stem! To de&ine these, Scicos makes use o& modes and sur&aces, which

    %ou must de&ine in %our block code, to determine where those discontinuties are and what to do on

    either side o& them!

    A sur&ace is sim$l% a variable in %our s%stem that has values that cross discontinuities in the s%stem!Scicos detects that a sur&ace is crossin) a discontinuit% b% detectin) that the sur&ace variable crosses

    Eero!

    A mode is a variable that holds values re$resentin) each distinct re)ion that e3ist in a sur&ace! So, i&

    a sur&ace has a sin)le Eero-crossin), then there would be two mode values to re$resent that the s%stem

    is on one or the other side o& the Eero crossin)!

    /n the s%stem in euations *D, there are three sur&aces, de&ined as s0FxG #hi, s1FxG #lo and s2Fx,

    The three sur&aces there&ore each have a Eero crossin)H one at #hi, one at Eero and one at #lo!There is a

    sin)le mode variable that can have &our discrete values re$resentin) each o& the &our Eero-crossin)

    re)ions! @e will make the &ollowin) de&initions &or mode'

    mode=* xLhimode=2 0xLhimode= Llox0

    mode= xLlo

    2D

    % Creating the Computational &unction

    nce %ouve de&ined the eutions &or the block, %ou are read% to write the C code that im$lements

    it! /n the code will be a &unction that Scicos calls to com$ute the behavior o& the block! This is called

    the com$utational &unction!

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    5/25

    %.1 The Code

    +or our e3am$le block, / will sim$l% $resent the code here, and then comment on ke% $arts o& the

    code! So, here is the code, saved in the &ile lim_int_comp.c'

    1 // This is the computational function for a Scicos model block.2 // The model is of a variable-gain integrator with hard high and low limits.

    34 !include "scicos/scicos#block4.h"$ !include %math.h&' !include %stdio.h&( !include %stdlib.h&)1* !define r#+,n i 0eteal+norttrsblk n1i511 !define r#67Tn i 0eteal6utorttrsblk n1i51213 // parameters14 !define 8hi 0etpartrsblk*5 // integrator high limit1 !define 8lo 0etpartrsblk15 // integrator low limit1$1' // inputs1( !define in r#+,** // integrator input1) !define gainp r#+,1* // integrator gain when 9 & *

    2* !define gainn r#+,2* // integrator gain when 9 %: *2122 // states23 !define 9 0etStateblk*5 // integrator state24 !define 9dot 0et;erStateblk*5 // derivative of the integrator output22$ // outputs2' !define out r#67T* * // integrator output2( !define +gain r#67T1 * // integrator gain2)3* // other constants31 !define surf* 0et0trsblk*532 !define surf1 0et0trsblk1533 !define surf2 0et0trsblk2534 !define mode* 0eth=l CC in % * DD mode* :: mode#h>=l gain : gainpA$ else if mode* :: mode#h=l> CC in & * DD mode* :: mode#h=>l' gain : gainnA( 9dot : gain ? inA) breakA$*$1 case 1B

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    6/25

    $2 // compute the outputs of the block$3 if 9 &: 8hi DD 9 %: 8lo$4 +gain : *A$ else if 9 & *$$ +gain : gainpA$' else$( +gain : gainnA$) out : 9A'* breakA'1

    '2 case )B'3 // compute =ero crossing surfaces and set modes'4 surf* : 9 - 8hiA' surf1 : 9A'$ surf2 : 9 - 8loA'''( if get#phase#simulation :: 1') @(* if surf* &: *(1 mode* : mode#>h=lA(2 else if surf2 %: *(3 mode* : mode#h=l>A(4 else if surf1 & *( mode* : mode#h>=lA($ else(' mode* : mode#h=>lA

    (( E() breakA)* E)1 E

    %.2 Commentar' on the Code

    Line 5:4e sure to Iinclude Jscicos;block!hK! This header de&ines macros that are used to access dataelements in the scicos_blockdata structure! The macros used in this e3am$le are GetRealInPortPtrsline *0D, GetRealOutPortPtrsline **D, GetRparPtrslines *-*D, Gettateline 2D, Get!ertateline 2D, GetGPtrslines *-D and Get"odePtrsline D! More in&ormation on these and othermacros is available &rom the Scilab online hel$! T%$e helpat the Scilab command $rom$t, then )o to

    Scicos: Bloc diagram editor and simulator | whatis scicos | C Macros!DLines 10-de&ine a bunch o& macros that make it easier to access elements o& the Scicos block datastructure! / &ind it much easier to use names that corres$ond to each o& m% blocks $arameters, states,in$uts and out$uts, rather than to be constantl% workin) with anon%mous $ointers and arra% elements!

    Lines 10-11are hel$er macros &or accessin) block in$uts and out$uts!

    Lines 1-15name the u$$er and lower limit o& the inte)rator! The limits are $assed to the block as real$arameters!

    Lines 1!-"0name the in$uts to the inte)rator block! +or this block, all the in$uts are real-valuedvalues!

    Lines "#-"name the blocks state and derivative!Lines "$-"!name the out$uts o& the inte)rator block! +or this block, all the out$uts are real-valuedvalues!

    Lines #1-##name the three sur&aces in the block!

    Line #names the sin)le mode variable o& the block!

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e 6

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    7/25

    Lines 1-name the &our mode values that the mode variable ma% hold!

    Lines %-&1is 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 reuestin) the block to com$ute its derivative!The derivative euation to be used de$ends on the mode, which can be seen in lines 7 and 9!

    Com$are the euations in this section o& the code to the euations )iven in sections 2 and !Lines %1-$0:@hen &la) is one, the Scicos inte)rator is reuestin) 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 &inddocumentation e3$lainin) whether mode should be used, but in all the e3am$le code / looked at, it wasnot used, so m% assum$tion is that it should not be used here!

    Lines $"-!&:@hen &la) is nine, the Scicos inte)rator is reuestin) in&ormation about modes and Eerocrossin)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!

    4 Compiling and Linking

    @ith the C code in hand, %ou are now read% to com$ile and link it into Scilab!

    (.1 "tarting "cilab

    +irst, run Scilab! Lou will need to have Scilab runnin) in the director% that contains %our source&ile! The easiest wa% to do that is to ust o$en a terminal in %our workin) director%, and invoke Scilab

    &rom there! therwise, use the chdir()&unction inside the Scilab console to chan)e to %our workin)director%!

    The screenshots below show how / invoke Scilab &or this tutorial'

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e 7

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    8/25

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e ?

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    9/25

    (.2 In)oing the Compiler and *iner

    k, so Scilab is runnin) and is in the ri)ht director%! Com$ilin) and linkin) is done sim$l% b%invokin) the &ollowin) command in Scilab'

    ilib#for#linkFlim#intFFlim#int#comp.oF5FcFF

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    10/25

    Thats itB The C &ile is com$iled and linkedB

    + Testing the ,e- Blocow that weve linked the code, it would be nice to run it to tr% it out! @e can do a uick-and-dirt%

    test usin) the =11

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    11/25

    +.2 Bloc Configurations

    +.2.1 *eftand C*/C0c Bloc

    +.2.2 ighthand C*/C0c Bloc

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e **

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    12/25

    +.2.% 3E,"4f Bloc

    +.2.( Middle 3E,"4 Bloc

    +.2.+ Bottom 3E,"4 Bloc

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e *2

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    13/25

    +.2.5 CM"C/6E Bloc

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e *

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    14/25

    +.2.7 genericbloc% Bloc

    The con&i)uration &or this block deserves some e3$lanation'

    Simulation &unction' This is the name o& the linked C com$utational &unction thatim$lements the block!

    +unction t%$e' A C block is t%$e !

    /n$ut $orts siEes' The block has three *3* scalarD in$uts!

    /n$ut $orts t%$e' The three in$uts are o& t%$e real!

    ut$ut $orts siEes' The block has two *3* scalarD out$uts!

    ut$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 blcks state is 0!2!

    There are no discrete states, thus no initial condition!

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e *

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    15/25

    /nitial obect state'I ha$e not %igured out &hat this is 'et(

    The blocks $arameters u$$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!

    +.% unning the "imulation

    ow that our model is built, we can run it! +irst, set u$ the simulation $arameters &rom theSimulate N Setu$ menu in the Scicos editor'

    Then run the simulation Simulate N

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    16/25

    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 customcom$utational &unction! The ne3t section shows how to do this!

    5 Creating the Interfacing &unction

    The inter&acin) &unction is written in Scilab lan)ua)e, and is used to de&ine the a$$earance andbehavior o& the )ra$hical elements o& the block in the Scicos editor! As / did with the com$utational&unction earlier, / will sim$l% $resent the code, and then $rovide commentar% a&terwards!

    5.1 The Code

    Save the &ollowin) code into the &ile lim)int)int/2sci!

    1 function >GtGp5 : 8+ : 5A4 G : 5A tGp : 5A

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e *6

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    17/25

    $' //dispHob() select Hob1* case FplotF then11 standard#drawarg11213 case FgetinputsF then14 >GtGp5 : standard#inputsarg11 //dispsci2e>p>

    1$1' case FgetoutputsF then1( >GtGp5 : standard#outputsarg11) //dispsci2e>p>2*21 case FgetoriginF then22 >G5 : standard#originarg123 //dispsci2e>p>242 case FsetF then2$ //messagesci2e>parg12' > : arg12( graphics : arg1.graphics2) e>prs : graphics.e>prs3* model : arg1.model31 while It do

    32 ok8hi8lo9initiale>prs5 : getvalueFSet funnG integrator parametersF..33 Fhigh limitFAFlow limitFAF9initialF5..34 listFvecF1FvecF1FvecF1..3 e>prs3$ if Jok then breakend3' model.state : 9initial53( model.rpar : 8hiA8lo53) graphics.e>prs : e>prs4* >.graphics : graphics41 >.model : model42 break43 end444 case FdefineF then4$ //messageFin defineF4' 8hi : 1.*4( 8lo : -1.*4) 9initial : ** model : scicos#model1 model.sim : listFlim#intF42 model.in : 1A1A153 model.out : 1A154 model.state : 9initial5 model.dstate : *5$ model.rpar : 8hiA8lo5' model.blocktGpe : FcF( model.nmode : 1) model.n=cross : 3$* model.dep#ut : It It5$1$2 e>prs : string8hiA8loA9initial55$3 gr#i : F>:orig1G:orig2w:s=1h:s=2FA$4 Ft>t:FFKunnGFFAFF+ntegratorFF5FA

    $ F>stringb>*.2?w G*.2*?h t>t *.*?w *.$*?h FFfillFFFA$$ Ft>t:FFinFFAFFFFAFFgainpFFAFFFFAFFgainnFF5FA$' F>stringb>*.*2?w G*.*(?h t>t *.2?w *.(*?h FFfillFFFA$( Ft>t:FFFFAFFoutFFAFFFFAFFgainFFAFFFF5FA$) F>stringb>*.'3?w G*.*(?h t>t *.2?w *.(*?h FFfillFFFA'* 5'1 > : standard#define4 25modele>prsgr#i'2'3 end'4

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e *7

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    18/25

    ' endfunction

    5.2 Commentar' on the Code

    4e&ore / be)in with the commentar%, let me $oint out that much o& whats in the inter&acin)&unction / dont &ull% understand! / have started with known workin) e3am$les and the availabledocumentation to come u$ with this &unction! 8owever, documentation is slim which is one reason

    /m writin) this tutorialBD! So, / will e3$lain onl% what / understand!Line 1:The name o& the &unction, #/M/T, will be used inside the Scicos editor to access the &unctionblock! :ee$ this in mind &or later!

    Lines $3 153 1&3 "#3 "%3 %:These are debu))in) lines! /& %ou uncomment them, %ou will be able to seedebu))in) in&ormation in the Scilab console and in dialo) bo3es while %ou construct and run themodel!

    Lines 103 1#3 1$3 "1:These cases ust use the JstandardK &unctions that are $rovided with Scilab! =oodluck &indin) documentation on how these &unctions work and i& %ou do &ind it, $lease let me knowBD

    Lines "5-#:This case de&ines the behavior o& the dialo) bo3 that a$$ears when %ou double-click theblock! /t uses the )etvalueD &unction in lines 2- to dis$la% the dialo) and return the values entered!The initial condition o& the model state is assi)ned in line 7, while the $arameters, #hi and #lo, areassi)ned to the model in line ?! All the return values are stored in the structure variable 3 in lines 0and *!

    Lines 5-$1:This case de&ines the a$$earance o& the &unction block and initialiEes all the modelvariables when the block is &irst $laced into the Scicos dia)ram!

    Lines 50-%0de&ine the blocks model $ro$erties! Com$are closel% the assi)nments in lines *-60 withthe initialiEers in the =11

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    19/25

    7 Testing the Interfacing Function

    ow that we have written the inter&acin) &unction, it is a sim$le matter to use it! Assumin) %oustill have Scicos o$en, activate the Scilab window &rom the Scicos Tools menuD and enter the&ollowin) command to create the inter&acin) &unction in Scilab'

    eec lim)int)int/2sci;

    =o back to Scicos b% clickin) the mouse in the Scicos window, o$en the dialo) &rom the +dit |

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    20/25

    nce a)ain, run the model! Lou should )et the &ollowin) $lot'

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e 20

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    21/25

    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 %oure 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$ insim$le debu))in) lines, so / wont discuss that an% &urther! 8owever, &or C code, s$rinklin) $rint&Dcalls into the code ma% not be su&&icient or ma% not work at all, es$eciall% i& %our Scilab instance isrunnin) as a se$arate $rocess, as stdout ma% not )o to a console! So, %ou will need to use )db! /n thissection, /ll )ive %ou a ver%D hi)h level overview o& the $rocess! And remember, what / describe hereis what / do on m% "buntu s%stem! /t will likel% work the same on most Oni3 s%stems, but is liable notto work the same on @indows s%stems!

    8.1 First teps

    /n order to make %our li&e much easier, we will start b% makin) a macro &ile &or )db that willdis$la% Scicos model values! Create a &ile in %our workin) director% called )db;cmd, and $ut the

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e 2*

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    22/25

    &ollowin) content into it'

    define show+nputs printf "+nputsB " set Li : * while Li % Larg* -& nin printf "inIi : Ig " Li ?double?Larg* -& inptrLi5 set Li : Li 1 end printf "Mn"end

    define show6utputs printf "6utputsB " set Li : * while Li % Larg* -& nout printf "outIi : Ig " Li ?double?Larg* -& outptrLi5 set Li : Li 1 end printf "Mn"end

    define showarms printf "rarmsB "

    set Li : * while Li % Larg* -& nrpar printf "rpIi : Ig " Li Larg* -& rparLi5 set Li : Li 1 end printf "Mn" printf "iarmsB " set Li : * while Li % Larg* -& nipar printf "ipIi : Ii " Li Larg* -& iparLi5 set Li : Li 1 end printf "Mn"end

    define showState

    printf "StateB " set Li : * while Li % Larg* -& n> printf ">Ii : Ig " Li Larg* -& >Li5 set Li : Li 1 end printf "Mn" printf ";erivB " set Li : * while Li % Larg* -& n> printf ">dIi : Ii " Li Larg* -& >dLi5 set Li : Li 1 end printf "Mn"end

    define show

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    23/25

    while Li % Larg* -& ng printf "HIi : Ig " Li Larg* -& HrootLi5 set Li : Li 1 end printf "Mn" printf "

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    24/25

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e 2

  • 7/13/2019 Tutorial Creating a C Function Block in Scicos

    25/25

    otice 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 hel,steand continuecommands 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

    Tutorial: Creating a C Function Block in ScicosPhil Schmidt, email'$hil&arm.&astmail!&m Pa)e 2