Join us today!
FB_init with derived function blocks
If you are using FB_Init method for a function block and implementing inheritance, FB_Init of the child function blocks must have the same parameters as the parent of FB_Init. Otherwise, you would get the error below:
We will create 3 function blocks in this tutorial. The first function block will be FB_Base which will be requiring the initialization of sPath in its FB_init method. We will then add FB_Motion function block which will derive from FB_Base. In the FB_init of FB_Motion, we will need to initialize the fSpeed. Lastly, we will create FB_Motion_Extended which will derive FB_Motion and we will add an input called 'fTemperature 'its FB_init.
FB init of FB_Base:
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) sPath : STRING; END_VAR
_sPath := sPath;
FB init of FB_Motion:
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) sPath : STRING; fSpeed : REAL; END_VAR
_fSpeed := fSpeed;
With the addition of fSpeed, we have overwritten the parent's FB_Init method. If sPath is not included here, you would get the error mentioned above. The order of the parameters have to match the order in the FB_Init of parent function block. If you declare the sPath after fSpeed, you would get the same error again.
FB_Init of FB_Motion_Extended:
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) sPath : STRING; fSpeed : REAL; fTemperature : REAL; END_VAR
_fTemperature := fTemperature;
Again, notice that we are following the same parameter order.
When declaring these function blocks, you would have to initialize all the required parameters. FB_init method of the parent function blocks will be called implicitly.
fbBase : FB_Base(sPath:='path_base'); fbMotion : FB_Motion(sPath:= 'path_motion',fSpeed := 25); fbMotionExt : FB_Motion_Extended(sPath:='path from extended motion',fSpeed:= 15,fTemperature:= 36);
When you go online and try to monitor the FB_Base function block, if you choose the fbMotionExt option, you would see that sPath has been initialized with 'path from extended motion' value.
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
Hello, here is very well explained and detailed FB_init:
https://stefanhenneken.net/2019/07/26/iec-61131-3-parameter-transfer-via-fb_init/
https://github.com/runtimevic
https://github.com/TcMotion
https://www.youtube.com/playlist?list=PLEfi_hUmmSjFpfdJ6yw3B9yj7dWHYkHmQ
https://github.com/VisualPLC
- 17 Forums
- 267 Topics
- 942 Posts
- 0 Online
- 722 Members