CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Thu Sep 20, 2018 5:12 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Fri Mar 16, 2018 11:40 am 
Offline

Joined: Thu Jul 09, 2015 9:55 pm
Posts: 24
Hello

I have initiated a function block several times and want to loop through to check for a variable.
Code:
VAR CONSTANT
   numOfGensets      : BYTE := 2;
END_VAR
VAR
   gen1, gen2      : fbGenset;
   gensets         : ARRAY[1..numOfGensets] OF fbGenset := [gen1, gen2];
END_VAR;

.... Main prog....

gen1();
gen2();

// Loop through all initiated gensets
FOR i := 1 TO numOfGensets DO
   IF gensets[i].breakerStatus() = BREAKER_STATUS.TRIPPED THEN
      tripped := TRUE;
   END_IF
END_FOR


Code:
METHOD breakerStatus : BREAKER_STATUS

// Check status of circuit breaker/generator breaker
IF closed THEN
   breakerStatus := BREAKER_STATUS.CLOSED; // Closed
ELSIF NOT closed THEN
   breakerStatus := BREAKER_STATUS.OPEN; // Open
ELSIF tripped THEN
   breakerStatus := BREAKER_STATUS.TRIPPED; // Tripped
END_IF


This dosent work as desired as gen1 and gen2 isnt updated cyclically in the array.
How should this be done the proper way without having to update the array every scan?


Last edited by somlioy on Wed Mar 28, 2018 11:17 am, edited 1 time in total.

Top
   
PostPosted: Fri Mar 16, 2018 12:09 pm 
Offline
User avatar

Joined: Fri Feb 10, 2017 6:12 am
Posts: 49
Code:
program PLC_PRG
var constant   
    numOfGensets: byte := 2;
end_var
var
    i: byte;
    gen1, gen2: ton;
    gensets: array [ 1 .. numOfGensets ] of pointer to ton := [ adr( gen1 ), adr( gen2 ) ];   
end_var
// ------------------
gen1( pt := t#10s );
gen2( pt := t#20s );

// Loop through all initiated gensets
for i := 1 TO numOfGensets do
   
   if gensets[i]^.pt = t#10s then gensets[i]^.pt := t#55s; end_if
   
end_for


Top
   
PostPosted: Fri Mar 16, 2018 4:31 pm 
Offline

Joined: Thu Jul 09, 2015 9:55 pm
Posts: 24
Brilliant, thanks!


Top
   
PostPosted: Wed Mar 28, 2018 11:23 am 
Offline

Joined: Thu Jul 09, 2015 9:55 pm
Posts: 24
New but similar question.

Assume two FBs where FB_B extends FB_A.

Lets say I have declared some instances of FB_B and a few of FB_A.

How can I loop through all of the declared instances of both FB_B and FB_A to do something thats declared in FB_A?

---
Question 2:
Is it possible to have a variable shared between declared instances, not beeing in a GVL?


Top
   
PostPosted: Thu Mar 29, 2018 8:09 am 
Offline
User avatar

Joined: Fri Feb 10, 2017 6:12 am
Posts: 49
Quote:
How can I loop through all of the declared instances of both FB_B and FB_A to do something thats declared in FB_A?

You can use method FB_init() for this. Online help.

Code:
METHOD FB_Init : BOOL
VAR_INPUT
  bInitRetains : BOOL; // TRUE: the Retain-variables are initialized (reset warm / reset cold)
  bInCopyCode : BOOL;  // TRUE  the instance will be copied to the copy-code afterward (online change)
END_VAR


Quote:
Is it possible to have a variable shared between declared instances, not beeing in a GVL?

It is possible, but why you need this? (you can use var_stat... end_var in FB_A)

Code:
function_block FB_A
var_input
    value: int := 0;   
end_var
var_stat
    m_index: int := 0;
    m_instances: array [ 0 .. 1000 ] of pointer to FB_A;
end_var
-----------------------
method public FB_Init : bool
var_input
    bInitRetains : bool; // initializing of retain-variable
    bInCopyCode : bool; // instance is copied to copy-code
end_var

m_instances[ m_index ] := this;

m_index := m_index + 1;
-----------------------
method GetInstAt : pointer to FB_A
var_input
    index: int;
end_var

GetInstAt := m_instances[ index ];
-----------------------

function_block FB_B extends FB_A

-----------------------

program PLC_PRG
var
    bFirstCycle: bool := true;
    f1: FB_A;
    f2, f3, f4: FB_B;
    p: pointer to FB_A;
end_var

if bFirstCycle then

    f1.value := 1;
    f2.value := 2;
    f3.value := 3;

    p := f1.GetInstAt(3);

    if p <> 0 then p^.value := 4; end_if // Check f4.value online

    bFirstCycle := false;
   
end_if


Top
   
PostPosted: Mon Apr 02, 2018 9:25 pm 
Offline

Joined: Thu Jul 09, 2015 9:55 pm
Posts: 24
Hmm. Ok. Not sure how to do that.

Reason behind the looping is to monitor the state of a variable in FB_A (aswell as FB_B which extends FB_A). Normally I'd do this as proposed by you in a earlier post, but this dosent work when I also need to include both FB_B and FB_A.

Reasons behind shared variable is because I want a interlock between declared instances. Say FB_A has a method for controlling something external (relay for example) and if any of FB_A (aswell as FB_B which extends FB_A) has this output active none of the others shouldnt be able to actuate the external output.

Maybe you have a better solution to this?


Top
   
PostPosted: Wed Apr 04, 2018 7:17 am 
Offline
User avatar

Joined: Fri Feb 10, 2017 6:12 am
Posts: 49
There is a library with synchronization elements: CAA Types Extern with BOLT and SEMA. To use them you need to know more precisely what you want.

Interlocking - what does this mean in your opinion? How should this work? Other fbs must wait or do nothing or return error state?


Top
   
PostPosted: Wed Apr 04, 2018 1:31 pm 
Offline

Joined: Thu Jul 09, 2015 9:55 pm
Posts: 24
Each FB has a variable to start a sequence (action), if any of the FBs has say "BREAKER_STATUS.CLOSED" active (from first post), then the others should not be able to activate the closing sequence.


Top
   
PostPosted: Sun Apr 15, 2018 9:10 pm 
Offline

Joined: Thu Jul 09, 2015 9:55 pm
Posts: 24
Any more tips?


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 9 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: Aliazzz and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited