Self-Tuning PID Controller for Quadcopter using Fuzzy Logic

Tracking has become a necessary feature of a drone. This is due to the demand for drones, especially quadcopters, to be used for activities such as surveillance, monitoring, and filming. It is crucial to ensure the quadcopters perform the tracking with stable flight. Despite the advantages of having VTOL ability and great maneuverability, quadcopters require an effective controller to overcome their under-actuation and instability behavior. Even though a PID controller is commonly used and promising with its simple mechanism, it requires very proper tuning to ensure the stability of the system is not affected. In this paper, a simple Fuzzy algorithm is proposed to be incorporated into a PID controller to form a self-tuning Fuzzy PID controller. The Fuzzy logic controller works as the self-adjuster to the PID parameters. A mathematical model of the DJI Tello quadcopter is derived with position and attitude control loops that are designed to track a variety of trajectories with stable flight. The proposed method uses a simple architecture where the ranges of PID parameters are used as scaling factors for Fuzzy controller outputs. The results of the simulations show the tracking error performance metrics, which are IAE, ISE, and RMSE, are smaller compared to the values of the PID controller. Beyond its impact on quadcopter control, the proposed self-tuning approach holds promise for broader applications in nonlinear systems


Introduction
An unmanned aerial vehicle, or UAV, is a drone that can fly and maintain altitude, allowing it to undertake more cost-effective operations and vital missions without requiring a human operator or jeopardizing human life [1].UAVS are widely addressed as drones, which is an acronym for Dynamic Remotely Operated Navigation Equipment [2].Drones are classified based on a few parameters, such as size, mass, endurance, flight altitude, function, payload, configuration, and flying principle [2] [3].In the configuration class, drones are divided according to the type of wings and rotors.Similarly, both types of drones rely on the aerodynamic action of their wings or rotors to lift upward [2].The focus of this work is on quadcopter UAVs of the multi-rotor type.
Due to their enhanced stability and endurance in several activities, drones, including quadcopters, have piqued the interest of a wide range of military and civilian service industries, such as synthetic aperture radars (SAR), search and rescue, monitoring and tracking, evacuation, medical aids and package delivery, crop dusting, and even filming or photographing [1] [4].In addition, drones can operate effectively in areas of short path length, minimum turning angle, maximum roll, and fast speed thanks to their great mobility, portability, scalability, and flexibility [1][4] [5].Quadcopters have become the most favorable drones for performing a variety of activities, as mentioned, thanks to their advantageous inherent dynamic nature, which provides them with maneuverability in addition to their simple mechanism and ability to perform VTOL and hovering [5].
Despite the appeal they offer, operating quadcopters is a hard task as under-actuation, nonlinearity and instability characterize these aerial devices, which are aggravated by the difficulty in modelling due to multivariate, and hard to control due to the coupled variables [4] [6].Furthermore, tracking trajectory is one of the key functions of the quadcopter, especially when performing activities with limited time to implement measurements and decision-making procedures such as inspection of natural disaster areas and preliminary evaluation analysis.Thus, to efficiently stabilize quadcopters and accommodate a variety of operational conditions, innovative control systems are necessary.
The linear controllers, namely proportional-integral-derivative (PID), linear-quadratic-regulator (LQR), and H∞ have been widely used and effectively implemented in quadcopters.However, these methods possess limitations, with improperly tuned PID prone to instability, the less robustness of LQR leading to an inability to deal with nonlinearities, and the highly complex H∞ requiring crucial parameter adjustment [7].Nonetheless, these methods have been commonly used from the early development of quadcopters until today due to their effectiveness and competence in obtaining stable flight conditions [7].Hence, this work proposes a comprehensive approach by integrating fuzzy logic adaptability into the PID controller structure, yielding a self-tuning PID controller that improves stability and control precision.
In [8][9], a Fuzzy PID controller is proposed to control the attitude movements of a quadcopter and aims to have stable flight with fast response and a robust system.The method successfully improved the performance of attitude movement, but no guarantee of the stability of position movement is discussed in these papers.Therefore, the method is guaranteed to yield a good result for attitude tracking.In the case of position tracking [10], a robust Fuzzy PID controller is designed for the position loop and attitude loop based on Fuzzy adaptation scheme.The proposed method used the Karnik-Mendel algorithm to calculate the switch points of the system in addition to the requirement to decide the appropriate scaling factors that work as adjusters to the system.However, the proposed method could be time-consuming and tedious despite its effectiveness in improving performance.Thus, this work aims to design a self-tuning Fuzzy PID controller based on Fuzzy adaptation but using a simpler algorithm.
The core contribution of this work are as follows: 1. Development of a PID controller designed for attitude and position control loop to stabilize and track diverse trajectory shapes.
2. Implementation of an automated PID gain tuning process via Fuzzy logic to improve system performance.
3. Comprehensive performance comparison between the conventional PID approach and the proposed Fuzzy PID method involving analyses of transient response and tracking accuracy.
The subsequent sections of this paper are organized as follows: Section 2 presents the derivation of the quadcopters dynamic model in the 'x' cross configuration.Section 3 describes the design of both PID and Fuzzy PID controllers for the altitude, attitude, and position tracking.Section 4 provides an in-depth analysis of simulation results, comparing the performances of the PID and Fuzzy PID controllers.Finally, Section 5 concludes the paper, summarizing the key findings and contributions.

