Petri net-based semi-compiled code generation for programmable logic controllers

: Industrial discrete event dynamic systems (DEDSs) are commonly modeled by means of 1 Petri nets (PNs). PNs have the capability to model behaviours such as concurrency, synchroniza-2 tion, and resource sharing, compared to a step transition function chart or GRAphe Fonctionnel 3 de Commande Etape Transition (GRAFCET) which is a particular case of a PN. However, there 4 is not an effective systematic way to implement a PN in a programmable logic controller (PLC), 5 and so it is very common the implementation of such a controller outside a PLC, in some external 6 software that will communicate with the PLC. There have been some attempts to implement PNs 7 within a PLC, but they are dependent on how the logic of places and transitions is programmed for 8 each application. This work proposes a novel application-independent and platform-independent 9 PN implementation methodology. This methodology is a systematic way to implement a PN 10 controller within industrial PLCs. A great portion of the code will be validated automatically prior 11 to PLC implementation. Net structure and marking evolution will be checked on the basis of PN 12 model structural analysis, and only net interpretation will be manually coded and error-prone. 13 Thus, this methodology represents a systematic and semi-compiled PN implementation method. 14 A use case supported by a digital twin (DT) is shown where the automated solution required by a 15 manufacturing system is carried out and executed in two different devices for portability testing, 16 and the scan cycle periods are compared for both approaches. 17


Introduction
Discrete event dynamic systems (DEDSs) are widely present in industrial manufac-20 turing processes.A DEDS is a dynamic, asynchronous system, where state transitions are 21 initiated by events that occur at discrete instants of time [1].They are usually modeled 22 by finite state automata with partially observable events together with a mechanism for 23 enabling and disabling a subset of state transitions [2].The following are some examples:

•
Clients arriving or leaving a waiting queue to be attended at a desk.

25
• A waiter serving two customers, noting requests and serving them in any order.

26
• Synchronization of traffic lights in road intersections, after expiration of each state 27 temporization (Figure 1).distributed, parallel, nondeterministic, and/or stochastic [3].A PN is a graphical tool    (RG), from m, is denoted by RG(N,m).C and m 0 corresponding to the example are as 59 follows: PN dynamics is characterized by the marking, i.e. tokens (dots) distribution among 61 places, and its evolution.Marking progress depends on the controlled firing of transitions 62 i.e. their enabling and the compliance of their associated firing conditions.

63
In terms of structural validation, [3] defines basic properties to be fulfilled by a PN, 64 such as its liveness, cyclicity, and boundedness.The existence of the aforementioned 65 formal mathematical definition (C, m 0 ), as well as formal validation methods [3] [8] 66 [9], make it possible to detect, in the modeling phase, (i) blocking states (markings), (ii)

76
The PLC is the most widely used controller in industrial environment.It is a control

82
PN implementations in PLCs depend on how the logic of places and transitions 83 is programmed.There is not any broadly used systematic method of implementation.

84
Therefore, there may be as many PN implementation methods as programmers, and any 85 improvement in the PN involves code regeneration.There have been some attempts 86 towards a systematic PN implementation method, however, although these attempts

87
propose an ordered method to program the logic of places and transitions, they have not 88 been widely adopted.instruction lists [23] [24] to more recent proposals in standard programming languages 130 [25] or structured text (ST) [26], for portability to different PLCs.In a recent paper, [27] 131 proposes editing both net structure and its interpretation in a proprietary graphical 132 environment and automatically converting them into PLC code.

133
Existing PN implementations in PLCs present some weaknesses.Thus, they are fully 134 interpreted developments, totally dependent on the application and the programmer.

135
The coding workload is significant and error-prone.

Structural validation 249
There are software tools available on the market which provide PN editing capabili-250 ties and token moving among places simulation [29].Platform Independent Petri net 251 Editor 2 (PIPE2) [30] [31], for example, owns also validation and performance analysis 252 features.Net invariants computing allows testing structural properties as boundness, 253 liveness and deadlock freeness.Finally, a file in HTML format with PN structure's matrix 254 representation can be generated, as well as validation process results.

255
Note that a 100% automatic validation is not possible.For example, a necessary 256 condition is tested for liveness, but it does not fully guarantee this PN property.PIPE2   This section describes the use case in which the experimentation was carried out.

272
The required resources are listed, the process to be automated is described, and the 273 implementations performed are explained.

