# Petri Net-Based Semi-Compiled Code Generation for Programmable Logic Controllers
## 1 Introduction
### Related Works and Proposal  
Several works introduce PN implementation methodologies into standard PLC languages and in an interpreted basis, i.e., with structure and marking management integrated into the code itself. Ref. [16] presents a method for generating ladder diagrams (LDs), which are widely used for discrete event control, that enables faster development, debugging, and re-engineering. It is based on PN models that are valid for formal system analysis, as well as their ability to verify liveness and cyclicity. It also addresses the need for building PNs in a simple way, as well as to be able to evaluate their properties and simulate token evolution. Ref. [17] points out the limitation of LD-based implementations in a context in which systems are of increasing complexity, and the need for tools for control, analysis, evaluation, and simulation of systems, presenting PN as an integrated solution and making a survey about approaches for its conversion to LD. [PERSON]. [4] defines a set of rules for implementing LDs and overcomes merely intuitive ad hoc developments. The review by [18] references these PN to LD conversions, among other implementations of logic controllers. In addition, literature shows the use of other PLC programming languages as well. References evolve from the mentioned LDs [19; 20; 21; 22] and/or instruction lists [23; 24] to more recent proposals in standard programming languages [25] or structured text (ST) [26], for portability to different PLCs. In a recent paper, [PERSON]. [27] proposes editing both net structure and its interpretation in a proprietary graphical environment and automatically converting them into PLC code. Existing PN implementations in PLCs present some weaknesses. Thus, they are fully interpreted developments, totally dependent on the application and the programmer. The coding workload is significant and error-prone. Moreover, any modification in net structure or interpretation involves code rewriting/regenerating. In terms of PN validation, it is conventionally performed at the end of development, on the equipment already assembled, on a digital twin (DT) that emulates it, or by forcing input values and observing output behaviors in the PLC development environment itself. This paper proposes a methodology to implement a PN within a PLC in a simple way, making use of the mentioned formal mathematical definition of a PN, that makes possible to automatically validate part of the PLC program, minimizing coding workload and, consequently, error-prone points. Thus, a previously validated PN matrix representation will be directly copied into a PLC device data memory. A fixed program blocks will manage the marking of the PN and the programming workload will be limited to PN interpretation, 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  
Figure 3: SFC-based programming in PLC development environment. 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, respectively. Exclusively the mentioned program section remains to be manually coded and validated.