New Software Tool for Modeling and Control of Discrete-Event and Hybrid Systems Using Timed Interpreted Petri Nets

: For the development of modern complex production processes in Industry 4.0, it is appropriate to effectively use advanced mathematical models based on Petri nets. Due to their versatility in modeling discrete-event systems, Petri nets are an important support in creating new platforms for digitized production systems. The main aim of the proposed article is to design a new software tool for modeling and control of discrete-event and hybrid systems using Arduino and similar microcontrollers. To accomplish these tasks, a new tool called PN2ARDUINO based on Petri nets is proposed able to communicate with the microcontroller. Communication with the microcontroller is based on the modiﬁed Firmata protocol hence, the control algorithm can be implemented on all microcontrollers that support this type of protocol. The developed software tool was successfully veriﬁed in control of laboratory systems. In addition, it can be used for education and research purposes as it offers a graphical environment for designing control algorithms for hybrid and mainly discrete-event systems. The proposed software tool can improve education and practice in cyber-physical systems (Industry 4.0).


Introduction
Development of cyber-physical systems is a complex discipline that includes many activities, e.g., system design, specification of required properties, implementation, testing and further development of the system [1]. These operations are important for the final product, therefore it is necessary to create a model of the system first [2,3]. Development of control methods for discrete-event and hybrid systems belongs to modern trends in automation and mechatronics. Hybrid systems in manufacturing processes combine time-driven and event-driven in automotive industry. Control of such systems is challenging due to the necessity to apply control methods both for discrete-event systems (where the Petri nets formalism can be helpful) and standard continuous-time control methods (e.g., PID (proportional-integral-derivative) algorithms) [4]. With an appropriate software development methodology, and software and hardware modules, these control methods can be synergistically combined to yield a proper and unique control system that allows joining discrete-event and continuous-time control methods for an effective hybrid systems control. This is useful in systems which require using different control algorithms depending on the state of the system-for example in Java. It is released under an open-source license. Its main advantage is well-structured code, clear design and direct support from developers. It also contains the possibility of determining the boundedness of the Petri net and it can inspect P (place) invariants and T (transition) invariants. The disadvantage is that it supports only basic Petri nets. As part of the solution of our research, it was necessary to add a support for timed Petri nets interpreted for control.
The developed extension of this tool is called PN2ARDUINO and was fully tested in [24,25]. The main aim of the proposed article is to present the developed software for control of discrete-event and hybrid systems verified on laboratory discrete-event and hybrid systems.
There are several Petri net-based control concepts. Petri net as a control logic must be connected with the controlled system (e.g., using a microcontroller). One of the main aspects of the control system design is the question whether the Petri net's logic should be stored in the microcontroller or in the personal computer (PC) able to communicate with the microcontroller. Both approaches have their advantages and disadvantages.
If the Petri net's logic is stored in the microcontroller, the main advantage is the control unit independence from the software application (program on a PC). The Petri net logic is modeled using a PC, and then the Petri net is translated into a program code which is loaded into the microcontroller. Afterwards, the PC and the microcontroller can be disconnected. Another advantage is the capability of real-time control. Disadvantages include limited computational and memory resources of the microcontroller, need for repeated program compiling and its uploading into the microcontroller (mainly during the development phase). The proposed solution is shown in Figure 1. When the Petri net's control logic is stored on a PC in a specialized SW application, it is possible to control the system directly. In the microcontroller, only the program with communication protocol is stored. This communication protocol (in our case Firmata [26]) is used for communication between the PC and the microcontroller. This solution eliminates the necessity of recompiling and reuploading the program during the development. The next advantage is the elimination of restrictions on computing and storage resources because a PC has almost unlimited resources compared with a microcontroller. One of the disadvantages is that the control system cannot respond in real time. The proposed solution is shown in Figure 2. These differences are specified in Table 1.  New software module PN2ARDUINO is based on the second approach ( Figure 2) so the Petri net runs on a personal computer. For communication between SW application and microcontroller, the Firmata protocol [26] has been used. Firmata is a protocol designed for communication between a microcontroller and a computer (or a mobile device like a smartphone, tablet, etc.). It is based on MIDI messages [27]. MIDI (musical instrument digital interface) message is made up of 8-bit status byte which is generally followed by 1 or 2 data bytes. Firmata protocol can be implemented in firmware of various microcontrollers; mostly Arduino-family microcontrollers are used. On the PC, a client library is needed. These libraries are available for many languages like Java, Python, .NET, PHP, etc.
On the Arduino side, the Standard Firmata 2.3.2 version is used, the client application on the PC is based on Firmata4j 2.3.3 library which is programmed in Java. The advantage of using Firmata consists of the possibility of using another microcontroller compatible with Firmata.
PN2ARDUINO extends PNEditor with many features. For Petri nets modeling, there is a possibility of adding time delays to transitions, and capacity for places. Also, automatic mode of transitions firing was added for automatic control purposes as only manual mode is available in PNEditor.
In PN2ARDUINO, a new module is added to PNEditor to enable communication with the compatible microcontroller. This module consists of two parts. The first part establishes connection with the microcontroller by setting the COM port where the microcontroller is connected. The second part provides a capability of adding Arduino components to Petri net places and transitions. The following types of Arduino components are supported: digital input and output, analog input, servo control, PWM (pulse-width modulation) output, message sending, and custom SYSEX message [26] sending. Originally, SYSEX (System Exclusive) messages are model-specific messages for setting various parameters of a MIDI device. The idea for SYSEX is to have a second command space using the first byte after the SYSEX Start byte. The key difference is that the data can be of any size, rather than just 1 or 2 bytes for standard MIDI messages.
The use-case diagram of the developed SW tool is depicted in Figure 3, and the class diagram is shown in Figure 4.
As stated, transitions and places can be associated with Arduino components. Digital and analog inputs serve as enabling conditions for Petri net transitions. Digital and PWM outputs and messages are used as executors of the respective actions.
The interesting functionality is the capability of sending custom SYSEX messages. The user must enter a SYSEX command (0x00-0x0F) and optionally also the content of the message. The message is sent when the token comes to the place or when the transition is fired. SYSEX messages have been used e.g., in the proposed hybrid control example in the last section of this article. Here, the SYSEX message notifies the microcontroller that a different PID control algorithm is to be used. Then the PID algorithm is switched, and the controlled system remains stable.
A main window of PN2ARDUINO consists of a quick menu, main menu, canvas for Petri net modeling and log console. PN2ARDUINO supports two modes-a design mode and a control mode, the control mode can be manual or automatic.
First, it is necessary to initialize communication with Arduino (Setup board in the menu). Then it is possible to add Arduino component to the place or to the transition ( Figure 5). The example of analog input is shown in Figure 6.
Time policy is also supported-it is possible to add time delay to the transitions which can be deterministic or stochastic. Using a deterministic time policy, the delay is entered by user during Petri net modeling as a specific integer that represents the duration of the delay. After the specified delay, the transition is fired. The stochastic time policy is based on entering two time frames. The first represents the shortest possible delay and the second represents the longest possible delay. These time frames are also entered directly by user during modeling process. The specific value of the delay is determined randomly in the range of this entered interval. During enabling a transition that has a defined delay, tokens from all entry places are consumed first. Subsequently, a timer is started. After the specified delay, tokens are generated to all exit places.

