Lesson 06 · Part 2 · Control
PID: tune the wobble
Mission: Make a drone that tilts 10 degrees settle without wobbling, by tuning the gains of a two-loop controller.
The interactive version of this lesson needs JavaScript. Below are its steps as text.
Steps
1. Turn the wobble on
Look at the drone from behind: a bar with a propeller at each end, and a dashed horizon. Press the button. The simulator asks for a 10 degree roll (left side up) and sets the rate loop’s proportional gain to zero.
Why it matters: A PID controller turns the error between target and measurement into a motor command. Its proportional part, P, reacts to the error now, and its integral and derivative parts act on the past and the rate of change. With P at zero the other parts cannot hold the drone at the target.
2. Add back the proportional gain
Set the rate proportional gain back to the firmware value, 250. Press Run and watch the roll line reach the target band.
Why it matters: The firmware default of 250 is the value in the Crazyflie source file for roll and pitch rate. The gain multiplies an error measured in degrees per second, so a 1 degree per second error becomes a command of 250 units before scaling. Scaling is one of the simulator’s stated assumptions.
3. Find the knee with the slider
Drag the rate kp slider in the panel below the stage, from 0 to 400 in steps. Press Run after each change and note the overshoot number in the readout.
Why it matters: More gain corrects faster, but past a point it only adds ringing and little else. Watch the overshoot as well as the settle time, because a fast drone that overshoots is not steady.
4. The angle loop sets the rate target
Press the button to run the full firmware cascade: angle loop kp 6 and ki 3, rate loop kp 250, ki 500, kd 2.5. The angle loop sets how fast the drone should turn; the rate loop makes it turn at that speed.
Why it matters: The outer loop asks for a turn rate, and the inner loop delivers it. The inner loop has to be quicker, otherwise the outer loop keeps asking for turns the drone has not yet made.
5. Raise the angle gain, then overdo it
Set the angle loop kp to 12 with the slider and press Run. Then set it to 20 and press Run again.
Why it matters: A higher angle gain asks for faster turns, which helps until the rate loop cannot follow. Then the drone overshoots, and the extra gain makes it worse.
6. Bump it and see the hold
Press Bump it. The simulator gives the drone a sideways push of 0.05 newtons (N) for 0.2 s while it holds its place with the position loop.
Why it matters: A newton is the unit of force; 0.05 N is about the weight of a 5 g mass. Holding attitude and position together is what makes a drone keep its place in a gust.
7. Your reading: write down one run
Open the Notebook tab. Enter the settle time and overshoot from one run you did above, and the target you used.
Why it matters: Turning a run into numbers with units is how a tuning change is checked. Entries stay in this browser only if you choose to save them.
Go deeper
The maths in this lesson
One PID term per loop, sampled every Δt seconds. Error e is what you want minus what you measure.
e(t) = target − measured u(t) = kp·e + ki·∫e dt + kd·de/dt angle loop: target = roll setpoint (deg), measured = roll (deg); output = rate setpoint (deg/s) rate loop: target = rate setpoint (deg/s), measured = roll rate (deg/s); output = command torque = S·u, with S the simulator scale of 4.9e-8 N·m per unit (assumed, see below)
Units are kept honest: kp in the rate loop is a command per degree per second of error, and the angle loop is a rate per degree of error.
Where the numbers come from
- Firmware gains (verified): PID_ROLL_RATE_KP 250.0, KI 500.0, KD 2.5; PID_ROLL_KP 6.0, KI 3.0; PID_YAW_RATE_KP 120.0, KI 16.7. Read from platform_defaults_cf2.h in bitcraze/crazyflie-firmware (https://raw.githubusercontent.com/bitcraze/crazyflie-firmware/master/src/platform/interface/platform_defaults_cf2.h).
- Mass 29 g takeoff weight and 92 mm motor-to-motor (verified): Bitcraze Crazyflie 2.1 datasheet Rev 3, https://www.bitcraze.io/documentation/hardware/crazyflie_2_1/crazyflie_2_1-datasheet.pdf. The brief’s 27 g did not match it.
- Torque and thrust coefficients and the inertia values (unverified): Förster (2015) BS thesis, ETH Zurich, https://research-collection.ethz.ch/handle/20.500.11850/214143. The thesis PDF was not read in full.
- Scale S from output to newton-metres, motor lag of 30 ms, position and altitude gains, the integral clamp, and linear drag: simulation choices, labelled in crazyflie.js.
Model limits
- Rigid body, no wind, no sensor noise in the attitude loop, no motor saturation beyond a simple per-motor limit.
- The yaw angle loop is not modelled; yaw is held at zero rate.
- The 30 ms motor lag is an assumption. Real motors and drivers add their own delays, so real gains will differ.