## 2 Materials and Methods
### Design of the DC-AC Controller
#### 2.3.2 Phase-Locked Loop (PLL)  
The PLL is used for synchronizing and monitoring the grid. The phase angle of the grid voltage must be calculated in real time to define the power ratio between the grid and the inverter [28]. In this investigation, the second-order generalized integrator (SOGI)-based phase-locked loop (PLL) is implemented (Figure 13) because it presents harmonic immunity, a fast tracking accuracy, and a rapid dynamic response [6].  
Figure 13: Second-order generalized integrator (SOGI)-based phase-locked loop (PLL) [19].  
The phase detector is composed of an orthogonal signal generator and obtains the Park transform of the waves to detect the components \(q\) and \(d\) in the rotating reference frame. For the orthogonal signal generator, a second-order generalized integrator (SOGI) [28] is used, as shown in Figure 14. The input signal is the voltage of the power grid. As output signals, two sine waves \(v^{\prime}\) and \(qv^{\prime}\) with a phase shift of \(\pi/2\) are generated. The component \(v^{\prime}\) has the same phase and amplitude as the input voltage signal \(V_{LN}\).  
Using the trapezoidal approximation, the closed-loop discrete transfer functions (\(Hd(z)\) and \(Hq(z)\)) of the SOGI are:  
\[H_{d}(z)=\frac{v^{\prime}}{v}=\frac{b_{0}-b_{0}z^{-2}}{1-a_{1}z^{-1}-a_{2}z^{- 2}}, \tag{50}\]  
\[H_{q}(z)=\frac{qv^{\prime}}{v}=\frac{qb_{0}+2 qb_{0}z^{-1}+qb_{0}z^{-2}}{1-a_{1} z^{-1}-a_{2}z^{-2}}, \tag{51}\]  
where  
\[\begin{split}& b_{0}=\frac{x}{x+y+4},\quad qb_{0}=\frac{ky}{x+y+4}, \\ & a_{1}=\frac{2(4-y)}{x+y+4},\quad a_{2}=\frac{x-y-4}{x+y+4}, \end{split} \tag{52}\]  
where \(x=2k\omega_{n}T_{s}\), \(y=(\omega_{n}T_{s})^{2}\), \(\omega_{n}\) is the estimated grid frequency, \(T_{s}\) is the sampling time, and \(k\) is the gain that affects the SOGI bandwidth [28].  
With the generated orthogonal signal, the Park transform is applied with \(V_{\alpha}=v^{\prime}\) and \(V_{\beta}=qv^{\prime}\) to obtain the components \(Q\) and \(D\) in the rotating reference frame. The Park transform is given by:  
\[\begin{bmatrix}V_{d}\\ V_{q}\end{bmatrix}=\begin{bmatrix}\cos(\theta_{out})&\sin(\theta_{out})\\ -\sin(\theta_{out})&\cos(\theta_{out})\end{bmatrix}\begin{bmatrix}V_{\alpha} \\ V_{\beta}\end{bmatrix}. \tag{53}\]  
Defining an arbitrary input signal, \(V_{\alpha}=V_{in}\cos(\theta_{in})\) and \(V_{\beta}=V_{in}\sin(\theta_{in})\), the output of the phase detector (PD) is:  
\[PD=V_{in}\begin{bmatrix}\cos(\theta_{in}-\theta_{out})\\ \sin(\theta_{in}-\theta_{out})\end{bmatrix}=\begin{bmatrix}V_{d}\\ V_{q}\end{bmatrix}. \tag{54}\]  
Now, assuming that the PLL is synchronized in such a way that \(\theta_{in}-\theta_{out}\approx 0\), then, in \(Vq\), we get \(\sin(\theta_{in}-\theta{out})\approx\theta_{in}-\theta{out}\), which is the tracking angle error [19].  
Using control theory and using a PI controller as a low-pass filter to keep the error at zero, the closed-loop transfer function of the PLL [28] is given by:  
Figure 14: Diagram of the second-order generalized integrator (SOGI).  
\[\frac{\theta_{out}(s)}{\theta_{in}(s)}=\frac{k_{p}s+\frac{k_{p}}{T_{i}}}{s^{2}+k_{ p}s+\frac{k_{p}}{T_{i}}}. \tag{55}\]  
Now, comparing Equation (55) with the transfer function of a second-order system and defining a settling time of 30 ms, an error band of 5 %, and a damping coefficient of 0.7, we obtain the constants \(k_{p}=166.6\) and \(k_{i}=27\),755.55.  
Finally, using the bilinear transformation, the pulse transfer function of the low-pass filter is given by:  
\[\frac{y_{LP}(z)}{PD(z)}=\frac{b_{0}+b_{1}z^{-1}}{1-z^{-1}}, \tag{56}\]  
where,  
\[b_{0}=\frac{2k_{p}+k_{i}T_{s}}{2}=166.878, \tag{57}\]  
\[b_{1}=\frac{2k_{p}-k_{i}T_{s}}{2}=-166.322. \tag{58}\]