Case Study: Control of Laboratory Discrete-Event System
For verification of the developed software tool and discrete-event systems control method it was necessary to design a laboratory model of such a system. A fire alarm model was built. The scheme can be seen in Figure 7.
The fire alarm model consists of an active buzzer, photoresistor, three resistors and an NPN transistor. The NPN transistor is mandatory for active buzzer connection. The LED of Arduino in pin 13 is also used. A photoresistor was used instead of a smoke sensor to simplify the experiment. Next, the behavior of the system must be defined. When the photoresistor detects an excessive lighting (experimentally determined as an input value greater than 799 on the analog pin of Arduino Uno which resolution is from 0 to 1023) the intermittent tone of the buzzer is turned on. This tone alternates with LED lighting. When the value on the analog pin drops below 800, the sound and light effects stop. This is repeated cyclically.
Initial marking of modeled timed Petri Net interpreted for control (or sometimes known as timed interpreted Petri net) in PN2ARDUINO is shown in Figure 8.  Places of the Petri net (Figures 8-10) correspond to the following states: • p 1 -alarm does not detect fire • p 2 and p 3 -alarm is active (fire was detected) Transitions of Petri net (Figures 8-10) correspond with the following actions/events: • t 1 -alarm is turned on • t 2 -alarm makes a noise • t 3 -signal light blinks • t 4 and t 5 -alarm is turned off The token in place p 1 corresponds to the state when the fire alarm is not activated because the photoresistor has not detected the light intensity threshold.  When a value greater than 799 is detected on the analog pin of Arduino, the transition t 1 is fired. This transition is associated with Arduino component Analog Input where the range of input values is set. The transition is enabled depending on this range. p1 -alarm does not detect fire p2 -fire alarm is active p3 -fire alarm is active t1 -alarm is turned on t5 -alarm is turned off t4 -alarm is turned off t2 -alarm makes a noise t3 -signal light blinks Figure 10. Petri Net for fire alarm (t 2 is fired).
In Figure 9, the token is in the place p 2 . Transition t 2 is associated with Arduino component Digital Output (pin 8 in this case) where the buzzer is connected. This transition has also associated the function of a time delay (2 s) which means that the transition firing (and buzzer sound effect) lasts for 2 s.
In Figure 10, the token is in the place p 3 ( Figure 10). Transition t 3 is associated with Arduino component Digital Output (pin 13 in this case) where the build-in LED is connected. The time delay is set to 1 s, i.e., the LED diode is turned on for 1 s.
This process is repeated cyclically, and stops when the value on the analog pin drops under 800. Then the transition t 4 or t 5 is fired and the token moves to the place p 1 when the fire alarm does not detect the fire.
The reachability graph for described Petri net is shown in Figure 11. We can conclude that the ability of discrete-event control with PN2ARDUINO was successfully verified and can be generalized for other applications.

