Opis

Ebooka przeczytasz w aplikacjach Legimi na:

Androidzie
iOS
czytnikach certyfikowanych
przez Legimi
Windows
10
Windows
Phone

Liczba stron: 155

Odsłuch ebooka (TTS) dostepny w abonamencie „ebooki+audiobooki bez limitu” w aplikacjach Legimi na:

Androidzie
iOS

### Popularność

#### PREFACE

When I started as Assistant Professor (teacher) at Dania Academy, Randers, Denmark, one of my first tasks was to find books which were suited for the AP Graduate in Automation Engineering education. Especially within programming in Structured Text (ST) no relevant books exist. Books of about 300 pages only contained a few pages directly about ST, and only at a theoretical level.

My students demanded examples, guides and methods for ST programming. As a consequence, in January 2017, I started writing a material which was named:

”Get started with Structured Text”

Since then, the material has currently been updated, extended and used in my lectures. The material has been highly demanded among my students and now I have finished this book, so other interested readers can gain knowledge by studying this book.

It is my hope that you will enjoy this book.

I shall thank my students, fellow teachers and colleagues for feedback and inspiration.

Please, send them to [email protected]

Tom Mejer Antonsen

Denmark, Randers, June 2018

INTRODUCTION

1.1 B

ACKGROUND FOR

ST

1.2 Q

UALIFICATIONS FOR LEARNING

ST

1.3 F

OUNDATION OF KNOWLEDGE

1.4 A

DVANTAGES OF

ST

PROGRAMMING

1.5 D

ST

PROGRAMMING

HOW THE PLC EXECUTES PLC CODE

DATA TYPES

4.1 E

LEMENTARY DATA TYPES

(INT, REAL, BOOL)

4.2 I

NTRODUCTION TO DERIVED DATA TYPES

4.3 S

TRUCTURED DATA TYPE

, STRUCT

4.4 N

UMBERING DATA TYPE

, ENUM

4.5 S

UBRANGE DATA TYPE

4.6 R

ANGE OF IDENTICAL

, ARRAY

VARIABLE SCOPE

5.1 EXAMPLE: V

ARIABLES

, S

COPE AND

IO-

MODULES

NAMING OF VARIABLES

6.1 V

ARIABLES WITH UNIT

6.2 V

ARIABLES WITH FIXED VALUE

(CONSTANT)

MATH AND LOGIC

7.1 A

RITHMETIC

O

PERATORS

7.2 R

ELATIONAL

O

PERATORS

7.3 N

UMERIC

O

PERATORS

7.4 L

OGICAL

O

PERATORS

7.5 L

OGIC

,

MATH FORMULAS AND BRACKETS

()

WORKING ON VARIABLE ASSIGNMENT

8.1 MATH

CALCULATIONS CHALLENGE

8.2 D

IVISION BY ZERO

8.3 C

ALCULATING WITHUSING

REAL

AND

INT

8.4 D

ECIMAL ERRORS BY

REAL

8.5 D

ATA COMMUNICATION VARIABLES

8.6 D

ATA TYPE CONVERSATIONS FUNCTIONS

8.7 F

IND BINARY VALUES OF AN INTEGER

(M

)

8.8 C

ONVERT

REAL

INTO

2

DECIMALS

(2

DIGIT

REAL)

CONDITIONAL STATEMENT

9.1 IF-THEN-ELSE

9.1.1 EXAMPLE: IF-THEN-ELSE as latching relay

9.1.2 EXAMPLE: IF-THEN-ELSE open and close valve

9.2 CASE

9.2.1 EXAMPLE: CASE – Setting the motor speed

9.2.2 EXAMPLE: CASE – For executing programs

9.2.3 EXAMPLE: CASE – Recognizing numbers

9.3 I

TERATION STATEMENT

, LOOPS

9.4 FOR-DO S

TATEMENT

9.4.1 EXAMPLE: FOR – LOOPS, 4 times loop

9.4.2 EXAMPLE: FOR – LOOP and 3D ARRAY

9.4.3 EXAMPLE: Calculation of the average value

SPLIT-UP IN PROGRAM MODULES

10.1 F

UNCTIONS

10.2 F

