*2.1. Cascaded-PID*

The cascade strategy is a well-known scheme in control system design. It basically consists of the interaction between two control loops: an external one that is responsible for generating a reference signal and an internal, fed by the latter, which is responsible for computing the signals to drive the actuators.

The block diagram in Figure 2 illustrates the proposed simplified Cascaded-PID controller scheme. The external loop uses the position error to generate a setpoint reference value for the internal loop. Concerning the diagram, the internal control loop is responsible for controlling the UAV motors. The rotors ' velocities are calculated after demonstrating that both roll and pitch angles converge to their (generated) reference values.

**Figure 2.** Schematic of the Simplified Cascaded-PID controller.

As can be seen in the diagram of Figure 2, the proposed scheme uses the altitude and position errors for generating and sending the velocity commands to the flight control unit controller.

#### *2.2. ROS Integration*

The general idea of the proposed methodology is illustrated in Figure 3. It consists of using a companion computer to perform the control of the UAV.

**Figure 3.** Signal flowchart and architecture.

This strategy allows a more straightforward implementation and testing tasks that are benefited from more advanced sensors, such as light detection and ranging and waypoint control integration. The Flight Management Unit (FMU) is responsible for acquiring information from the peripheral sensors and modifying these values in the actuators to which it has access. The primary sensor data are fed through the FMU to a specific ROS driver, the MAVROS. The data are then forwarded to a ROS node, in which the controller is implemented. The reverse-path takes place to perform motor driving at the end of the control loop.

The ROS interface with the UAV works as follows. The companion computer has the ROS core processing data in each of the code's pre-configured nodes. The computer receives the UAV state information from Ethernet using the MAVLink communication protocol. The UAV state is made available as a ROS topic by the MAVROS ROS driver. The calculations that enable the Cascaded-PID controller are performed in the main ROS node, which publishes the current PID gains and the errors to a topic that is used by the fuzzy logic module. The same ROS node subscribes to the messages with the new

PID gains sent by the fuzzy logic module. When it receives a message, it makes the PID calculations and transmits the target velocities to the FMU by publishing a MAVROS message. The gains are changed at a rate of 10 Hz, which is the best possible MAVLink rate.

#### *2.3. Fuzzy Logic PID Tuner*

In Figure 4, the MATLAB/Simulink block diagram with the Fuzzy Logic and the connection between Simulink and ROS are presented. It is shown, on the left side of the diagram, the subscription block that reads the information from the topics sent by ROS. The messages are divided in five variables, so they can be individually read by Simulink. The variable of the error derivative was suppressed, as it can be directly calculated from the error variable using Simulink, which guarantees that both have the same rate. Some displays are added only to keep track of evaluation during the tests. The the error and it's calculated derivative are sent to the Fuzzy Logic Controller With Ruleviewer block. This particular block is responsible for reading the fuzzy controller file and performing the fuzzy logic calculations. After that, the values from the fuzzy controller are sent to ROS, on the publish block on the bottom of the figure. The ROS messages of "fuzzy\_values" and "defuzzy\_values" were created for this work.

**Figure 4.** Simulink model for the fuzzy controller.

The Membership Functions types were chosen as Gaussian for the inputs and triangular for the outputs. This is the setup present in most of the literature cited by this work when it comes to PID gains tuning with fuzzy logic. The existing works were followed as a start point regarding the rules, and the rules were defined according to the UAV's desired behavior. For instance, if the position error *X-error* and its time derivative *X-error-D* are sufficiently small in the present approach, there is no need for large PID gain values. Since no "crisp functions" were used, final gains regularly tend to be slightly different from those in the center of the membership function. The fuzzy inference technique adopted in this work is based on the classical Mamdani [42] inference method and on the centroid defuzzification method.

