This thread is privately moderated by Plasmatree, who may elect to delete unwanted replies.
Sep 07, 2017, 03:15 PM
Registered User
Data

# Betaflight PID-Analyzer

Hello,

I'm quite new to drones and was looking for a more data based approach to PID tuning.
I came up with a small program that calculates the actual system step response from recorded blackbox data.

The step response is a characteristic measure for PID performance and often referred to in tuning techniques. For more detail read: https://en.wikipedia.org/wiki/PID_co...#Manual_tuning
The program is Python based but utilizes Blackbox_decode.exe from blackbox_tools (https://github.com/cleanflight/blackbox-tools) to read logfiles.

This for example was the BF stock tune on my 2.5" CS110:

I recorded logs by hovering and doing small moves in the livingroom. The smooth tune I came up with looks like this:

The program calculates the system response from input (PID loop input = What the quad should do) and output (Gyro = The quad does). Mathematically this is called deconvolution, which is the invers to convolution: Input * Response = Output. A 0.5s long response is calculated from a 1.5s long windowed region of interest. The window is shifted roughly 0.2s to calculate each next response. From a mathematical point of view this is necessary, but makes each momentary response correspond to an interval of roughly +-0.75s.

Any external input (by forced movement like wind) will result in an incomplete system and thus in a corrupted response. Based on RC-input and quality the momentary response functions are weighted to reduces the impact of corruptions. Due to statistics, more data (longer logs) will further improve reliability of the result.

If it is set in Betaflight the P and I value of the PID will be different for high RC-inputs. This causes a different response. This fact is respected by calculating separate responses for inputs above and below 500 deg/s. With just moderate input, you will get one result, if you also do flips there will be two.

Keep in mind that if you go crazy on the throttle it will cause more distortion. If throttle-PID-attenuation (TPA) is set in Betaflight there will even be a different response caused by a dynamically lower P. This is the reason why the throttle and TPA threshold is additionally plotted.

How to use this program:
1. Record your log. Logs of 20s seem to give sufficient statistics. If it's slightly windy, longer logs can still give reasonable results.
2. Place your logfiles, blackbox_decode.exe and PID-Analyzer.exe in the same folder.
3. Run PID_analyzer.exe (this takes some seconds, it sets up a complete virtual python environment)
4. Drag and drop your file into the command window and press enter
5. Type in an optional name that will be appended to the log name. Or don't. Press Enter.
6. The logs are separated into temp files, read, analyzed and temp files deleted again.
7. A plot window opens and a .png image is saved automatically

The windows executable includes a virtual python environment and only requires you to drag and drop your Betaflight blackbox logfile into the cmd window. The code and windows executable can be found here: Github/PID-Analyzer

The whole thing is still under development and I was only able to test on win10 ad Betaflight 3.15. Results and input of different and more experienced pilots will be appreciated!
In case of problems please report including the log file.

Happy tuning,

Flo
Last edited by Plasmatree; May 30, 2018 at 03:58 AM.
 Sep 07, 2017, 03:33 PM 360° rodeo. Hi, What a very interesting post! Thanks for sharing, I'll give it a try for sure.
 Sep 08, 2017, 04:01 AM Registered User Thread OP Looking forward to your results! Here is a .gif showing the progress, while following the manual tuning procedure. I didn't realize that D-setpoint weight was ON until I started adding D and the overshoot actually grew! Note how also the rise time changes. Theoretically, copters with equal system responses should also fly/feel identical. Will be interesting to see if this holds true! *edit: This is not the recommended procedure! It's here to show the influence of different parameters for easier understanding!* Last edited by Plasmatree; Jun 14, 2018 at 08:13 AM.
 Sep 11, 2017, 05:40 AM sorry, my english is bad thats interersting, will have a closer look into that, curious to find if it works also with KISS logs
Sep 11, 2017, 02:05 PM
Registered User
Quote:
 Originally Posted by teramax thats interersting, will have a closer look into that, curious to find if it works also with KISS logs
There is only one way to find out )) This is really good stuff!!!!!
 Sep 12, 2017, 06:51 AM Registered User Thread OP I'm really curious! It would be awesome, if everybody who trys it, could give short notice here. I actually don't have any other logs than BF3.15. A lot of others should work, but it could be that some things are named differently and I would like to account for that. Anybody with logs from different versions? I will appreciate if you could at least post one of each version on here! Thanks, Flo
Oct 15, 2017, 02:37 PM
Registered User
Hi,
this seems to be a really handy tool. Thank you very much for sharing!
I'm runnung BF 3.20 and I was able to run the analyzer without any issue. I did attatch the plot.
The command prompt did output some messages:
347067 loop iterations weren't logged because of your blackbox_rate settings (176197ms, 87.50%)

