CODESYS Arrays : What are they? How are they used?

 

A couple of previous posts covered sorting CODESYS arrays and using them with pointers. There was never a preface to the topic of arrays.  So, this is a stab at clarifying the definition and application of CODESYS arrays.

CodesysArrayVisual
Visual of an Array

 

What and why of arrays?

An array is an set of data. It could be a collection of numbers or a collection of custom data types. One analogy of arrays is a bucket with numbered containers in it., arranged from container 0 or 1 to container x. X being the length of the array.  Each container can store some data.

The purpose:

  • Collect and organize data of a certain type.
  • Data in an array can be called with a common namespace.
  • Makes it easier to sort data
  • Easier to do math on a set of data points

 

In the memory layout of a PLC, array elements are usually stored in contiguous ( back-to-back) memory locations. This has some benefits for retrieval when reading from another PLC, HMI or SCADA system.

 

How are they used?

The basic declaration of an array:

 

TestArray : ARRAY[1..5] of INT;

 

It also offers the option of initializing the element values in the declaration:

 

TestArray : ARRAY[1..5] of INT:= [5,3,2,5,2];

 

Array lengths can be defined using a variable in the declaration.  That is to  tie the array size to a variable name instead of a fixed number in the declaration.  NOTE: The variable has to be declared as a VAR CONSTANT and can not be altered in runtime. The value of the VAR CONSTANT variable needs to be declared and initialized with a constant value in the programming environment.  The IEC 61131-3 standard also makes reference to this point.  So, the feature of using a constant in the declaration of array length only serves to centralize the location of initial setup of parameters,  if required by the control program.

 

TestArray : ARRAY[1..UserDefinedVarLength] of INT:= [5,3,2,5,2];

 

VAR_CONSTANT

UserDefinedVarLength : INT:= 5;

END_VAR

 

The above as noted in the CODESYS environment:

CODESYS Array declaration

CODESYS Array declaration in the actual declaration window

 

CODESYS Arrays can be multi-dimensional. That is, they can have more than one attribute and form a matrix. Multi-dimensional arrays are declared as follows:

MotorData: ARRAY [ 1..2,1..5] OF REAL: [845, 822, 808, 795, 834, 30.2, 29.0, 28.5, 29.5, 30.2 ];

The above array will initialize as follows:

[1,1] :=  845;

[1,2] := 822;

[1,3] := 808;