Case Study: Control of Laboratory Hybrid System
To verify the proposed software tool for hybrid systems control it was necessary to find an appropriate laboratory model. A DC motor with encoder was chosen; its parameters are in Table 2. An incremental encoder is used to measure speed for the feedback. The measured speed of the DC is the process value.
The DC motor was connected to Arduino Uno using a motor shield module based on dual full bridge driver L298. Using the motor shield, it is possible to independently control speed and direction of rotation of the DC motor. For speed measurement the hardware interruptions functionality of Arduino Uno has been used.
The speed of the motor is set by a pin denoted "PWM A". When the input is set to "PWM = 255" the Arduino program shows 186 rpm which approximately corresponds with parameters as stated by the manufacturer. The next step was measurement of the steady state I/O characteristics. The input (armature) voltage ranges from 0 V to 5 V which corresponds to a PWM signal from 0 to 255 (8-bit resolution), the sampling period was chosen-0.05 s.   To be able to use linear dynamic models, the working points had to be chosen from linear parts of the I/O characteristics Two working points have been chosen (u P 1 ,y P 1 ) and (u P 2 ,y P 2 ) where: Since the controller has been designed for a real system (fast dynamics, large noise, uncertainties), the controller parameters were tuned using practice-oriented design methods. The designed PID controller was implemented using the Arduino PID Library- [28]. Creation of a PID class object has the following syntax: PID (&Input, &Output, &Setpoint, Kp, Ki, Kd, Direction) Either DIRECT or REVERSE-determines which direction the output will move when faced with a given error.
The closed-loop scheme is in Figure 13. The monotype font was used for variables names in the Arduino program. The Setpoint is the desired speed (rpm). Due to the used data type in the Arduino program (long), multiples of ten of the setpoint are used. Using an extra order enables us to deal with an equivalent of a number with one decimal place. Output is the control variable ranging between 0 and 255 (8 bits) corresponding to the input voltage between 0 V and 5 V (PWM 0-255). Experiments showed that designing an effective PID controller for higher speeds (2nd working point: ω = 176 rpm) is not complicated. A satisfactory control performance can be achieved by tuning individual PID controller parameters.
However, it was not so easy to design an effective controller in the 1st working point (ω = 140 rpm); mostly, the closed-loop system was not stable. Hence, lower P and I values had to be used. It was expected that this controller will be effective also in the 2nd working point however with a worse performance (too long settling time). To switch between different control algorithms in individual working points, the proposed software for control of hybrid systems using Petri Nets can be used.
For the 2nd working point, a PID controller with parameter values P = 0.83; I = 5; D = 0.005 was designed. The closed-loop step response is shown in Figure 14 whereby a PWM step from 176 to 186 was realized in t = 5 s. The settling time is 1.1 s (considering a tolerance ±2 rpm). This controller was tested also in the 1st working point (for a step from 140 rpm to 146 rpm). It is obvious from the corresponding step response in Figure 15 that this controller does not provide a satisfactory performance. Step response (2nd working point): P=0.83; I=5; D=0.005 Step response (1st working point): P=0.83; I=5; D=0.005 Hence, for the 1st working point a different PID controller was designed with P = 0.0001; I = 1; D = 0.01. The closed-loop step response in Figure 16 shows that the controller works properly (the settling time is 1.3 s). When applied in the 2nd working point, this controller again did not work properly, as expected (larger settling time achieved with a PID controller with smaller P and I).
The achieved performance evaluated from the step response in Figure 17   Step response (1st working point): P=0.0001; I=1; D=0.01 Step response (2nd working point): P=0.0001; I=1; D=0.01 The analysis of achieved results revealed necessity to use different controllers in individual working points, or to design the controller using some advanced control design approach (robust, gain scheduled, switched). In case of switching between multiple controllers according to the working point it is possible to use the developed software module PN2ARDUINO. Switching between controllers and setpoints is based on SYSEX messages. Arduino and other microcontrollers that support Firmata protocol can be used. Development and verification of this software module are the most interesting results of the presented research.
A demonstration example of the proposed control method is in Figure 18. Consider the above-mentioned DC motor which must operate in two modes (working points). For effective setpoint tracking by the DC motor speed, controllers with different parameters must be used (a different controller for each mode). Switching between individual working points is carried out using a potentiometer connected to the analog input of the Arduino Uno microcontroller. Switching between controllers is provided by transitions switch1 and switch2 of Petri net according to the input value from the potentiometer. Input from the analog pin in Arduino is represented by a value ranging between 0 and 1023. The mean value (512) was used as a threshold. In the moment when the token in Petri net is moved to the places setpoint1 or setpoint2, a SYSEX message is sent. This message ensures the execution of a user-defined program code on the Arduino side, in this case the control algorithm. The (PID) algorithm for continuous-time control is independent of Firmata messaging, so it provides real-time control. The provided hybrid systems control case study is a basic example. Researchers in hybrid control design can use it for different and even more complicated scenarios.  As already stated, hybrid systems for Industry 4.0 manufacturing processes are a combination of time-driven and event-driven dynamics. Cyber-physical characteristics of production parts that undergo changes at various operation situations are described by time-driven control models, while the hybrid timing control of operations is described by event-driven models. We propose that manufacturing parts are characterized by cyber-physical states (e.g., velocity, position, pressure, temperature, etc.) subject to time-driven dynamics, and by temporal states (e.g., operation start and stop times) subject to event-driven dynamics. A hybrid system model for control of manufacturing processes represents the behavior of dynamical systems where the states can evolve continuously as well as instantaneously. Such systems arise when control algorithms that involve digital smart devices are applied to continuous-time systems, or due to the intrinsic dynamics (e.g., mechatronic systems with bumps, electrical and mechanical devices for switching control). Hybrid control may be used to improve performance and robustness compared to conventional control (PID), and hybrid dynamics may be unavoidable due to the interplay between digital and analog components of a complex manufacturing system.
An important class of discrete-event systems in practice are automated storage and retrieval systems (AS/RS). There is a big demand for new modeling and control methods of these systems in automotive industry in Slovakia. AS/RS are an important part of logistics. Presently, economic activity across continents is gaining unprecedented dimensions. The consumer lifestyle of most of the population requires production of large quantities of goods for a short time. The advantage of AS/RS consists especially in saving the human capital, and higher reliability. There is a big demand for new modeling and control methods of these systems in automotive industry in Slovakia (Volkswagen, Jaguar Land Rover, Peugeot-Citroen, KIA). The research about AS/RS, their examples and modeling of such systems using Petri nets can be found in [13,14].
The Smart Industry concept is a national initiative in Slovakia to transform and strengthen the industry using the Industry 4.0 methodology based on the latest research activities at universities and in companies mainly in automotive industry. Research and applications in this field are oriented mainly on small and medium-sized enterprises; one of important features is design of their optimal architectures and hybrid control of individual sub processes. An example of reference architecture of a workplace in which production is realized with minimal human intervention is shown in Figure 19.