The fuzzy rule Table 1 can be seen as the following. The symbol 'B' represents 'Big', 'M' for 'Medium', and 'L' for 'Low.' The outputs should be interpreted as *KP*, *KD*, and Alpha gains. Hence, the output notation 'LMB' should mean that the proportional (*KP*) gain is Low, the derivative (*KD*) gain is Medium, and Alpha gain is Big. For the integrative (*KI*) gain, the formula below is used [43]. As the desired behavior of the controller is the same for when the position error is positive or negative, the absolute value of the error was used. Regarding the derivative, it was the derivative of the absolute value of the error.

$$K\_I = K\_P^{\,2} / (\alpha K\_D) \tag{1}$$



Selecting the Range of PID Gains for Horizontal Motion Control

The methodology to define the range of each of the PID gains for the horizontal motion control (x and y axis) followed [44], and [43] with a modification proposed by this work.

First, the ultimate gain (*Ku*) and the oscillation period (*Tu*) [45] are obtained. This is done by removing the derivative and integral parts of the PID and increasing the proportional gain until the point that the output of the control loop has stable and consistent oscillations. In this work, the measured ultimate gain was of 2.2 and the oscillation period of 4 s.

With the measured values of *Ku* and *Tu*, the Zhao/Larson gains *KP* ,*min*, *KP* ,*max*, *KD* ,*min* and *KD* ,*max* were calculated:

$$K\_{P',min} = 0.32 K\_{\nu}, \quad K\_{P',max} = 0.6 K\_{\nu} \tag{2}$$

$$K\_{D',min} = 0.08 K\_{\nu} P\_{\nu} \quad \quad K\_{D',max} = 0.15 K\_{\nu} P\_{\nu} \,. \tag{3}$$

Provided that the traditional Ziegler-Nichols exhibits a high overshoot and that the overshoot is reduced but not removed by the above described method, this work proposes the following modification. The modification is to use a compression factor to reduce the fuzzy range of the PID gains, which will smooth the control signal and make it more suitable for UAV applications, where a fast convergence is desired but without a very high overshoot.

Therefore, the actual adopted gains *KP*,*min*, *KP* ,*max*, *KD* ,*min* and *KD*,*max* are obtained as follows:

$$K\_{P,min} = K\_{P',min} - \frac{K\_{P',min}}{7}, \quad K\_{P,max} = K\_{P',min} + \frac{K\_{P',max}}{7},\tag{4}$$

$$K\_{D, \min} = K\_{D', \min} - \frac{K\_{D', \max}}{7}, \quad K\_{D, \max} = K\_{D', \min} + \frac{K\_{D', \max}}{7}. \tag{5}$$

Finally, the summary of the limits can be seen in Figure 5, where the left shows the two inputs and the right shows the three outputs.

**Figure 5.** Fuzzy membership functions.

### **3. Results**

*3.1. Horizontal Motion Control Tests in the Simulation Environment*

The proposed solution was tested in a simulation environment and compared with other solutions. The other solutions adopted for comparisons are:


The tests were performed in a simulation environment (Figure 6) that runs in Windows operating system with Windows Subsystem for Linux (WSL) Ubuntu 18.04.5 LTS 64 bits. The computer has an Intel i9-9900K 3600 MHz processor with an NVIDIA GeForce RTX 2080Ti graphics card and 48 GB RAM.

**Figure 6.** Simulation Environment provided by the software Unreal Engine 4 with AirSim.

The software Unreal Engine 4 (UE4) with the AirSim plugin [46] was used for the simulations trials. AirSim was developed by Microsoft, and is an abbreviation for Aerial Informatics and Robotics Simulation. This plugin program provided some important resources for this work, especially with regard to input/output signals, corresponding to the behavior of sensors and actuators, sent to and received from the UAV flight controllers. In the tests, the UAV was commanded to go from position 2 m at East from takeoff position to −2 m. Therefore, a total movement of 4 m was performed. The command to move was sent around 1.6 s after starting recording the logs and building the graphs.

#### Simulation Results

First, the three tuning methods for the Cascaded-PID were evaluated (Figure 7). It is possible to notice that, as already shown in [43,44], the Zhao/Larson method achieves the same convergence time as the Ziegler-Nichols but with lower overshoot and faster accommodation profile. In UAV applications, fast convergence is desirable, but the accommodation property is more important, as the UAV usually needs to acquire data with onboard sensors. Therefore, the solution proposed in this work is better suited for UAVs as it can achieve accommodation in a faster way with significantly less overshoot. By considering an acceptable error of 1 cm, the proposed solution reaches accommodation in 6.7 s, against 10.8 s achieved by using the Zhao/Larson tuning approach and more than 20 s achieved with the Ziegler-Nichols tuning method.

The proposed method was also compared with ArduPilot's built-in controller in GUIDED mode, in which ArduPilot accepts target position commands and updates the velocity control at a rate of 50 Hz. A ROS message of the target position was sent using MAVROS. The result is presented in Figure 8. From the curve, it is possible to conclude that the ArduPilot achieves a shorter time of accommodation. However, the proposed method

performed very well to reach the desired position, being 1.5 s faster than the ArduPilot, with a small overshoot of less than 0.5 m (Figure 9).

**Figure 7.** The comparison among Cascaded-PID tuning methods.

**Figure 8.** ArduPilot and Proposed method compared.

**Figure 9.** Absolute Error comparison between ArduPilot and the proposed method.

It is important to note that the ArduPilot controller is very advanced and has been developed for many years, counting with a big team of developers and contributors.

Furthermore, in comparison with ArduPilot's results, in Figure 10 the accumulated error can be analyzed. The accumulated error is a measure that shows how much the UAV was away from the desired position over time. The upper line shows the ArduPilot's accumulated error in terms of position in X axis, while the lower line represents the results of this proposed method. Both have crescent errors before two seconds, but it is shown that the ArduPilot stabilizes this error almost in 6 s, while the proposed method takes more than that to be a fairly horizontal line. However, the most meaningful information in this comparison is that the difference between both methods at the end of the experimentation is almost 25 m. This means that counting on every small error that both had on the trajectory tests, the proposed method shows itself significantly more accurate in terms of position in x.

**Figure 10.** Accumulated Error comparison between ArduPilot and the proposed method.

Regarding the Fuzzy Logic PID gains used in the proposed method, they can be observed in Figure 11, by the side of a graph with the calculated error and error time derivative. The peak of the derivative error, in the beginning, is due to discontinuity generated by variable initialization and should not be considered. As explained in the previous section, the error curve is shown in absolute value.

The behavior of the PID gains variations shows that the Fuzzy Logic was properly configured as in the beginning when the error exhibits large values, the derivative gain has lower values but increases every time the error is approaching the setpoint. In addition, the derivative gain tends to reduce even more if the derivative of the error is negative, meaning that the UAV is progressing correctly. The integral gain tended to increase if there is an error for a long time, being smoothly reduced if the UAV is around the setpoint for some time. Regarding the proportional gain, it is possible to observe that it is basically directly following the error.

A fact that deserves to be highlighted in the graphs in Figure 11b are the oscillatory behaviors of the *P*, *I* and *D* gain estimates. These behaviors are probably caused by the measurement of the error derivative illustrated in Figure 12a, and are also propagated to the velocity command represented in Figure 12a. Although this type of oscillatory behavior is undesirable, it did not affect the actual measured velocity (Figure 12b). However, the issue regarding the chattering mitigation needs to be further investigated in future work.

**Figure 11.** (**a**) Measured errors; and (**b**) Fuzzy PID gains.

Finally, the commanded velocity and the measured velocity can be compared in Figure 12. The sample times are different because the commands are sent at a rate 2.5 times superior than the measurements. Therefore, the graphs have the same time range in seconds.

It can be observed that the actual measured velocity of the UAV does not reach the commanded velocity. That happens because new commanded velocities are sent before the UAV can reach the previously sent one. In addition, the UAV has a limited maximum velocity, which was set to 3 m/s. Therefore, if the Fuzzy logic Cascaded-PID calculates a commanded velocity larger than 3 m/s, as it happened in the beginning as the UAV was steady and the error was big, the UAV is not able to immediately reach that velocity because of inertia.

In another perspective, in Figure 13, the proposed method is compared to fixed PID gains using the mean values of the Fuzzy Logic gains. This clarifies the importance of the adaptive controller for such purposes, while the mean values of fuzzy gains present still reasonable results, the variation of the fuzzy gains shows a smaller overshoot on the first attempt to stabilize, oscillates less than the mean-gains approach, and achieve the desired position in X axis before than the mean-values method. Still compared to a the method using fuzzy gains' mean values, the accumulated position error in X axis is slightly smaller for the proposed method at the end of the experimentation in Figure 14. The accumulated error from the proposed method is greater than the mean-values method only near 7 s, where it is trying to stabilize for the last time, with no further oscillations, while the mean-values method oscillates and thus briefly achieves the objective position.

**Figure 12.** (**a**) Commanded; and (**b**) Measured velocities.

**Figure 13.** Comparison between the proposed method and using the mean values of the fuzzy logic gains.

**Figure 14.** Accumulated Error comparison between the proposed method and using the mean values of the fuzzy logic gains.

Finally, a last simulation was performed, when a wind of 15 m per second was introduced. The results can be seen in Figure 15. It is possible to observe that the proposed method has a better performance than the approach using only the mean values. In addition, it achieves nearly the same time of accommodation than the Ardupilot solution, with faster convergence.

**Figure 15.** Comparison among different methods in the presence of 15 m/s wind to the West.

#### *3.2. Altitude Control Tests in Real Flight*

In the real implementation, the multirotor Parot bebop 2 was used. This UAV is shown in Figure 16. Its system provides a compatible network interface for ROS, by furnishing the required sensor information as well as the control interface. Any other compatible UAV can also be used, replacing only the proper interfaces.

Only height control is used in the test, while the original FMU algorithm still handles stabilization and position control. This ensures safety once the UAV is stable and locked into position. Figure 17 shows the UAV flight on a blue screen room during the test.

**Figure 16.** The Parrot Bebop 2.

**Figure 17.** The UAV height control flight test.

In order to avoid discontinuous movements, the method was tested using ramp profiles from one altitude setpoint to the next one. Figure 18 shows the control signal and the error measurements. The dotted line represents the overall behavior of the control signal that is applied in discrete time instants. Note that the error has a well-behaved time profile. The slightly oscillatory behavior is due to the effects of environmental conditions.

**Figure 18.** Control signal and error during the experimental trial.

The control PID gains estimated by the proposed fuzzy algorithm are shown in Figure 19. Their performances are within the expected patterns. It is also noticeable that the derivative estimation changes were more aggressive than the other two.

**Figure 19.** Individual control response during the experiment.

#### **4. Discussion**

In the literature, most of the Fuzzy Logic tuning solutions have rules defining the PID gains (*KP*, *KD*, and *KI*) based on the output error and its time derivative. In this work, however, the absolute value of the output error is used to generate the Fuzzy rules. With this strategy, the deviation between the desired and current UAV position always assumes positive or null values, which facilitates the definition of "low", "medium" and "high" levels for the fuzzy algorithm. In the literature, it is common to find the same definitions of such levels for both the positive and negative excursions of the error time derivative, which is the same strategy adopted in fuzzy rules proposed in this work. Based on the observations made on the results obtained, we emphasize that the definition of different rules for the negative and positive excursions of the error derivative seems to influence the performance of the closed-loop and that, therefore, it should be an issue to be investigated in a future work.

