Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kalman or BIQUAD+FIR2 filter
#1
I am going to try to explain and hopefully help you setup your multi-rotor with the new fast Kalman (BF+K) or BIQUAD RC+FIR2 (B+F) filter.  I don't pretend to be an expert, a lot stuff I still am trying to learn.  Hopefully I can provide enough information for you to try these filters.  It beats reading through 45 pages on the RCgroups to figure it out.  I will edit this tutorial as I learn more information.

The first question is what is a Kalman filter.  It is basically an algorithm that makes an educated guess on where the system will be next.  The filter works best in a continuously changing system like our multi-rotors.  In the real world, navigation guidance system uses Kalman filter.

Fast Kalman filter is the contribution from RS2K (Kalyn from Raceflight) for Betaflight.  The BIQUAD RC+FIR2 is more or less the same as the Kalman filter implemented by the Betaflight team.  From this point on, I will just refer this as the B+F filter.  The Betaflight team, in a sense, optimized the Fast Kalman filter so lesser powered FC can run the filter.

There are three implementation of the filter.
The original implementation by RS2K (Fast Kalman filter).

https://www.rcgroups.com/forums/showthre...Betaflight

The Betaflight team officially implementation into betaflight 3.3 nightly build firmware.  Nightly build #493 to #521.  And the changeover to the BIQUAD RC+FIR2 filter. Nightly build #522-current
https://ci.betaflight.tech/job/Betaflight/
You can now click on the unstable released in BFC, 3.3 RC1 is available.

Things you will need
F3 or F4 FC.  F7?  If you are running an F3, you will likely limited to the B+F.  Don't even try the Fast Kalman with F3, the F4 has to be overclocked and a lot of things turn off to running this.  I am going to concentrate more on F4, because I have not tested any of the filter on F3.  F7 FC are not quite optimized yet, but some people are testing them.  Other seems to have trouble getting CPU usage down on F7.  I don't have any F7 FC, so I couldn't test.

If you want to run 32K/32K, your F4 FC will need to have a gyro that is capable of 32K gryo sampling and looptimes (MPU-9250, MPU-6500, ICM-20602).  From the consensus, 32K/16K actually works better.  But you can run these filter with any gyro and looptime you want.  The lowest I heard is 4K/4K.  The Q/R ratio has to be change for different gyro and looptime.  We will get into Q/R ratio later.

You will need ESC that are capable of running Dshot or Multishot.

I suggest before you load the new firmware, you want to make sure that your quad is flying fine on a stable firmware.  Especially if your quad is a new build.  You don't want to add extra level of complexity when trying to troubleshoot.

Download and flash your quad with one of the firmware from the above links.  I recommend 3.3 RC1, as it is likely the most stable.  Once you have everything working properly, you will want to copy the settings below and paste it into CLI and type "save".  The following setting is meant for a F4 FC with an 32K gyro running the Fast Kalman filter firmware or build #493-521.  Below, I will explain what most of the settings are for and suggested settings for F4 FC with MPU6000 gyro.  If you are running the B+F (#522-current or 3.3 RC1), where it said
set gyro_kalman_q = 300
set gyro_kalman_r = 80

you want to change this to

set gyro_filter_q = 300
set gyro_filter_r = 80

Code:
# Suggested starting point, copied from RS2K thread

feature AIRMODE
feature ANTI_GRAVITY
feature DYNAMIC_FILTER

# Disable the 1/2 notches
set gyro_notch1_hz = 0
set gyro_notch2_hz = 0

# Conversative defaults for Kalman Q/R noise covariances
set gyro_kalman_q = 300
set gyro_kalman_r = 80

# Increase gyro std deviation movement calibration threshhold: allows gyro calibration
set moron_threshold = 128

# Enable the 32kHz gyro sampling mode, e.g. for MPU-9250, MPU-6500, ICM-20602 et al.
set gyro_use_32khz = ON

# Disable ACC
set acc_hardware = NONE

# While throttle is below 1020 can be used for command stick sequences
set min_check = 1020

# Manual RC smoothing/interpolation on Roll, Pitch, Yaw, and Throttle. 14ms interval. Adjust to suit your radio protocol.
set rc_interp = MANUAL
set rc_interp_ch = RPYT
set rc_interp_int = 14