Conclusions
The article presents an extension of the PNEditor named PN2ARDUINO; able to communicate with microcontrollers supporting the Firmata protocol. The new software tool enables us to control discrete-event and hybrid systems using timed interpreted Petri nets thus supporting the control paradigm when in the microcontroller only the communication protocol is implemented. Petri nets control logic is stored in the computer which communicates with the microcontroller and sends control commands. The main benefit of the developed SW tool is the possibility to control complex discrete-event and hybrid systems exploiting the advantages of Petri nets formalism which can support many challenging scenarios in modern manufacturing systems within Industry 4.0.
Scientific and application contribution of the research can be summarized in the following points: • Development of the software application PN2ARDUINO supporting modeling and control of discrete-event and hybrid systems A new software application named P2ARDUINO based on the open-source Petri nets editor PNEditor was developed to support systems modelling by using timed interpreted Petri nets. Consequently, it enables us to realize control algorithms on Arduino-type microcontrollers as well as on other compatible microcontrollers which support the Firmata protocol used for communication between the computer and the microcontroller. • Verification of the newly developed application software system for control of discrete-event systems on a laboratory model To verify and demonstrate possibilities to control discrete-event systems using the developed application P2ARDUINO, a laboratory fire alarm model was proposed. The Arduino-type microcontroller Arduino Uno was chosen for control purposes. The respective Petri net was modeled in the original software PN2ARDUINO. Finally, control of the laboratory discrete-event system was demonstrated.

• Development and verification of the developed hybrid system control methodology based on high-level Petri net
To verify the developed hybrid system control methodology, it was necessary to set up a laboratory physical model of such type. A DC motor was chosen with an encoder realizing the feedback. Two working points on the steady state I/O characteristics were selected and two respective control algorithms (PID controllers) were designed. The optimal solution was to switch between the two control algorithms depending on the motor speed (working point).
The developed software application PN2ARDUINO showed to be appropriate for this purpose. Switching between individual control algorithms (controllers) and setpoints was realized via SYSEX messages offered by the Firmata protocol. It is the author's original solution which can be considered to be an important scientific and application contribution. Using the Firmata protocol, SYSEX type messages and possibility to implement user code on the microcontroller side enable us to apply any control algorithm realizable by the microcontroller.
Scientific and application contributions as declared in the three above points describe the original modeling and control procedures and solutions for discrete-event and hybrid systems, and can further be modified for next research and practice in Industry 4.0.
The next research will focus on the Petri net-based control methodology with the control logic directly implemented in the microcontroller.

Patents
The system for control of discrete-event and hybrid systems using Petri nets mentioned in the article is protected by utility model number 7982 by Industrial Property Office of the Slovak Republic