KNOWARE  

Software for Education and Industry


   Home   Order Form   

OOP (Object Oriented Programming)

with

CODESYS Made Simple

Want to learn how to use Object Oriented Programming (OOP) to program a PLC?  Tired of watching videos that move too fast with an accent you can't understand? Don't want to spend thousands of dollars to travel hundreds of miles to attend a class?  OOP (Object Oriented Programming) wth Codesys Made Simple will show you step-by-step how to program a PLC using OOP with Codesys in simple, easy to understand, examples.

What is a OOP (Object Oriented Programming)?
OOP is a way to package computer code or PLC (Programmable Logic Controller) instructions into reusable blocks or "objects" that can be used multiple times in a program or in other programs.  OOP is a way to add structure to a program and keep program sections separate from one another so that the program doesn't end up with "spaghetti code" where multiple instructions affecting multiple elements are all mixed up together. OOP is not a new programming language in-and-of itself, but rather a way to package code to make programs more efficient, reusable, and modifiable.  High level computer languages like C and C++ have used OOP for years, and recently OOP has crept into programming of PLCs. 

PLCs can be programmed in a number of languages, including:

Of these PLC programming languages, Structured Text is the most common language in which OOP is used.  This tutorial assumes you know how to launch Codesys, run Codesys programs in Simulation mode, force inputs to have certain values, and have some familiarity with Codesys Structured Text programming.  If you are new to Structured Text programming with Codesys, or just need a refresher on it, we recommend our tutorial entitled PLC Structured Text with Codesys.


What are "Objects"?
The "Objects" of Object Oriented Programming (OOP) are blocks of code created from Classes. Classes are like cookie-cutters, or templates, that are used to produce Objects that are like the cookies.  When an Object is created from a Class it is called an Instance of the Class. Thus, we say an Object is instantiated or created from a Class.  As many Objects can be instantiated from a Class as you want, just like you can make as may cookies from a cookie cutter as you want.  A Class can include Methods (Subroutines and Functions), Properties, and Variables.  Methods and Subroutines are groups of code that you can call whenever you want to get something done,  instead of re-writing those lines of code each time you need that thing done.  For instance, scaling an input, making a repetitive calculation, or spell-checking a document might be done by a Method.  Methods are Subroutines that return a value to the program that called it. Properties are characteristics of the Class, like whether the cookies have chocolate chips or not.  Methods can be called to change the Properties of the Class.  So you might write a Method to add chocolate chips to your cookies, thereby changing the chocolate chip Property of the Class.

In this tutorial we will begin by showing step-by-step how to create a Motor_Starter Class that mimics the common motor starting circuit shown below.  Then we will use the Motor_Starter Class and Inheritance to create a Jog_Motor Class and a Rev_Motor Class. Then other concepts of OOP like passing parameters, access modifiers, polymorphism, and interfaces will be explained with easy-to-understand examples.

Common motor starting circuit




There are two obstacles in using Codesys OOP to design the control for a machine or system:
1)  deciding what should be the Objects
2)  learning the syntax that Codesys uses to implement OOP.
Hopefully this tutorial will help overcome these obstacles.

If you aren't designing a system, but rather just troubleshooting or modify a machine that is controlled by a PLC program that uses OOP techniques, having a working knowledge of OOP will make the job much easier.

This tutorial is written by a Professional Engineer with 14 years industrial experience and 20 years experience teaching electrical engineering technology at the college level.

If you want to learn how to use CODESYS to program PLC Ladder Diagram and Function Block programs, you can purchase my PLC Ladder Logic and Function Block Programming with CODESYS V3.5 tutorial at http://www.knoware-online.com/codesys.html. Or if you want to learn how to use CODESYS to program PLC Instruction List, you can purchase my PLC Instruction List with CODESYS V3.5 tutorial at http://www.knoware-online.com/il_codesys.html.


If you want to run and solve Ladder Logic programs without using CODESYS you can purchase my PLC Simulator 10 at http://www.knoware-online.com/pc-plc.html. This simulator turns your PC into a PLC, has the "look and feel" of Allen-Bradley RSLogix® Micro software (though not exact), runs and solves Ladder Logic programs that you write, and allows you to build machines with limit switches, pilot lights, selector switches, solenoids, and conveyors that move in response to the ladder programs you write.
  
How to use this tutorial:
This tutorial was written with three principles in mind:
1)  All things can be taught by example (credit Albert Einstein)
2)  Most people learn better by "doing" as opposed to listening or reading.
3)  Sometimes we just need to get a "foothold" on a new concept so that further explanations make sense.

Read and perform the chapters in this tutorial in the order in which they are presented, since they tend to build on each other.  


What you need to use this tutorial:

(optional) CODESYS V3.5, a FREE DOWNLOAD at http://store.codesys.com/codesys.html?___store=en&___from_store=default.

OOP (Object Oriented Programming)

with

CODESYS Made Simple

Table of Contents
Chapter:

1. Introduction
                     
2. Creating a Class
                       
3. Adding Properties to a Class

4. Adding Methods to a Class
                                  
5. Creating Objects from a Class
         
6. Inheritance
 
7. Passing Parameters to Methods

8. Access Modifiers

9. Polymorphism

10. Interfaces                  
 

 

Sample excerpt from... 

 Chapter 6.  Inheritance

Step 6) Create a Class named Rev_Motor

Let's now assume that we need to add a 3 phase induction motor to the production line that can be jogged and reversed.   Figure 7 below shows a reversing motor starter in relay logic with jog capability.  

Figure 7  Reversing motor starter with jog capability




Rather than create a Rev_Motor Class from scratch, we can simply inherit the Jog_Motor Class, then add Forward and Reverse inputs, and  Coil_F and Coil_R outputs.  In other words, we can inherit an already inherited Class. This is like inheriting from your father who inherited from his father and is sometimes called Deep Inheritance.  

To create the Rev_Motor Class, right-click on Application, then click on Add Object>POU to open the Add POU window as shown in Figure 8 below.  Enter Rev_Motor for the Name of the POU, select Function Block, click the Extends box, click on the .... box to open the Input Assistant, click on Jog_Motor, select Structured Text (ST) as the Implementation Language, then click on Add.

The Extends box tells the Rev_Motor Class to inherit the Jog_Motor Class.  In a sense, we are extending the Jog_Motor Class into the Rev_Motor Class.

Figure 8  Creating the Rev_Motor Class




Your screen should now look like Figure 9 below.  Notice that the Rev_Motor Class has been added to the program tree on the left side of the screen, and that it has been labeled Rev_Motor (FB), meaning Function Block.  In other words, the Class you created named Rev_Motor is simply another user-defined Function Block.  Also notice that a "code window" tab has been added on the right side of the screen, and the first line indicates that the Rev_Motor Class EXTENDS the Jog_Motor Class.  This indicates that the Rev_Motor Class inherited the Jog_Motor Class.  

Step 7)  Add Inputs and Outputs to the Rev_Motor Class 

Add the Forward and Reverse inputs and the Coil_F and Coil_R outputs to the Rev_Motor Class as shown in Figure 9 below.  

Figure 9  After the Rev_Motor Class is created



Hardware/Software Requirements: