*3.1. State Machine*

The autonomous takeoff-tracking-landing system proposed in this paper builds upon a finite state machine (Figure 2) with five states: *landed*, *taking off*, *tracking*, *landing*, and *re-localizing*.

**Figure 2.** Finite state diagram that determines the behavior of the UAV.

*Landed* is the default state when launching the system and corresponds to the UAV resting on top of the landing platform, awaiting for a takeoff signal. As soon as this takeoff order is received, the drone's state changes to *taking off*, which represents the period during which the UAV is gaining altitude at a constant speed of 1ms−<sup>1</sup> along its z axis (please refer to Figure 1 for a view of the UAV's axis). The detection-localization algorithm (Section 3.2) is also launched at this point, as well as the tracking module (Section 3.3), so that the drone can start following the landing platform while ascending.

Once the nominal height has been reached (set to 4 m in our experiments), the state automatically changes to *tracking*, and the drone stops ascending. It will now follow the landing platform, keeping a constant altitude. To do so, a PID controller computes the necessary speed signals (both in the x and y axes) required to reduce the drone's distance to the landing platform's centroid in the xy-plane, which is parallel to the ground.

A land command (user-induced or automated) will trigger the start of the landing maneuver and shift the state to *landing*. The aerial robot will start its descent towards the moving landing platform at a constant downward speed of <sup>−</sup>0.3 m s−<sup>1</sup> along its z axis, while the height-adaptive PID controller provides the necessary speed commands along the UAV's x and y axis. Note that the PID gains are constantly being updated depending on the altitude (Section 3.3.1), thus height-adaptive.

**Recovery module.** The system can enter into recovery mode for either of the following two reasons: (1) the tracking algorithm registers when the landing platform was detected for the last time, and if more than 0.5 s pass without getting a new position, the state changes to *re-localizing*; (2) if the relative error between the landing platform's centroid and the UAV's body frame is bigger than a threshold (0.25 m in our experiments) at the final landing stages—when the sonar indicates values smaller than 0.7 m—the system will also enter recovery mode. In both cases, the drone will start gaining altitude at a speed of 1ms−1, and the height-adaptive PID controller will be turned off so that no speed commands are sent along the x or y directions. Note that the second condition is designed to work as a more strict and early detection of potential landing failures.

The intuition behind ascending vertically is that the viewed area by the UAV's downward-looking camera is gradually increased. When the landing platform is viewed again, the state is changed to tracking and maintained so until the nominal tracking altitude of 4 m is reached again. At that point, new incoming landing signals may be processed again. As we will show in a set of extensive experiments (Section 4.2.3), this re-localization strategy will prove to be key when the landing platform moves faster than the nominal velocity, keeping the system alive and preventing failed landings.