UNCTION

(FC)

AND

F

UNCTION

B

LOCK

(FB)

10.3 EXAMPLE: FC

TO CONVERSATION OF TEMPERATURE

10.4 EXAMPLE: FC

TO CALCULATE OF AVERAGE

WORKING WITH TEXT AND CHARS, STRING

11.1 EXAMPLE: FC

WITH

STRING

11.2 S

TANDARD FUNCTIONS

, STRING

BUILT-IN STANDARD FUNCTIONS

12.1 O

NE TIME PROGRAM EXECUTION

: F

IRST

S

CAN

B

IT

12.2 E

DGE DETECTION

(O

NE SHOT

): R_TRIG, F_TRIG

12.3 C

OUNTING FUNCTIONS

: CTU, CTD, CTUD

12.4 R

EPEATED PROGRAM

CALL

AND TIMER DELAY

: TON, TOF

SPECIAL FUNCTIONS AND STRUCTURES

13.1 S

IMPLE QUEUE STRUCTURE

13.2 FIF0 – F

IRST

I

N

F

IRST

O

UT

13.3 G

ENERATING RANDOM NUMBERS

(RND, R

ANDOMIZE

)

13.4 D

IGITAL

L

OW

-P

ASS FILTER

(LP-F

ILTER

)

13.5 S

IMULATION SIGNALS

13.6 C

ALCULATING TANK VOLUME

,

CYLINDER ON HEMISPHERE

BEST PRACTICE ST-PROGRAMMING

15.1 I

NDENTATION AND

SPACE

15.2 E

MPTY LINE BETWEEN CODE

15.3 A

VOID SPAGHETTI CODE

15.4 U

SE FUNCTIONS AND PROGRAM MODULES

15.5 U

SE OF VARIABLES

15.6 M

ISCELLANEOUS

15.7 C

ODE SHARING WITH THE INTERNET

15.8 OOP – O

BJECT

-O

RIENTED

P

ROGRAMMING

GUIDE TO PROGRAMMING EXERCISES

SUBJECT INDEX

#### 1 Introduction

This book gives an introduction to the programming language Structured Text (ST) which is used in Programmable Logic Controllers (PLC).

The book is primarily composed to be used at the 2-year Full-Time Higher education AP Graduate in Automation Engineering and the Part-Time Higher education AP Degree in Automation and Operation.

In a Siemens PLC, the programming is called Structured Control Language (SCL) which includes some differences in relation to ST.

The book systematically describes the basic programming, including tips/advice and practical experiences from the author.

Many clarifying explanations to the PLC code and focus on the fact that the reader should learn how to write a stable, robust, readable, structured and clear code are also included in the book. Furthermore, the focus is that the reader will be able to write a PLC code, which does not require a specific PLC type and PLC code, which can be reused. It must also be underlined that the solutions can be used on the international market for automation solutions.

It is recommendable to read the entire book and then use the book as a reference.

Sorry, but no warranty on the PLC code examples in this book is provided.

#### 1.1 Background for ST

ST is a high level programming language similar to Pascal Programming. Pascal Programming was widely distributed in Denmark from 1985 to approx. 2000 – a period of time in which many companies started developing software for PC, firstly DOS and since Windows.

ST is developed and published by International Electro technical Commission (IEC) in IEC 61131-3 International Standard in 1993. The standard consists of five PLC programming languages; where the LADDER Programming is the most well-known and most used.

ST programming for PLC Controls has since approx. 2010 been still more often published and since 2015 many companies in Denmark exclusively delivered PLC Controls, where ST is used as the favorite programming language. Consequently, still more employees are needed to understand and use ST, which is one of the arguments for distributing this book.

#### 1.2 Qualifications for learning ST

It is not a necessary that the reader knows how to program in LADDER. However, certain knowledge of mathematics, mechanics, electronics, automation and basic PLC is necessary to be able to learn ST.