Quadcopter Modelling
A quadcopter is a rotary-wing UAV that is equipped with four rotors.The rotors are commonly arranged in a symmetrical cross (x) or plus (+) configuration [11].As a quadcopter is a six-degreeof-freedom aircraft, there are six variables that represent the quadcopter's positions and orientations.
The mathematical model of the cross (x) configuration based on DJI Tello was derived in this work.This section covers the quadcopter's description, dynamic model derivation, and parameters.

Quadcopter's Description
The quadcopter employs a pair of blades rotating in the same direction, which are positioned on the x and y coordinates of the body frames to make it easier to control as each movement in either direction requires the speed of two blades responsible for the desired direction to be controlled [11].The cross (x) configuration of the quadcopter with rotor 1 and rotor 2 rotating anticlockwise and rotor 3 and rotor 4 rotating clockwise is illustrated in Fig. 1, [12] [13].The difference in lift force is used to generate motion by adjusting the rotor's velocity in pairs.The manipulation of rotors to create forward, backward, right-ward, left-ward, vertical, and hovering motions of the quadcopter is shown in Table 1, [3]  The modeling techniques of quadcopter are based on the physics of the system with the system further broken into smaller subsystems for easier analysis, design, and modelling [11].In order to simplify the mathematical equations in modeling, several different assumptions are made while still constructing a fairly realistic model [11].The universal assumptions are herein presented [4] [11][14] [17][18]: 1.The quadcopter is perfectly symmetrical with respect to XY axes in structure with diagonal inertia matrix.Thus, cross (x) configuration can use plus (+) degree of freedom.
2. The quadcopter is equipped with four motors on its rigid frame physical structure.
3. The quadcopter has thrust and drag constants that are proportional to the square values of the motor's speed.
4. The quadcopter's center of mass and the origin of the body-fixed frame coincided.
5. The quadcopter has rigid propellers that work under the same conditions all the time with no blade flapping.

