Petri Net-Based Semi-Compiled Code Generation For
Petri Net-Based Semi-Compiled Code Generation For
sciences
Article
Petri Net-Based Semi-Compiled Code Generation for
Programmable Logic Controllers
Igor Azkarate 1, * , Mikel Ayani 2 , Juan Carlos Mugarza 1 and Luka Eciolaza 1
Abstract: Industrial discrete event dynamic systems (DEDSs) are commonly modeled by means of
Petri nets (PNs). PNs have the capability to model behaviors such as concurrency, synchronization,
and resource sharing, compared to a step transition function chart or GRAphe Fonctionnel de
Commande Etape Transition (GRAFCET) which is a particular case of a PN. However, there is not an
effective systematic way to implement a PN in a programmable logic controller (PLC), and so the
implementation of such a controller outside a PLC in some external software that will communicate
with the PLC is very common. There have been some attempts to implement PNs within a PLC, but
they are dependent on how the logic of places and transitions is programmed for each application.
This work proposes a novel application-independent and platform-independent PN implementation
methodology. This methodology is a systematic way to implement a PN controller within industrial
PLCs. A great portion of the code will be validated automatically prior to PLC implementation. Net
structure and marking evolution will be checked on the basis of PN model structural analysis, and
only net interpretation will be manually coded and error-prone. Thus, this methodology represents a
Citation: Azkarate, I.; Ayani, M.; systematic and semi-compiled PN implementation method. A use case supported by a digital twin
Mugarza, J.C.; Eciolaza, L. Petri (DT) is shown where the automated solution required by a manufacturing system is carried out and
Net-Based Semi-Compiled Code
executed in two different devices for portability testing, and the scan cycle periods are compared for
Generation for Programmable Logic
both approaches.
Controllers. Appl. Sci. 2021, 11, 7161.
https://doi.org/10.3390/app11157161
Keywords: Petri nets; programmable logic controllers; process modeling; digital twin
Academic Editors: Luis Gomes and
João Paulo Barros
t0 t1 t2 t3 t4
1 −1 0 0 0 p0
0
1 −1 0 0 p1
−1 0 1 0 0 p2
C = 0
0 0 −1 1 p3
0
0 0 1 −1 p4
1 0 0 −1 0 p5
0 −1 0 0 1 p6
p0 p1 p2 p3 p4 p5 p6
m0t = ( 1 0 0 1 0 0 1)
t0
p0 p5 p3
t1 t3
p1 p6 p4
t2 t4
p2
powerful than those proposed for GRAFCET, mainly related with divergence-convergence
managing [15].
i.e.: assigning conditions (inputs queries) to transitions and actions (outputs writing) to
places. In this way, projects can be carried out and commissioned in a very efficient way.
This represents a semi-compiled PN implementation instead of an exclusively interpreted
one.
The proposed PN implementation approach can contribute to the state-of-the-art in
several aspects (Figure 4):
• The PN model is constructed, evaluated and simulated in a simple way, which is
demanded by authors such as [16]. A visual and intuitive software tool is used to code,
generating structural data that are managed by fixed program blocks, and validate
part of the implementation. While the tool presented in [27] is exclusively for PN
structure and interpretation editing, in this work it is proposed the use of a commercial
software that provides structure editing, simulation, and validation.
• Coding workload is reduced to net interpretation: assignment of actions to places
and conditions to transitions of the net, thus to process outputs and inputs, respec-
tively. Exclusively the mentioned program section remains to be manually coded
and validated.
• Any change in net structure or initial marking is direct and impacts only the con-
troller’s data area, never the program, which remains unmodified. In contrast, liter-
ature shows code generation methodologies, transforming complete PNs (structure
and interpretation) to PLC standard programming languages. Ref. [28], for instance,
automates and accelerates PN to LD conversion supported by characteristic matrices,
but without automatically generating them or using validation rules, and any change
involves all code to be generated and manually validated.
In the context of this work, simple PNs are used, with event/action interpretation. We
limit ourselves to basic structural analysis without any previous performance evaluation
by means of other temporal and/or stochastic interpretation.
Computer PLC
PN
interpretation
management
PLC
(to be coded)
development PN
Outputs
PN Inputs
environment modelling structural data
(validated) PN
and structure and
simulation
Parser
marking
tool management
(fixed)
Data Program
→
→
memory memory
Process
2. Methodology
This section shows the proposed algorithm and its implementation methodology.
• Coding in ST, due to (i) simple portability regardless of target device vendor, (ii)
operating matricially element by element.
• Variables required for algorithm proper operation already pre-declared, and PN struc-
ture matrix representation not declared, for its direct transfer by a parser application.
Note that the only task related to PN structure is transferring its matrix representation
(C, m0 ) to PLC data memory. PN interpretation has to be coded, differing upon particular
application.
In compliance with the specifications described above, the algorithm kernel imple-
ments the transition firing and marking updating rules for a general PN, as detailed in
Algorithm 1. Provided with any particular PN pre-incidence and post-incidence matrices
and initial marking vector contained in a PLC data block, this algorithm allows token
movements among places managing, thus allowing transition enabling, firing and result-
ing marking computation. It is coded in ST, in order to operate more simply with matrices
and for an easier portability to other manufacturers’ devices. The program block execution
flow is described below.
Startup
Only once, and before the continuous scan cycle execution, initial marking value is
given to the current one, and incidence matrix is calculated from the pre-incidence and
post-incidence ones.
Enabling
This program block determines transitions enabled by current marking. Marking
vector is compared with each of the columns of the pre-incidence matrix to determine
whether the marking enables the corresponding transition. If so, it is annotated in an array
of enabled transitions. This function will not be executed again until some change has
occurred in current marking, that is, until some transition has already been fired.
Firable
It is checked whether currently enabled transitions are likely to be fired. According to
net interpretation, it evaluates fulfillment of transitions corresponding to firing conditions,
Appl. Sci. 2021, 11, 7161 8 of 18
listing them in an array. Subsequently, it is evaluated whether they are liable to be fired,
reflecting it in another array.
Make a Decision
A rule is implemented in this block. Note that a firable transition does not have to
be fired at that instant. Firing could be postponed. It may not even occur, if the firing of
another transition causes it to become no longer enabled and firable. This property of PNs
is particularly important and interesting in the modeling of sequencing problems: order
control in the sequence of transitions firing. A function is introduced for the decision of
the transition to be fired among the liable to be fired ones, maybe being in conflict. In this
implementation a simple decision rule is applied: the last of those present in the list of
firable is fired. Once the selection has been made, it is possible to update the net marking
after the transition is fired.
Marking Update
This is a calculation operation. As a consequence of the firing of previously selected
transitions among firable ones, new net marking is computed. It implies updating it by
adding to marking vector the incidence matrix column corresponding to the fired transition.
Actions
This block updates actions related with places, as net interpretation determines,
according to achieved new marking.
1 PN design
5 System commissioning
Note that specifications definition, characterizing the desired operation and iden-
tifying key signals, is a previous work to be done, as in any automation project, and
independent of the proposed methodology. It involves project creation in the development
environment, hardware definition, program block addition and transfer of those that are
fixed and application independent, and declaration and initialization of variables (internal
and I/O). Furthermore, if virtual commissioning (VC) of the automated solution is planned,
a process DT must be designed and developed.
Appl. Sci. 2021, 11, 7161 9 of 18
2.2.1. PN Design
Once specifications are defined, the PN structural design is carried out for its sub-
sequent analysis. Interpretation is added, associating conditions and actions with PN
structure transitions and places, respectively.
3. Use Case
This section describes the use case in which the experimentation was carried out. The
required resources are listed, the process to be automated is described, and the implemen-
tations performed are explained.
following five states: waiting to be loaded with a part, being loaded, operating, waiting
to be unloaded, being unloaded. The purpose of R is to load/unload parts to/from the
stations before/after the operations they perform. Being single and shared, it can be in
one of these states: waiting for servicing, or executing one of four movements: M1 station
loading with a part from input; M1 unloading, a part is driven to intermediate buffer; M2
station loading with a part from the buffer; M2 unloading, a part is driven to system output.
It was considered to be an interesting case for a PN-based implementation, due to the
use of a limited-capacity intermediate buffer, and the existence of a shared resource, R, that
executes one movement each time.
Machine 2
Finished parts
→→→
Temporary buffer
Robot
→→→
3.2. Resources
For each of the necessary resources, it is specified what was employed in this particular
use case.
• An industrial system and process emulation tool or DT. It can support the whole
process of development, simulation and validation, connected to the emulated PLC in
a software-in-the-loop (SIL) configuration or to the physical controller in HIL.
For this study Simumatik3D V1.0.3 (S3D) [33] was used for VC, connected to a physical
PLC.
• A PLC programming environment, as well as the device itself and its emulation tool.
Two control devices from different vendors were required, with their respective
development platforms:
– SIMATIC S7-1500 CPU 1512C-1 PN of Siemens, with TIA Portal V15.1 for coding
and PLCSIM Advanced V2.0 SP1 for emulation. The algorithm was available for
these tools.
– CPU NX102-1020 of Omron, with Sysmac Studio 1.25 for coding and emulation.
• A PN modeling and analysis tool.
The above-mentioned PIPE2 tool was used for (i) PN structure edition, (ii) PN liveness
and/or boundedness properties necessary conditions fullfilment verification, and (iii)
PN representative matrices generation.
• In addition, an application was developed for converting the data generated by PIPE2
(HTML format) into a text format suitable for insertion in the variable declaration
modules of various development environments, including TIA Portal and Sysmac
Studio.
p10 p20
t10 t20
p11 p21
t11 t21
H
p12 p22
t12 t22
p13 p23
M
t13 t23
p14 p24
t14 O t24
After PN structural analysis (Section 2.2.2) and matrix generation (Figure 10), struc-
tural data transfer was carried out (Section 2.2.3), previously converted to the syntax of the
target development environment using a proprietary application. Both the characteristic
arrays declaration and their contents were transferred, i.e., there was no need to declare
them in the environment. Other necessary variables were stored in data memory.
The only non-fixed part of the algorithm was coded (Figure 11), the one corresponding
to PN interpretation (Section 2.2.4). For each boolean data that indicated whether or not the
conditions associated with each transition had been met, the result of the corresponding
combination of logical queries was assigned. Outputs status was managed according to
current marking. Note that this coding can be performed in any language supported by
the development environment used.
VC of the automated system (Section 2.2.5) was supported by the DT already described.
The operation validation was carried out by observing, in the DT, PLC controlled signals,
and PN marking evolution in the development environment itself. A test protocol was
followed starting from initial conditions, i.e., temporary buffer provided with three parts:
• Temporary buffer output sensor forced failure, so that no parts were supplied to
M2 . Periodic entry of parts into the process. Buffer filling, with a part waiting to be
unloaded from M1 , and a part at process input.
• Sensor repair. Periodic entry of parts into the process. Full emptying of tempo-
rary buffer.
Appl. Sci. 2021, 11, 7161 14 of 18
• Continuous verification that at most only one command is given to the transport sys-
tem.
Figure 11. PN interpretation coding detail: editing of conditions associated with each transition.
• ST: algorithm that processes both net structure and interpretation, by means of IF
THEN structures in ST.
• LD: algorithm that processes both net structure and interpretation, by means of set
and reset of variables, in LDs.
• SFC: graphic modeling and description method, suitable for GRAFCET and available
in TIA Portal.
For each of the developments and control devices, scan cycle time measurements were
performed, based on the connection of the physical PLC with the process DT, according to
a HIL setup.
4. Results
The purpose of carrying out several implementations on controllers from different
vendors was, on the one hand, to compare the scan cycle time required by each approach,
and on the other hand, testing algorithm portability between development platforms. This
section presents the results obtained.
• Fixed program blocks, independent of both PLC and PN, manage net marking evo-
lution, using the above-mentioned data structure. This is why the approach can be
considered semi-compiled. ST programming makes this part standard.
• Consequently, the workload in terms of coding is reduced to associating conditions
to transitions and actions to places, i.e., PN interpretation. It can be added in any
language supported by the corresponding environment.
These points are based on the results achieved and already shown in Section 4:
• The scan cycle time required by the algorithm does not differ from that of similar, fully
interpreted approaches, i.e., with both net marking and net interpretation management
integrated into the code itself.
• The portability to the desired PLC is simple and not time-consuming. As far as the
code is concerned, fixed program block contents are directly transferred. Regarding
variables in data memory, it is necessary to declare and initialize those required for the
algorithm to work. Arrays representing net structure are generated in a PN design and
simulation software, and adapted by a proprietary parser software to PLC particular
variables definition and initialization.
It is considered that the contribution of the work presented in this paper may have
these implications:
• PN-based automated industrial systems are implemented in a simple way, with a
reduced programming workload and, consequently, fewer potential errors in the
control device code. The inclusion or modification of a PN is simple, requiring editing,
validation, and transfer of new data into the PLC.
• The way to manage several nets (associated with respective subsystems) is to do it as
a single one (as a single system), in which some arcs are considered to be zero weight.
User-defined data types can be used if larger matrices are required than the limit set
by the development tool.
• For technicians used to working with GRAFCET, the paradigm shift to a PN-based
approach is not a handicap. Similar modeling skills are needed.
Note that the system under study has made it possible to test the algorithm by
applying it in a case present in literature as suitable for the application of PNs. Shared
resources and multiple marking make it more demanding for a GRAFCET-based approach.
The existence of tokens (N = {0, 1, 2, ...}) in a place in a PN versus states (bool) of steps
in a GRAFCET, gives the former the capability to integrate the system’s characterization
into the model’s structure, against further coding work in the latter. The use of a DT of
the process has allowed the testing of the algorithm without availability of the necessary
equipment, reproducing scenes difficult to assemble or reproduce.
The following may be some limitations of the approach presented:
• Net interpretation programming, the only part to be properly coded, is done according
to the user’s choice; no user-friendly or visual tool is available, yet.
• The algorithm involves an entire project in the development environment, is not
parameterizable. Its portability between platforms can be simplified.
• PNs have as elements of their matrix representation natural numbers, often 0 or 1 for
ordinary PNs. Extending this approach to high-level PNs, such as Predicate/Transition
nets (PrT-nets) [34] or Coloured Petri nets (CPNs) [35], in which information is at-
tached to each token, would increase the description capacity (making the validation
process more difficult) and lead to significantly smaller nets.
Finally, and aligned with the above, the following future research directions have been
identified:
• Further reduction of the coding workload by making it more systematic to add
interpretation to the PN may be an interesting challenge. There is significant research
on automatic code generation.
Appl. Sci. 2021, 11, 7161 17 of 18
• The parameterization of a program block encapsulating the algorithm can make its
handling an easier task. It can also be interesting to implement the model in MatLab,
Java, and/or ADA, in order to compare both the cost of development and their
performance.
• A high-level PN-based approach, applying object oriented techniques and/or featur-
ing attributes to tokens, places (actions) and transitions (conditions), reducing the size
of the model. User defined data types (UDTs) could help in PLC programming.
• Distributed PNs, in which the management of a master net is used to operate over the
others, through open communication protocols such as OPC UA or message queue
telemetry transport (MQTT).
Author Contributions: Conceptualization, J.C.M.; methodology, I.A., J.C.M. and L.E.; software, I.A.,
M.A. and J.C.M.; validation, I.A. and J.C.M.; investigation, J.C.M.; writing—original draft prepara-
tion, I.A.; writing—review and editing, I.A., M.A., J.C.M and L.E.; visualization, I.A.; supervision,
L.E.; project administration, L.E. All authors have read and agreed to the published version of
the manuscript.
Funding: This research received no external funding.
Institutional Review Board Statement: Not applicable.
Informed Consent Statement: Not applicable.
Acknowledgments: We thank Jatsu Lizarribar, member of the Department of Basic Sciences at
Mondragon Unibertsitatea, for his support in algebra topics.
Conflicts of Interest: The authors declare no conflict of interest.
Abbreviations
The following abbreviations are used in this manuscript:
References
1. Ben-Naoum, L.; Boel, R.; Bongaerts, L.; De Schutter, B.; Peng, Y.; Valckenaers, P.; Vandewalle, J.; Wertz, V. Methodologies in
discrete event dynamic systems. J. A 1995, 36, 3–14.
2. Sobh, T. Discrete Event Dynamic Systems: An Overview; Technical Reports; CIS: 1991. Available online: https://repository.upenn.
edu/cis_reports/388/ (accessed on 24 January 2020).
3. Murata, T. Petri nets: Properties, analysis and applications. Proc. IEEE 1989, 77, 541–580.
4. Moreira, M.V.; Basilio, J.C. Bridging the Gap Between Design and Implementation of Discrete-Event Controllers. IEEE Trans.
Autom. Sci. Eng. 2014, 11, 48–65. doi:10.1109/TASE.2013.2281733.
Appl. Sci. 2021, 11, 7161 18 of 18
5. Silva, M.; Teruel, E. Petri nets for the design and operation of manufacturing systems. Eur. J. Control 1997, 3, 182–199.
6. Silva, M.; Teruel, E. A systems theory perspective of discrete event dynamic systems: The Petri net paradigm. In Proceedings
of the CESA’96 IMACS Multiconference: Computational Engineering in Systems Applications, Lille, France, 9–12 July 1996;
pp. 1–12.
7. DiCesare, F.; Harhalakis, G.; Proth, J.M.; Silva, M.; Vernadat, F. Practice of Petri Nets in Manufacturing; Springer: London, UK, 1993.
8. Reisig, W. Petri Nets: An Introduction; Springer: Berlin/Heidelberg, Germnay, 1985.
9. Peterson, J.L. Petri Net Theory and the Modeling of Systems; Prentice Hall PTR: Hoboken, NJ, USA, 1981.
10. David, R. Petri Nets and Grafcet for Specification of Logic Controllers. IFAC Proc. Vol. 1993, 26, 683–688. doi:10.1016/S1474-
6670(17)49215-9.
11. Michel, G. Programmable Logic Controllers: Architecture and Applications; John Wiley & Sons, Inc.: Hoboken, NJ, USA, 1990.
12. David, R.; Alla, H. Petri Nets and Grafcet: Tools for Modelling Discrete Event Systems; Prentice-Hall, Inc.: Hoboken, NJ, USA, 1992.
13. Cansever, G.; Kucukdemiral, I.B. A new approach to supervisor design with sequential control Petri-net using minimization
technique for discrete event system. Int. J. Adv. Manuf. Technol. 2005, 29, 1267–1277. doi:10.1007/s00170-005-0010-4.
14. Stanton, M.; Arnold, W.; Buck, A. Modelling and Control of Manufacturing Systems Using Petri Nets. IFAC Proc. Vol. 1996,
29, 4754–4759. doi:10.1016/s1474-6670(17)58432-3.
15. David, R.; Alla, H.L. Du Grafcet Aux réseaux de Petri; Hermes: London, UK, 1992.
16. Tzafestas, S.G.; Pantelelis, M.G.; Kostis, D.L. Design and Implementation of a Logic Controller using Petri Nets and Ladder Logic
Diagrams. Syst. Anal. Model. Simul. 2002, 42, 135–167. doi:10.1080/02329290212164.
17. Peng, S.S.; Zhou, M.C. Ladder diagram and Petri-net-based discrete-event control design methods. IEEE Trans. Syst. Man Cybern.
Part C 2004, 34, 523–531. doi:10.1109/TSMCC.2004.829286.
18. Zaytoon, J.; Riera, B. Synthesis and implementation of logic controllers—A review. Annu. Rev. Control 2017, 43.
doi:10.1016/j.arcontrol.2017.03.004.
19. Uzam, M.; Jones, A. Discrete event control system design using automation Petri nets and their ladder diagram implementation.
Int. J. Adv. Manuf. Technol. 1998, 14, 716–728. doi:10.1007/BF01438224.
20. Peng, S.; Zhou, M. Sensor-based stage Petri net modelling of PLC logic programs for discrete-event control design. Int. J. Prod.
Res. 2003, 41, 629–644. doi:10.1080/0020754021000042364.
21. Borges, M.U.; Lima, E.J. Conversion methodologies from Signal Interpreted Petri Nets to Ladder Diagram and C language in
Arduino. Int. J. Mech. Eng. Educ. 2018, 46, 302–314. doi:10.1177/0306419018759921.
22. Lee, G.B.; Zandong, H.; Lee, J.S. Automatic generation of ladder diagram with control Petri Net. J. Intell. Manuf. 2004, 15, 245–252.
doi:10.1023/b:jims.0000018036.84607.37.
23. Frey, G. Automatic implementation of Petri net based control algorithms on PLC. In Proceedings of the 2000 American Control
Conference, ACC (IEEE Cat. No. 00CH36334), Chicago, IL, USA, 28–30 June 2000; Volume 4, pp. 2819–2823.
24. Heiner, M.; Menzel, T. A Petri net semantics for the PLC language Instruction List. In Proceedings of the IEE Workshop on
Discrete Event Systems (WODES’98), Sardinia, Italy, 26–28 August 1998; pp. 161–165.
25. Korotkin, S.; Zaidner, G.; Cohen, B.; Ellenbogen, A.; Arad, M.; Cohen, Y. A Petri Net formal design methodology for discrete-event
control of industrial automated systems. In Proceedings of the 2010 IEEE 26th Convention of Electrical and Electronics Engineers
in Israel, Eilat, Israel, 17–20 November 2010; pp. 431–435.
26. Markiewicz, M.; Gniewek, L. A Program Model of Fuzzy Interpreted Petri Net to Control Discrete Event Systems. Appl. Sci. 2017,
7. doi:10.3390/app7040422.
27. Rezig, S.; Ghorbel, C.; Achour, Z.; Rezg, N. PLC-based implementation of supervisory control for flexible manufacturing systems
using theory of regions. Int. J. Autom. Control 2019, 13, 619. doi:10.1504/ijaac.2019.101911.
28. Aspar, Z.; Shaikh-Husin, N.; Khalil-Hani, M. Algorithm to Convert Signal Interpreted Petri Net Models to Programmable Logic
Controller Ladder Logic Diagram Models. Indones. J. Electr. Eng. Comput. Sci. 2018, 10, 905. doi:10.11591/ijeecs.v10.i3.pp905-916.
29. Petter, J.; Sawicki, S. A Comparison of Petri Net Simulation Tools. In Proceedings of the XXV Seminário de Iniciação Científica,
Ijuí, Brazil, 25–29 September 2017; pp. 26 – 27.
30. Dingle, N.J.; Knottenbelt, W.J.; Suto, T. PIPE2: A Tool for the Performance Evaluation of Generalised Stochastic Petri Nets.
SIGMETRICS Perform. Eval. Rev. 2009, 36, 34–39. doi:10.1145/1530873.1530881.
31. Platform Independent Petri Net Editor 2. Available online: http://pipe2.sourceforge.net/ (accessed on 24 January 2020).
32. Johnstone, M.; Creighton, D.; Nahavandi, S. Enabling industrial scale simulation/emulation models. In Proceedings of the 2007
Winter Simulation Conference, Washington, DC, USA, 9–12 December 2007. doi:10.1109/wsc.2007.4419701.
33. Simumatik3D—Home. Available online: https://simumatik3d.github.io/ (accessed on 10 August 2020).
34. Genrich, H.J.; Lautenbach, K. System modelling with high-level Petri nets. Theor. Comput. Sci. 1981, 13, 109–135.
35. Jensen, K. Coloured Petri Nets: Basic Concepts, Analysis Methods and Practical Use; Springer Science & Business Media:
Berlin/Heidelberg, Germany, 2013; Volume 1.