How to do PID tuning and log the data

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
jlee
Beginner
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 position_commander_demo.py being useful. I also looked at basiclog.py 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.



antoniodecri
Beginner
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

Hello,
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 basiclog.py file
Best
Antonio

kristoffer
Bitcraze
Posts: 109
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

Hi!

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 https://www.bitcraze.io/docs/crazyflie- ... 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 motion_commander_demo.py or position_commander_demo.py.
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 autonomousSequence.py 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

scf.cf.param.set_value('pid_attitude.roll_kd', 0.1)

Post Reply