# Minimum throttle sent to ESCs when armed
set min_throttle = 1050

# Unsynchronized PWM to enable 32kHz ESC output
set use_unsynced_pwm = ON

# Multishot (use DSHOT600/1200 if using 8k/16k SyncPWM e.g. for anti-turtle / dshot beacon)
set motor_pwm_protocol = MULTISHOT
set motor_pwm_rate = 32000

# Slightly increased defaults for RC Deadbands
set deadband = 5
set yaw_deadband = 5

# 16kHz PID loop when using 32kHz gyro sampling (/2)
set pid_process_denom = 2

# Add the FFT debug information to the Blackbox log
set debug_mode = FFT

# Enable CPU overclocking to either 192mHz, 216mHz, or 240mHz
set cpu_overclock = ON

# Use either PT1 or FIR lowpass
set dterm_lowpass_type = PT1

# Set frequency based on analysis of Blackbox FFT
set dterm_lowpass = 80

# Disable dterm notch
set dterm_notch_hz = 0

# Slightly adjust PID
set p_pitch = 61
set i_pitch = 55
set d_pitch = 20
set p_roll = 46
set i_roll = 45
set d_roll = 20
set p_yaw = 60
set i_yaw = 55

# Enable Derivative term for Yaw
set d_yaw = 10

The three features you will need are Airmode, Anti-gravity and Dynamic Filter.  For Airmode, you can actually put it on a switch and you can remove the permanent Airmode feature.

You will turn off your gyro notch and dterm notch filters.

The Q/R ratio.  Q and R value is what determines the amount of filtering.  If you set them to zero, then you are disabling the filter.  Anything other then zero, you are enabling the filter.  So it doesn't matter what the actual value of Q and R is, it is only their ratio that matters.

Example:
Q = 100 R = 10
Ratio = 100/10 = 10

Q = 1000 R = 100
Ratio = 1000/100 = 100

To the filter, these values are exactly the same.

There are two ways you can determine your Q/R ratio.  The first way is trial and error.  You start with the suggested values for Q and R.  Basically, higher Q/R ratio is less filtering and lower ratio is more filtering.  I suggest leaving R alone.  If your quad is flying great, raise Q by 100-200 to reduce filtering.  If it flies even better keep on raising.  If it doesn't get better, then take the Q back down.  If your quad doesn't fly well on starting values, then lower Q by 100-200.  Remember it is the ratio that matters.  When Q is less than R, you don't want to dramatically lower Q so much because you will dramatically change the ratio.  If you need more resolution for the ratio, you can multiple both Q and R by a factor of 5 or 10.  Then continue fine tuning Q.  Example, you current have Q =35 R=80, you can make Q=175 and R=400 and keep the same ratio.  Now you have more Q values to play with.

The second way to determine Q/R ratio is using Blackbox log.  One of the setting you will turn on is "set debug_mode = FFT".  Go fly, and get a Blackbox log.  Open the Blackbox Explorer.  Go to graph setup and add debug_FTT.

[Image: 3lHOdjvl.png]

Four things will be added to the graph.  Click on the little graph symbol to the right of gyro_raw[roll].

[Image: PjInWMDl.png]

The graphic analyser will pop-up.  Look at the max motor noise.  In my example, it is about 225Hz.  That's means I can use more filtering.  You want to set your Q/R ratio to get a cutoff as close as possible to 225Hz.  You can use the Excel Calculator in the attachment to get your Q and R values.  Or you can use this link.
https://quadmeup.com/bf_kalman_calculator/

[Image: x9gewwYl.jpg]

As you can see, I change my Q value to 38, which give me a cutoff value of 224Hz

[Image: iregDSll.png]

With the calculator, you can also change your gyro rates to match your settings.  32K = 32000; 8K = 8000; 4K = 4000

Moron_threshold??? Well, it just delays the gyro from calibrating after powering up.  If you are one of those people that moves the quad after plugging in the lipo, you can increase the value.  Here is a good explanation.
https://oscarliang.com/moron-threshold-betaflight/

set gyro_use_32khz = ON.  Just enable 32K sampling.  Only for 32K gyro, otherwise you want to turn this off.

set acc_hardware = NONE.  Turns off accelerometer to save CPU usage.  That means no auto-level mode.