Quadcopter Dynamic Model
The body-fixed frame,  = {, , } and the earth-fixed frame,  = {, , } of quadcopter can be seen as in Fig. 2, in which their relationship is satisfied as {} =  {} [6][11] [13][15][18] [19].The quadcopter coordinates are generalized with six degrees of freedom, where absolute positions are described by (, , ) and the orientations are respectively described by roll (), pitch () and yaw ().(1) The elementary rotations about the , , and  axes are defined using Euler angles.The final orientation of the quadcopter with respect to the corresponding inertial axis, inertial trigonometric functions, and their matrix representations are shown in Fig. 3 As quadcopter model is assumed to be a rigid body, hence Newton-Euler is used to describe the dynamic quadcopter where Newton second rule of motion is used to obtain the translation and the rotation is obtained from Euler's equation of rotation [24] [25].The equation ( 4) is dynamic quadcopter translational that made of gravitational coefficient, , the z-axis vector matrix,   = [0 0 1]  Vol. 3, No. 4, 2023, pp.728-748 A'dilah Baharuddin (Self-Tuning PID Controller for Quadcopter using Fuzzy Logic) where  1 is the total thrust force generated by four rotors,  1 = ∑   4 =4 [24].The  is the mass of quadcopter.The translational drag of airframe is   = [ 1 ̇ 2 ̇ 3 ]  where  1 ,  2 and  3 are the air drag coefficients of translational [25].The equation ( 4) is rewritten as equation ( 5) which represents the translation of dynamic quadcopter.
The rotation of dynamic quadcopter is presented in equation ( 6).The moment of inertia,  is defined as  =  [      ]  as it is a 3-by-3 diagonal matrix, the rotor inertia,   = −Ω 1 + Ω 2 − Ω 3 + Ω 4 , the total rotor speed, Ω  generated from rotors, working in two pairs. 2 ,  3 and  4 are the total torque related to quadcopter [24] [25]. 4 ,  5 and  6 are the aerodynamic friction coefficients.The equation ( 6) is rewritten as equation ( 7) and equation ( 8) is the control input vector of quadcopter where  is the distance between the rotor axis and the center of quadcopter mass,  is thrust constant and  is the thrust factor. [ [ The derived model simulated in MATLAB Simulink with parameters of DJI Tello as listed in Table 2.The listed parameters are adopted from existing studies using DJI Tello quadcopter [17][25].In this work, the trajectory tracking controller for the quadcopter is designed without considering external disturbances such as wind disturbances or sensor noise, as no hardware integration is conducted to evaluate the proposed method.Fig. 4, Fig. 5, Fig. 6 and Fig. 7 show the derived model of quadcopter, motor speed calculation, rotation and translation of dynamic quadcopter respectively.

Quadcopter Controller Design
A quadcopter is unstable and require controllers to work properly.Many control strategies and algorithms can be used.This paper utilized a PID controller and applied Fuzzy logic techniques to form a self-tuning PID controller.The controller is applied to all variables (, , , , , ) of dynamic model of quadcopter.The cascade control structure of the quadcopter is shown in Fig. 8.In controller designing process some changes take place in the model derivation for position control.The derived translational of dynamic quadcopter in equation ( 5) are changed for x and y axes translation, where the ̈ and ̈ are redefined as  x and  y , respectively.The redefined model for  and  axes is written in (9) [23].The desired roll,   and pitch,   are not defined in the simulation as this work aims to track the desired trajectory   ,   ,   , and   .Based on virtual control of position theorems, the generation desired roll,   and pitch,   and stabilizing the altitude of quadcopter is obtained by position control [23].According to the theorems equation ( 11) is redefined as equation ( 12) and equation (13) where  1 is the output of altitude,  control.

PID Controller
To achieve precise tracking and good performance, PID-type controllers are usually used in the cascade control system [25].In the process, a PID controller works in close form to calculate the difference between a set point and the desired set point and works on minimizing it by tuning the control variables [24] [25].The relation between the error of the system and PID controller output is defined by equation (12), where equation (13) represents the difference between desired and generated output [19][21] [22] [25].
is proportional gain,   is integral gain and   is derivative gain.  is increased to reduce steady-state error, yet overly high values   can cause unstable oscillations.  is is increased to remove the feedback system's steady-state error, while   is increased to lessen the overshoot and settling time of the feedback system output signal [12]

PID Tuning Method
PID controllers possess no knowledge of the process plant, which causes them to be unable to adjust and respond to changes.Thus, it requires proper tuning to generate satisfying performance.One of the most commonly used methods is Ziegler-Nichols, which is divided into step response rules and frequency response rules.Both rules started with setting integral and derivative gain to zero while raising proportional gain to a value that will oscillate the system [28] [29].However, a system with no sustained oscillation is unable to be tuned with this method.In this work, the derived model used a manual tuning approach to tune the PID parameters, as in attempts to utilize Ziegler-Nichols, resulting in no sustained oscillation.Increasing each PID gain affects the transient response of the system, and the effects are summarized in Table 3.Based on the rules listed, the PID parameters obtained for PID controller are listed in The rules for manual tuning of PID parameters are listed [30][31]: 1.   ,   and   are set to zero. 2.   is raised until oscillations are fully sustained or nearly sustained, if fully sustained is impossible to reach.The simulation time is advised to be set at a long simulation stop time.3.   is raised slightly to reduce the oscillation to 1 period.The simulation can be run with shorter simulation stop time.4.   is increased until the steady state is near the setpoint, and the single oscillation oscillates around the setpoint.5.The rules are repeated until the desired output is achieved.

Fuzzy Controller
The manual tuning method is an easy way for PID controllers to obtain a reasonable result because it has an intuitive approach and little process knowledge is required.However, it may take a long time to obtain the gains that generate a reasonable result, and it is difficult to determine if the final settings are optimal.In addition, there is no guarantee that the system will attain a durable and stable solution, which could put the entire plant at risk.Due to their little knowledge of the process plant, PID controllers are unable to adjust themselves automatically when the system faces a certain change [32].Hence, the metaheuristic, specifically the Fuzzy logic technique, is integrated into the PID controller to ensure the output is obtained as desired and the parameters are tuned automatically when changes are applied to the system.Fuzzy logic is a robust command that does not require precise knowledge of a mathematical model [7] [33].By exploiting a human understanding of the plant in the control design process and decision-making, Fuzzy control works based on Fuzzy set theory, linguistic variables, and Fuzzy inference [34].A Fuzzy control system is composed of Fuzzification, Fuzzy rule bases, Fuzzy inference, and Defuzzification [35], Fig. 10 shows the general block diagram of the Fuzzy inference system [36].
The minimum and maximum variable ranges of the PID controller gains are obtained from the trial-and-error simulation using PID controller.The same values are used into equation ( 16), equation (17) and equation (18), and the Fuzzy PID controller is simulated as shown in Fig. 16, [32][38] [40].

Results and Discussion
The simulations using MATLAB Simulink for the proposed controller are conducted on a few different trajectories.The simulation time step is set to 0.01s.Each trajectory is simulated with both the PID controller and the proposed Fuzzy PID controller.The results obtained are compared and analyzed with Integral-Absolute-Error (IAE) in equation ( 19), and Integral-Time-Absolute-Error (ISE) in equation (20) for position of the quadcopter [10] [39].The total root mean square error (RMSE) is also calculated for position using equation ( 21), [40] [41].
In trajectory tracking, the desired roll and pitch are no longer defined, as the desired roll and pitch are the outputs of position control.Thus, the desired position,  and  are defined instead.The desired altitude,  is defined as in altitude and attitude tracking, in which the initial value is 0 and the desired value is 1.The desired yaw,  is set from 0.5 to 0. These inputs are predefined input values for circular, square, lemniscate, and zigzag trajectories.The simulation stop time for every trajectory is set to 180 s.

Circular Trajectory
The desired positions for circular trajectory are set as   = 10sin (  10 ) and   = 10cos (  10 ).These pre-defined desired outputs are used for both PID and Fuzzy controllers.Fig. 17 shows the circular trajectory tracking output of the quadcopter with the PID controller, and Fuzzy PID controller outputs.

Fig. 17. The outputs of circular tracking
Based on Fig. 17, both controllers successfully track the desired trajectory with obvious improvement in altitude tracking.Table 7 listed the information of the tracking by both controllers.From the listed data, both controllers show almost significant records.However, altitude shows improvement in the rise time record, where the value decreased by 31.18% with the Fuzzy-PID controller.A significant improvement is observed in position y as well, where the rise time is reduced by 21.10% with the Fuzzy PID compared to the PID controller.The overshoot percentage of position y also reduced by 34.32%.On the other hand, position x shows a small improvement in rise time, where the value is decreased by 0.21% with the Fuzzy PID controller.
The performance of both controllers is evaluated by tracking errors.the values of the tracking errors for circular tracking are listed in Table 8.Even though the improvement only occurred to position y and altitude, the values of IAE, ISE and RMSE reduced significantly with the Fuzzy PID controller.The desired trajectory is successfully tracked by both controllers.From Fig. 18, no significant difference is observed except the higher overshoot of altitude tracking is generated by the Fuzzy PID controller.By comparing the results from Fig. 20 and the data listed in Table 9, the higher overshoot percentage of altitude is verified as the value record by the Fuzzy PID controller is increased by 29.17% compared to the PID controller.The tracking errors values of square tracking in Table 10 shows the integration of Fuzzy techniques to PID controller improves the outputs of the system, as all errors measured from the Fuzzy PID are smaller than the PID controller.

Lemniscate Trajectory
The desired position inputs for the lemniscate trajectory are set as   = 10sin (  10 ) and   = 10cos (  20 ).The result of PID controller and Fuzzy PID controller is shown in Fig. 19.The outputs shown in Fig. 19 illustrate almost identical tracking of position x and y by both controllers, but different tracking outputs of altitude are observed.The tracking of altitude by the Fuzzy PID controller presents a trajectory with a bigger overshoot of altitude compared to the PID controller.This is verified by the data in Table 11, where the overshoot produced by the Fuzzy PID controller is higher in value by 26.05% than the PID controller.However, the rise time and the settling time of altitude are reduced by 30% and 17.62%, respectively.A slight improvement in positions  and  is also observed, where the rise time of position  is reduced by 0.22% and 0.91% for position .The output of the tracking by the Fuzzy PID is proven to be better than that of the PID controller and verified by the data in Table 12, where the errors are reduced when Fuzzy PID controller is used in the simulations.Based on Fig. 20, the desired trajectory is successfully tracked by both controllers.No significant difference is observed in the tracking of positions  and .However, the tracking of altitude showed different behaviors between the controllers, where the overshoot is bigger in tracking with the Fuzzy PID controller than the PID controller.This behavior is also proven with the data from Table 13, where the overshoot percentage of altitude is 26% bigger when the Fuzzy PID controller is used.Yet, the rise time and settling time have improved by 31.24% and 15.36%, respectively.
Based on Table 13, there is a small difference in the rise time of position  the overshoot of overshoot of position .The rise time of position  is reduced by 0.51%, while position  overshoot is reduced by 4.71%.The rise time of position  makes a big difference between the controllers, as the recorded value dropped by 88.54% with the use of the Fuzzy PID controller.The values of tracking errors recorded by the Fuzzy PID controller lower than the PID controller.These values are listed in Table 14.

Spiral Trajectory
For spiral trajectory tracking, the desired positions are set as   = 10sin (  10 ) and   = 10cos (  10 ), similarly to circular trajectory tracking except for the altitude,  need to be set as an increasing value.Thus, a ramp with a slope of 0.5 is set as the desired value of .The spiral tracking output is shown in Fig. 21.  15 shows that the overshoot of Fuzzy PID controller is 0.35% bigger in position  and 30.54% lower in position .The other values show no big difference between the controllers.However, the values of tracking errors in Table 16 shows that Fuzzy PID controller produced better tracking results as the values of errors for the Fuzzy PID controller are smaller than the PID controller.

Discussion
In general, the desired trajectory tracking is successfully obtained by the simulations of both the PID controller and the Fuzzy PID controllers.The integration of Fuzzy controller into the PID controller shows improvements in system performance without the need to tune the parameters manually.The system is adjusted by the Fuzzy inference in the Fuzzy controller, which automatically tunes the parameters to produce better results compared to the PID controller.Even though the recorded rise time, settling time and overshoot percentage have no big difference between the controller, the measured tracking errors of the Fuzzy controller are smaller than the PID controller.
The system is simulated with five different trajectories to test its effectiveness to adjust and produce a good result without further tuning the parameters.In comparison of spiral trajectory tracking in this paper and the existing study [10], the value is not as big, where in this work only 27.31% of reduction was recorded compared to 87.62% in the existing work.However, such a difference is expected as the existing work an applied additional algorithm to generate the scaling factor for the Fuzzy controller, while this paper proposes a simpler algorithm for self-tuning Fuzzy PID controller.Thus, limitations should be expected along with the results.
The proposed method is proven to be effective theoretically.However, the application of the system to real-time may require further adjustment and additional algorithm as the system in this paper is modelled without considering the factors such as power consumption, external disturbances, and noises.

Conclusion
This work aimed to obtain a mathematical model of a quadcopter and design a controller to control position and attitude for trajectory tracking.A model of quadcopter based on DJI Tello drone is modelled and two control techniques are designed.The designed control systems are a conventional PID controller and a self-tuning Fuzzy PID controller.The proposed method used a simple Fuzzy controller algorithm.The simulations of five different trajectories shows the results improved and better with the integration of Fuzzy technique, where the values of tracking errors reduced with the application of Fuzzy controller.However, in order to apply the system to real-world application, further adjustments might be required, especially to overcome the external disturbances and noises.In the coming future works, the proposed method will be further evaluated and tested in both simulation and real-time applications with the presences of disturbances, uncertainties, and noises.

Fig. 10 .
Fig. 10.Fuzzy inference systemFor position, altitude, and attitude of quadcopter control, the inputs are the error, () which represents the difference between the desired and generated trajectory, and the change rate of the error,

Fig. 11 .
Fig. 11.The inputs membership functions of position and altitude

Fig. 14 .
Fig. 14.The output membership functions of attitude Mamdani-type Fuzzy controllers use IF-THEN rules to form the linguistic form of Fuzzy logic called the membership function.Based on seven linguistic variables, 49 rules of membership functions are formed for each output.The rules of membership functions for the quadcopter are listed in Table5and Table 6[37].

Fig. 21 .
Fig. 21.The outputs of spiral trajectory tracking From Fig. 21, both controllers show the ability to achieve desired trajectory.The output of the Fuzzy PID controller seems to have smaller overshoot in the positions.The value of the overshoot percentage recorded in Table15shows that the overshoot of Fuzzy PID controller is 0.35% bigger in position  and 30.54% lower in position .The other values show no big difference between the controllers.However, the values of tracking errors in Table16shows that Fuzzy PID controller

Table 2 .
Adopted parameters of the DJI Tello quadcopter

Table 3 .
The effect of increasing the PID gains value

Table 4 .
The gains of PID parameters for PID controller

Table 7 .
The step info of circular tracking

Table 8 .
The tracking errors of circular trajectory A'dilah Baharuddin (Self-Tuning PID Controller for Quadcopter using Fuzzy Logic)

Table 9 .
The step info of square tracking

Table 10 .
The tracking errors of square tracking

Table 11 .
The step info of lemniscate tracking

Table 12 .
The tracking errors of lemniscate tracking

Table 13 .
The step info of zigzag tracking

Table 14 .
The tracking errors of zigzag tracking

Table 15 .
The step info of spiral tracking

Table 16 .
The tracking errors of spiral trajectory