Regarding Kalman filtering, see the definition on Baidu Encyclopedia
The core idea of the algorithm is to calculate the current optimal value based on the current instrument "measured value" and the "predicted value" and "error" of the previous moment.
When predicting the quantity at the next moment, the most prominent point is that the error is included in the calculation, and it is divided into two types: prediction error and measurement error. It is commonly called noise. Another very big feature is that the error exists independently and is never affected by the measurement data.
Next, let’s understand the meaning of several parameters in a Kalman filter: Probability, Random Variable, Gaussian Distribution, State-space Model, etc.
There are already many articles on the Internet about the meaning and derivation of the Kalman formula. I will not go into details here, but directly look at the implementation of the C code.
/*
The R value is fixed. The larger the Q value, the more trust the measured value has. The infinite Q value means that only the measured value is used.
The smaller the Q value, the more trust the model prediction value has. A Q value of 0 means that only the model prediction value is used.
*/
//Parameter 1
float KalmanFilter( float inData )
{
static float prevData = 0; //Previous data
static float p = 10, q = 0.001, r = 0.001, kGain = 0; // q controls the error r controls the response speed
p = p + q;
kGain = p / ( p + r ); //Calculate Kalman gain
inData = prevData + ( kGain * ( inData - prevData ) ); //Calculate the estimated value of this filter
p = ( 1 - kGain ) * p; //Update measurement variance
prevData = inData;
return inData; //Return estimated value
}
Now let's test the effect of Kalman filtering. A sawtooth wave is generated by the function generator and sent to the AD port of the microcontroller. After the microcontroller reads the collected AD data, it passes through the Kalman filtering algorithm, and then the sampled data and filtered data are generated through the serial port and displayed on the serial port waveform display software.
void main( void )
{
while( 1 )
{
val1 = ReadVol_CH3(); //Read AD sampling data
dat = ( float )val1;
dat = KalmanFilter( dat ); // Kalman filter
printf("A%drn",val1); //print result
printf("B%2frn",dat);
}
}
Now let's look at the results of the filtering
The blue curve is the original sampled data curve, and the orange curve is the curve after Kalman filtering.
Next, change the values of Q and R to test the filtering effect.
The modified parameters are as follows
//Parameter 2
unsigned long kalman_filter( unsigned long ADC_Value )
{
float LastData;
float NowData;
float kalman_adc;
static float kalman_adc_old = 0;
static float P1;
static float Q = 0.0003;
static float R = 5;
static float Kg = 0;
static float P = 1;
NowData = ADC_Value;
LastData = kalman_adc_old;
P = P1 + Q;
Kg = P / ( P + R );
kalman_adc = LastData + Kg * ( NowData - kalman_adc_old );
P1 = ( 1 - Kg ) * P;
P = P1;
kalman_adc_old = kalman_adc;
return ( unsigned long )( kalman_adc );
}
Test waveform
The blue curve is the original sampled data curve, and the orange curve is the curve after Kalman filtering.
By comparing the waveform of the first test with that of the second test, it can be found that the waveform after the second Kalman filter changes greatly. After the parameters are changed, the sawtooth wave is filtered into a nearly straight line.
It can be seen that different R and Q values will have a great impact on the measurement results.
Q: Process noise. As Q increases, the dynamic response becomes faster and the convergence stability becomes worse.
R: measurement noise. As R increases, the dynamic response slows down and the convergence stability improves.
How to select specific parameters can only be adjusted slowly by yourself according to the measurement results in the application. At present, no authoritative information has been found to explain how to select these parameters.
Previous article:STM8S ADC initialization settings and applications
Next article:MCU ADC sampling algorithm ---- first-order low-pass filtering
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- About Slope Sports
- Looking for single-cell lithium battery (3.7V) protection chip
- Port the STM32F407 keil version program to IAR for ARM
- Reimagining Antenna Design Solutions for Next-Generation Mobile Devices
- Electronic skin
- IEEE 802.11 common technical standards
- Better than seeing! See how 77GHz millimeter-wave radar protects ADAS
- How to improve the power of voltage doubler rectifier circuit
- DAP Miniwiggler emulator usage issues
- Selection and application of transmitters in several special working conditions