308
For this study Simumatik3D V1.0.3 (S3D) [33] was used for VC, connected to a 309 physical PLC.For both implementations carried out on PLCs from different vendors, the proce-335 dure described in 2.2 was applied and the following starting points were assumed:

403
The purpose of carrying out several implementations on controllers from different 404 vendors was, on the one hand, to compare the scan cycle time required by each approach, 405 and on the other hand, testing algorithm portability between development platforms.

406
This section presents the results obtained.

28 •
Flexible and/or discrete part manufacturing systems, those concerning with robots 29 and programmable machines behaving with concurrent evolution, synchronization 30 and/or shared resources features.In the frame of this set a use case is analyzed in 31 Section 3.

32 1. 1 .
PN-based DEDSs modeling 33 Petri nets (PNs) are a powerful method to model and control such DEDSs as they 34 have the capability to represent systems characterized as being concurrent, asynchronous,35

Figure 3 .
Figure 3. SFC-based programming in PLC development environment.

77
device for DEDSs based on information from process and operator[10].Its use is due 78 to several characteristics that differentiate it from a conventional computer [11]: (i) 79 cyclic execution of the control program, (ii) reliability, (iii) adaptation to an industrial 80 environment with electrical noise, vibrations, extreme temperatures and humidity, and 81 (iv) easy maintenance.

197198
StartupOnly once, and before the continuous scan cycle execution, initial marking value is 199 given to the current one, and incidence matrix is calculated from the pre-incidence and 200 post-incidence ones.201 Enabling 202 This program block determines transitions enabled by current marking.Marking 203 vector is compared with each of the columns of the pre-incidence matrix to determine 204 whether the marking enables the corresponding transition.If so, it is annotated in an 205 array of enabled transitions.This function will not be executed again until some change 206 has occurred in current marking, that is, until some transition has already been fired.207 Firable 208 It is checked whether currently enabled transitions are likely to be fired.According 209 to net interpretation, it evaluates fulfillment of transitions corresponding firing condi-210 tions, listing them in an array.Subsequently, it is evaluated if they are liable to be fired, 211 reflecting it in another array.212 Make a decision 213 A rule is implemented in this block.Note that a firable transition does not have to 214 be fired at that instant.Firing could be postponed.It may not even occur, if the firing of 215 another transition causes it to become no longer enabled and firable.This property of 216 PNs is particularly important and interesting in the modeling of sequencing problems: 217 order control in the sequence of transitions firing.A function is introduced for the 218 decision of the transition to be fired among the liable to be fired ones, maybe being 219 in conflict.In this implementation a simple decision rule is applied: the last of those 220 present in the list of firable is fired.Once the selection has been made, it is possible to 221 update the net marking after the transition is fired.222 Preprints (www.preprints.org)| NOT PEER-REVIEWED | Posted:

230 2 . 2 .
Implementation procedure 231The proposed procedure for implementing a PN from an algorithm that meets the 232 specifications described, suitable for any process and PLC, consists of the following 233 phases, as shown in Figure 5.It contains the phases of the implementation, only one of 234 them being a source of errors in the code: that of net interpretation programming.The 235 code reaches this phase mostly fixed and validated, pending manual addition/validation 236 of the mentioned code lines.237 Note that specifications definition, characterizing the desired operation and iden-238 tifying key signals, is a previous work to be done, as in any automation project, and 239 independent of the proposed methodology.It involves project creation in the develop-240 ment environment, hardware definition, program block addition and transfer of those 241 that are fixed and application independent, and declaration and initialization of vari-242 ables (internal and I/O).Furthermore, if virtual commissioning (VC) of the automated 243 solution is planned, a process DT must be designed and developed.

244 2 .
2.1.PN design 245 Once specifications are defined, the PN structural design is carried out for its 246 subsequent analysis.Interpretation is added, associating conditions and actions with 247 PN structure transitions and places, respectively. 248

Figure 6 .
Figure 6.Detail of a RG generated by PIPE2.

264 2 . 2 . 4 .
Interpretation coding 265The corresponding code for each case has to be developed.Conditions and actions 266 are edited in program memory, according to particular net interpretation.

267 2 . 2 . 5 .
System commissioning 268 This step implies verifying the correct behavior of the automated process, according269

