A new Matlab coder for generating Structured Text Language from matrix expression for PLC and PAC controllers

This paper presents a new Matlab toolbox for synthesis of Structured Text (ST) code for Programmable Logic Controllers (PLC) and Programmable Automation Controller (PAC). This tool can directly generate IEC 61131-3 Structured Text Language from Matlab script for selected Integrated Development Environments (IDEs). The generated code can be verified and compared with the results obtained with Matlab simulation. After this, generated code can be used in IDEs, compiled and uploaded to a PLC or PAC controller for final verification. This approach leaves all available Matlab toolboxes for programmers use, thus allowing fast and easy synthesis developed algorithms.


Introduction
Nowadays, when industrial processes, as well as systems that controls them, become more and more complex, new challenges emerges. The most obvious one, directly related to the safety and security, but also to cost minimization is the reliability of used software and hardware. Secondly, the time of software implementation and its deployment to production hardware affects the time-to-delivery and total production costs. One of the possible solutions that can overcome at least part of the mentioned issues is automatic synthesis of standardized IEC 61131-3 Structured Text [7] code for Programmable Logic Controllers (PLC) and Programmable Automation Controller (PAC) controllers. Many researchers focuses on the different aspects of this area. Some of them proposes component-based modeling [16], others on the code reusability [1,5], function-block-based modeling [6], generative programming [4] or sequential circuit synthesis [9]. While a modeling can take different forms, all of them leads to a code synthesis. All mentioned approaches have their potential advantages and drawbacks. It is enough to mention code reusability and function-block-based solution. The first one proposes the import/export mechanisms to reuse once designed and written code between platforms. While the idea is quite simple, the realization with numerous hardware platforms may not be so trivial as it is common that some manufacturers treats non-standardised elements on their own (i.e. totally different ST variable definition in IDE, Proficy Machine Edytion IDE, Step7 IDE etc.). Additionally solution does not offer the homogenous, safe verification environment. Leaving IDE in the middle of the binary code generation, allows hardware-dependent optimization (being a part of software delivered by controller manufacturer). The function-block-based is on the other side limited by function-blocks, and complicated programs become also graphically complex (cf. [6]). The last one, however, offers simulation perspectives. Due to the authors knowledge there is in fact a gap in the area of code synthesis for PLC and PAC. Especially, when modern controllers are capable of handling complex arithmetic operations in real-time. This paper proposes a solution for model-based code-synthesis in terms of mathematical modeling. The toolbox described in later sections is capable of Matlab-to-ST code conversion, preserving structure of mathematical components, such as matrices and vectors. Therefore, a model-based controller or filters can be built relatively fast. They can be also easily verified before uploading them into the device. This phenomena can be quite useful when on a PLC or PAC instead of classical industrial control scheme, the modern control would be implemented. Also thanks to the direct use of Matlab it is possible to use such tools as neural networks toolbox, LMIs [3,12], P-matrices [8] and many others (on the high quality level). The recent works in the area of Fault Detection and Isolation (FDI) [21,20,11,15] as well as Fault-Tolerant Control (FTC) clearly show a new direction for the industrial evolution. If one would compare any currently used control approach with any advanced FTC the superiority of the last one becomes more than obvious [18,22,13,17,19,10]. However the main obstacles are -the lack of trust form the industry in new techniques and the lack of tools for simple and fast implementation of advanced modes into PLC/PAC. The paper is organised as follows. Section 2 is devoted to the structure of the framework, as well as its general capabilities. Subsequently Section 3 shows some examples. Finally Section 4 summarizes the paper.

Toolbox structure
The main objective of this section is to present structure of toolbox, functionalities and the generic functions of the framework. The Toolbox for generating ST language was implemented using object-oriented programming techniques in Matlab. The use of this programming techniques allows to implement interface for the toolbox in an efficient way. Also it allows fast expansion by providing a properly built interfaces. The number of parameters that should be passed to the toolbox is as small as possible, providing a high user experience and usability. All of this allows comfortable usage, with only basic knowledge about Matlab and simple extension to any unimplemented ST version. Apart of above the framework consists of two main classes, from which one of them directly implements the conversion interface. The second one is a storage class based on the factory pattern [2]. Therefore, it fulfil two roles, one -stores all components for language conversion and code generation, secondly builds convertible objects. The toolbox has few main functionalities. The first functionality allows to select IDE. To do so user should use the following command: Currently the toolbox supports the following IDE name: (i) 'Cscape' -Cscape IDE for Horner APG PLC, (ii) 'PME' -Proficy Machine Edition IDE for GE PAC controllers, (iii) 'Step7' -Step7 for Siemens Simatic S7 300/400 controllers.
The code generation procedure requires the object ConversionStorage to be made, as one of its methods invokes the language conversion. The toolbox check name of IDE. If user use IDE name that not supported now, the error will return. The numbers for supported IDE will grow. The storage needs to consist of all variables used in the final program, therefore each variable from the workspace needs to be registered in the instance cs cs = ConvertionStorage(IDE); one matrix and one column vector and expression that multiplies two above matrices C = Ab. (2) The above will simply create a C variable in Matlab Workspace. In order to create a Toolbox valid variable for conversion, one simply changes the expression into a set of char The above expression describe Luenberger Observer and it is commonly used in control theory. In (5) u, y, andx are input signal. The codes example (the values for each matrices is omitted) EXAMPLE 3: This example shows how generate code for more advanced Matlab functions. In the following example we want to generate code based on the Jacobian of function: s = jacobian(2xysin(z) + 3xsin(y)cos(z)) The full code for example s = 'jacobian(2*X*Y*sin(Z) + 3*X*sin(Y)*cos(Z), [X, Y, Z])'; sc.addVariable(X); sc.addVariable(Y); sc.addVariable(Z); sc.convert(s) Listing. 5 and Listing. 6 show ST code. From listings, it can be seen that the toolbox allows to generate code for more advanced Matlab functions. EXAMPLE 4: The next example shows code for following dynamic linear discrete-time system where

Concluding Remarks
The paper deals with the emerging and important issue of Structured Text code synthesis for PLC and PAC controllers. Authors proposes a Matlab based, model-based approach to automatic code generation. Thanks to Matlab powerful seamless toolboxes any demanding control task can be solved efficiently and subsequently tested within the encapsulated and safe environment. This procedure saves cost, time and equipment from damage. At the same time final product such as control scenario or observer structure can be converted into ST language and uploaded into the controller of given manufacturer. Siemens, General Electric or Horner makes no difference thanks to a interface-like object-oriented design of a framework. The Matlab toolbox build for this task can be easily scaled to a incoming needs. Furthermore, a future development of the toolbox is planed, e.g.to automatically handle gain scheduling from Linear Parameter-Varying [14] models or membership functions from Takagi-Sugeno [23] models.

Acknowledgments
Authors would like to express their sincere thanks to colleagues from the Institute for valuable hints and motivating discussions. The work was supported by the National Science Centre of Poland under grant: 2013/11/B/ST7/01110