In addition, one of the main challenges when using Fuzzy Logic for PID online tuning is to define a proper range of *KP*, *KD*, and *KI* values. This work presents a method for defining a proper range for the horizontal motion PID gains specifically applied to UAVs based on the existing literature and with a novel modification that smooths the behavior. Regarding the altitude controller, the range of PID gains was chosen empirically. Similar tuning methods could have been used for this purpose, such as the Ziegler-Nichols ultimate gain and oscillation period method, or some variant tuning scheme as fast as the Zhao/Larson method, but with better settling time.

Regarding the results, some oscillations were verified on the commanded PID gains, which may be due to different rates among the different systems (ROS, FMU, MAT-LAB/Simulink). This affected the commanded velocity but did not affect the actual measured velocity. The MATLAB/Simulink was used for this solution's proof of concept version, and the Fuzzy Logic algorithm should be embedded in the ROS Python or C++ scripts in future work. By doing so, we expect to achieve a faster and more synchronized system.

In terms of evaluation, this research work opens up several future possibilities. For instance, it is expected the deployment of this method in a real UAV inspection mission. Besides, the authors intend to insert the proposed control algorithm along with an optical flow algorithm for performing autonomous flight.

#### **5. Conclusions**

Nowadays, UAVs have been widely applied in diverse applications because of their flexibility of maneuvers, reducing risks to human life, ease to control, and cost-effectiveness. A drawback is the necessity of a platform with a high degree of stability to prevent the UAVs from falling and maintaining the desired orientation and path during the flight. Many solutions rely on PID controllers. However, its design and adjustment can be difficult. Therefore, this work proposed a novel approach composed of a Cascaded-PID Fuzzy Logic controller, where the system have adaptive capabilities engendered by the Fuzzy Logic part.

In this specific scenario, the fuzzy rules along with the membership functions play a crucial role in defining whether PID gains should prioritize stability. For instance, enhancing derivative gain in a constant position (zero error) scenario or prioritizing high gain PI for faster response when position error and its time derivative are both large.

Simulations and flight tests were conducted to demonstrate the effectiveness of the proposed method. As it can be seen from the results and discussions, when compared to the traditional Ziegler-Nichols method and another fuzzy method from the literature, the proposed solution provides much faster accommodation and smaller error amplitudes. Regarding the comparison with the built-in controller present in the ArduPilot flight control unit, the proposed solution achieves faster convergence and smaller accumulated error.

**Author Contributions:** Conceptualization, F.A.A.A. and M.F.P.; methodology, F.A.A.A. and I.P.G.; software, F.A.A.A., I.P.G. and G.F.C.; validation, M.F.P. and A.R.L.Z.; formal analysis, A.R.L.Z., L.F.A. and D.B.H.; investigation, F.A.A.A., M.F.P. and A.R.L.Z.; writing—original draft preparation, F.A.A.A., I.P.G., A.G.d.M. and G.F.C.; writing—review and editing, L.F.A., M.F.P., A.R.L.Z. and D.B.H.; visualization, F.A.A.A., A.G.d.M. and M.F.P.; supervision, F.A.A.A., M.F.P. and D.B.H.; project administration, F.A.A.A. and M.F.P.; funding acquisition, M.F.P., A.R.L.Z. and D.B.H. All authors have read and agreed with the submission of the current manuscript version.

**Funding:** The authors also would like to thank their home Institute CEFET/RJ, the federal Brazilian research agencies CAPES (code 001) and CNPq, and the Rio de Janeiro research agency FAPERJ, for supporting this work.

**Institutional Review Board Statement:** Not applicable.

**Informed Consent Statement:** Not applicable.

**Data Availability Statement:** The source-codes are openly available in https://github.com/piradata/ wpg, accessed on 23 December 2021.

**Conflicts of Interest:** The authors declare no conflict of interest.

### **Abbreviations**

The following abbreviations are used in this manuscript:


#### **References**

