You are on page 1of 40

Programming PLCs with

IEC 61131-3
MEI-56206 Production Automation Exercises

Niko Siltala, niko.siltala@tut.fi


PLC = Programmable Logic Controller
MEI-56206 Production Automation Exercises

16.1.2017

72

Agenda
PLC HW & Operational Principle (Recap)
PLC Programming
Standard IEC 61131-3
Structural components
(Use of) Instance of FB
Details of IEC 61131-3 Programming
languages

From State models to PLC language


MEI-56206 Production Automation Exercises

16.1.2017

73

Recap of PLC HW

MEI-56206 Production Automation Exercises

16.1.2017

74

Base parts of PLC


PC +
development App

Power source

Input units

CPU

Output units
Special units

Prg Port

Hand held programming device

Counter inputs
Encoder inputs
Pulse outputs
Potentiometer,
temp. sens.

Barcode reader

MEI-56206 Production Automation Exercises

Computer,
HMI

16.1.2017

75

About inputs and outputs


IO modules establish interface to sensors and actuators
With use of different kinds of input, output, special, and
fieldbus modules different process signals (type and
magnitude) can be connected to logic

MEI-56206 Production Automation Exercises

16.1.2017

76

PLCs Operating principle

MEI-56206 Production Automation Exercises

16.1.2017

77

Operating principle of logic


Logics executes programs cyclically, where each cycle
consist of sequence of repeated routines
During typical cycle is read inputs, execute whole program
(evaluating all lines), update outputs, monitor state of CPU
and auxiliary devices, and handle HMI and communication
(e.g. ethernet, serial ports)
R Inputs

Exe. Logic

W Outputs

R Inputs

Exe. Logic

W Outputs

Cycle Time = X ms

MEI-56206 Production Automation Exercises

16.1.2017

78

Real Time & Determinism


RT = fast response & short cycle time
Det = constant cycle time / deviation of CT (t) 0 s

PLC
R Inputs

R Inputs

Exe. Logic W Outputs


Idle

Cycle Time = X ms

Idle time: 0..Z ms


Config:
Cycle time

SoftPLC PC -Based

Exe. Logic W Outputs

tOS

Logic time = Y ms
R Inputs

Exe. Logic W Outputs

R Inputs

Exe. Logic

Disk UI tasks

Cycle Time = X ms

Config:
Cycle time
tOS max. % Y of X

MEI-56206 Production Automation Exercises

16.1.2017

79

Systems Response
Target:

small deviation
(Determinism)
prev. slide t 0 s
AND

Density function of Systems Latency Time

fast response (RT)


Cycle time 0 s

Small deviation is more


important!
Ideal situation: Impulse, at
moment 0 s.
MEI-56206 Production Automation Exercises

16.1.2017

80

PLC Programing

MEI-56206 Production Automation Exercises

16.1.2017

81

PLC Programming
Program can be usually made offline without PLC HW
PLC HW is usually required for testing. PLC is connected to
development environment PC via serial/USB/ethernet port
Establishing connection between PLC and PC; downloading
application program to controller; and performing the testing
procedure.
Dev. Env. Usually offers means for monitoring states, memory,
and execution of PLC program and affecting on it.
Sometimes possible to test program directly on development
environment. However this requires modelling and simulation
of HW (SW in the loop).
Programming device
MEI-56206 Production Automation Exercises

16.1.2017

82

Standard IEC 61131