Since it is recommended to use Multishot to save CPU usage, you want to calibrate your ESC, set your min_check and min_throttle.  You can use DSHOT if turning it on isn't making your CPU usage too high.  You can find the CPU usage percentage at the bottom of the Betaflight configurator.  It is recommended that you don't go over 50%.  If you use Multishot and 32K, you want to turn on use_unsynced_pwm.  And you want to set motor_pwm_rate = 32000.  Again, this is to save CPU usage.  If you are running lower sampling, you can leave pwm at synced.  

[Image: VQpRl3Cl.jpg]

Because of the vast computing power for the Fask Kalman, you will need to overclock the CPU.  If you plan on using B+F, there is a good chance you don't have to overclock.  You risk overheating your CPU if you overclock.  It is recommended that you don't fly on hot days if you are overclocking.  I know that the B+F uses less CPU power, so it is very likely you can run Dshot with 32K.

RC smoothing/interpolation is the firmware smoothing out stick movement.  By default, Betaflight has it on AUTO.  If you have CPU to spare, you can leave it on AUTO.  If you don't, then you will need to use the manual settings.  If you are getting a lot of D-kick, you will have to adjust the rc_interp_int.  Here is a good video on the explanation and how to adjust it.  You want interpolation for Roll, Ptich, Yaw and Throttle (RPYT)


Lastly, the recommended PID settings.  For myself, I still have to do just slight tuned it to the way I wanted my quad to feel.  Otherwise, the recommended PIDs are pretty good.

The rest of the settings I did not mentioned, I don't have a clue what they really do.  Well, I actually have some knowledge but not enough to explain it.

For B+F and 16K-8K gyro and looptime, these are the settings I would start with.  Please do check your CPU usage after you save the settings.  If you are not running 32K, your CPU usage should not be a problem.

Code:
feature AIRMODE
feature ANTI_GRAVITY
feature DYNAMIC_FILTER

# Disable the 1/2 notches
set gyro_notch1_hz = 0
set gyro_notch2_hz = 0

# Conversative defaults for Kalman Q/R noise covariances
set gyro_filter_q = 300
set gyro_filter_r = 80

# Increase gyro std deviation movement calibration threshhold: allows gyro calibration
set moron_threshold = 128

# Disable ACC
set acc_hardware = NONE

# Manual RC smoothing/interpolation on Roll, Pitch, Yaw, and Throttle. 14ms interval. Adjust to suit your radio protocol.
set rc_interp = MANUAL
set rc_interp_ch = RPYT
set rc_interp_int = 14


# Slightly increased defaults for RC Deadbands
set deadband = 5
set yaw_deadband = 5

# 16kHz PID loop when using 32kHz gyro sampling (/2)
set pid_process_denom = 2

# Add the FFT debug information to the Blackbox log
set debug_mode = FFT

# Enable CPU overclocking to either 192mHz, 216mHz, or 240mHz
set cpu_overclock = ON

# Use either PT1 or FIR lowpass
set dterm_lowpass_type = PT1

# Set frequency based on analysis of Blackbox FFT
set dterm_lowpass = 80

# Disable dterm notch
set dterm_notch_hz = 0

# Slightly adjust PID
set p_pitch = 61
set i_pitch = 55
set d_pitch = 20
set p_roll = 46
set i_roll = 45
set d_roll = 20
set p_yaw = 60
set i_yaw = 55

# Enable Derivative term for Yaw
set d_yaw = 10


For 4K/4K, change the R value to 20 instead of 80.

Anyway, if there are mistakes, please let me know so I can correct them.

UPDATE: If you are going to use 3.3 nightly #603 and up (likely also 3.3 RC2), gyro_filter_q and gryo_filter_r have been replaced. You will not longer use the spreadsheet or q/r ratio calculator to figure out cutoff hz. The new CLI parameter is gyro_stage2_lowpass_hz, use it to set your cutoff.

UPDATE 2: If you are using Fast Kalman Filter firmware by Kalyn, you need the CLI command below to activate it.
set gyro_stage2_filter_type = FKF

new suggested Q value is 400 and R value is 88


Attached Files
.xlsx   FKF Cutoff Calc.xlsx (Size: 3.84 KB / Downloads: 254)
[-] The following 9 users Like voodoo614's post:
  • joohaky, Esy, Logan629, sirdude, ZERONEST, Oscar, sloscotty, Tom BD Bad, RENOV8R
