Drone PID Controller Lab
Drone PID Controller Lab
The PID Controller is one of the most widely used feedback control implementations. The behavior of the PID
Controller can be easily modified by adjusting (or excluding) any of its three parameters: Kp, Ki, and Kd. The
below figure shows the PID algorithm graphically using block diagram.
For the STEVAL, the ‘desired values’ are the roll, pitch, yaw, and thrust received from the user via
the ST Drone smartphone application or the R/C receiver if available.
The ‘observed values’ are the current estimates of roll, pitch and yaw produced by the AHRS, as well
as the current thrust, a value easily tracked by the drone.
Page 1
Embedded Systems University of Maine
As shown in the figure above, the project includes three modules: (1) remote control receiver via
Bluetooth or R/C (not available by default), (2) real-position estimator, and (3) the PID controller.
The real-position estimator takes the measurements from MEMS sensors, including accelerometers
(ax, ay, az) and gyroscope (gx, gy, gz), and uses the Attitude and Heading Reference System (AHRS)
algorithm to calculate the quaternion elements (q0, q1, q2 and q3), and finally translates the
quaternion elements to Euler angles (roll, pitch and yaw).
The PID controller takes two sets of inputs: the target attitude and the estimated Euler angles. It
calculates the duty cycle of PWM outputs to control the speed of four motors.
Page 2
Embedded Systems University of Maine
The following diagrams will illustrate the drone movements that can be induced by the user, as well
as the rotor movements corresponding to those movements.
Manipulation of the drone’s thrust, controlled via the vertical axis on the left joystick, produces an
equal change in speed for all four rotors, allowing for the drone to change its vertical offset.
The drone’s yaw can be adjusted using the horizontal axis of the left joystick. When the yaw is
adjusted, additional thrust is applied to the two rotors spinning in the desired direction.
The drone’s pitch and roll can be adjusted by adjusting the vertical and horizontal axes of the right
joystick, respectively. An adjustment to pitch or roll results in a speed increase for one rotor and a
speed decrease for the other rotor spinning in the same direction.
Page 3
Embedded Systems University of Maine
Now, we will discuss the actions that the ST Drone will take in response to these different user
commands.
1. As stated before, when the thrust is adjusted, the speeds of all four rotors are modified equally.
This is done by the PID control nodule in two steps:
Step 1: The thrust value is calculated using the following equation:
Step 2: The motor PWM values are calculated through the use of the motor mixing
algorithm. This produces an output PWM value for each motor between 0 and 1900. The
motor mixing algorithm is shown below in Figure 8.
Page 4
Embedded Systems University of Maine
Because we have two sets of sensor measurements, one from the accelerometer and the other from
the gyroscope. We will calculate the error in two stages:
xs1
Kp1 ys1 Kp2, Ki2, Kd2
zs1
error1 error2
Ɵ×(BlueNRG/RC)
xs2
Ɵ y (BlueNRG/RC)
Ɵ z(BlueNRG/RC) ys2
zs2
Ɵ×(Accelerometer) w ×(gyroscope)
Ɵ y (Accelerometer) w y (gyroscope)
Ɵ z(Accelerometer) w z (gyroscope)
Figure 10. PID Outer and Inner Loop
The following uses the x axis as an example to illustrate the outer and inner loop control. The
calculation for the y and z axis is very similar.
x s 1=erro r 1 × K p1
limit
d ( erro r 2 )
x s 2=K p 2 ×erro r 2+ K i2 ∫ erro r 2 dt + K d 2
dt
−limit
2. The proportional parameter is easier to fine tune because it has the biggest effect on
stability/oscillation. For this drone I and D would deserve some fine tuning; in particular D parameter
is affecting when the drone is in outdoor condition with wind, but it’s not easy to test it in the lab.
Pitch and roll usually have the same parameters unless the quadcopter have not a symmetrical
structure (rare case), while usually YAW have different parameters.
Here is a short video on each of the PID parameters with a visual representation,
https://www.youtube.com/watch?v=LtocGBngSrA&t=26s
Here is a video showing how to fine tune the PID parameters, and how important PID is.
https://www.youtube.com/watch?v=AN3yxIBAxTA&t=534s
Page 5
Embedded Systems University of Maine
void FlightControlPID_innerLoop( ) {
Page 6
Embedded Systems University of Maine
Page 7
Embedded Systems University of Maine
In this lab section, you will study the impact of the PID control parameters to drones in a restrained
setting, as shown in the above figure.
For safety reasons, it is recommended to tie the drone to a simple framework as shown below.
1. What are the default parameters for the inner loop controller and the outer loop controller?
Outer
X(Pitch) Y(Roll) Z(Yaw)
Partitional 3 3 4
Integral 0 0 0
Inner
X(Pitch) Y(Roll) Z(Yaw)
Partitional 80 80 900
Integral 80 80 3
Derivative 10 10 3
2. What are the impacts if we increase the P parameters of the inner loop controller for the axis
corresponding to the pitch and roll axis?
It increases the amount the drone wants to change its position by, and their for overshooting the
target value. Also makes the drone very hard to control because it can become unstable.
3. What are the impacts if we decrease the P parameters of the outer loop controller for the axis
corresponding to the pitch and roll axis?
It takes longer for the drone to get to the target value and makes it off balance and slower to
change position compared to the normal values.
Page 8