Students educated in a higher programming language (e.g. VB, NET, C, C#, Java) have the abilities to learn ST relative easy, as the programmable structures look like one another. The execution of the program in a PLC varies, however, a lot from a traditional PC program or a Web application.

The educational time for ST programming is like as other text programming languages expected to be from 3 to 5 years.

#### 1.3 Foundation of knowledge

The author has 25 years of experience within specification, development and delivery of complex control systems and supervision systems. Of the 25 years, the author has 7-years of experience within Pascal Programing and 12 years within automation solutions and systems involving PLC. The experiences of comprises employment in four international companies and delivery of more than thousand control system solutions for 20 countries. Thus this experience provides an important basis for the substance in the book.

Within later years, the author has been teaching PLC Control Systems at higher educations. The students have from 0 to 20 years of vocational experience within PLC, automation and technological service.

Furthermore, the internet, the standard DS/EN 61131-3 and series of books of PLC Control are applied as inspiration and clarification of ways to present problems.

The basis of the book is a material which is currently compiled with feedback from lecturers and students attending the AP Education in Automation Engineering at the local Dania Academy, ‘Erhvervsakademi Dania’, Randers, Denmark. The material is thus currently updated so that it answers all the questions which the students typically ask through-out the period of studying.

The author is Bachelor of Science in Electrical Engineering (B.Sc.E.E.)

#### 1.4 Advantages of ST programming

ST is a very flexible and universal programming language. ST program code can easily be copied between different PLC types and be sent via e-mails as it is based on text and not graphics like the LADDER programming does.

The ST program code is similar to text sentences and work is carried out the same way as a word processor program (as e.g. Microsoft Word) which makes it easier to work on. Consequently, the same working methods are applied as in a word processor program.

Because of its very structured nature, ST is ideal for tasks based on complex math, code reuse or decision-making (e.g. automatic energy optimization, algorithms, data collection and regulation in process plants).

Having the experience with PLC Programming the transition to other programming languages within PLC Control and automation will be easier; i.e. programming robotics or Visual Basic Programming.

Within later years still more companies have switched to ST Programming which is due to the fact that ST provides a series of advantages compared to the four other PLC programming languages (LAD, SFC, FDB and IL).

ST Programming code can relatively easy be copied between different PLC types. 1)It is the easiest PLC language for mathematical calculations, formulas and algorithms 2) and large amounts of data (bigdata)PLC solutions are more demanding today than 20 years ago 3)Many widespread PLC programming languages (C++, C#, PASCAL, VB) reminds very much of the ST program structure.The other PLC languages (LAD, SFC, and FBD) require that parts of them are programmed in ST.It takes up less space when the PLC code must be documented, described and printed compared to the other PLC languages.It is easiest PLC language to version control via comments in the program code or via GIT 4) or Subversion 4)

The PLC programming language Instruction List (IL) which is applied for complex PLC Controls is expected to be outdated within a few years (cf. DS/EN 61131-3 section 7.2.1) and it is expected that ST will replace these solutions.

#### 1.5 Disadvantages of ST programming

A big disadvantage is the fact that many technicians and electricians are only capable of programming in LADDER and it is difficult for them to understand the ST programming which is based on text and is not graphical as in the LADDER 5)

Programming in ST can easily be confusing as certain experience in structuring a program in an appropriate way is required.

Inexperienced people may have difficulties in fault-finding in a ST program.

Small (Micro) PLC does normally not allow ST Programming.

It is not possible to apply ST Programming in a safety PLC 6)

Reaching the expert level in ST programming often takes 3 to 5 years after ending the education/course.

1) This is possible by using Copy-Paste and minor corrections. Siemens uses e.g. # before local variables and Allen Bradley another syntax for function ‘calls’.

2) Mathematical calculations are similar to mathematical formulas. See page 45

3) Today there is more focus on energy optimization, automatic operation and data collection. These are all solutions which requires more complex PLC coding than merely an ordinary ‘relay/circuit breaker’ with start/stop functions.

4) The tools GIT and Subversion are practical tools in order to track (follow) corrections and extensions in the PLC Code. This makes sure that it is possible to fetch an earlier version (edition) of the right PLC Code in question.

5) In order to help people who are used to use LADDER programming to start ST programming instead, chapter 14, page 108, shows examples of chosen LADDER Programming and equivalent ST Programming.

