## 2 Materials and Methods
### Considered Control System and Control Purpose (Formulation of Optimization Problem)  
The control system of multirotor UAV from Figure 2 considered here is based on cascaded control loops. There is control of angles _roll_ (\(\theta\)) and _pitch_ (\(\phi\)) around the \(x_{b}\) and \(y_{b}\) axes, according to the set (desired) position in the \(x_{e}\) and \(y_{e}\) axes in faster, internal control loops. Their control is performed in slower external loops. The control of \(\theta\) and \(\phi\) angles occurs indirectly in the realization of autonomous flight trajectory expressed using the vector of desired position trajectory \(\underline{p}_{d}=\left(x_{d},y_{d},z_{d}\right)^{T}\) and desired angle of rotation _yaw_ (\(\psi_{d}\)) around the \(z_{e}\) axis. The purpose of the autonomous control is then to ensure the smallest tracking errors \(e(t)\) during the UAV flight, i.e., the difference in the values of the reference signals (desired) and output signals (actual/measured) [41]:  
\[\underline{e}_{p}=\underline{p}_{d}-\underline{p}_{m}, \tag{1}\]  
\[e_{\psi}=\psi_{d}-\psi_{m}, \tag{2}\]  
where the \(m\) index refers to the measured values. Bearing in mind that in UAVs the current tracking error information from (1) and (2) is used as the input of a given fixed-value controllers, in the commonly used proportional-derivative (PD) controller structure or proportional-integral-derivative (PID), it is proposed to use this information (as well as information from the output of a given type of controller with control signal \(u(t)\)) to formulate a measure of the tracking quality during UAV flight, i.e., the cost function/performance index \(J(t)\) (see Figure 3), defined as follows:  
\[J\left(t\right)=\int_{0}^{t_{a}}\left(\alpha\left|e\left(t\right)\right|+ \beta\left|u\left(t\right)\right|\right)dt, \tag{3}\]  
where \(t_{a}\) is the time of gathering information (to calculate new controller gains) in the optimization procedure. By introducing the penalty for excessive energy expenditure (expressed in the cost function through actual values of the control signal \(u(t)\)), it is possible to shape expectations towards transients and the controller's dynamics profile (providing smooth or dynamic flight trajectories). At small values  
Figure 1: The _Bebop 2_ quadrotor (and its coordinate system) during one of the initial experiments with the carrying of payload conducted in _AeroLab_ of _Poznan University of Technology_. of the \(\beta\), the controller works aggressively, using more energy, often at the expense of the appearance of overshoot, which is undesirable in missions and tasks requiring high flight precision. Unconstrained control signal \(u(t)\) is calculated from the controller's equation, which in the case of PID structure it is given by  
\[u(t)=k_{P}e(t)+k_{I}\int_{0}^{t_{h}}e\left(t\right)dt+k_{D}\frac{d}{dt}e(t), \tag{4}\]  
where \(t_{h}\) is a flight time horizon, \(k_{P}\) is the proportional gain, \(k_{I}\) represents the integral gain and \(k_{D}\) the derivative gain, respectively. Gains \(k_{P}\) and \(k_{D}\) are expected to be found using the proposed iterative learning method. **Remark 1**. : _In the article, when there is a reference to the PID controller, it should be remembered that only the \(k_{P}\) and \(k_{D}\) gains are tuned automatically, whereas the value \(k_{I}\) (used to eliminate the steady-state error) is selected in a manual manner. The proposed auto-tuning method can be used to optimize the gains of any type of controller with three (or even more) parameters; however, this will result in a longer tuning time. Therefore, from the application point of view, it is better to use the procedure presented further in the article._  
Recalling (4), this work deals with the search for the controller gains \(k_{P}\) and \(k_{D}\), to minimize the cost function (3). That is, the current controller design procedure can be posed as an optimization problem where the solution to the following problem is sought:  
Figure 3: General block diagram of the control system with optimization.