CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Tue Feb 18, 2020 7:21 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 18 posts ]  Go to page 1 2 Next
Author Message
PostPosted: Fri Dec 30, 2016 1:13 pm 
Offline

Joined: Sat Jan 02, 2010 2:05 pm
Posts: 97
Hello all,

I've tried to search help files and Google, but it is clear that my Google Fu is not as good as needed now.

I'm trying to put some function blocks inside an array.
The problem with this super-easy task is that those function blocks use extensively the FB_Init function so I do need to initialize the parameters of those FB_Init functions at the array declaration and I don't know which syntax to use.

Anyone here has done this and would like to share it?

Thank you all!

Happy new year to all the members here!

_________________
http://www.robotecnik.com - robots, CNC and PLC programming.


Top
   
PostPosted: Fri Dec 30, 2016 1:52 pm 
Offline

Joined: Fri Dec 09, 2016 11:44 am
Posts: 17
Joan Murt wrote:
Hello all,

I've tried to search help files and Google, but it is clear that my Google Fu is not as good as needed now.

I'm trying to put some function blocks inside an array.
The problem with this super-easy task is that those function blocks use extensively the FB_Init function so I do need to initialize the parameters of those FB_Init functions at the array declaration and I don't know which syntax to use.

Anyone here has done this and would like to share it?

Thank you all!

Happy new year to all the members here!


viewtopic.php?f=24&t=7438


Top
   
PostPosted: Fri Dec 30, 2016 2:01 pm 
Offline

Joined: Sat Jan 02, 2010 2:05 pm
Posts: 97
Hello camferti,

Sorry, but this is not working:

Code:
fbE : ARRAY [1..4] OF fb_E:= [(ptrCF:=ADR(fb1CF)),(ptrCF:=ADR(fb2CF)),(ptrCF:=ADR(fb3CF)),(ptrCF:=ADR(fb4CF))];


The FB_Init of fb_E looks like:

Code:
METHOD FB_init : BOOL
VAR_INPUT
   bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
   bInCopyCode : BOOL;  // if TRUE, the instance afterwards gets moved into the copy code (online change)
   
   ptrCF : POINTER TO fb_E := 0;
END_VAR


Of course I've also tried something like:

Code:
fbE : ARRAY [1..4] OF fb_E:= [(bInitRetains :=TRUE,bInCopyCode :=TRUE,ptrCF:=ADR(fb1CF)),(bInitRetains :=TRUE,bInCopyCode :=TRUE,ptrCF:=ADR(fb2CF)),(bInitRetains :=TRUE,bInCopyCode :=TRUE,ptrCF:=ADR(fb3CF)),(bInitRetains :=TRUE,bInCopyCode :=TRUE,ptrCF:=ADR(fb4CF))];


Same results...

See that I'm trying to initialize the "constructor" (FB_Init) of the very same function block I'm instantiating...

Thank you all!

_________________
http://www.robotecnik.com - robots, CNC and PLC programming.


Top
   
PostPosted: Fri Dec 30, 2016 3:11 pm 
Offline
Frequent User
Frequent User

Joined: Wed May 04, 2016 6:00 pm
Posts: 288
I dont know if it could be that you need, but..

I have made dynamic arraylist fb similar that it is in Java. You could instantiate the fbs outside arraylist and adding its to it.


Top
   
PostPosted: Fri Dec 30, 2016 3:35 pm 
Offline

Joined: Sat Jan 02, 2010 2:05 pm
Posts: 97
Hi Josep M. Rams,

If I understand you well you are proposing to create the function blocks as I'm doing now (outside of the array) and then add them into the array (an array of references or pointers to the same function blocks).

This is what I'm doing now as it is a workaround to be able to put all the function blocks operations inside a loop if needed.

This is a good option but I would prefer being able to make it at the declaration stage as then the job is done only once.

Thank you for your proposal.

And of course, bon any nou!

_________________
http://www.robotecnik.com - robots, CNC and PLC programming.


Top
   
PostPosted: Sat Dec 31, 2016 12:20 pm 
Offline

Joined: Fri Dec 09, 2016 11:44 am
Posts: 17
I do not understand the purpose of all this, to be able to think a solution

You declare a type

TYPE POINT :
STRUCT
x:INT;
y:INT;
END_STRUCT
END_TYPE

Do you use the type
VAR
arr: ARRAY [0..10] OF POINT :=[(X:=0,Y:=0),(X:=250,Y:=50),(X:=1000,Y:=1000),(X:=1000,Y:=1000),(X:=1000,Y:=1000),(X:=1000,Y:=1000),(X:=1000,Y:=1000),(X:=1000,Y:=1000),(X:=1000,Y:=1000),(X:=1000,Y:=1000),(X:=1000,Y:=1000)];
END_VAR


