PID tuning

PID controller tuning

WARNING: When tuning the PID controller of a quadcopter, always take safety precautions. Secure the quadcopter in a safe environment to prevent accidents, as improper tuning can cause instability and unpredictable behavior. Use a tether or safety net, and wear protective gear to minimize risks. Make sure you have configured the emergency stop to a button on your controller.

Tuning a PID controller for a quadcopter is necessary for ensuring stable, responsive and precise flight control. For a custom-built quadcopter, for example using the Crazyflie Bolt, tuning becomes essential to adjust and optimize the drone’s performance according to the specific components and configurations used.

This tutorial provides fundamental PID tuning information aimed at beginners, covering essential concepts and step-by-step instructions. Additionally, it demonstrates specific tuning operations within our ecosystem, offering practical guidance that can also benefit advanced users.

Prerequisites

Make sure that you have gone through the how to get flying with a controller userguide before continuing with this tutorial.

Basic concept

The PID controller controls a process with three parameters; Proportional (P), Integral (I), and Derivative (D). It manages the process based on the error, which is the difference between a desired setpoint and the actual process value.

  1. Proportional (P): This term produces an output that is proportional to the current error value. If the error is large, the control output is large, and if the error is small, the control output is small. It’s calculated as P=Kpe(t)P=K_p \cdot e(t), where KpK_p​ is the proportional gain and e(t)e(t) is the error at time tt.

  2. Integral (I): This term is concerned with the accumulation of past errors. It integrates the error over time, which helps eliminate residual steady-state error. It’s calculated as I=Ki0te(τ)dτI=K_i \int_0^t e(\tau)d\tau, where KiK_i​ is the integral gain.

  3. Derivative (D): This term predicts future error based on its rate of change. It provides a damping effect, reducing the tendency to overshoot. It’s calculated as D=Kdde(t)dtD=K_d\frac{de(t)}{dt}​, where KdK_d​ is the derivative gain.

The combined output of a PID controller is the sum of these three terms: u(t)=P+I+Du(t)=P + I + D​

This output u(t)u(t) is used to adjust the process to achieve the desired setpoint.


The gains KpK_p (Proportional Gain), KiK_i (Integral Gain), and KdK_d (Derivative Gain) are used to tune a PID controller:

  1. KpK_p: Adjusts the responsiveness to the current error. Higher KpK_p​ increases response speed but can cause overshoot.
  2. KiK_i: Adjusts the response based on the accumulation of past errors. Higher KiK_i eliminates steady-state error but can cause instability.
  3. KdK_d: Adjusts the response based on the rate of error change. Higher KdK_d provides damping, reducing overshoot and oscillations.

Tuning involves balancing these gains to achieve stable and accurate control.

Our firmware uses a cascaded PID controller structure, with separate controllers for attitude rate, attitude, velocity, and position.

Setup

In this tutorial, we use cfclient to tune the PID controllers because it provides a comprehensive environment that allows us to simultaneously adjust PID gains, plot setpoints and state estimates, and control the drone with a controller or position setpoints.

To effectively tune each controller in the cascaded PID system, you must provide setpoints of the same order or higher than the respective controller’s level. For instance, position setpoints are necessary for tuning the position controller, while attitude, velocity or position setpoints suffice for the attitude controller. Attitude rate, attitude and velocity controllers are best made by conducting manual flight tests with a controller, as it allows for testing a variety of maneuvers. In this tutorial, we assume users will use a controller for manual flight tests. Setpoints can also be written and adjusted using other methods like through cflib or the app layer. However, using cflib to connect to the drone will prevent cfclient from connecting, so you cannot plot data or adjust gains directly from cfclient. Using the app layer can be cumbersome since it requires firmware flashing each time setpoints need adjustment, making the process more time-consuming.

Depending on what controller mode you will use to control your Crazyflie, it may be unnecessary to tune the higher level controllers.

  1. Open up cfclient
  2. Connect and configure your controller
  3. Connect to your Crazyflie
  4. Enable the plotter tab
  5. Enable the tuning toolbox

Basic PID Tuning

Set up the plotter to monitor both your desired setpoint and the estimated value of the controlled state. Adjust the gains in the tuning toolbox accordingly, observe the response in the plotter to fine-tune until the graphs of your desired setpoint and the estimated state align as closely as possible. WARNING: Do not change PID values while the drone is flying. Always land the drone before making adjustments.

Begin by tuning the attitude rate controller, then proceed to the attitude controller. These adjustments are best made after manual flight with a controller, as this allows you to fine-tune the PID controllers according to your specific flight preferences. Whether you prefer subtle inputs for a smooth flight, or you aim for a highly agile and responsive drone, manual flight testing enables you to tailor the tuning for the desired flight characteristics.

my alt text
Interface with tuning toolbox and plotter displaying the roll angle setpoint and the roll angle state estimate.

For tuning the velocity and position controllers some form of positioning system is required. The easiest method of tuning the velocity controller is to enable hover or position hold assisted control in the Flight Control tab. In these modes, velocity setpoints are automatically adjusted based on your controller inputs. Note that you must configure and hold a button on your controller to activate the assisted mode. For tuning the position controller we must send position setpoints. The most effective method for tuning this controller is by using the Command Based Flight Control in the Flight Control tab of the client.

Tips:

  • Increase the number of samples or increase the logging period in the relevant logging configurations to extend the time window of the plot.
  • Untick “Auto update graph” to pause the plotter and analyze the data.
  • Tick “Link roll and pitch” to adjust the gains for both axes simultaneously. Roll and pitch can typically be tuned simultaneously due to symmetrical design of the Crazyflie.