274 3 . 1 .
Manufacturing process under study 275 3.1.1.Description 276 The system under study is the one shown in Figure 7.It has two machines (M 1 277 and M 2 ), in which parts have to be worked consecutively.A temporary buffer with 278 limited (7) capacity and a robot (R) complete the manufacturing cell.M 1 and M 2 can 279 be in one of the following five states: waiting to be loaded with a part, being loaded, 280 operating, waiting to be unloaded, being unloaded.The purpose of R is to load/unload 281 parts to/from the stations before/after the operations they perform.Being single and 282 shared, it can be in one of these states: waiting for servicing, or executing one of four 283 movements: M 1 station loading with a part from input; M 1 unloading, a part is driven 284 to intermediate buffer; M 2 station loading with a part from the buffer; M 2 unloading, a 285 part is driven to system output.286It was considered to be an interesting case for a PN-based implementation, due to 287 the use of a limited-capacity intermediate buffer, and the existence of a shared resource, 288 R, that executes one movement each time.

303
For each of the necessary resources, it is specified what was employed in this 304 particular use case.305 • An industrial system and process emulation tool or DT.It can support the whole 306 process of development, simulation and validation, connected to the emulated PLC 307 in a software-in-the-loop (SIL) configuration or to the physical controller in HIL.

-
environment, as well as the device itself and its emulation 311 tool.312 Two control devices from different vendors were required, with their respective 313 development platforms: 314 SIMATIC S7-1500 CPU 1512C-1 PN of Siemens, with TIA Portal V15.1 for 315 coding and PLCSIM Advanced V2.0 SP1 for emulation.The algorithm was 316 available for these tools.

317 -Figure 8 .
Figure 8. Emulation of the PLC-controlled part of the process.

326 3 . 3 .
Manufacturing system automation 327 The automated solution for the robotic cell under study was implemented by means 328 of PN-based approach for two PLCs from different vendors.In addition, each of the 329 controllers was programmed following other similar, fully interpreted solutions, in 330 order to compare the scan cycle time required: two PN-based, using LDs and ST, and 331 a GRAFCET-based one, exclusively for Siemens, by means of SFC.This subsection 332 describes the work performed.

Figure 11 .
Figure 11.PN interpretation coding detail: editing of conditions associated with each transition.
that processes both net structure and interpretation, by means of set 396 and reset of variables, in LDs.397 • SFC: graphic modeling and description method, suitable for GRAFCET and avail-398 able in TIA Portal.399 For each of the developments and control devices, scan cycle time measurements 400 were performed, based on the connection of the physical PLC with the process DT, 401 according to a HIL setup. 402

Table 1 .
Interpretation to be added to the PN structure shown in Figure2.

30 July 2021 doi:10.20944/preprints202107.0674.v1
71Finally, PN interpretation is defined by events modeled in transitions, which lead 72 to marking changes, and actions associated to places.It can consist of: (i) time (constant, 73 random) for performance evaluation; (ii) condition action for control.Table 1 shows the 74 interpretation corresponding to the example.75 Preprints (www.preprints.org)| NOT PEER-REVIEWED | Posted:
89 1.3.GRAFCET: a particular case of PN 90 A step transition function chart or GRAphe Fonctionnel de Commande Etape Tran-120 of building PNs in a simple way, as well as to be able to evaluate their properties and 121 simulate token evolution.[17] points out the limitation of LD-based implementations 122 in a context in which systems are of increasing complexity, and the need for tools for 123 control, analysis, evaluation and simulation of systems, presenting PN as an integrated 124 solution and making a survey about approaches for its conversion to LD. [4] defines a 125 set of rules for implementing LDs and overcomes merely intuitive ad hoc developments.126 The review by [18] references these PN to LD conversions, among other implementations 127 of logic controllers.In addition, literature shows the use of other PLC programming 128 languages as well.References evolve from the mentioned LDs [19] [20] [21] [22] and/or 129

