## 3 Flight Controller Design
### Control Allocation  
In this section, the design of the control allocation algorithm, which provides the mapping from the virtual control quantities to the manipulated inputs of the UAV, is presented. In the hover mode, the tilt tri-rotor UAV has five actuators that consist of three rotors and two servos, and the attitude and position are controlled by these five actuators. In order to achieve stable flight control, a control allocator-based optimization algorithm is proposed.  
From Figure 3, there are five outputs \([\omega_{1},\omega_{2},\omega_{3},\alpha_{1},\alpha_{2}]\) to be solved by four inputs \([R,P,Y,T]\) in the control allocator, and the number of unknowns is greater than the number of equations, so it can be treated as an optimization problem. To solve this problem, a cost function which is related to the consumption of power is proposed as follows,  
\[J=\omega_{1}^{4}+\omega_{2}^{4}+\omega_{3}^{4} \tag{50}\]  
Moreover, it can be rewritten as  
\[J=U_{1}^{2}+U_{2}^{2}+U_{3}^{2}+U_{4}^{2}+U_{5}^{2} \tag{51}\]  
where  
\[\begin{cases}U_{1}=\omega_{1}^{2}\cos\alpha_{1}\\ U_{2}=\omega_{1}^{2}\sin\alpha_{1}\\ U_{3}=\omega_{2}^{2}\cos\alpha_{2}\\ U_{4}=\omega_{2}^{2}\sin\alpha_{2}\\ U_{5}=\omega_{3}^{2}\end{cases} \tag{52}\]  
The solving of these nonlinear coupled equations is transformed into a constrained optimization problem.  
\[\begin{split}& min\;\;J=U_{1}^{2}+U_{2}^{2}+U_{3}^{2}+U_{4}^{2}+U_{5}^{2}\\ & s.t\begin{cases}-k_{f}U_{1}r_{1y}+k_{d}U_{2}-k_{f}U_{3}r_{2y}-k_{d}U_{4 }-R=0\\ k_{f}U_{1}r_{1x}+k_{f}U_{3}r_{2x}+k_{f}U_{5}r_{3x}-P=0\\ k_{f}(U_{2}r_{1y}+U_{4}r_{2y})+k_{d}(U_{1}-U_{3}+U_{5})-Y=0\\ k_{f}U_{1}+k_{f}U_{3}+k_{f}U_{5}-T=0\end{cases}\end{split} \tag{53}\]  
Note that \(J\) is a continuously differentiable function. The second-order partial derivative matrix of \(J\) is given as  
\[\
abla^{2}J=2I \tag{54}\]\(\
abla^{2}J\) is a positive definite matrix, so \(J\) is a convex function, and the optimization problem can be solved by using the Lagrangian multiplier method [35]. The Lagrange function is defined as  
\[\begin{split} L=& U_{1}^{2}+U_{2}^{2}+U_{3}^{2}+U_{4}^ {2}+U_{5}^{2}\\ &+\lambda_{1}(-k_{f}U_{1}r_{1y}+k_{d}U_{2}-k_{f}U_{3}r_{2y}-k_{d} U_{4}-R)\\ &+\lambda_{2}(k_{f}U_{1}r_{1x}+k_{f}U_{3}r_{2x}+k_{f}U_{5}r_{3x}-P)\\ &+\lambda_{3}(k_{f}(U_{2}r_{1y}+U_{4}r_{2y})+k_{d}(U_{1}-U_{3}+U_ {5})-Y)\\ &+\lambda_{4}(k_{f}U_{1}+k_{f}U_{3}+k_{f}U_{5}-T)\\ &\
abla L(U)=2U+\Sigma\Lambda=0\end{split} \tag{55}\]  
where \(U=[U_{1},U_{2},U_{3},U_{4},U_{5}]^{T}\) and \(\Lambda=[\lambda_{1},\lambda_{2},\lambda_{3},\lambda_{4}]^{T}\).  
\[\Sigma=\begin{bmatrix}-k_{f}r_{1y}&k_{f}r_{1x}&k_{d}&k_{f}\\ k_{d}&0&k_{f}r_{1y}&0\\ -k_{f}r_{2y}&k_{f}r_{2x}&-k_{d}&k_{f}\\ -k_{d}&0&k_{f}r_{2y}&0\\ 0&k_{f}r_{3x}&k_{d}&k_{f}\end{bmatrix} \tag{56}\]  
The equality constraints in (54) can be rewritten as  
\[\zeta U=C \tag{57}\]  
where \(C=[R,P,Y,T]^{T}\).  
\[\zeta=\begin{bmatrix}-k_{f}r_{1y}&k_{d}&-k_{f}r_{2y}&-k_{d}&0\\ k_{f}r_{1x}&0&k_{f}r_{2x}&0&k_{f}r_{3x}\\ k_{d}&k_{f}r_{1y}&-k_{d}&k_{f}r_{2y}&k_{d}\\ k_{f}&0&k_{f}&0&k_{f}\end{bmatrix} \tag{58}\]  
Because \(\zeta\) is not a square matrix, from (55) and (57), we can obtain  
\[\Lambda=-2(\zeta\Sigma)^{-1}C \tag{59}\]  
\[U=\Sigma(\zeta\Sigma)^{-1}C \tag{60}\]  
Therefore, the values of the actual outputs are given as  
\[\begin{cases}\omega_{1}^{2}=\sqrt{U_{1}^{2}+U_{2}^{2}}\\ \omega_{2}^{2}=\sqrt{U_{3}^{2}+U_{4}^{2}}\\ \omega_{3}^{2}=U_{5}\\ \alpha_{1}=\arctan(\frac{U_{2}}{U_{1}})\\ \alpha_{2}=\arctan(\frac{U_{4}}{U_{3}})\end{cases} \tag{61}\]  
The proposed control allocation algorithm can improve the control accuracy with less power consumption.