# Anomaly Detection for Hydraulic Power Units--A Case Study
## 2 Materials and Methods
### Data Acquisition and Communication
#### 2.1.1 Hardware and Software Architecture  
All available measurements were aggregated in Siemens SIMATIC S7-400 [29] Programmable Logic Controller (PLC) acting as Modbus Slave. From proper input registers of the Modbus map, the following measured variables were available:  
* Pressure behind filter, bar;
* Engine oil pressure, bar;
* Fuel level, %;
* Water level in the tank, %;
* Fuel consumption, L;
* Engine coolant temperature, \({}^{\circ}\)C;
* Water temperature in the tank, \({}^{\circ}\)C;
* Oil temperature, \({}^{\circ}\)C;
* Power, W;* Rotation speed, \(\frac{1}{min}\);
* Oil flow, \(\frac{L}{min}\).  
There are more variables available in PLC; however, they are diagnostics and binary quantities such as the presence of a sensor. Eventually, they are control-related variables such as set points. The SIMATIC S7-400 PLC unit can be considered relatively obsolete in comparison with, for example, S7-1200/1500, also manufactured by SIEMENS [30]. However, for practical and business reasons, there is no need to replace the PLC. Required functionalities such as the acquisition of signals from sensors and sharing measurements using the MODBUS protocol are ensured by the SIMATIC S7-400 unit. The replacement of the PLC would generate additional costs, require time for the PLC programmer, and also require taking HPU from the client for a while.  
It would be possible to fulfill project requirements such as sending data to the cloud, and the use of anomaly detection algorithms using modern industrial software solutions, even compatible with SIEMENS, such as MindSphere [31]. However, using custom software implemented on the edge computer and in the cloud, rather than the use of out-of-the-box solutions, gives more flexibility in the context of further software and analytics development.  
Software written in Python 3.8 was implemented to acquire data on the edge computer MOXA UC8100 [32] and send it to the cloud. Therefore, two functionalities were written:  
1. Reading data from Modbus (program acting as Modbus Master);
2. Sending data to the cloud in a proper format.  
Thanks to software written on PLC, measurements were available through only one protocol--Modbus TCP. Only one program for data acquisition was necessary. The implementation of \(1\) involved the PyModbus Python package on BSD license [33]. Sending data to the cloud was performed using mechanisms available direct out-of-the-box from Python SDK for Azure IoT Edge [34] in the same program as Modbus Master.  
The program fulfilling functionalities from points \(1\) and \(2\) has about 200 lines of code in total. The Pseudocode of the program is presented in Algorithm 1. Implementation in the development environment was performed as any other Python script. Deploying the solution into production is a bit more complex. Therefore, a separate paragraph (Section 2.1.2) for describing this process with the whole ecosystem around it is needed.  
```
0:\(N=11\)\(\triangleright\) Number of measurement variables \(t\gets 1\)\(\triangleright\) Time in seconds \(X_{t}\gets x_{1},x_{2},\ldots x_{N}\)\(\triangleright\) Vector of current measurement variables \(X_{t-1}\leftarrow\mathbf{0}\in R^{N}\)\(\triangleright\) Vector of previously read measurement variables while True do if Connection with PLC is established then  Read measurement variables from PLC and assign it to \(X_{t}\). if Value of any corresponding variable differs in \(X_{t}\) and \(X_{t-1}\)then  Send values of corresponding variables to the cloud. end if \(X_{t-1}\gets X_{t}\) else  Try to establish connection with PLC end if  Wait a second. \(t\gets t+1\) endwhile
```  
**Algorithm 1** Program for reading measurements from PLC and sending them to the cloud  
Figure 2 schematically shows the high-level solution architecture described above.