6) A separate PLC or special areas in an ordinary PLC are used to disconnect motors and other moveable parts if the emergency stop device is activated. The warranty must be a total of 100 % in order to have a proper disconnection and thus the PLC code is executed in a PLC in a safety mode, which is approved for this purpose.

#### 2 How the PLC executes PLC code

It is important to know how a PLC executes a program which must be taken into consideration when the PLC program is written. A PLC executes programs sequentially in real time which means that the single program parts must be executed within a short time. The program modules (parts) are executed at a fixed interval (PLC scan time) e.g. 50 [ms]. Some of the fastest PLCs may have a scan time at 1 [μs].

Program modules with different scan time e.g. 500 [ms] or each minute are possible. Sensors might occur which do not change their value quickly (e.g. a temperature sensor) and thus it is unnecessary to obtain quick scan time for all program parts. A large program including many calculations, takes longer time to execute and therefore it will be necessary to obtain different scan times for different program modules.

The below flow diagram shows the basic mode of operation for a PLC:

#### The flow diagram shows the following points:

When power is connected to the PLC it will start up / boot and load the operative system, named firmware in a PLC system. This will assure that the PLC program is familiar with the connected hardware (HW).

After startup all output modules are set to the value to which they are initialized. It is important that all outputs have the right startup value so that the machine does not carry out any unfortunate actions before the PLC program has started.

Now a data communication is made via a network (fieldbus). Hereby many variables are received and many are sent out to other units (e.g. control panels, other control systems or instruments). There are many types of Fieldbuses (e.g. Profibus, Profinet og EtherNet/IP). However, they basically have equal functions.

Now values from all sensors, contacts, breakers, instruments and components on the machine/unit are received from the input modules.

Execution of all PLC programs once, dependent on scan time. Programs are split up as follows:

Program modules. Se section 10 page →

Functions. See section 10.1 page →

Functions (FC) and Functions Blocks (FB). See page →

Programs must be split up in order to create a good program structure.

Write values to all output modules, e.g. new settings to motors/engines, valves, lamps and instruments.

All sequences, points 3 to 6 will be repeated, which is one program scan.

The execution of the program only stops if:

The PLC program is set to STOP mode

If a run time error occurs

The PLC is powed off or loss the power

#### 3 Comments in programming code

Comments are a very important part of programming. Comments in the programming code assist you and your colleague when later adding in the code. Use comments to explain what a specific PLC code performs, so you can remember it later yourself. In many cases, the PLC code can be self-explanatory, therefore it is a better choice only to make comments when coding is complex.

There are two types of comments in ST:

Line Comment

// Line comment. Forward-slash is written in front of EVERY line.

// Also used to remove/sort out PLC code – i.e. a code which is not executed

// The code has disappeared if it is deleted, therefore place // in front of the line

// instead of deleting the code by placing // in front of the code, the code is still to

// be seen but not executed

Block Comment

(* Block comment is initiated by start parentheses and a star. It is finalized by a star and end parentheses. They are used for making more lines of PLC code inactive *)

Line comments can be only be positioned on the same line in front of or after code.

Comments positioned between (* and *) are named block comments and are used in order to remove/sort out more lines of code or to write comments filling up more lines.

For every program module or function comments at the top lines are used so that another programmer quickly can read a description or introduction to the program module or the function.

It is recommended that the top lines contain a version log so it is possible to recognize what has currently been changed in the PLC code and by whom:

A few PLC types cannot handle the special localized language letters such as æøå/ÆØÅ in the comment lines. It is therefore in general recommended to utilize the English alphabet in both the comment lines and the programming as localized special letters are often not accepted and, furthermore, many companies choose to write their PLC code in the English language. Another reason for writing the PLC code in English is the fact that many companies are working internationally.

IMPORTANT

Do remember to correct the comments and version log if anything is subsequently changed in the PLC code.

TIPS:

A good idea could be to use comments to describe what any PLC code could do, before starting to write the PLC code.

It could support the single programmer to gain more structure and again help the reader to understand the PLC code.

#### 4 Data types

Just like other programming languages, the IEC 61131-3 standard provides many different data types, being both elementary and diverted ones. A data type defines how much memory capacity is needed to a variable value and by that is meant the largest and smallest value in the variable.