# Autotune

Auto-tuning automates the process of tuning the PX4 rate and attitude controllers, which are the most important controllers for stable and responsive flight (other tuning is more "optional"). It is currently enabled for multicopter, fixed wing, and hybrid VTOL fixed wing vehicles.

Tuning only needs to be done once, and is recommended unless you're using a vehicle that has already been tuned by the manufacturer (and not modified since).

:::warning Auto-tuning is performed while flying. The airframe must fly well enough to handle moderate disturbances, and should be closely attended:

* Test that your vehicle is [stable enough for autotuning](#pre-tuning-test).
* Be ready to abort the autotuning process. You can do this by changing flight modes or using an auto-tune enable/disable switch ([if configured](#enable-disable-autotune-switch-fixed-wing)).
* Verify that the vehicle flies well after tuning. :::

@[youtube](https://youtu.be/5xswOhhqrIQ)

## Pre-tuning Test

The vehicle must be able to fly and adequately stabilize itself before running auto-tune. This test ensures that the vehicle can fly safely in position controlled modes.

:::note During [Airframe Setup](airframe) you should have selected the frame that most closely matches your vehicle. This may fly well enough to run autotuning. :::

To make sure the vehicle is stable enough for auto-tuning:

1. Perform a normal preflight safety checklist to ensure the flight zone is clear and has enough space.
2. Takeoff and prepare for the test
   * **Multicopters:** Take off and hover at 1m above ground in [Altitude mode](../flying/flight_modes/altitude_mc) or Stabilized mode.
   * **Fixed-wing:** Take off and fly at cruise speed in [Position mode](../flying/flight_modes/position_mc) or [Altitude mode](../flying/flight_modes/altitude_mc).
3. Use the RC transmitter roll stick to perform the following maneuver, tilting the vehicle just a few degrees: _roll left > roll right > center_ (The whole maneuver should take about 3 seconds). The vehicle should stabilise itself within 2 oscillations.
4. Repeat the maneuver, tilting with larger amplitudes at each attempt. If the vehicle can stabilise itself within 2 oscillations at \~20 degrees move to the next step.
5. Repeat the same maneuvers but on the pitch axis. As above, start with small angles and confirm that the vehicle can stabilise itself within 2 oscillations before increasing the tilt.

If the drone can stabilize itself within 2 oscillations it is ready for the auto-tuning procedure.

If not, go to the [troubleshooting](#troubleshooting) section, which explains the minimal manual tuning to prepare the vehicle for auto-tuning.

### Auto-tuning Procedure

The auto-tuning sequence must be performed in a **safe flight zone, with enough space**. It takes about 40 seconds ([between 19 and 68 seconds](#how-long-does-autotuning-take)). For best results, we recommend running the test in calm weather conditions.

The recommended modes for autotuning are [Hold mode](../flying/flight_modes/hold) (fixed wing) and [Altitude mode](../flying/flight_modes/altitude_mc) (MC), but any other flight mode can be used. During auto tuning, the RC sticks can still be used to fly the vehicle.

:::note The auto-tuning sequence can be aborted at any time by changing flight modes or using the [enable/disable Autotune switch](#enable-disable-autotune-switch-fixed-wing) (if configured). :::

The test steps are:

1. Perform the [pre-tuning test](#pre-tuning-test).
2. Takeoff using RC control and prepare for test:
   * **Multicopters:** Takeoff using the remote controller in [Altitude mode](../flying/flight_modes/altitude_mc). Hover the vehicle at a safe distance and at a few meters above ground (between 4 and 20m).
   * **Fixed-wing:** Once flying at cruise speed, activate [Hold mode](../flying/flight_modes/hold). This will guide the plane to fly in circle at constant altitude and speed.
3.  Enable autotune.

    :::tip If an [Enable/Disable Autotune Switch](#enable-disable-autotune-switch-fixed-wing) is configured you can just toggle the switch to the "enabled" position. :::

    1.  In QGroundControl, open the menu: **Vehicle setup > PID Tuning**

        ![Tuning Setup > Autotune Enabled](https://4155462212-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LArEa7z2SPawfl3HpCD%2Fuploads%2Fgit-blob-985ad6b18e85a8609fff664ca789900c53a20596%2Fautotune.png?alt=media)
    2. Select either the _Rate Controller_ or _Attitude Controller_ tabs.
    3. Ensure that the **Autotune enabled** button is enabled (this will display the **Autotune** button and remove the manual tuning selectors).
    4. Read the warning popup and click on **OK** to start tuning.
4. The drone will first start to perform quick roll motions followed by pitch and yaw motions. The progress is shown in the progress bar, next to the _Autotune_ button.
5. Apply the tuning:
   * **Fixed Wing:** The tuning will be immediately/automatically be applied and tested in flight (by default). PX4 will then run a 4 second test and revert the new tuning if a problem is detected.
   * **Multicopters:** Manually land and disarm to apply the new tuning parameters. Takeoff carefully and manually test that the vehicle is stable.
6. If any strong oscillations occur, land immediately and follow the instructions in the [Troubleshooting](#troubleshooting) section below.

Additional notes:

* **VTOL:** Hybrid VTOL fixed wing vehicles must be tuned twice, following multicopter instructions in MC mode and fixed-wing instructions in FW mode.
* **Multicopter:** The instructions above tune the vehicle in [Altitude mode](../flying/flight_modes/altitude_mc). You can instead takeoff in [Takeoff mode](../flying/flight_modes/takeoff) and tune in [Position mode](../flying/flight_modes/position_mc) if the vehicle is is _known_ to be stable in these modes.
* **Fixed wing:** Autotuning can also be run in [Altitude mode](../flying/flight_modes/altitude_mc) or [Position mode](../flying/flight_modes/position_mc). However running the test while flying straight requires a larger safe area for tuning, and does not give a significantly better tuning result.
* Whether tuning is applied in-air or after landing can be [configured using parameters](#apply-parameters-when-in-air-landed).

## Troubleshooting

#### The drone oscillates when performing the testing maneuvers prior to the auto-tuning

* slow oscillations (1 oscillation per second or slower): this often occurs on large platforms and means that the attitude loop is too fast compared to the rate loop.
  * **Multicopter:** decrease [MC\_ROLL\_P](../../advanced_config/parameter_reference#MC_ROLL_P) and [MC\_PITCH\_P](../../advanced_config/parameter_reference#MC_PITCH_P) by steps of 1.0.
  * **Fixed-wing:** increase [FW\_R\_TC](../../advanced_config/parameter_reference#FW_R_TC) and [FW\_P\_TC](../../advanced_config/parameter_reference#FW_P_TC) by steps of 0.1.
* fast oscillations (more than 1 oscillation per second): this is because the gain of the rate loop is too high.
  * **Multicopter:** decrease `MC_[ROLL|PITCH|YAW]RATE_K` by steps of 0.02
  * **Fixed-wing:** decrease [FW\_RR\_P](../../advanced_config/parameter_reference#FW_RR_P), [FW\_PR\_P](../../advanced_config/parameter_reference#FW_PR_P), [FW\_YR\_P](../../advanced_config/parameter_reference#FW_YR_P) by steps of 0.01.

#### The auto-tuning sequence fails

If the drone was not moving enough during auto-tuning, the system identification algorithm might have issues to find the correct coefficients. Increase the [FW\_AT\_SYSID\_AMP](../../advanced_config/parameter_reference#FW_AT_SYSID_AMP), [MC\_AT\_SYSID\_AMP](../../advanced_config/parameter_reference#MC_AT_SYSID_AMP) by steps of 1 and trigger the auto-tune again.

#### The drone oscillates after auto-tuning

Due to effects not included in the mathematical model such as delays, saturation, slew-rate, airframe flexibility, the loop gain can be too high. To fix this, follow the same steps described [when the drone oscillates in the pre-tuning test](#the-drone-oscillates-when-performing-the-testing-maneuvers-prior-to-the-auto-tuning).

#### I still can't get it to work

Attempt manual tuning using the appropriate guides:

* [Multicopter PID Tuning Guide](../airframes/frames_multicopter/config_mc/pid_tuning_guide_multicopter_basic) (Manual/Simple)
* [Multicopter PID Tuning Guide](../airframes/frames_multicopter/config_mc/pid_tuning_guide_multicopter) (Advanced/Detailed)
* [Fixed-Wing PID Tuning Guide](../airframes/frames_plane/config_fw/pid_tuning_guide_fixedwing)

## Optional Configuration

### Apply Parameters When In-Air/Landed

By default MC vehicles land before parameters are applied, while FW vehicles apply the parameters in-air and then test that the controllers work properly. This behaviour can be configured using the [MC\_AT\_APPLY](../../advanced_config/parameter_reference#MC_AT_APPLY) and [FW\_AT\_APPLY](../../advanced_config/parameter_reference#FW_AT_APPLY) parameters respectively:

* `0`: the gains are not applied. This is used for testing purposes if the user wants to inspect results of the auto-tuning algorithm without using them directly.
* `1`: apply the gains after disarm (default for multirotors). The operator can then test the new tuning while taking-off carefully.
* `2`: apply immediately (default for fixed-fings). The new tuning is applied, disturbances are sent to the controller and the stability is monitored during the next 4 seconds. If the control loop is unstable, the control gains are immediately reverted back to their previous value. If the test passes, the pilot can then use the new tuning.

### Enable/Disable Autotune Switch (Fixed Wing)

A remote control switch can be configured to enable/disable autotune (in any mode) using an RC AUX channel.

To map a switch:

1. Select an RC channel on your controller to use for the autotune enable/disable switch.
2. Set [RC\_MAP\_AUX1](../../advanced_config/parameter_reference#RC_MAP_AUX1) to match the RC channel for your switch (you can use any of `RC_MAP_AUX1` to `RC_MAP_AUX6`).
3. Set [FW\_AT\_MAN\_AUX](../../advanced_config/parameter_reference#FW_AT_MAN_AUX) to the selected channel (i.e. `1: Aux 1` if you mapped `RC_MAP_AUX1`).

The auto tuner will be disabled when the switch is below `0.5` (on the manual control setpoint range of of `[-1, 1]` and enabled when the switch channel is above `0.5`.

If using an RC AUX switch to enable autotuning, make sure to [select the tuning axes](#select-tuning-axis-fixed-wing) before flight.

### Select Tuning Axis (Fixed Wing)

Fixed wing vehicles (only) can select which axes are tuned using the [FW\_AT\_AXES](../../advanced_config/parameter_reference#FW_AT_AXES) bitmask parameter:

* bit `0`: roll (default)
* bit `1`: pitch (default)
* bit `2`: yaw

## Developers/SDKs

Autotuning is started using [MAV\_CMD\_DO\_AUTOTUNE\_ENABLE](https://mavlink.io/en/messages/common.html#MAV_CMD_DO_AUTOTUNE_ENABLE) MAVLink command.

At time of writing the message is resent at regular intervals to poll PX4 for progress: the `COMMAND_ACK` includes result that the operation is in progress, and also the progress as a percentage. The operation completes when the progress is 100% or the vehicle lands and disarms.

:::note This is not a MAVLink-compliant implementation of a [command protocol long running command](https://mavlink.io/en/services/command.html#long_running_commands). PX4 should stream progress as the protocol does not allow polling. :::

The feature is not yet supported by MAVSDK.

## Background/Detail

PX4 uses [PID controllers](../development/concept/architecture/controller_diagrams) (rate, attitude, velocity, and position) to calculate the outputs required to move a vehicle from its current estimated state to match a desired setpoint. The controllers must be well tuned in order to get the best performance out of a vehicle. In particular, a poorly tuned rate controller results in less stable flight in all modes, and takes longer to recover from disturbances.

Generally if you use a [frame configuration](airframe) that is similar to your vehicle then the vehicle will be able to fly. However unless the configuration precisely matches your hardware you should tune the rate and attitude controllers. Tuning the velocity and position controllers is less important because they are less affected by vehicle dynamics, and the default tuning configuration for a similar airframe is often sufficient.

Autotuning provides an automatic mechanism to tune the rate and attitude controllers. It can be used to tune fixed wing and multicopter vehicles, and VTOL vehicles when flying as a multicopter or as a fixed wing (transition between modes must be manually tuned). In theory it should work for other vehicle types that have a rate controller, but currently only the above types are supported.

Automatic tuning works well for the multicopter and fixed wing vehicle configurations supported by PX4, provided the frame is not too flexible (see [below for more information](#does-autotuning-work-for-all-supported-airframes)).

The vehicle must be flying in an altitude-stabilized mode ([Altitude mode](../flying/flight_modes/altitude_mc), [Hold mode](../flying/flight_modes/hold), or [Position mode](../flying/flight_modes/position_mc)). The flight stack will apply a small disturbance to the vehicle in each axis and then attempt to calculate the new tuning parameters. For fixed wing vehicles the new tuning is applied in-air by default, after which the vehicle tests the new settings and reverts the tuning if the controllers are not stable. For multicopter, the vehicle lands and applies the new tuning parameters after disarming; the pilot is expected to then take off carefully and test the tuning.

The tuning process takes about 40 seconds ([between 19 and 68 seconds](#how-long-does-autotuning-take)). The default behaviour can be configured using [parameters](#optional-configuration).

### FAQ

#### What frames types are supported?

Autotuning is enabled for multicopter, fixed wing, and hybrid VTOL fixed wing vehicles.

While it is not yet enabled for other frame types, in theory it an be used with any frame that uses a rate controller.

#### Does autotuning work for all supported airframes?

The mathematical model used by autotuning to estimate the dynamics of the drone assumes this it is a linear system with no coupling between the axes (SISO), and with a limited complexity (2 poles and 2 zeros). If the real drone is too far from those conditions, the model will not be able to represent the real dynamics of the drone.

In practise, autotuning generally works well for fixed wing and multicopter, provided the frame is not too flexible.

#### How long does autotuning take?

Tuning takes 5s-20s per axis (aborted if tuning could not be established in 20s) + 2s pause between each axis + 4s of testing if the new gains are applied in air.

A multicopter must tune all three axes, and by default does not test the new gains in-air. Tuning will therefore take between 19s (`5 + 2 + 5 + 2 + 5`) and 64s (`20x3 + 2x2`).

By default a fixed wing vehicle tunes all three axes and then tests the new gains in-air. The range is therefore between 25s (`5 + 2 + 5 + 2 + 5 + 2 + 4`) and 70s (`20x3 + 3x2 + 4`).

Note however that the above settings are defaults. A multicopter can choose to run the tests in air, and a fixed wing can choose not to. Further, a fixed wing can choose to tune fewer axes.

Anecdotally, it usually takes around 40s for either vehicle.

## See also

* [Multicopter PID Tuning Guide](../airframes/frames_multicopter/config_mc/pid_tuning_guide_multicopter_basic) (Manual/Simple)
* [Multicopter PID Tuning Guide](../airframes/frames_multicopter/config_mc/pid_tuning_guide_multicopter) (Advanced/Detailed)
* [Fixed-Wing PID Tuning Guide](../airframes/frames_plane/config_fw/pid_tuning_guide_fixedwing)