IEC, International Electrotechnical Commission,
prepared by PLCopen (http://www.plcopen.org/)
Standard aiming to harmonise methods and languages for
programming PLCs
Published in December 1993

Objectives and Benefits:


Reduce wasting of (human) resources (education, learning,
changes, problem tracing & solving)
Re-use of code enables focus to solving (new) problems
Minimise misunderstanding and errors
Guarantees widespread use of programming techniques
Enables working (programming) with same projects in
different locations, countries. Cross utilisation of code across
projects.
MEI-56206 Production Automation Exercises

16.1.2017

83

Standard IEC 61131


Standard is assembled from nine parts, which defines and
guides use of PLC HW and actions

Part 1 General information


Part 2 Equipment requirements and tests
Part 3 Programming Languages
Part 4 User Guidelines
Part 5 Messaging service specification
Part 6 Functional Safety
Part 7 Fuzzy control programming
Part 8 Guidelines for the application and implementation of
programming languages
Part 9 Single-drop digital communication interface for small sensors
and actuators (SDCI)
i.e. IO-Link

MEI-56206 Production Automation Exercises

16.1.2017

84

Standard IEC 61131-3


Third part of standard deals with both textual
and graphical PLC programming languages
and data types
It defines several data types, which can be used
for programming
Many actions can be done with data type BYTE,
but in sake of clarity and saving memory specific
data type should be selected according the
usage

MEI-56206 Production Automation Exercises

16.1.2017

85

IEC 61131-3 / data types


nimi

tyyppi

koko (bitti)

alue

BOOL

boolean

01

SINT

short integer

-128 127

INT

integer

16

-32768 32767

DINT

double integer

32

-2.1e-9 2.1e9

LINT

long integer

64

-9.2e19 9.2e19

USINT

unsigned short integer

0 255

UINT

unsigned integer

16

0 65536

UDINT

unsigned double integer

32

0 4.3e9

ULINT

unsigned long integer

64

0 1.8e20

REAL

real number

32

LREAL

long real

64

TIME

kesto

DATE

pivys

TIME_OF_DAY

aika

DATE_AND_TIME

pivys ja aika

STRING

string

vaihtelee

vaihtelee

BYTE

8 bitti

WORD

16 bitti

16

DWORD

32 bitti

32

LWORD

64 bitti

64

MEI-56206 Production Automation Exercises

16.1.2017

86

Programs, FBs and


instances

MEI-56206 Production Automation Exercises

16.1.2017

87

Structure of PLC
Configuration / Program

MEI-56206 Production Automation Exercises

16.1.2017

88

Relation of program types


1. PRG = Program
2. FB = Function Block
3. F
= Function
Type

Visibility

Instances

Can call or include other

PRG

FB

PRG

Global

FB

Local

0..N

Global

MEI-56206 Production Automation Exercises

16.1.2017

89

PRG, FB and F - interface


FB name
INPUT
Name, data type,
[initial value],
[comment]

OUTPUT
IN_OUT
By reference

VAR
Local variables
MEI-56206 Production Automation Exercises

16.1.2017

90

FB Implementation vs.
instance (ilmentym)
Cylinder (1 actuator, 1 control signal, 2 sensors)
FB implementation ~ class description (luokka)

Abstraction of cylinder

Top limit

Functionality, model and logic

Low limit

FUNCTION_BLOCK FB_Cylinder

FB instance (ilmentym)
Activity for single device
Created as variables in memory

Referring the
implementation by the
type of the variable

fbCylinder_A : FB_Cylinder;
fbCylinder_B : FB_Cylinder;

Benefits

Ylraja
Ylraja
Ylraja
Alaraja
Alaraja
Alaraja

Implement once utilise many times

Ease of maintenance
Clear interfaces

Calling an
instance

Functionality is equal to all implemented devices,


B
A
But all implementations can be in different states (see. Cylinders or lifts)
fbCylinder_A (
Ctrl_Close := bCloseA,
Limit_up := I_SensA_Up,
Limit_down := I_SensA_Down,
bDone => bDone,
OutToValve => O_Valve_11);

fbCylinder_A
Ctrl_Close
Limit_up
Limit_down

MEI-56206 Production Automation Exercises

FB_Cylin
der

16.1.2017

bDone
OutToValve

91

Calling a FB
Calling INSTANCE of FB
= Executing its code
On each PLC cycle or when needed
Call can be partial. All ports (IF inputs/outputs) are
not defined or connections are missing

MEI-56206 Production Automation Exercises

16.1.2017

92

Calling a FB
Changing / reading values of a single instance of
FB
Its code is NOT executed or evaluated
no state change occurs

E.g.:
Writing input:
fbDrive.wTarget:= GPOS_LOADING;
Reading output (e.g. in transition condition):
fbDrive.bMoveDone
Reading output and assigning it into another variable:
bMuttuja1 := fbDrive.bMoveDone;
State of input can be read with same method
MEI-56206 Production Automation Exercises

16.1.2017

93

Function
Global
Does not preserve its state between two
adjacent calls (e.g. values of variables) No
counters or timers!!
Can NOT have VAR_OUTPUT type of
variables, only one (1) return value
For math operations E.g. add, storeToQueue

MEI-56206 Production Automation Exercises

16.1.2017

94

Programming Languages

MEI-56206 Production Automation Exercises

16.1.2017

95

IEC 1131-3 :
Programming
languages
Textual

Instruction List (IL)


[Kskylista]
LD

ANDN

ST

Step 1

Sequential Function
Chart (SFC)
[Sekvenssikaavio]
Structured Text (ST)
[Rakenteellinen teksti]

FILL

Transition 1
Step 2

Empty

Transition 2
Step 3

Graphical
C:= A AND NOT B;

Graphical

Function Block Diagram (FBD) Ladder diagram (LD)


[Tikapuukaavio]
[Toimilohkokaavio]
AND

A
B

A B

-| |--|/|----------------( )

MEI-56206 Production Automation Exercises

16.1.2017

96

Programming languages
(IEC 61131-3)
1.
2.
3.
4.

Instruction List (IL) (= Kskylista)


Structured Text (ST) (= Rakenteellinen teksti)
Ladder diagram (LD) (= Tikapuu kaavio)
Function Block Diagram (FBD)
(= Toimilohko kaavio)

5. Sequential Function Chart (SFC)


(= Sekvenssi kaavio)

Higher level sequence description

Others: state model, flow chart, PetriNet,


IEC61499, C
MEI-56206 Production Automation Exercises

16.1.2017

97

Instruction List (IL)


[Kskylista]

Textual, low level programming language

Relative to: Assembly


(Language for first micro controllers (PC-devices))

Instructions are short and simple text


commands
One operation per row

Users, who wants to


program low level HW
related code

Starting and stopping motor with IL

MEI-56206 Production Automation Exercises

16.1.2017

98

Ladder diagram (LD)

[Tikapuukaavio, Relekaavio]
LD looks like electrical circuit diagram (relay logic), used
commonly in industry.
Used long before standard was developed
Used a lot (still today)
User, who are familiar with electrical drawings
easy to read, smaller need for training
Input Instruction

MEI-56206 Production Automation Exercises

Output Instruction

16.1.2017

99

Function Block Diagram (FBD)


[Toimilohkokaavio]

Builds from function blocks and functions which


executes actions
Functionality is hidden inside function blocks, and only
interface is exposed outside (inputs and outputs)
With FBD can be implemented clear and easily
understandable program / part of program
User, who wants program without deep understanding of
programming (more like configuring graphically)

MEI-56206 Production Automation Exercises

16.1.2017

100

FBD: Chemical Process


Agitator

TempControl

Feed valve

Heat

Acidic reagent

Heat

Alkali reagent
Temp sensor

TempSensor

PV
pH sensor
Heater band

Cool

SetPoint
Cool

pHSensor
pHControl
AddAcid
MainSequence

Harvest valve

SetPoint AddAcid

Start
Stop
Duration

Start

Temp

Stop

pH

Duration

Agitation

PV

AddAlkali

AddAlkali

AgitateControl
MotorSpeed

FillState

SetPoint

MotorSpeed

PV
AgitateSpeed
ValveControl
Fill
Fill

State
ValvePositions

Harvest
Harvest

MEI-56206 Production Automation Exercises

16.1.2017

101

Structured Text (ST)

[Rakenteellinen teksti, Strukturoitu teksti]

High level programming language. Block structure


Relative language: PASCAL
Well applicable for processing numerical values
Allows complex structures and nested statements.
Conditional structures (IF-THEN-ELSE; CASE),
Iterations(REPEAT-UNTIL; WHILE-DO)
Math functions (SQRT(), SIN(), .)

For those who are used to high level


programming IF (LIMIT_SWITCH_1 AND BOX) THEN
GATE1 := TRUE;
languages
GATE2 := FALSE;
ELSIF ((LIMIT_SWITCH_2 OR (WEIGHT<>SETPOINT)) THEN
GATE1 := FALSE;
END_IF;
MEI-56206 Production Automation Exercises

16.1.2017

102

Example of ST
PROGRAM PRG_Jaa
VAR_INPUT
a : INT;
b : INT;
END_VAR
VAR_OUTPUT
c : INT;
END_VAR
IF b <> 0 THEN
c := a / b;
ELSE
c := 0;
END_IF;
END_PROGRAM
MEI-56206 Production Automation Exercises

16.1.2017

103

Sequential Function Chart (SFC)


[Sekvenssikaavio, askelkaavio, vuokaavio]

Top level sequence description


Not a programming language per se
Parts must be programmed with one
of four previously listed languages

Aimed for step wise controls

Step 1

FILL

Transition 1
Step 2

Empty

Transition 2

Step 3

Control problem is divided into smaller steps


(state models)
Supports both sequential and parallel steps

Graphical programming method, which defines


problem & solution visually
overall picture, fast diagnostics
MEI-56206 Production Automation Exercises

16.1.2017

104

Sequential Function Chart (SFC)


Execution of the program
progress from initial step (Init in
example) to next ones according
the transitions (connected lines).
Each step (state, [tila]), can be
programmed with selected
language, that suites best for
task (e.g. ST or LD).
There is one or more transitions
(conditions) between steps. After
transition gets true, ti will be
triggered and step change
happens.

MEI-56206 Production Automation Exercises

16.1.2017

106

1.L

Step Execution Order

2.L

4.L
3.O

5.O
7.L

6.O

8.O

2.L

L =
=
O=
=

Loops until Transition == True


0..N times
Executed exact ones
1..1 time

Loops might lead into trap


e.g. Counter
c := c +1;

9.L

MEI-56206 Production Automation Exercises

16.1.2017

107

Proprietary solution

SFC Example
Be careful with S, N, P,
for Booleans
Program parts cannot modify
Use ONLY for flow control

Special case of

MEI-56206 Production Automation Exercises

16.1.2017

108

From State Model


to PLC code

MEI-56206 Production Automation Exercises

16.1.2017

109

SFC Direct graphical transformation


ST + Case & IF structures
States as integers (or enumerations)
drawback: only single state is active at time

LD + applying a systematic method

See:
http://engineeronadisk.com/book_plcs/plc_
sequa2.html#16763

From State Model


to PLC code

Methods for transition, state model and


outputs (2 different methods)
States as integers
Drum sequencer available only in specific environments
MEI-56206 Production Automation Exercises

16.1.2017

110

Drum Sequencer (1/2)


Associates to LD programs as external function

http://www.unitronics.com/KnowledgeBase/U90Ladder/Drum/dru
m_sequencer.htm

MEI-56206 Production Automation Exercises

16.1.2017

111

Drum Sequencer (2/2)


http://forum.hosteng.com/wndm/HTMLHelp1/Instruction
_Set/DRUM_Drum.htm

MEI-56206 Production Automation Exercises

16.1.2017

112

You might also like