Change data types and go
good luck


Top
   
PostPosted: Sat Dec 31, 2016 12:42 pm 
Offline

Joined: Sat Jan 02, 2010 2:05 pm
Posts: 97
@camferti

First of all, thank you for answering.

Then, the problem here is much more complex than what you are suggesting:

I do need to create a function block (A) that configures itself at runtime.
In order to do that I've created a Class Factory (which is another function block (CF) that using interfaces, pointers and the newly added in V3 DMA (dynamic memory assignment) creates the inner function blocks for A.
This avoids the need to create each possible combination and it eases and reduces the code maintenance.

In order to do that easily and avoiding having to code anything in the code area, I'm using the FB_Init (what a constructor would be for a class in normal OOP) function for both CF and A in order to be able to configure them.

All this said, what you suggest is the initialization for a specific user defined type (a structure, ...) even this works, I don't need that.

I do need to be able to initialize the specific FB_Init inside an array of CF at the declaration stage. And this is what I've not found how to achieve. Not documented in the Codesys, TwinCAT neither SoMM documentations, neither in the Google results I've found.

what I'm using outside the aforementioned array is:

Code:
fbCF    : fbClassFactory(1, 27, 3);
fbA   : fbA(ADR(fbCF));


As you can see here I'm passing parameters to the fb_Init function at declaration stage and I would like to be able to do the same in arrays.

That's what I'm asking exactly here.

_________________
http://www.robotecnik.com - robots, CNC and PLC programming.


Top
   
PostPosted: Sat Dec 31, 2016 1:09 pm 
Offline

Joined: Fri Dec 09, 2016 11:44 am
Posts: 17
Joan Murt wrote:
@camferti

First of all, thank you for answering.

Then, the problem here is much more complex than what you are suggesting:

I do need to create a function block (A) that configures itself at runtime.
In order to do that I've created a Class Factory (which is another function block (CF) that using interfaces, pointers and the newly added in V3 DMA (dynamic memory assignment) creates the inner function blocks for A.
This avoids the need to create each possible combination and it eases and reduces the code maintenance.

In order to do that easily and avoiding having to code anything in the code area, I'm using the FB_Init (what a constructor would be for a class in normal OOP) function for both CF and A in order to be able to configure them.

All this said, what you suggest is the initialization for a specific user defined type (a structure, ...) even this works, I don't need that.

I do need to be able to initialize the specific FB_Init inside an array of CF at the declaration stage. And this is what I've not found how to achieve. Not documented in the Codesys, TwinCAT neither SoMM documentations, neither in the Google results I've found.

what I'm using outside the aforementioned array is:

Code:
fbCF    : fbClassFactory(1, 27, 3);
fbA   : fbA(ADR(fbCF));


As you can see here I'm passing parameters to the fb_Init function at declaration stage and I would like to be able to do the same in arrays.

That's what I'm asking exactly here.

So far as I can see the problem is the statement at run time?
You can attach a small example of what you need?
Next week, I will pass the query to the corresponding department


Top
   
PostPosted: Sat Dec 31, 2016 1:18 pm 
Offline

Joined: Sat Jan 02, 2010 2:05 pm
Posts: 97
Hi again camferti,

You can take a look at this other post here in the same forum... it shows exactly what I'm after:

http://forum.codesys.com/viewtopic.php?t=5663

There the proposed solution by scott_cunningham is to use a flag and at the code just initialize the values as desired.

I'd love to do it at the declaration stage (as DavidWSteadman asks).

I'm complicating it much more by using the class factory and all those things, but at the end the problem I'm trying to solve is that one.

Thank you again!

_________________
http://www.robotecnik.com - robots, CNC and PLC programming.


Top
   
PostPosted: Sat Dec 31, 2016 2:05 pm 
Offline

Joined: Fri Dec 09, 2016 11:44 am
Posts: 17
In the problem, which is MyObject?
A variable ?. what type?
A definite type?
a function?
Where declares the structure of MyObject?


DavidWSteadman, Declare a function that refers to itself in the first step?

Function x
Var imput
Arr array [0..1] of x;
End_var

This can not work


Top
   
PostPosted: Sat Dec 31, 2016 2:50 pm 
Offline

Joined: Sat Jan 02, 2010 2:05 pm
Posts: 97
Hello again,

Some extra explanations in the pictures comments.

You can see three different ways to instantiate the MyObject function block in MyProgram.

The first method of instantiation works well, but it forces me to create instances individually and I loose the possibility to put them inside an array. This is how I'm doing it right now.

The second method doesn't work for me as I do need the FB_Init call which is not called/parameterized.

