# Optimal Control for a Three-Rotor Unmanned Aerial Vehicle in Programmed Flights
## 3 Control and Optimization  
The experimental parameter is the ascent of the tricopter to a height of 0.5 m. The control system diagram created in Simulink program is presented in Figure 4.  
The individual blocks correspond to the tricopter control elements and model its dynamics. The \"reference velocities\" block, together with the PID controller, performs the flight controller's tasks by generating rotational speeds for all motors. The next block generates control signals. These signals are processed by \"linear motion\" and \"rotational motion\" blocks, which include modeled tricopter dynamics. Using a dedicated toolbox and the PID tuner function [21], the initial gains for the PID controller controlling the height of the tricopter were selected. The PID tuner block (Figure 5) allows for optimizing the gains for a given controller during simulation. The authors then proceeded to select gains using optimization methods. The following methods were used: _frinicon_, _frininmax_, _lsqonlin_, and _frinsearch_[22]. _Frinicon_ is a function that finds the minimum of a constrained function using a quasi-Newton algorithm that makes quadratic approximations at each iteration. _Frininmax_ uses the sequential quadratic programming (SQP) method, which solves a sequence of quadratic programming subproblems. _Lsqonlin_ is a function that finds the minimum using the Levenberg-Marquardt algorithm, which makes a linear approximation at a large distance from the minimum and  
Figure 4: Block diagram of tricopter altitude control. Figure 5: View of the PID tuner. a quadratic approximation in the vicinity of the minimum. _Frininsearch_ finds the minimum of an unconstrained function using the Nelder-Mead simplex method, which does not use function derivatives. In this method, constraints were imposed programmatically. The integral of the absolute error criterion (IAE) was used to evaluate the control quality:  
\[J=\int_{-\infty}^{\infty}f(|e(t)|,t)dt, \tag{25}\]  
where:  
\(J\)--calculated error,  
\(e(t)\)--error,  
\(t\)--time. This criterion was chosen based on available literature [23] as the most suitable for a measurable assessment of control quality. To achieve the goal of selecting PID controller parameters, an optimization function was called to search for three gains for the altitude controller. \[Kpz,Kiz\,Kdz=fmintricopter\ dynamic\ function,PID\ starting\ parameters,options, \tag{26}\]  
where:  
\(Kpz,Kiz,Kdz\)--sought altitude PID controller gains,  
_fmin_--optimization function called as minimizing the IAE error,  
_tricopter dynamic function_--modeled dynamics, graphically presented in Figure 4,  
_starting parameters_--initial gains selected using PID Tuner,  
_options_--simulation parameters, responsible for, among other things, the number of iterations. Each of the optimization functions described above--_fmincon_, _fminimax_, _lsqonlin_, and _fminsearch_--was called in sequence using _fmin_. The comparison of the obtained results is presented in tabular form in Section 4.2. Subsequent experiments, in which values for the remaining controllers were selected, were performed analogously to Equation (26), with the difference that gains for all controllers were searched for. In this case, the optimization function takes the form:  
\[ \begin{bmatrix}Kp_{x},Kiz\,Kd_{x}\,Kp_{y},Kiy\,Kdy,Kp_{z},Kiz\,Kd_{z}\,Kp_{\phi},Ki_{\phi}\,Kd_{\phi}\,Kp_{\theta},Ki_{\theta}\,Kd_{\theta}\,Kp_{\psi},Ki_{\psi}\,Kd_{\psi}\ \end{bmatrix} \\ =fmin(tricopter\ dynamic\ function,PID\ starting\ parameters,options). \end{bmatrix} \tag{27}\]  
The structure of the optimization methods allows for searching for the minimum based on a single error criterion. To use the optimization methods, weights were assigned to each value controlled by PID. \[w_{x}+w_{y}+w_{z}+w_{\phi}+w_{\theta}+w_{\psi}=1 \tag{28}\]  
By using weights, it is possible to obtain a single output for the minimization function. The values of individual weights were selected for each experiment by searching the space, taking into account the authors' experience in flight.