## 3 System Design
### PID Control Algorithm  
Due to the fact that the actual spray operation is affected by many factors, the system has a certain delay from receiving the flow change signal to adjusting the flow to the target value. Therefore, the actual flow will fluctuate around the target flow. The PID control algorithm is based on a control law of the system error. The algorithm is an optimal control adjusted by proportion (P), integration (I), and differentiation (D). It has the characteristics of a simple principle, high control precision, easy implementation and strong practicability [22]. The PID control algorithm has obvious effects on the process of the dynamic system calibration of continuous systems. In order to accurately control the  
Figure 10: Program flow chart.  
flow of the system and ensure the accuracy and stability of the variable spray operation, the PID control algorithm is used to achieve the closed-loop control of the system [23]. The equation of PID control is as follows:  
\[\begin{array}{ll}u(k)&=K_{P}^{e(k)}+\frac{TK_{P}}{T_{I}}\sum\limits_{i=0}^{k}e (i)+K_{P}T_{D}\frac{e(k)-e(k-1)}{I}\\ &=K_{P}^{e(k)}+K_{I}\sum\limits_{i=0}^{k}e(i)+K_{D}^{[e(k)-e(k-1)]}\end{array} \tag{4}\]  
where \(K_{P}\) is the proportional gain; \(K_{I}\) = \(TK_{P}/T_{I}\) is the integral time constant; \(K_{D}\) = \(K_{P}T_{D}\) is the differential time constant; \(u(k)\) is the output of the control system at sampling time \(k\); \(e(k)\) is the system output deviation from the input quantity at sampling time \(k\), and is described as \(e(k)\) = \(y(k)\) - \(r(k)\), \(y(k)\) is the output feedback value, and \(r(k)\) is the reference input value.  
The flow control process of the miniature diaphragm pump has the characteristics of large inertia lag and time variance. The process of adjusting the variable spray of the micro-diaphrag pump can be described as a second-order pure lag system. The transfer function is as follows:  
\[G(s)=\frac{C(s)}{R(s)}=\frac{Ke^{-\tau s}}{(T_{1}s+1)(T_{2}s+1)} \tag{5}\]  
where \(K\) is the amplification factor; \(\tau\) is the pure lag time in second; \(T_{1}\) and \(T_{2}\) are time coefficients.  
In the process of adjusting the flow rate of the diaphragm pump, \(K\), \(\tau\), \(T_{1}\) and \(T_{2}\) are closely related to the performance of the system components and environmental factors [24]. The numerical value of the parameters was obtained through simulation. Therefore, the value of the amplification factor \(K\) is 1, the value of the pure lag time \(\tau\) is 0.25, and the values of the time constants \(T_{1}\) and \(T_{2}\) are 0.05 and 1.849, respectively. The variable spray system PID control model was created by the simulink component of MATLAB software (MathWorks, USA), as shown in Figure 11. According to the simulation model, when the given signal is a unit step signal, the PID simulation coefficient is debugged by the optimized parameter design method to obtain the optimal control effect. In actual use, the algorithm needs to be run through the STM32 chip. After repeated simulation and experiments, the values of \(K_{P}\), \(K_{I}\), and \(K_{D}\) are set to 6, 0.8, and 0.5, respectively. When the input is the unit \"1\", the response curve obtained from the step response output of the system is shown in Figure 12.  
Figure 11: PID regulation simulation model.  
According to the step response curve of the system in Figure 12, the target flow is set to \"1\". When the PID algorithm is not added, the system receives the change signal of the flow and has a certain delay in response. About 300 ms of time is needed to change the flow to the target value; moreover, after reaching the target value, the actual flow value fluctuates around the target value, which has a certain deviation from the target flow. However, after the PID control, the response time is shortened to about 15 ms. At the same time, the actual flow reaches the target flow value and then stabilizes in the vicinity of the target value. To a certain extent, the system response speed and stability are improved.