Function Block, Initialisation at instantiation

2007-08-29
2007-09-27
  • annodomini2 - 2007-08-29

    Hi,

     I new to the board and CoDeSys, but not to software.
    
    
    
     Had a search but doesn't appear to be anything relating to this.
    
    
    
     Now my question is regarding Function Blocks, which generally need an instance of to be used (for reusability), now can I initialise some internal variables/constants for a specific instance? And if so, How?
    

    Thanks Anno

     
  • Igor Petrov - 2007-08-30

    1) You can do global initialization (for all instances). Example:

    FUNCTION_BLOCK SyncSwitch

    VAR_INPUT

    bSync: BOOL := TRUE;

    …

    2) In the instances you can redefine the initialization:

    SyncSwitch_1 : SyncSwitch := (bSync := FALSE);

    3) You can use different direct variables in different instances. See Variable Configuration in the manual.

     
  • annodomini2 - 2007-08-31

    Igor Petrov hat geschrieben:
    1) You can do global initialization (for all instances). Example:
    FUNCTION_BLOCK SyncSwitch
    VAR_INPUT
    bSync: BOOL := TRUE;
    …
    2) In the instances you can redefine the initialization:
    SyncSwitch_1 : SyncSwitch := (bSync := FALSE);
    3) You can use different direct variables in different instances. See in the manual.

    Thanks, I assume this also applies to the VAR declaration block and not just VAR_INPUT?

     
  • Igor Petrov - 2007-08-31

    annodomini2 hat geschrieben:
    Thanks, I assume this also applies to the VAR declaration block and not just VAR_INPUT?

    IEC programmers are used to concentrate all parameters in inputs. Therefore in LD or FBD they connect external constants with the graphic box (FB instance) and get an easy to review and change diagram.

    But if you consider it reasonable… Why not:

    FUNCTION_BLOCK xz_xz

    VAR_INPUT

    bIn1 : BOOL;
    

    END_VAR

    VAR_OUTPUT

    bOut1 : BOOL;
    

    END_VAR

    VAR

    byVar1 : BYTE := 0;
    

    END_VAR

    ;

    ...

    Declaration:

    xz1 : xz_xz := (byVar1 := 1);

    By the way, in CoDeSys you can use 'Actions' in FB = very useful feature. You could use it for a few 'smart initialization'. For example if you need to calculate something during the initialization.

     
  • annodomini2 - 2007-08-31

    Igor Petrov hat geschrieben:
    By the way, in CoDeSys you can use 'Actions' in FB = very useful feature. You could use it for a few 'smart initialization'. For example if you need to calculate something during the initialization.

    Sounds interesting, will read up on that, thank you.

    So for example if you had an FB that read a specific CAN message from different sources, for a specific instance you could initialise the instance with the Device Id of a specific source and then at initialisation produce and store the entire message Id?

     
  • Igor Petrov - 2007-08-31

    Without actions you need extra inputs for parameters or for change the behavior. As result FB looks like a hedgehog

    With actions you can write: MyFB1.Stop(), MyFB1.Set_new_ID(), MyFB1.Run()…

    Unfortunately FB has no a specialized automatic action for initialization (it is possible in SFC), but you could call any action only once for a special work.

     
  • annodomini2 - 2007-08-31

    Can't wait till we get 3.0, The limitaiton of this is that the Actions require the usage of the defined inputs for the type, rather than a 'Class' with methods/functions that operate on the instance data.

    Real limitation of the language/system.

     
  • spfeif - 2007-09-26

    Igor can you elaborate slightly on your last post about actions with the example of myFB1.Run().. etc.. It sounds interesting but I am not sure I understand your point. Or we might be thinking of the same thing.

     
  • annodomini2 - 2007-09-27

    spfeif hat geschrieben:
    Igor can you elaborate slightly on your last post about actions with the example of myFB1.Run().. etc.. It sounds interesting but I am not sure I understand your point. Or we might be thinking of the same thing.

    Actions are required to use the function interface of the heading block.

    In 99% of all other true object orientated languages procedures of the defining class can have a different function interface.

    Does the method changes in 3.0 support this?

     

Log in to post a comment.