The third one is what I'm interested in but doesn't work.

Thank you for your time.


You do not have the required permissions to view the files attached to this post.

_________________
http://www.robotecnik.com - robots, CNC and PLC programming.


Top
   
PostPosted: Sat Dec 31, 2016 4:11 pm 
Offline

Joined: Fri Dec 09, 2016 11:44 am
Posts: 17
Joan Murt wrote:
Hello again,

Some extra explanations in the pictures comments.

You can see three different ways to instantiate the MyObject function block in MyProgram.

The first method of instantiation works well, but it forces me to create instances individually and I loose the possibility to put them inside an array. This is how I'm doing it right now.

The second method doesn't work for me as I do need the FB_Init call which is not called/parameterized.

The third one is what I'm interested in but doesn't work.

Thank you for your time.


Do you want to use a variable that you define within a function in the program?
it's not possible.
Why do you want to do that? I do not see the meaning. What are you trying to do?

In the first line you make a call to the function "myObject (1)" so it does not give error
In the second and third where you make the call to the function? You do not start the instance


happy New Year


Top
   
PostPosted: Sat Dec 31, 2016 5:22 pm 
Offline

Joined: Sat Jan 02, 2010 2:05 pm
Posts: 97
Hi again camferti...

This is possible using the new Object Oriented Programming paradigm Codesys V.3 offers.

FB_Init is not a "normal" function.
It is the constructor function and it is used specifically to initialize internal things of the newly created function block. (Somehow configure it).
this function is automatically called each time you instantiate the object that owns it (so each time you instantiate the fb that has it implemented).

See the manual page of TWinCAT (It's the only one I've found on the Internet) here: https://infosys.beckhoff.com/content/1033/tc3_plc_intro/9007199390945931.html?id=8333023073559976250

As a good example, it works when used alone (first line) in the program:

Code:
MyObjectAlone : MyObject(1);


Here I'm not calling FB_Init at all, but it is automatically called and it receives 1 as the parameter ID.
But it is not working when used in an array as I can't parameterize the FB_Init function...

The reason of this to fail must be:

A) The syntax I'm using is wrong (which is what I believe).
B) The implementation of this feature is not done even the error message doesn't suggest this.

In the newly attached picture you can see the errors the compiler show.

Hope this clarifies a little bit more what happens here.

Thank you very much.

Again, happy new year.


You do not have the required permissions to view the files attached to this post.

_________________
http://www.robotecnik.com - robots, CNC and PLC programming.


Top
   
PostPosted: Wed Jan 04, 2017 8:33 pm 
Offline

Joined: Sat Apr 16, 2016 5:23 pm
Posts: 64
Joan if I understand correctly, perhaps you can do something like the following example. I haven't tried it so don't know if this approach works.

Code:
// *** FB Definition
FUNCTION_BLOCK MyObject
VAR
   Id         : INT;
END_VAR

// *** Init Method
METHOD FB_init : BOOL;
VAR_INPUT
   bInitRetains   : BOOL;
   bInCodyCode   : BOOL;
   List         : POINTER TO ARRAY [0..32767] of MyObject;
   First         : INT;
   Last         : INT;
END_VAR
VAR
   i         : INT;
END_VAR
FOR i:=First TO Last BY 1 DO      // Search the list for self
   IF List^[i]=THIS^ THEN
      ID := i;
      Exit;
   END_IF
END_FOR

// *** Usage
VAR
   ObjectArray   : ARRAY [1..4] of MyObject(List:=ADR(ObjectArray), First:=1, Last:=4);
END_VAR

_________________
Rick


Top
   
PostPosted: Thu Jan 05, 2017 4:07 pm 
Offline
Frequent User
Frequent User

Joined: Wed May 04, 2016 6:00 pm
Posts: 288
Dears.

From your post I will test to pass parameters to FB_Init when the fB is created via __new and it works doing:

tst:=__new(test_class(temp:=450));

(Being temp an input of FB_init),

After that I have test to do the same in arrays, but I only can pass the same parameter to all FB_inits of the array:

tst2:ARRAY[0..2] of test_class(temp:=450);
All tst2 elements are initialized with temp:=50.

But in array initialization you could initialize internal FB variables:

tst2:ARRAY[0..2] of test_class:=[(tt:=1),(tt:=2),(tt:=3)];

Being tt internal variables of the FB not input variables of FB_init. That variables are setted after FB_init, but before a function with {'attribute call_after_init'}.

You can use internal variables as constructor inputs and put the constructor code in a function with 'call_after_init' attribute.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 18 posts ]  Go to page 1 2 Next

All times are UTC+01:00


Who is online

Users browsing this forum: Andreas Kehrer and 5 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