1 / 68

3 PLC Programming

EPFL, Spring 2017. 3 PLC Programming. The long march to IEC 61131. NEMA Programmable Controllers Committee formed (USA). PLC industry needs aggreement on Data types (operations may only be executed on appropriate types) Programming languages

Download Presentation

3 PLC Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EPFL, Spring 2017 3 PLC Programming

  2. The long march to IEC 61131 NEMA Programmable Controllers Committee formed (USA) PLC industry needs aggreement on • Data types (operations may only be executed on appropriate types) • Programming languages • Software structure (program organization units for modularity, encapsulation) • Execution GRAFCET (France) DIN 40719, Function Charts (Germany) NEMA ICS-3-304, Programmable Controllers (USA) IEC SC65A/WG6 formed DIN 19 239, Programmable Controller (Germany) IEC 65A(Sec)38, Programmable Controllers MIL-STD-1815 Ada (USA) IEC SC65A(Sec)49, PC Languages IEC SC65A(Sec)67 IEC 848, Function Charts IEC 64A(Sec)90 it took 20 years to make that standard… IEC 1131-3 Type 3 report recommendation IEC 61131-3 name change 70 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 Source: Dr. J. Christensen

  3. "Real-Time" languages Extend procedural languages to express time Languages developed for cyclic execution and real-time (“introduce programming constructs to ("application-oriented languages") influence scheduling and control flow”) • ADA • Ladder Diagrams • Real-Time Java • Function block diagrams MARS (TU Wien) • • Instruction lists • Forth • GRAFCET • “C” with real-time features • Sequential flow charts • etc… etc... could not establish themselves wide-spread in the control industry. Now standardized as IEC 61131

  4. Matching the analog and binary world Binary World Analog World Pneumatic and electromechanical Relay control, pneumatic sequencer I1 controllers A B C P1 P2 regulation, controllers combinatorial sequential continuous processes discrete processes variables with non-overlapping values, transition from one state to another is abrupt, caused by external events. time constant of the control system must be at least one order of magnitude smaller than smallest time constant of plant. PLC

  5. START STEP T1 N D1_READY ACTION D1 STEP A ACTION D2 D D2_READY T2 N D3_READY ACTION D3 STEP B ACTION D4 D D4_READY T3 VAR CONSTANT X : REAL := 53.8 ; Z : REAL; END_VAR VAR aFB, bFB : FB_type; END_VAR bFB(A:=1, B:=‘OK’); Z := X - INT_TO_REAL (bFB.OUT1); IF Z>57.0 THEN aFB(A:=0, B:=“ERR”); END_IF IEC 61131-3 programming languages http://www.isagraf.com Sequential Flow Chart (SFC) Graphical languages Function Block Diagram (FBD) PUMP CALC DO >=1 AUTO IN1 OUT V MAN_ON IN2 ACT Ladder Diagram (LD) CALC PUMP AUTO OUT IN1 OUT ACT Textual languages Structured Text (ST) IN2 MAN_ON Instruction List (IL) A: LD %IX1 (* PUSH BUTTON *) ANDN %MX5 (* NOT INHIBITED *) ST %QX2 (* FAN ON *)

  6. Importance of IEC 61131 IEC 61131-3 are the most important automation languages in industry. 80% of all PLCs support it, all new developments are based on it. Depending on the country, some languages are more popular than others. IEC 61499 extends IEC 61131 with an event-driven model, has not established itself yet. More information: http://www.plcopen.org/pages/tc1_standards/downloads/plcopen_iec61131-3_feb2014.pptx http://www.plcopen.org/pages/pc2_training/downloads/index.htm

  7. Connecting to Input/Output The inputs and outputs of the PLC must be connected to (typed) variables IN_1 OUT_1 The I/O blocks are configured to be attached to the corresponding I/O groups.

  8. Connecting to Input / Output All program variables must be declared with name and type, initial value and volatility. A variable may be connected to an input or an output, giving it an I/O address. Several properties can be set: default value, fall-back value, store at power fail,…

  9. Function Block Diagrams • Graphical language to express programs in a way similar to electronic circuits • Using predefined and custom functions, like Matlab / Simulink • For continuous functions and combinatorial logic, may have memory (e.g. RS-flip-flops) Example 1: A & C B Example 2: external outputs external inputs Trigger & Tempo S Q Spin Running R Reset

  10. Function block elements parameters input signals output signals "continuously" PID set point command executing block, independent, overflow measurement no side effects The block is defined by its: • Data flow interface (number and type of input/output signals) • Black-box behavior (functional semantic, e.g. in textual form). Signals are typed connections that carry a pseudo-continuous data flow set point (set point) (set point)

  11. Function block rules Each signal is connected to exactly one source. • This source can be the output of a function block or a plant signal. • The type of the output pin, the type of the input pin and the signal type must be identical. Signals should flow from left to right and from top to bottom Retroactions are an exception to this rule, where the signal direction is identified by an arrow (forbidden in some editors, use global variables instead). a b x z c y

  12. Types of Programming Organisation Units (POUs) 1) “Functions” - are part of the base library. - have no memory. Examples: and gate, adder, multiplier, selector,.... 2) “Elementary Function Blocks” (EFB) - are part of the base library - may have a memory ("static" data). - may access global variables (side-effects !) Examples: counter, filter, integrator,..... 3) “Programs” (Compound blocks) - user-defined or application-specific blocks - may havea memory - may be configurable (control flow not visible in the FBD Examples: PID controller, overcurrent protection, motor sequence (a library of compound blocks may be found in IEC 61804-1)

  13. Function block library The programmer chooses the blocks in a block library, similarly to the hardware engineer who chooses integrated circuits in a catalogue. The library describes the pinning of each block, its semantics and theexecution time. The programmer may extend the library by defining function block macroscomposed of library elements. If some blocks are used often, they will be programmed in an external language (e.g. “C”, micro-code) following strict rules.

  14. Library functions for discrete and continuous plants Basic blocks Basic blocks Summator / SubtractorMultiplier / DividerIntegrator / DifferentiatorFilter Min, Max Logical operations(AND, OR, …)Flip-flopSelector m-out-of-n Multiplexer m-to-nTimerCounterMemorySequencing Regulation Functions P, PI, PID, PDT2 controllerFixed set-pointRatio, multi-component regulation2-point regulation3-point regulationOutput value limitationRamp generatorAdaptive regulationDrive Control Compound blocks Display Manual input, touch-screen Safety blocks (interlocking) Alarm signaling Logging

  15. MoveAbsolute Axis Axis AXIS_REF AXIS_REF Done BOOL BOOL Execute REAL CommandAborted Position BOOL Velocity REAL BOOL Error Acceleration REAL ErrorID WORD Deceleration REAL Jerk REAL MC_Direction Direction Function block library for specialized applications standardized blocks are defined in libraries, e.g. motion control or robotics

  16. Specifying the behaviour of function blocks Time Diagram: 0 T y x x y T Truth Table: x1 x2 y x1 S 0 0 previous state 0 1 0 R x2 1 0 1 1 1 1 x y Mathematical Formula: Calculates the root mean square of the input with a filtering constant defined in parameter „FilterDelay“ Textual Description:

  17. Function block specification in structured text

  18. Execution of function blocks Segment or POU (program organization unit) X01 A X F1 F2 B F4 C X02 F3 Y Machine Code: function F1 input1 A The function blocks are translated to machine language (intermediate code, IL), that is either interpreted or compiled to assembly language Blocks are executed in sequence, normally from upper left to lower right The sequence is repeated every t ms. input2 B output X01 F2 X01 X F3 B C X02 F4 X X02 Y

  19. Input-output of function blocks Run-time: read write inputs outputs I X O I X O I X O time individual period execute • Executed cyclically: • all inputs are read from memory or plant (possibly cached) • segment is executed • results are written into memory or to plant (possibly to cache) • Order of execution of the blocks does not matter • For speed it can help to impose execution order on blocks • Different segments may be assigned different periods

  20. IEC 61131-3 library (extract) Arithmetic Functions Selector Boolean Functions Binary selection adder Select one of N inputs depending on input K ADD subtractor Flip-flop SUB dominant setQ:=S1|(Q&!R) Up counter multiplier MUL Integrator INT divider DIV (if reset) Out := PV, else Out:= Δt *In + Out Reset PV dominant resetQ:=!R1&(Q|S) In CU – input (rising edges count up) R – reset counter (CV:=0) PV – preset value Q – 1 if CV >= PV CV – current value Trigger rising edgedetector More details http://calc1.kss.ia.polsl.pl/content/dydaktyka/PC/PLC_IEC61131-3.pdf

  21. Exercise: What do the following blocks do ? (Δt = 1) 2. In1 Remember: INT If (Reset) : Out := PV, Else: Out := Δt *In + Out 1. (1024) Out In2 (initially 0) (2) What are the values of Out? What happens if Reset = 1 or if Out is initially 1024? What are the values of Out? In Out ADD (10) CTU 3. t1 t2 t3 t4 t5 t6 t7 t8 CU (initially 2) CU DIV Q RESET CV PV Reset = 0, PV = 3, CV = #up Q = (CV >= PV) ? INT Flipflop: dominant set or reset? 4. Reset = 0 S dominant setQ:=S1|(Q&!R) SR PV S1 In R Q R RS dominant resetQ:=!R1&(Q|S) S Q R1 Q http://tinyurl.com/IAFunctionBlock

  22. Exercise: What do the following blocks do ? 2. IfOut=0 initially: 0, 0, 0, 0, 0IfReset=1 initially: 1024, 1024, 1024, 1024 If Out =1024 initially: 1024, 1536, 2304, 3456 1. 2, 12, 22, 32, 42 3. CV = 1, 1, 2, 2, 3, 3, 4, 4 Q = 0, 0, 0, 0, 1, 1, 1, 1 4. S dominant setQ:=S1|(Q&!R) SR S1 R R Q Q ftp://advantechdownloads.com/Training/KW%20training/ http://tinyurl.com/IAFunctionBlock

  23. Exercise: Asymmetric Sawtooth Wave Build an asymmetric sawtooth wave generator with IEC 61131 function blocks 5s 12s 75 0 Hints:- Compute the slopes - Use integrators, comparators, flip-flops and selectors -25

  24. Exercise: Saw-tooth FBD Out 75.0 0 -25.0 + 8.3 -20.0 Other solutions exists.

  25. Function block decomposition A function block describes a data flow interface. Its body can be implemented differently: Elementary block The body is implemented in an external language (micro-code, assembler, IEC 61131 ST): procedure xy (a,b:BOOLEAN; VAR b,c: BOOLEAN); begin ...... = .... end xy; Compound block The body is realized as a function block program Each input (output) pin of the interface is implemented as exactly one input (output) of the function block. All signals must appear at the interface to guarantee freedom from side effects. . =

  26. Function block segmentation An application program (task) is decomposed into segments ("Programs") for easier reading, each segment being represented on one (A4) printed page. • Within a segment, the connections are represented graphically . • Between the segments, the connections are expressed by signal names . Segment A X1 M2 M1 Y1 Segment B X2 Y2 M1 X3 M2

  27. Structured Text (Strukturierte Textsprache, langage littéral structuré) Structured Text is an imperative language similar to Pascal (If, While, etc..) The variables defined in ST can be used in other languages ST is used for complex data manipulation and to write blocks Caution: writing programs in structured text can breach real-time rules !

  28. Structured Text Examples IF tank.temp > 200 THEN pump.fast :=1;pump.slow :=0; pump.off :=0; ELSIF tank.temp > 100 THEN pump.fast :=0; pump.slow :=1; pump.off :=0; ELSE pump.fast :=0; pump.slow :=0; pump.off :=1; END_IF; IF( Switch_0 AND Switch_1 ) THEN Start_Motor := 1; Start_Count := Start_Count + 1; END_IF; pos := 0; WHILE((pos < 100) & s_arr[pos].value <> target)) DO pos := pos + 2; String_tag.DATA[pos] := SINT_array[pos]; END_WHILE; Predefined functions, e.g.:SIZE(SINT_array, 0, SINT_array_size); Count the number of elements in SINT_array (array that contains inputs) and store result in SINT_array_size (DINT tag). [http://literature.rockwellautomation.com/idc/groups/literature/documents/pm/1756-pm007_-en-p.pdf]

  29. Structured Text Example Move ASCII characters from a SINT array into a string tag. (In a SINT array, each element holds one character.) Stop when you reach the carriage return. • element_num := 0; • SIZE(SINT_array, 0, SINT_array_size); • WHILE SINT_array[element_num] <> 13 DO • String_tag.DATA[element_num] := SINT_array[element_num]; • element_num := element_num + 1; • String_tag.LEN := element_num; • IF element_num = SINT_array_size then • exit; • END_IF; • END_WHILE; • Explanations: • Initialize element_num to 0. • Count number of elements in SINT_array(ASCII characters), store in SINT_array_size. • If the character at SINT_array[element_num] = 13 (carriage return), then stop. • Set String_tag[element_num] = the character at SINT_array[element_num]. • Add 1 to element_num. This lets the controller check the next character in SINT_array. • Set length member element_num (records number of characters in String_tag so far.) • If element_num = SINT_array_size, then stop. (if at end of array and no carriage return.) • Go to 3. [http://literature.rockwellautomation.com/idc/groups/literature/documents/pm/1756-pm007_-en-p.pdf]

  30. Structured Text Exercise • A user-defined data type (structure) stores information about an item in an Inventory array: • • Inventory[i].ID: Barcode ID of the item (string data type) • • Inventory[i].Qty: Quantity in stock of the item (DINT data type) • An array of the above structure contains an element for each different item in your inventory. You want to search the array for a specific product (by its barcode) and determine the quantity in stock. • Pseudocode: • Get size of Inventory array and store result in Inventory_Items (DINT tag). • Loop over positions in array. • If Barcode matches the ID of an item in the array, then:a. Set the Quantity tag = Inventory[position].Qtyb. Stop. [http://literature.rockwellautomation.com/idc/groups/literature/documents/pm/1756-pm007_-en-p.pdf]

  31. Structured Text Exercise Solution • A user-defined data type (structure) stores information about an item in an Inventory array: • • Inventory[i].ID: Barcode ID of the item (string data type) • • Inventory[i].Qty: Quantity in stock of the item (DINT data type) • An array of the above structure contains an element for each different item in your inventory. You want to search the array for a specific product (by its barcode) and determine the quantity in stock. • Pseudocode: • Get size of Inventory array and store result in Inventory_Items (DINT tag). • Loop over positions in array. • If Barcode matches the ID of an item in the array, then:a. Set the Quantity tag = Inventory[position].Qtyb. Stop. Solution: SIZE(Inventory,0,Inventory_Items); FOR position:=0 to Inventory_Items - 1 DO IF Barcode = Inventory[position].ID THEN Quantity := Inventory[position].Qty; EXIT; END_IF; END_FOR; [http://literature.rockwellautomation.com/idc/groups/literature/documents/pm/1756-pm007_-en-p.pdf]

  32. SFC (Sequential Flow Chart) (Ablaufdiagramme, diagrammes de flux en séquence ) START STEP T1 N ACTION D1 D1_READY STEP A STEP B ACTION D2 D D2_READY T2 • Describes sequences of operations and interactions between parallel processes. • Derived from Grafcet and SDL (Specification and Description Language, used for communication protocols), mathematical foundation lies in Petri Nets.

  33. SFC: Elements S0 event condition "1" ("1" = always true) transitions example transition condition Sa Ea Ec = ((varX & varY) | varZ) states Sb Eb token Sc • Program consists of states connected by transitions. • A state is activated by a token (the corresponding variable becomes TRUE). • Token leaves state when transition condition (event) on state output is true. • Only one transition takes place at a time, • Execution period is configuration parameter (task to which program is attached) Rule: there is always a transition between two states, there is always a state between two transitions

  34. SFC: Initial state State which come into existence with a token are called initial states. All initial states receive exactly one token, the other states receive none. Initialization takes place explicitly at start-up. In some systems, initialization may be triggered in a user program (initialization pin in a function block).

  35. SFC: Switch and parallel execution E0 "1" token switch : token crosses the first active Sa transition (at random if both Ea and Eb are true) Note: transitions are after the switch Ea Eb Sc Ec Sb Sd Ed Se token forking : when the transition Ee is true, the token is replicated to all connected states Note: transition is before the fork Ee Ef Sg token joinwhen all connected states have tokens and transition Eg is true, one single token is forwarded. Note: transition is after the join Sf Eg

  36. SFC: P1, N and P0 actions P1 State1_P1: do at enter State1 N State1_N: do while P0 State1_P0: do at leaving P1 (pulse raise) action is executed once when the state is entered P0 (pulse fall) action is executed once when the state is left N (non-stored) action is executed continuously while the token is in the state P1 and P0 actions could be replaced by additional states. The actions are described by a code block written e.g. in Structured Text.

  37. Special action: the timer rather than define a P0 action “ reset timer….”, there is an implicit variable defined as <state name>.t that express the time spent in that state. S S.t > t#5s Sf

  38. trap SFC: Exercise Variables Input: I0, I1, I2, I3 (boolean); Output: Trap = {0: closed; 1: open} Speed = {+20: +1 m/s; +1: +5 cm/s; 0: 0m/s} Register = {0: closed; 1: open} negative values: opposite direction Register = {0: closed; 1: open} +speed Inputs generate “1” as long as the tag of the vehicle (1cm) is over the sensor. I0 I1 I2 I3 Speed = 5 cm/s from I1 to I0 and from I2 to I3, faster otherwise. Initially: move vehicle at reduced speed until it touches I0 and open the trap for 5s(empty the vehicle). 4 - Go back to I0 1 - Let the vehicle move from I0 to I3 5 - Open the trap and wait 5s. 2 - Stop the vehicle when it reaches I3. repeat above steps indefinitely 3 - Open the tank during 5s.

  39. Exercise SFC Example Solution

  40. SFC: Structuring Every flow chart without a token generator may be redrawn as a structured flow chart (by possibly duplicating program parts) Not structured structured A a A B a b d B d C b c C B' c b d A' a

  41. Function Blocks And Flow Chart Function Blocks: Sequential Flow Charts: Continuous (time) control Discrete (time) Control • Many PLC applications mix continuous and discrete control. • A PLC may execute alternatively function blocks and flow charts. • Communication between these program parts is possible. Principle: A flow chart taken as a whole can be considered a function block with binary inputs (transitions) and binary outputs (states).

  42. Executing Flow Charts As blocks A function block may be implemented in different ways: procedure xy(...); begin ... end xy; function blocks flow chart extern (ST/IL) Function blocks and flow chart communicate over binary signals.

  43. Flow Charts or Function Blocks ? A task can sometimes be written indifferently as function blocs or as flow chart. The application may decide which representation is more appropriate: Flow Chart Function Block S a "1" R b c NOT d c b d a

  44. Flow Charts Or Blocks ? (2) Flow Chart Function Blocks init ≥ "1" S R A A & a a B S b R B C c & b S R C & c In this example, a flow chart seems to be more appropriate:

  45. Exercise: write the SFC for this task open V1 until tank’s L1 indicates upper level open V2 during 25 seconds open V3 until the tank’s L1 indicates it reached the lower level while stirring. heat mixture during 50 minutes while stirring empty the reactor while the drying bed is moving repeat V2 V1 upper lower L1 MS V3 H1 T temperature(sensor) heater (actor) V4 MD

  46. Ladder Diagrams (1) (Kontaktplansprache, langage à contacts) Ladder Diagrams is the oldest programming language for PLC - based on relay intuition of electricians. - widely in use - not recommended for large new projects. Output (actions) Input instructions (conditions) Rung 0 Rung 1 Rung 2

  47. Ladder Diagrams (2) The contact plan or "ladder diagram" language allows an easy transition from the traditional relay logic diagrams to the programming of binary functions. It is well suited to express combinational logic It is not suited for process control programming (there are no analog elements). The main Ladder Diagrams symbols represent the elements: contact travail Arbeitskontakt make contact contact repos Ruhekontakt break contact bobine Spule relay coil

  48. Ladder Diagrams Example (3) make contact (contact travail) origin: electrical circuit 02 01 relay coil (bobine) 03 50 break contact (contact repos) 02 01 rung 50 corresponding ladder diagram 03 50 05 "coil" 50 is used to move other contact(s) 44

  49. Ladder Diagrams (4) Binary combinations are expressed by series and parallel relay contact: Ladder Diagrams representation “logic" equivalent Series + 01 02 01 50 50 02 Coil 50 is active (current flows) when 01 is active and 02 is not. + Parallel 01 01 40 02 40 02 Coil 40 is active (current flows) when 01 is active or 02 is not.

  50. Ladder Diagrams (5) The Ladder Diagrams is more intuitive for complex binary expressions than literal languages textual expression 1 2 3 4 ! 1 & 2 & ( 3 & ! 4 | ! 5 & 6 ) = 50 Or N1 & 2 STR 3 & N4 STR N5 & 6 / STR & STR = 50 50 5 6 0 1 4 5 12 50 N0 & 1 STR 2 & 3 / STR STR 4 & 5 STR N 6 & 7 2 3 6 7 / STR & STR STR 10 & 11 / STR & 12 = 50 11 10

More Related