30 July 2021 doi:10.20944/preprints202107.0674.v1
Moreover, any modification in 136 net structure or interpretation involves code rewriting/regenerating.In terms of PN 137 validation, it is conventionally performed at the end of development, on the equipment 138 already assembled, on a digital twin (DT) that emulates it, or by forcing input values 139 and observing output behaviors in the PLC development environment itself.140This paper proposes a methodology to implement a PN within a PLC in a simple 141 way, making use of the mentioned formal mathematical definition of a PN, that makes 142 possible to automatically validate part of the PLC program, minimizing coding workload 143 and, consequently, error-prone points.Thus, a previously validated PN matrix represen-144 tation will be directly copied into a PLC device data memory.A fixed program blocks 145 will manage the marking of the PN and the programming workload will be limited to 146 PN interpretation, i.e.: assigning conditions (inputs queries) to transitions and actions 147 (outputs writing) to places.In this way, projects can be carried out and commissioned in 148 a very efficient way.This represents a semi-compiled PN implementation instead of an 149 exclusively interpreted one.150The proposed PN implementation approach can contribute to the state-of-the-art in 156 for PN structure and interpretation editing, in this work it is proposed the use of a 157 commercial software that provides structure editing, simulation and validation.158• Coding workload is reduced to net interpretation: assignment of actions to places 159 and conditions to transitions of the net, thus to process outputs and inputs, respec-160 Preprints (www.preprints.org)| NOT PEER-REVIEWED | Posted: tively.Exclusively the mentioned program section remains to be manually coded 161 and validated.162 • Any change in net structure or initial marking is direct and impacts only the con-163 troller's data area, never the program, which remains unmodified.In contrast, 164 literature shows code generation methodologies, transforming complete PNs (struc-165 ture and interpretation) to PLC standard programming languages.[28], for instance, 166 automates and accelerates PN to LD conversion supported by characteristic matri-167 ces, but without automatically generating them or using validation rules, and any 168 change involves all code to be generated and manually validated.169 In the context of this work, simple PNs are used, with event/action interpreta-170 tion.We limit ourselves to basic structural analysis without any previous performance 171 evaluation by means of other temporal and/or stochastic interpretation.172 2. Methodology 173 This section shows the proposed algorithm and its implementation methodology.174 2.1.Algorithm for PN marking management 175 The basis of the proposal is a PN-based algorithm with compliance with the follow-176 ing specifications: 177 • An appropriate program block structure, with a fixed part (related to PN struc-178 ture and marking evolution) and a customizable or to be programmed part (PN 179 interpretation) properly differentiated.180 • Apply PN marking evolution rules.181 • Coding in ST, due to (i) simple portability regardless of target device vendor, (ii) 182 operating matricially element by element.189 In compliance with the specifications described above, the algorithm kernel imple-190 ments the transition firing and marking updating rules for a general PN, as detailed 191 in Algorithm 1. Provided with any particular PN pre-incidence and post-incidence 192 matrices and initial marking vector contained in a PLC data block, this algorithm allows 193 token movements among places managing, thus allowing transition enabling, firing and 194 resulting marking computation.It is coded in ST, in order to operate more simply with 195 Preprints (www.

preprints.org) | NOT PEER-REVIEWED | Posted: 30 July 2021 doi:10.20944/preprints202107.0674.v1 Algorithm 1:
Transition firing and marking updating for a general PN i = 1, ..., m; where m is the cardinality of P; ∧ j = 1, ..., n; where n is that of T. marking initialization: x(p i ) := x 0 (p i ); incidence matrix initialization: Inc(p i , t j ) := Post(p i , t j ) − Pre(p i , t j ); while TRUE do create list of enabled transitions E(t j ): a transition is enabled ⇐⇒ x(p i ) ≥ Pre(p i , t j ), ∀p i ∈ I(t j ); I(t j ) is the set of input places of a transition t j ∈ T. compute conditions list: Cond(t j ), ∀t j ∈ E(t j ); create list of firable transitions F(t j ): a transition is firable ⇐⇒ Cond(t j ) = TRUE, ∀t j ∈ E(t j ); if ∃t j /F(t j ) = 0 then decide on which transition t j ∈ F(t j ), if any, should be fired; if it is decided to fire t j ∈ F(t j ) then compute new marking reached: x (p i ) := x(p i ) + Inc(p i , t j ); update marking: x(p i ) := x (p i ); update actions: ∀p i /x(p i ) > 0 =⇒ Acc(p i ) := TRUE; end end end matrices and for an easier portability to other manufacturers' devices.The program 196 block execution flow is described below.

Table 2 .
Interpretation to be added to the PN structure shown in Figure9.

Table 3 .
Measured scan cycle periods.The scan cycle time for each of the implementations has been measured for each 409of the PLCs used, in order to determine whether or not the algorithm presented in 410 this work consumes more resources than other approaches.Table 3 summarizes the 411 results obtained.More than 100000 scan cycles have been executed in all tests.Note that 412 Omron´s Sysmac Studio provides the average value of the scan cycle period.Siemens' 413 TIA Portal, on the other hand, gives the current value.In the latter case, 10 samples