Reply
Login to remove this ad | Register Here
#2
Excellent write-up Voodoo, lots of useful info.
Reply
#3
Thanks. Definitely worth trying the filter now that it is in RC.
Reply
#4
I've got an Omibus F4 ij my QAV that I'm going to play around with on the weekend. But what I'm really interested in is the just released BrainFPV Radix that's on it's way. Thing is it uses the Bosch BM1160 gyro, so I've got some research to do. Looks like I'll be able to run 32/16 but I don't even think there's a newer target than the 3.2.4 that it comes loaded with. Definitely isn't in the github
Reply
#5
Since Brainfvp writes custom firmwares for their FC, you are kinda out of luck.
Reply
#6
The filter is actually called RC(LPF) + FIR2, not Biquad+FIR2.
RC+FIR2 can be implemented as a single second order filter, hence the biquad name.
This filter is what current Fast Kalman implementation converges to, you may see Fast Kalman as a differential equation and RC+FIR2 as it's one and only solution given fixed initial conditions.
[-] The following 1 user Likes AndreyM's post:
  • Tom BD Bad
Reply
#7
Thanks for the correct. I still see the filter referred to as the BIQUAD RC+FIR2 in the #603 nightly build.

Also nice that you can specify cutoff without all the spreadsheet calculations.
Reply
#8
(17-Feb-2018, 05:06 AM)voodoo614 Wrote: Thanks for the correct. I still see the filter referred to as the BIQUAD RC+FIR2 in the #603 nightly build.

Also nice that you can specify cutoff without all the spreadsheet calculations.
That's correct, biquad RC + FIR2, not Biquad+FIR2 :-)
Reply
#9
I made the corrections.

UPDATE: If you are going to use 3.3 nightly #603 and up (likely also 3.3 RC2), gyro_filter_q and gryo_filter_r have been replaced. You will not longer use the spreadsheet or q/r ratio calculator to figure out cutoff hz. The new CLI parameter is gyro_stage2_lowpass_hz, use it to set your cutoff.
[-] The following 1 user Likes voodoo614's post:
  • Rush
Reply
#10
New firmware is available for Fast Kalman Filter.
https://www.rcgroups.com/forums/showpost...tcount=746

To enable the filter

set gyro_use_32khz = ON
set cpu_overclock = ON
set gyro_stage2_filter_type = FKF
set gyro_kalman_q = 400
set gyro_kalman_r = 88
Reply
#11
I do believe for BIQUAD RC+FIR2 on the regular ßF branch overclocking is not needed. Overclocking is necessary for RS2K's FKF version.
Reply
#12
That is correct. Only FKF needs overclocking. I am not sure about F3 though. So I just make a note that you want to watch your CPU usage.
Reply
#13
(17-Feb-2018, 10:02 AM)voodoo614 Wrote: I made the corrections.

UPDATE: If you are going to use 3.3 nightly #603 and up (likely also 3.3 RC2), gyro_filter_q and gryo_filter_r have been replaced.  You will not longer use the spreadsheet or q/r ratio calculator to figure out cutoff hz.  The new CLI parameter is gyro_stage2_lowpass_hz, use it to set your cutoff.

Thanks a lot for your guide, I have a question:
On the gyro_stage2_lowpass_hz I have to put my max motor noise, for example using your values:

gyro_stage2_lowpass_hz = 225Hz
?
Reply
#14
(22-Feb-2018, 09:24 AM)Rush Wrote: Thanks a lot for your guide, I have a question:
On the gyro_stage2_lowpass_hz I have to put my max motor noise, for example using your values:

gyro_stage2_lowpass_hz = 225Hz
?

Yes, you want to set that to your max motor noise or just a slightly below that.
Reply
#15
In the latest RC2 release, I think they took out the filter for all F3. For those that want to run FKF on F3 and can't wait for BF, you can download ButterFlight. I have not tested, so you are on your own.

http://butterflight.co/
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Camera Looking for an ND filter for your GoPro Hero 8? Maybe Camera Butter has your solution 5zero7rc 0 1,397 11-Jan-2020, 05:20 PM
Last Post: 5zero7rc
  Vbat Filter amer 2 1,833 10-Apr-2016, 12:28 PM
Last Post: amer


Login to remove this ad | Register Here