Creating Function Blocks and UDT's - Part 1: Function Blocks

Creating Function Blocks and UDT's - Part 1: Function Blocks

INTRO

If you have been in the automation industry then you have probably heard the terms Function Blocks and UDT's. Mabey your new in the field of automation and have never heard the terms Function Blocks or UDT's? Perhaps you have been in the field for some time and have never had the opportunity to create either a Function Block or a UDT? I can honestly say this was the case for me up until a couple years ago when my main role was commissioning and troubleshooting. I never really had the opportunity to create anything like a UDT or a Function Block. Well times have changed for me and now I have progressed into becoming a well rounded PLC and HMI programmer thanks to a lot of self study, forums, You Tube Videos, and late nights in my basement lab practicing on actual PLC's and simulators!!

In this 2 part series I am going to show you how to make a Function Block and a UDT. Now you may be use to working with a particular platform but the theory is the same whether you use for example Rockwell or in my examples, OMRON. There are many great platforms out there and most will follow the same theory so lets dig in with Part 1!

FUNCTION BLOCKS

Lets begin with the Function Block. What is a Function Block? A Function Block in nothing more than some logic that is written to execute some action based on some conditions which is encapsulated into a nice reusable block of code. Now this may not be the official definition but that's what it is in a nut shell. Let me explain in more detail as well as answer why we would want to create a function block.

Lets say you need to write some code that will control 3 or more VFD's. Each VFD will send and receive the following information to or from the PLC.

  • Start Signal
  • Stop Signal
  • Run Command Flag
  • Alarm Flag
  • Alarm Reset
  • Running Status Flag
  • Fail to Start Flag
  • Speed Setpoint
  • Current Running Speed
  • VFD Total Run Time

As you can see from the image below, this is a ton of variables that need to be defined for each VFD.

No alt text provided for this image


We can easily write this in ladder logic but I bet you agree that it was a lot of work creating all the dedicated tags for each VFD as seen in my Variables List!! That's 30 variables for just 3 VFD's. Imagine doing this for say...15 VFD's!!

Now lets see what this looks like when written out in ladder logic!!! Now it was hard for me to fit all 12 rungs in the view but as you can see in the image I have created ladder logic for each of the three VFD's which are essentially the same logic repeated over 3 times. The only difference is the variables for each of the VFD's are specific to that VFD.

Ladder logic for 3 VFD's

I encourage you to try this. You will quickly see that this is time consuming even for just 3 VFD's with only 10 signals. Imagine how long this would take if you had 15 VFD's with 15 signals each!! This is where the Function Block comes in handy. Now when I say Function Block I am talking about creating your own Function Block. PLC Software will come with pre loaded Function Blocks that are already created for you. Take the simple Timer On instruction. That is an example of a Function Block that comes with the software. For our VFD's we are going to create a custom Function Block that will reduce development time. How you ask? Well let me show you!

Remember I said that Function Blocks are basically encapsulated code that can be easily reused for repetitive tasks? Well our Ladder Logic above definitely is a repetitive task! I am now going to show you how I can take our 12 lines of code and do the exact same logic in only 3 lines of code. That's 1 line of code for each VFD!!

Now in OMRON the first thing I need to do is create the Function Block. In Sysmac Studio all you need to do is Right Click on Function Block, select ADD, and choose either Ladder or Structured Text. To keep it simple I will do my example in Ladder Logic.

Creating a new function block

Once you select Ladder a new blank Function Block routine will have been created. Feel free to rename it to what makes sense to your application. For my example I will name my Function Block "VFD". When you double click on the new Function Block you will be navigated to a blank screen. This is where you will write your logic that will be contained within the Function Block.

No alt text provided for this image

Since we have already created some ladder logic for our VFD's I am simply going to go back and copy the logic for one of the VFD's and paste it within the Function Block logic. Now when working in the Function Block routine, we have to define the variables for that Function Block. We also have to define the variable as to how it is to be used in the Function Block. For OMRON Sysmac Studio we have 3 options. Each option is explained as follows;

  1. Internals: Think of Internals as an internal variable that is accessible to only the Function Block. Internal variables cannot be accessed outside of the Function Block.
  2. In/Out: Think of the In/Out variables as data that can pass information In and Out of the Function Block. Within this area you set the variable to be either an Input or an Output.
  3. Externals: External variables are Global Variables that are accessed from the Global Variables List.
No alt text provided for this image

So after I have pasted my ladder logic into the Function Block area I will need to modify my variables to a more generic name. So instead of having for example VFD1StartCMD, I will simply just call the variable StartCMD.

Creating my Function Block variables is really easy! All I need to do is Right Click on my new variable, select Register in Variable Table and select the appropriate type.

No alt text provided for this image

In the image below I will make the AlarmRST an Input Variable.

No alt text provided for this image

Now why is it important to select whether a variable is an Input, Output, Internal, or External? Well let me show you that very soon!

Now that I have assigned all my Function Block variables and am happy with the logic I can close the routine. Ya that's right! That's all there is to creating a Function Block! All I really did was write some ladder logic and assign some variables just like I did when I created the ladder logic for each VFD. But this is where your going to see the power of the Function Block! Lets now go back and do those 3 lines of logic for the 3 VFD's using our new Function Block!!

In the image below you can see that I have already assigned 2 of the 3 Function Blocks. How I did this was simple! Once I inserted a blank Function Block I clicked in the empty box labeled "Enter Type Name". Once you click in here start to type the name of your Function Block. A list of available Function Blocks that you have created will be available to you. Since I only have the one Function Block then all it will show is that one.

No alt text provided for this image

Above in the image can you see how the Function Block is layout? That all comes back to how I assigned my Function Block Terminals! A note before we move on. I also have to assign a name (variable) to each Function Block. This is how we designate a Function Block to each of our VFD's.

So now that I have my Function Blocks designated I can go ahead and now assign the respective variables for each of the Function Blocks Input and Output Variables.

No alt text provided for this image

As you can see in the above image all I did was use our existing variables that we used in our initial 12 rung ladder logic. But can you see how awesome and efficient it is to use our new Function Block!!! What we did previously in 12 rungs of logic we now have done in only 3 lines of logic!!

That's all there is to creating a Function Block and like I said in the beginning, the same theory applies to any platform of software, with some minor variances mind you.

This concludes Part 1 of this series on creating Function Blocks and UDT's. Look for Part 2 where I will show you how we can take our 3 VFD's and Function Block to become even more efficient in our programming!

Until then keep on learning!

James G Ashton M.E., C.Tech.

? 2021


Well explained …waiting for other parts..

回复

Function blocks rule.

Thomas Fuscic

Automation Manager at Dalton Electrical Co Ltd.

4 年

Well said. If only FBs could be a global UDT, at the moment I make a UDT per FB for SCADA templates etc, unless there is an easier way... in any case Sysmac is a great platform. ??

要查看或添加评论,请登录

James A.的更多文章

社区洞察

其他会员也浏览了