Shop our Airplanes Products Drone Products Sales
Thread Tools
This thread is privately moderated by Plasmatree, who may elect to delete unwanted replies.
Sep 07, 2017, 03:15 PM
Registered User
Thread OP
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:
Name: BLACKBOX_LOG_20170714_135930__0.png
Views: 410
Size: 789.4 KB
Description:

I recorded logs by hovering and doing small moves in the livingroom. The smooth tune I came up with looks like this:
Name: BTFL_BLACKBOX_LOG_CS110_20170831_211916_0.png
Views: 392
Size: 486.6 KB
Description:

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.
Sign up now
to remove ads between posts
Sep 07, 2017, 03:33 PM
360 rodeo.
OrtisFlow's Avatar
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.

Name: Stack-4.gif
Views: 427
Size: 1.16 MB
Description:

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
teramax's Avatar
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
FedorComander's Avatar
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
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.
Apr 06, 2018, 06:33 AM
Registered User
Thread OP
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 ).

Thanks again for your input!
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)
Apr 08, 2018, 05:36 PM
Registered User
Thread OP
Hi Mark!

Nice to see you using it.

The error message is coming from blackbox_decode and just informs you about the logging rate that was used. The logging rate is ~1khz and 87.5% of data was dropped, which corresponds to the loops running at 8khz. Nothing to worry about.

I think you got it right so far and cleared up stuff for some viewers.
That the response, even if the traces suggest it, does not completely converge to one is something that already bugged me for a while. Thanks to your video I had a closer look at the height of results again. You are right that in theory it should be easily addressable via I-term.
Unfortunately there is something else: The smoothing factor during deconvolution is necessary to suppress noise, but if set too high can distort and lower the response. It seems to be a bit too high at the moment and I will decrease it in the next update to get better results. The best would be to dynamically determine it for every log (or even time frame) since it's related to the s/n ratio of the data. Some publications I read on that topic seemed to suggest that the ideal smoothing is also depending on the log rate. I will see in which sense this is true for us.
Would you mind posting the logfile? I would like to test different settings.

Looking at the attached log I would increase p a bit to get a sharper edge. A too low p and thus a too soft response leads to more accumulation in I which then often causes a longer "bump" after the rise (as seen during 0.5-1.5s in roll of the attached log).


The colormap is chosen intentionally over the old standard of "jet". If you are interested why, here is a nice talk on that topic:
A Better Default Colormap for Matplotlib | SciPy 2015 | Nathaniel Smith and Stéfan van der Walt (19 min 9 sec)

I also think it would be even more confusing/less intuitive to understand and not much gain if there is stronger gradient information in that plot.
(+less smoothing will amplified these plots anyways...)

The "<" is indeed wrong. I really don't know how I missed that in the official release.

And lastly kudos for you channel. It's currently the best deep dive on stuff like this!


Quick Reply
Message:
Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Help! Quadcopter bounces on landing with betaflight and PID issues Pyrofan Multirotor Drone Talk 5 Jan 23, 2017 09:43 PM
Help! Uncontrollable quad - Are my default PIDs way to high? - Betaflight HB Crew Beginner Multirotor Drones 8 Nov 21, 2016 11:46 AM
Mini-HowTo Finding your initial PIDs with Betaflight/Cleanflight neonbjb Beginner Multirotor Drones 0 May 24, 2016 11:52 AM
Question Step-by-step PID tuning guide? (Luxfloat / Betaflight) xoxota Beginner Multirotor Drones 10 Jan 24, 2016 02:15 PM
Discussion Thug 180v4 cobra 2206 4s PID help Betaflight lowslung Mini Multirotor Drones 1 Dec 29, 2015 10:56 AM