Do I need to change my blackbox settings?

Best regards
Timo

### Images

 Oct 16, 2017, 02:54 AM Registered User I tried the analyzer with my 3.2 bb logs and it is working fine. I don't really understand the spectrogram though. Can you please elaborate a little bit? I've got a copter which doesn't get to strength 1.00 and stay around 0.75 like in the plot from Timoski. Does it mean the I term should be incremented? Should it reach the 1.00 at all?
 Nov 07, 2017, 06:29 PM Registered User Very cool stuff. So this only works with ßF BBL? or would RaceFlight logs work as well. They are in .txt format.
Feb 16, 2018, 02:36 PM
Getting my US RDA of SnPb

# Very cool!

I just stumbled on this yesterday. It was mentioned briefly on Joshua Bardwell's YouTube livestream and I was curious.

I have successfully run some log files that I had on my PC, but my quads were home and I was traveling for work. After looking at the animated gif of your tuning process and the idealized animation on the Wikipedia article I think I get how to proceed.

I like that this takes some of the subjectivity away. I never got quite as good as I wanted with the blackbox viewer, then Betaflight got so good that people seemed to just say it's good enough, go fly.

I'm looking forward to using this on my smaller quads, since those were always harder to get to where I'm happy, and some never quite did.

Well done!
 Mar 22, 2018, 06:04 AM Registered User Thread OP Thaks! It's in 0.21 now with improved averaging and there is a Wiki now where I'm still adding more stuff to!
Mar 27, 2018, 09:33 PM
Registered User
Thank you Flo!

I've been struggling to tune my quad (I had built one using AeroQuad, but decided to repurpose the motors / ESCs with a new naze32 board and frame). There's some low-frequency wobble I can see in flight that I haven't been able to tune out; and high-frequency vibration visible in Blackbox viewer, though it doesn't seem like P is too high in flight. Anyway, I found this thread looking for more tuning ideas.

Do you have advice on interpreting analysis results? (Does the plot give you an indication of what PID values to try next?)

Here are blackbox files and associated analysis -- one of the analyses is attached.

Edit for posterity: I re-did the tuning, and ended up setting higher D and lower I. I think the wobble was originally due to high I causing overshoot. I found this PID demo helpful in understanding what incorrect values on the various terms look like.

### Images

Last edited by markfickett; Mar 22, 2021 at 08:59 PM. Reason: Followup for posterity
Apr 06, 2018, 06:33 AM
Registered User
Hi Mark!
I see you fiddled with the colormap
I think the "high frequency" (its actually like 60 hz) oscillation is coming from your frame/construction.
Is it stiff enough? Any loose parts?

The tune itself looks really soft with a risetime of >100ms. I don't know the CLF pids, but just from the plot it looks like it can need way higher P, I and D. 60hz probably passes all lowpass filters and might be amplified if you increase P, so the first step is to get rid of the resonance. If the controller is busy reacting to the resonance the influence on the overall movement is kind of washed out.
I plotted it slightly different to see whats going on (framelen 5s, resplen 1.5s since you know the code anyways. I will add it as an option for the future, should be easy after mergig your pull ).

### Images

Apr 06, 2018, 07:33 AM
Registered User

# Blackbox_rate Question

Morning Gents,

What does this error message mean? Is there a target rate folks should be logging to use PlasmaTree?

Also, thoughts on the attached analysis. The gent I'm trying to help with this log has micro-oscillations at timestamp 11s thru 15.5s (also shown at 27s thru 31.5s). I'm seeing micro vibrations that are being smoothed to motion at 18 to 30hz region (smoothed through soft mounting and software filters). If I'm reading the PlasmaTree right it is suggesting a little more I on Pitch and Yaw (as a side) and maybe some more D on Pitch? Thoughts?

One idea that is rattling around in my head is making sure he has caps on the electronics to kill ESC noise and maybe also suggest that he trys removing motor soft mounts to move the vibrations up in frequency that the FC see so the software filters can attack directly? Just a thought. Not sure yet.

Finally, touched some on PlasmaTree on my last BB Log review video. Hope I got it right! Want to try to fully understand this tool to feature some more. Good stuff!!

Can the colormap be changed to what Mark did above? Shows a little more differential?

Finally, noted the "><" is backwards on the legend for rates ABOVE 500deg/sec.

 Blackbox Review on Prop. Wash + some Plasmatree (11 min 35 sec)