Cool Gears Image

Advanced PLC Programming with Automation Direct

In this article PLCGurus.NET member and contributor, Tony LeRoy, is back! Get ready as Tony discusses advanced PLC programming techniques leveraging object oriented design principles and Automation Directs Do-More PLC Platform. If you have any questions for Tony regarding this article, be sure to leave a comment below…enjoy!

Modern programmable logic controllers (PLCs) are unlocking the power of object-oriented programming (OOP) design constructs and concepts. The Automation Direct Do-More Platform is no exception to this. Using this platform I will walk you through some concrete examples of how tap into these advanced PLC programming constructs.

The computer science definition: an object oriented programming language is a paradigm that is based on the concept of object or structures that allows for the grouping of known attributes or properties. The ‘Struct’ keyword is a mechanism in the Do-More that allows us to accomplish this.

Employing advanced PLC programming techniques and objected oriented design methodologies results in more efficient, cleaner programs, allowing the PLC programmer to more readily stream-line his or her code. It also aids in the development of better documentation by allowing the naming of variables to be more intuitive and meaningful.

That said, I still recommend documenting your project extensively no matter what programming methodologies are used.

Utilizing advanced PLC programming techniques and object oriented design patterns allows you to abstract elements of your logic to make them Generic in nature. This means, equipment of the same type, with similar features, can have a template file, or base file, or starting point to work from. Then by way of instantiation, you simply make calls to, or create different instances for each individual item. This is very powerful!

Object oriented programming constructs in any PLC is set up by using something known as UDTs (User Data Type or User Defined Type, depending on manufacturer). A UDT is exactly what it sounds like. It defines another type of data in the PLC to be used.

If you have worked with Allen Bradley or Siemens, you have more than likely been exposed to UDTs. Automation Direct allows this feature in their Do-More platform with a few caveats which I’ll get into.

Advanced PLC Programming – An Example

Before we begin, let’s do a pseudo-example of a user data type. Say that we have a few dogs. Each dog is a dog type, but the characteristics of each dog may vary, namely, age, name, and color. A dog structure may look a little like this:

Advanced PLC Programming-1

Now, we can create a variable and make it the data type of dog. Compare that to creating a string variable and naming it “dogName.” It follows the same idea. Now that we have a template for the rest of our dogs.

Advanced PLC Programming-1b

We just created two new variables named Fido and Spot and characterized them with our structure. Now we can take different attributes individually about each dog.

Note: this is an intermediate to advanced tutorial using more advanced PLC programming techniques. Basic PLC functions such as counters and timers are expected to be known and will be used in this tutorial.

For this project, all you need is the Do-More Designer software which is free. The software also comes with a built-in simulator, so no hardware has to be purchased.

Once the software is open, go to the Configure Tab on the top toolbar.

Advanced PLC Programming-2a

Next, navigate down to the memory configuration. In the left window within the System Configuration, click on the Structs (Built-In/UDT) tab. Click on Add User Data Type.

Advanced PLC Programming-2b

Once you create a new UDT, the first thing you will need to do is to give the UDT a name. For this tutorial, I’ll be demonstrating how a UDT can be configured for a motor, and I will name it Motor_UDT.

This menu has many useful functions such as viewing the memory layout of a UDT. While the usefulness of seeing the memory layout is beyond the scope of this tutorial, it is useful to know that UDTs are created and divided into 32-bit sections.

The overall UDT is now established, but a UDT structure must have members. Click on the Add button in the bottom left to add a member.

Advanced PLC Programming-2c

Remember, a structure is just a bunch of variables grouped together. On the User Data Type Field Definition menu, define a member of the structure.

For this motor example, we want to create some fields that we would like to use to control the motor. The motor has a start button, a stop button, thermal or magnetic overloads, and also, we need to keep up with the total time that the motor has run so that it can be serviced accurately.

The Start button is just a simple switch, so change the field name to something similar. We do not need to include “Motor_1” or anything like that in the name of the member because the member will know which motor to reference with identifiers discussed later in this tutorial.

Continue adding members (fields) until all related control items have been listed.

Advanced PLC Programming-2d

Now we have our structure built and ready to use! Click the OK at the bottom of the screen. If this is a brand-new UDT, the interface will prompt you to initialize the structure and tie it to a variable. Do-More gives a few options.

Advanced PLC Programming-2e

If you only have a single item, then use the Heap Item option, however, I always recommend to use the Memory Block option. The memory block option creates an array of the structure. If you have similar equipment controlled in the same way, the memory block option is ideal and will also help with expansion if future equipment is added.

Upon clicking Memory Block define the name of the UDT. We will use MOTOR in this case. Also, make sure that your UDT is selected as the data type. Lastly, define the size of the array. If you have plenty of memory, I suggest always making this number more than what you actually need.

I always create at least one over the amount that I need simply because I don’t like starting my UDTs at 0. For this tutorial, I create a block size of 16.

Advanced PLC Programming-2f

Advanced PLC Programming – Time to Test

Now the fun begins! Right now, our UDTs are just floating in memory, not tied to anything in particular. Let’s add some logic to show the simplicity and elegance of UDTs.

In this example, I create three programs. Inputs will tie my physical inputs to my UDT members, Outputs will tie the motor to the MOTOR1.RUN command and the Control Program is the logic to make all of the items interact and function together.

If you want to learn more programming tricks, and the reasoning behind splitting the inputs, outputs, and control, take a look at my article 5 Skills Every PLC Programmer Should Have. I go over this in Skill #1!

Advanced PLC Programming-3

The program written above is a simple latching circuit with the addition of overloads and an hour-meter.

Instead of pulling from predefined variables, the items can be tied to the members of a motor with the notation MOTOR[motor number].(UDT member). The period in the middle denotes that you need to access a member from that structured variable.

This is the exact same as when you use a Timer.DN to access the done bit of the timer. Timers also have the .ACC member which is just another member of the timer data type.

Advanced PLC Programming-3b

Advanced PLC Programming-3b

With all of the variables set up, now the program will run. Start the simulator, connect to the PLC, and download the program. Hit X0 on the simulator. The motor should come on and the timer should start counting!

Advanced PLC Programming-3c

And there you have it! A fully-functioning program created using some advanced PLC programming techniques and object-oriented programming paradigms!

Some final words from PLCGurus.NET

For more articles from PLGurus.NET contributor, Tony LeRoy, check these links out:

If you haven’t already done so yet, I do encourage you to become a member of the site.

PLCGurus.NET is quickly becoming one of the largest and fastest growing communities of professional engineers, technicians and technologists who all share a passion for industrial automation and control systems.

Registration is and will always remain completely free. Register Here!

Also, be sure to check out the PLCGurus.NET YouTube Channel to see some great videos…and don’t forget to like and subscribe to our channel! If you enjoyed this article be sure to check out some of these good reads too:

Lastly, if you run  into any problems in your day-to-day engineering activities please be sure to check out our Live and Interactive PLC Forum! And if you so desire, assist other community members by replying or offering helpful information to the questions or challenges they may be facing right now! Thanks for reading.