How to do PID tuning and log the data

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
Posts: 2
Joined: Tue Nov 05, 2019 3:16 pm

How to do PID tuning and log the data

Post by jlee » Tue Nov 05, 2019 3:28 pm

Hi, guys. Before posting my questions please understand that I am new to crazyflie and has little background in coding.

Using crazyflie 2.1, I am trying to write a python code that can tune the PID gains while performing sequential maneuver. The example is like the following:

Step 1: Connect to the crazyflie and lift it up to (x,y,z)=(0,0,0.5) with gain set #1.
Step 2: Move to (1,0,0.5) -> (1,1,0.5) -> (0,1,0.5) -> (0,0,0.5)
Step 3: Change the PID gains (gain set #2) and lift it up to (0,0,1)
Step 4: Repeat Step 2.
While doing it I want to log the position and attitude data, and save it to a file.

I have been looking at cflib examples and found being useful. I also looked at but could not fully understand how it works. And I cannot find a code where I can update PID gains in real time during flight.

Also, is there a way to do it in cfclient?

I know some of you might think my questions are too easy or vague, but I really need help from you guys to start off my experience with crazyflie.

Posts: 2
Joined: Tue Oct 15, 2019 9:33 am

Re: How to do PID tuning and log the data

Post by antoniodecri » Wed Nov 06, 2019 9:53 am

as far as i know it is possible and in the example folder in crazyflie-lib-python there is an example on how to change the parameters.
Try with those libraries.
You can also use the low level commander libraries or high level commander libraries and add them in the file

Posts: 256
Joined: Tue Jun 30, 2015 7:47 am

Re: How to do PID tuning and log the data

Post by kristoffer » Thu Nov 07, 2019 9:24 am


There are a few options for this. If you want to do it from the Client it will be a bit more manual, but it is easy to play around with parameters. You will have to fly manually though, since it is not possible to be connected to the client and a script at the same time.

1. In the client make sure you have the Parameters and Plot tabs open (use the View/Tabs menu)
2. In the Parameters tab find the parameters you want to tune and double click to change the values.
3. In the Plotter tab you can plot the data you are interested in, see ... t/#logging for details of how to do it

If you want to automate it from a script I think the best starting point would be the or
They use the Sync family of classes which makes it easy to do sequential tasks.

To get the logging into this you will have to add that asynchronously and you can get inspiration from the start_position_printing() function in the script. You should call it (or probably a modified version of it) directly after

Code: Select all

with SyncCrazyflie(uri, cf=Crazyflie(rw_cache='./cache')) as scf:
to set it up when you have connected to the crazyflie.

To set a parameter and modify the PID settings in your sequence, you would do something like

Code: Select all'pid_attitude.roll_kd', 0.1)

Post Reply