[1,4 := 795;

[1,5] := 834;

[2,1] := 30.2;

[2,2] := 29.0;

[2,3] := 28.5;

[2,4] := 29.5;

[2,5] :=30.2;

An example application with a  visual representation of the multi-dimensional array above could be as follows:

MultidimensionalArray
An visual representation of the array example above

 

CODESYS Functions that can be used with Arrays:

There are a number of functions that can be used with arrays. Often times, for organizing and sorting data in arrays, pointers are used. Other than that, the following are some functions and their applications to arrays:

SizeOf

This function returns the number of bytes reserved for an area.  If an array is made up of INT datatypes, each element would take up 2 bytes. The total number of bytes would be number of elements times 2. For REAL datatypes it would be a multiplier of 4.

Checking the size of an array in bytes.
Checking the size of an array in bytes.

CheckBounds

This is added as an implicit check. It shows up as ‘ POU for Implicit Checks’ when Add Object is selected and check arrays during runtime to determine if any code is attempting to access array elements that are outside the declared range.

 

CODESYS ARRAYS
Implicit Check included in the CODESYS environment for array bounds checking

 

 

NOTE: Sum and arithmetic functions ( ADD, MUL, SUB…etc) usually accept INT, REAL’s and ANY_NUM type inputs. As such, feeding an array to the input of a math function will usually result in an error. Arrays have to be broken out to individual elements to be manipulated and used for number crunching.

To wrap up, a video on arrays in CODESYS from Kurt Braun’s YouTube channel

 

There are probably a number of functions related to arrays that folks out there use. If you know of one, please share in the comments section below.

Resources: Staying on the topics of arrays, the following articles are on the same topic:

CODESYS Array Sorting in PLC Programs

CODESYS Pointers and Dereferencing

If organizing and handling data in your PLC programs is important to you, check them out.

 

Share

Remote PLC Connection: Connecting to a SoMachine M241 PLC using InHand Networks IG601 Cellular Gateway and Device Touch

 

The ability to connect to a PLC remotely (or HMI) and to pull data, access webservers and download programs is increasingly a requirement on industrial automation projects. Remote data access and program maintenance saves travel time to sites and simplifies daily operations. Some of the PLC features that enable this are:

  • Webserver on PLC and HMI: Allows data monitoring and some updates to PLC configurations

 

  • WebVisualization: These are customizable user interface pages that are embedded in the PLC. WebVisu is the CODESYS term for these HTML5 pages. Some examples of their usage from YouTube are noted here.

 

  • PLC Programming software remote gateway support: This is the ability for a PLC programming software to connect to remote Ethernet based devices. This could be for program downloads or regular program viewing.

This article will cover the steps for remotely connecting from the SoMachine PLC programming software to a remote M241 PLC. The InHand Networks IG601 cellular gateway device is used as the intermediary. Other examples of intermediary gateway devices may include Netbiter by HMS and eWon ( also by HMS now I believe).

Over the last few years, the criteria I have laid out for these devices have included the means to use them without the manufacturer’s cloud- based connection service. Simultaneously, keeping the option to route more secure connections through the cloud service, only when required, is a plus.

 

The criteria for the gateway device:

 

  • Ability to setup port forwarding at the gateway device
  • Ability to use a VPN connection for secure PLC program downloads
  • Ability to support a static IP at the gateway device and allow for the above mentioned VPN connection at the same time.

The InHand Networks IG601 meets these criteria. Getting this set up can be accomplished with two sets of steps. The InHand steps and the SoMachine steps.

InHand Networks Steps:

 

  1. Connect to the InHand Device Cloud at http://g.inhandnetworks.com/. For initial setup, follow instructions per the Device Manager configuration video.

 

  1. Install Device Touch on the connecting PC. The installer is available in the Device Cloud portal. The next steps will assume proper setup of the Device Cloud and connection using Device Touch on the local PC. Instructions on Device Touch are available at the following link.

 

NOTE: While in the Device Cloud, be sure to set up a site, a device and the PLC. I forgot to add the ‘Controller’ once. It took some time and a call to InHand’s support team to figure it out.

InHandsite_Gateway_PLC
InHand Device Cloud

 

  1. Open Device Touch on your computer and connect with your Device Cloud account. Once connected to the cloud. Connect to the specific site/gateway but selecting it and clicking on the ON/OFF slider bar.
InHandDeviceTouch
Device Touch connection

SoMachine Steps

  1. In SoMachine, configure connectivity in the SoMachine gateway. The gateway is accessible by double clicking on the root of the controller directory in the device tree.
SoMachine Gateway setup for Remote PLC Connection
SoMachine Gateway

5. The connection method is set to IP address. In the example, the remote controller IP address was set to 10.0.0.100 with a subnet mask of 255.255.255.0

ConnectionMode
IP Address connection mode

    6.  Per step 3 above, if Device Touch is active and the Maintenance Channel is on/ready (screen cap below), go to the Online menu and select Login.

 

IP Address Remote PLC
IP Address Connection Mode

NOTE: The update key on the gateway will not result in the detection of the controller in the Gateway list. Entering the IP address and selecting ‘Login’ actually triggers the process if searching for the device and placing it in the available controllers list in the Gateway. The Update button in the Gateway may not find remote devices. As such, the remote PLC may not show up even if by clicking on the Update button.

 

 

  1. The gateway will scan the remote network via the Device Touch adapter. Once the controller has been found, it will show up on the controller list in the gateway with a REM icon next to it.
RemotePLCDetected
Remote PLC detected

Note: The standard warning message indicating the pending connection to a controller will pop up prior to an actual login. Entering Alt-F will proceed with the connection and potential remote download of a program. Clicking the Cancel button will abort the connection.

 

Troubleshooting tip:

Should the remote controller not be detected using the IP address connection method above, restart the SoMachine gateway as shown in the screen cap below. Subsequently repeat step 4 above.

RestartPLCGateway
Restart SoMachine Gateway

Should questions or comments arise, put it in the comments section below.

 

 

 

Share

VFD Input Rectifier

 

The VFD input rectifier is the first of the three main stages on a VFD. The other two being the DC bus and the inverter. The rectifier stage converts AC line voltage supplied to the drive to DC.

The rectifier is usually a silicon controller rectifier ( SCR) or a diode. The difference between the two being that the SCR types can increase switching gradually thus increasing the voltage applied to charge the DC bus (second stage of the three stages of an AC drive). The diode types rely on a pre-charge circuit to perform the gradual voltage ramp up of the DC bus capacitors.

For drive rectifier stages, most commonly, there is a 6 diode or  6 SCR arrangement that makes up what is called a 6-pulse rectifier. A good illustration of this process from a TranspowerNZ video on YouTube

 

 

There are 12 pulse and 18 pulse diode arrangements that can be used to make up the rectifier stage of a drive. The main purpose of the 12 diode and 18 diode arrangements are to achieve lower harmonic distortion on the line side. The 12 pulse would be made up of 2 sets of 6 pulse rectifiers supplying the DC bus in parallel and the 18 pulse with 3 sets of 6 pulse rectifiers. Understandably, the higher order arrangements take up more space and cost more.

 

Considering that the main purpose of these arrangements is to reduce harmonic distortion, there are other options besides 12 or 18 –pulse rectifiers that can be considered for the same or better results. These options are to include a filter on the line side of the drive or by adding an active front end (AFE). MTE (maker of filter and power quality equipment) has a good paper comparing 18-pulse VFD’s with a 6-pulse VFD and their Matrix filter. The essence of it is that it costs less, takes up less space and consumes less energy (specifically for the 100hp test case).

Share

Choosing between variable frequency drives (VFD) and soft starts

 

Soft starts usually vary voltage with SCR'sSoft starts usually vary voltage with SCR’s

A key difference between drives and soft starts:

Drives can continuously vary the frequency and voltage supplied to the motor. Soft starts vary only the voltage supplied to the motor, and usually only when ramping a motor up and down.

What does this mean?

Soft starts may vary the speed of the motor during startup and ramp down but this is done by reducing the motor voltage. On this note, the soft start is also called a reduced voltage starter. Drives do the same but also have the option to control motor speed by varying the voltage frequency instead of the voltage.  Motor speed is directly related to its supply voltage frequency.

 

Inherently, a drive or a soft start reduces the inrush current that every motor is subject to when starting across the line. From this perspective, either a drive or a soft start will probably prolong the life of a motor -specifically if compared to an across the line starter. This is a general statement and like most general statements, there are some conditions.  Specifically for drives, this general statement usually applies to inverter duty rated motors which can withstand the continuous high frequency switching (PWM) of the drive. Otherwise, there is a risk in applying a drive to a motor. It might heat up the winding insulation and ultimately break it down.

 

The soft start topology is usually a single stage SCR based switching scheme with a bypass. The bypass takes over  for operation at full speed (diagram above). The SCR’s fire for gradually longer parts of the AC voltage cycle until the entire AC wave is passed through to the motor. At this point, operation is handed over to a bypass.

 

VFD's vary voltage and frequency with the 3 stage design.VFD’s vary voltage and frequency with the 3 stage design.

 

The drive topology has 3 stages (diagram above), with a rectifier taking in line supply and then a DC bus capacitor that stores and buffers the DC energy within the . The final stage is the inverter which is usually made up of IGBT’s at the motor supply side of the drive. The IGBT’s can continuously operate at varied gating frequencies to produce a variable frequency supply to the motor.

When choosing between drives and soft starts, some key differences to consider are:

Key differentiators

VFD Soft Start Meaning
Speed Variability Continuously variable speed throughout operation Initial ramp up of voltage/speed. Subsequently pegged to line frequency. Drives can save energy if the load does not need to run at full speed. Soft starts do not save energy in full speed operation. Then again, some applications are designed to operate in full speed operation. These loads will not benefit from a drive from this perspective.
Control features More control features: Features that take effect during operation at regulated speeds. Less control features as speed is not regulated besides during startup and ramp down.
Application Torque Constant torque supported- i.e. high torque at low speed.

Examples: Screw compressors, conveyors.

Variable torque applications- lower torque at low speed.

Examples: Centrifugal pumps, fans.

Some loads require a high amount of torque when starting. A VFD is applied on these applications
Main features Reduce inrush and continuously vary frequency and motor speed. Energy savings during operation. Reduce initial inrush. Energy savings during ramps, no energy savings after ramp up and ramp down.

 

 

 

Share

CODESYS Visualization (and WebVisualization)

A CODESYS Visualization or Viz, is a way to embed a graphical interface into a PLC program. Interfacing with the PLC programs visually allows for quicker troubleshooting among other benefits. Further to that WebVisualizations publish a CODESYS visualization in HTML5. This enables user access to a PLC visualizations via a web browser.

Several examples of Visualizations and WebVisualizations exist online. Visualization and WebVisualization screens are created with drag and drop objects which are configured instead of coded. This simplifies screen development instead of having to build Java or HTML based objects for web accessible screens on the PLC.

The collection of YouTube based Visualizations highlighted below showcase the possibilities with the various screen objects across various applications – some industrial and some exemplar/illustrative.

The first example  is of an elevator application built in CODESYS. Credit to donkeykowng on creating this for a school project.

The next example is of an industrial mixing process with multiple tanks with levels updated dynamically. Credit to tweektweak23.

The next two credited to Kye-Hun Park .
Tetris . This visulization was built in SoMachine 4.1 which uses CODESYS 3.5
Snake game
 
Combination of  2D conveying and tank on a filling application.
CNC application courtesy of Martin van rijn
Another tank level application. Credit to Huseyin Gevrek
Material handling example with a couple of axis of a pick and place and a conveyor.
Credit to binyam .yo
More recent update of Codesys includes support for multi-touch. (from CoDeSysMotion YouTube page)
 A key benefit of a visualization is that it allows for parts of the PLC program to be graphically represented and viewed without the PLC programming software. This is given that the programmer or designer of the system designs and embed appropriate visualizations to represent the system. Being able to pull up a graphical representation of the system on a PC/tablet/phone browser allows for preliminary  troubleshooting without too many people having to have the programming software license . If the problem  is beyond wiring , mechanical or I/O issues , then someone with the programming software can be dispatched.

 

Share

Importance of Modbus Polarization Resistors

Modbus has been around for several decades and is widely implemented.  There are several elements of starting up a system with Modbus serial networks that usually get repeated. These include address, baud rate and parity. One element which does not get too much mention is the Modbus polarization resistors which can play a major role.

What does the polarization resistor do?

The detection margin requirement across the B and A signals is usually only about 200mV. With polarization resistors, the actual margin is ‘widened’ specifically when the signal drivers are not ‘driving’ thus allowing for better noise tolerance as well. This is especially true with networks that have variable speed/frequency drives  with high frequency switching being a large source of noise. The resistors are known as pull-up and pull-down resistors as they tie the B signal up to the 5V rail and the  A signal down to 0V.

On a recent startup, the PLC and HMI program was downloaded and the control program was being tested when the system started to act erratically. Closer observations revealed that the Modbus serial communications with the drive was sporadically dropping out, specifically when the drive was running the motor. All read values were becoming zeroes when this happened.

After trying several things, the polarization resistors solved the problem.

 

PolarizationResistorsThings to note about polarization resistors:

  1. Applied only once on each network. PLC’s may have this as an optional item in the Modbus port configuration. Slave devices usually do not have it though many manufacturers will advise on how to apply if when needed.
  2. The presence of polarization resistors could reduce the number of slaves that can be had on the network, specifically if a lower pull-up or pull-down resistance is used. This and many other important points about Modbus is covered in the specification at Modbus.org.
  3. What size resistors to use? The Modbus standard specification calls for resistance between 450 ohms and 650 ohms. Manufacturers usually have a guide. A good example to calculate it out is noted on page 11 of this document.

Polarization resistors are one factor to note in troubleshooting Modbus serial networks, others include proper grounding practices and proper usage of terminating resistors.

Share

CODESYS Array Sorting for PLC programs

 

BubbleSort

 

Sorting functions have increasing use in PLC programs thanks to more powerful processors and more memory. One example is sorting an array of motor runtimes from lowest runtime to highest runtime so that the next run cycle will utilize the motor with the lowest runtime first. This example is useful for pump applications and is referred to in the post about pointers in CODESYS.

To implement a pump runtime based sort, other elements in CODESYS such as DUT’s( user defined data types)  and structures could probably be useful. The reason is that the result of the sorting function is to know which pump to run next. This can be done by having two dimensions in the array, a Pump ID/Number along with the Pump Runtime. For the purpose of simplicity, only a simply bubble sort with two FOR loops is shown below, the DUT approach is not shown here.

A simple bubble sort can be implemented in CODESYS with the following code. Instead of sorting the actual array, this example sorts an array of pointers ( dereferenced to get the actual values from the pointers). This way the original array data locations do not need to be altered. Not altering the original array locations is a good thing because in some cases, these array locations may be hardcoded to physical inputs.

(*Prior to implementing the sort, an array if pointers is loaded *)

FOR l:=0 TO n BY 1 DO

pointToArrayElement[l]:=ADR(TestArray[l]);

END_FOR

(*Bubble sort- The value of n should be equivalent to the last array element*)

FOR i:=0 TO n BY 1 DO 

                                                (* The second FOR loop compares two elements of the array at a time and sorts*)

FOR j:=0 TO n-1 BY 1 DO

IF(pointToArrayElement[j]^>pointToArrayElement[j+1]^) THEN

temp := pointToArrayElement[j+1];

pointToArrayElement[j+1] := pointToArrayElement[j];

pointToArrayElement[j] := temp;

END_IF

END_FOR

END_FOR

 

The variables used in this example:

 

VAR

TestArray: ARRAY [0..4] OF Pump;
TestArrayPointers: ARRAY [0..4] OF POINTER TO INT;

i: INT;
n: INT;
j: INT;

l: INT;
temp: pointer to INT;
pointToArrayElement: ARRAY OF POINTER TO INT;
END_VAR

 

Other notables: If this is implemented in a function or even in a program , the integers used in the FOR loop iteration need to be reset after the sorting is complete and it would be required to sequence through the loading of the array of pointers, sorting  and resetting of the iterative integers possibly using a CASE structure  or some IF statements.

 

 

 

Share

PLC Programming Patterns- The Debounced Threshold/Limit

Patterns in PLC Programming: The threshold + timer function – Debounced Threshold

PLC programs are frequently made up of the same sets of functions or patterns of logic. These patterns perform commonly required control routines. Examples include threshold detection for alarm purposes, input/output mapping and scaling functions, to name a few. These combination of functions make up re-usable patterns or function sets that form the building blocks of a PLC program.

Repeated usage of some functions in a PLC programming environment holds true to the universal 80/20 rule ;20% of the functions available in a programming environment( such as timers and logic functions) would probably be used to build 80% of PLC programs.

The debounced alarm/threshold pattern is one of these commonly required PLC programming patterns. This pattern is probably better written in ladder diagram   (instead of any of the other 61131-3 languages), for two reasons:

  • Maintenance personnel will be able to view the status of the process/monitored variable, associated timer and output all in the same place
  • It can be programmed in 2 rungs of ladder compared to more than 10 lines of code.

The ladder diagram version could look something like this:

Alarm ACTIVE
DebouncedThreshold_LD_ALARMAutoManualReset

Note: An Auto/Manual Reset Provision is built into the Alarm Reset Timer rung.

Alarm NOT ACTIVE

DebouncedThreshold_LD

The following pattern is the debounce pattern for detecting limit breaches along with a timer, written in Structured Text for the CODESYS environment. As described above, the structured text version takes more space and possibly looks more intimidating specifically to someone who is used to the ladder environment.

CODESYS-LimitThresholdScreenCap

The plain text version of the structured text version is attached below:

PROGRAM DeBounced_Limit

VAR

irProcessValue : REAL; (*Process value scaled from analog input signal*)

rProcessLowLimit : REAL; (*User defined process value low limit alarm threshold*)

qxLowLimitAlarm : BOOL;(*Low limit alarm bit*)

LowLimitAlarmTimer : TON; (*Process value low limit alarm trigger debounce timer*)

LowLimitAlarmResetTimer : TON; (*Process value low limit alarm reset debounce timer*)

LowLimitDebounceT : TIME; (*Process value low limit alarm trigger debounce time*)

LowLimitResetDebounceT : TIME; (*Process value low limit alarm reset debounce time*)

xAutoReset : BOOL;(*User enabled alarm auto reset function*)

ixUserReset : BOOL;(*User triggered alarm reset function for manual reset*)

END_VAR

(*If value is lower than low limit, start timer, else reset timer*)

IF irProcessValue < rProcessLowLimit THEN

LowLimitAlarmTimer(in:=TRUE, PT:=LowLimitDebounceT);

ELSE

LowLimitAlarmTimer(in:=FALSE);

END_IF

(*If timer lapses, trigger alarm bit*)

IF LowLimitAlarmTimer.Q THEN

qxLowLimitAlarm:=TRUE;

END_IF

(*If value is higher than low limit, start alarm reset timer, else reset debounce timer*)

IF irProcessValue > rProcessLowLimit THEN

LowLimitAlarmResetTimer(in:=TRUE, PT:=LowLimitResetDebounceT);

ELSE

LowLimitAlarmResetTimer(in:=FALSE);

END_IF

(*If timer lapses, and system permits auto-reset, reset alarm bit*)

(*If the system does not permit auto-reset, add user invoked reset bit here*)

IF LowLimitAlarmResetTimer.Q AND (xAutoReset OR ixUserReset) THEN

qxLowLimitAlarm:=FALSE;

END_IF

Share

CODESYS Pointers and Dereferencing

Pointers and Arrays in CODESYS

The CODESYS language supports the usage of pointers which is essentially a variable that stores the location of another variable or data point instead of its value. The actual value of a variable that a pointer points to can be retrieved by dereferencing the pointer.

 

What could it be used for?

       1.Storing the location of the variable saves space specifically when pointing to large                 data points or structures.

  1. Pointers allow for sorting and re-arrangement of data sequences without actually changing the location of the original data points. To characterize in a diagram:

Pointers and Arrays Diagram

  1. Going down the path of object oriented programming in PLC’s/PAC’s, this supports abstraction by separating the usage, manipulation and parts of an object from the original object itself. This helps reduce the PLC code required to handle every scenario and also helps in situation where the original object can not be manipulated because its hardcoded to an input ( for example).

 

Example:

 

Let’s say the array of data on the left side of the diagram above is read (as inputs) from a set of 4 drives controlling 4 pumps. From top of the array to the bottom Array location 0 would be for Pump 1 , location 1 for Pump 2 …..location 4 for Pump 4.

 

If the data is each of the pumps runtime, the pointers to the runtime allow for it to be sorted and stored in the pointer of arrays without changing the order of the original array which is important, as the original array of runtime data may be hardcoded to the actual input or read values from the drives.

 

Secondly, when the decision is made to call a pump to run, the same mechanism of pointers and arrays and de-referencing can be used to point to the command word of each of the pumps on a communication bus.

The end result is the amount of code required in determining which of the pumps to run is reduced.

Pump Example Pointers and Arrays

What does the syntax look like in CODESYS?

To create a pointer:

The address operator or ADR is used.

Let’s say the variables are:

 

PROGRAM PLC_PRG

VAR

j: INT;

k:INT;

pointToJ: POINTER TO INT;

END_VAR

The pointer assignment would be:

 Pointer

in plain text:

pointToJ:=ADR( j);

Note: ADR is a CODESYS operator and not an operator prescribed in IEC61131-3.

 

To de-reference a pointer:

The ^ operator is used to de-reference pointers.

Example:

in plain text:

k:=pointToJ^;

This would yield the value of integer j.

SimulatedPointerDeReferencing

 

Share