First of all, make a small advertisement. I have always liked drones. Recently, I have time to study the Pixhawk (PX4) flight controller, and write the summary and experience in the official account “SantyNotebook” to share with you. Interested friends can come together Let’s talk about how to write drone flight control from scratch~
This article is simultaneously published in the official account push “Gyroscope Filtering and Sensor Temperature Compensation”
We know that the IMU is easily affected by the vibration of the aircraft body. Physically, a shock absorber is usually installed at the installation position of the flight control; and the measurement results of the IMU and the barometer will drift due to temperature changes. Physically, a constant temperature device is usually set to reduce this. kind of influence
How to do it at the software level? Let’s take a look at the principles of frequency domain filtering and temperature compensation and their implementation on PX4
A gyroscope filter
Filtering is used to filter out high-frequency noise and retain low-frequency signals. The defect of filtering processing is that it will cause signal lag. There are two methods of designing filters in the industry, time domain and frequency domain. Filters designed in the time domain, independent of specific models. Frequency domain filtering is more targeted, but computationally expensive
1 principle
1.1 Time Domain Filtering
The filter is mainly to perform convolution calculation on the discrete signal, and the design of the filter in the time domain depends on the empirical formula or the observation of the output waveform to adjust the parameters
The finite impulse response digital filter (Finite Impulse Response Digital Filter, FIR) infinite impulse response digital filter (Infinite Impulse Response Digital Filter, IIR) is expressed as follows
FIR
IIR
It can be seen that FIR is the smoothing process of the signal looking forward at several time points, and IIR is not only the historical signal, but also combined with the output feedback for smoothing
1.2 Frequency Domain Filtering
Filter parameters can also be designed in the frequency domain
The Fourier principle shows that the sequence or signal of continuous measurement can be expressed as an infinite superposition of sine wave signals of different frequencies. Then, the Fourier transform of the continuous-time signal can be used to analyze the frequency spectrum of the signal
Its expression can be generalized to discrete signals in form. In contrast, the Discrete Fourier Transform (DFT) spectrum analysis method is more widely used in engineering, such as image processing, because it allows the computer to process the collected discrete signals
And FFT (Fast Fourier Transform Fast Fourier Transform) is a fast implementation algorithm of DFT, which reduces the computational complexity from
From the point of view of the FFT derivation process, its essence is to convert the polynomial coefficient representation into a point value representation within a lower time complexity, and the inverse Fourier transform iFFT converts the point value representation back to the coefficient representation
Suppose the sampling frequency is Fs, and the number of sampling points is N. After doing FFT, the frequency represented by a certain point
Dividing the modulus value of a + bi at this point by N/2 is the amplitude of the signal corresponding to the frequency (for DC signals, it is divided by N >); the phase of this point is the phase of the signal corresponding to this frequency. Phase Available Expressions
come and get
During project implementation, it is usually necessary to truncate periodic time-domain signals. If the truncation time length is not an integer multiple of the period, the signal will leak after truncation. At this time, windowing is needed to make the time-domain signal better meet the periodicity requirements of FFT processing and reduce leakage. Multiplying the original truncated signal with this window function is essentially to unequally weight the time domain signal
The Hanning window is a commonly used window function, and its time domain expression is
With frequency domain characteristics, signals of specific frequencies and amplitudes can be suppressed to achieve noise reduction
If the noise spectrum distribution is narrow, a notch filter can be used, and if it is wide, a band-stop filter can be used. Generally, a notch filter is used in combination with a low-pass filter. Compared with ordinary high-pass and low-pass filters, the notch filter precisely suppresses the frequency band, making the signal attenuate rapidly at certain frequency points
PX4 adopts Notch notch filter and filter series noise reduction
Transfer function of the second-order notch filter for continuous systems
Where is Notch width, unit rad/s; is the notch central frequency, unit rad/s
It can be discretized by bilinear transformation or pole-zero matching method, and PX4 adopts the latter. For the convenience of readers to understand the code, only the pole-zero matching method is derived here
The zero poles are expressed as
where ; is the pole of the notch filter, with
Zero-pole matching means that the zero-pole in the s domain corresponds to the z-domain, and the i-th zero-pole correspondence is
So, the z-plane transfer function is expressed as
where is the discretization sampling time, unit s
When the s field is 0, the z field is 1, that is
substitute available
So there is
The difference equation in the time domain is
can be used to iterate over new samples
For another low-pass filter, the parameter design needs to be combined with the cut-off Frequency, etc., due to space limitations, the content is omitted
2 PX4 filter implementation
2.1STM32 support for DSP
Discrete signal processing on STM32 requires CortexM4 hardware support. Cortex-M4 supports advanced SIMD, MAC instructions. Additionally, Cortex-M4F devices have an FPU (Floating Point Unit) to handle floating point calculations
At the software level, the open source Arm Compiler 5 implements a set of DSP function libraries for discrete signal processing
source code, manual
2.2 Implementation process
PX4 borrows this part of the code to complete the FFT operation, the process is as follows
(1) Convert floating-point numbers to fixed-point format during initialization
for (int n = 0; n < _imu_gyro_fft_len; n ++ ) { const float hanning_value = 0.5f * (1.f - cosf(2.f * M_PI_F * n / (_imu_gyro_fft_len - 1))); arm_float_to_q15( &hanning_value, &_hanning_window[n], 1); }
(2) When the gyroscope data is updated, execute the filtering logic
void GyroFFT::Update(const hrt_abstime & amp;timestamp_sample, int16_t *input[], uint8_t N) { ... }
(3) Window function, multiplication
arm_mult_q15(gyro_data_buffer[axis], _hanning_window, _fft_input_buffer, _imu_gyro_fft_len);
(4) Perform RFFT inverse fast Fourier transform
arm_rfft_q15( &_rfft_q15, _fft_input_buffer, _fft_outupt_buffer);
(5) Determine the noise point according to the peak value, and use the Quiin method to interpolate to estimate the noise frequency
void GyroFFT::FindPeaks(const hrt_abstime & amp;timestamp_sample, int axis, q15_t *fft_outupt_buffer) { ... }
(6) Combining frequency domain characteristics, design filters, and perform filtering iterations
// Apply dynamic notch filter from FFT if (_dynamic_notch_fft_available) { for (int peak = MAX_NUM_FFT_PEAKS - 1; peak >= 0; peak--) { if (_dynamic_notch_filter_fft[axis][peak].getNotchFreq() > 0.f) { _dynamic_notch_filter_fft[axis][peak].applyArray(data, N); } } }
In this way, the output signal can be used in the upper layer application
Two temperature compensation
1 principle
Temperature has a significant impact on the measurement results of the IMU and barometer modules. The sensor has a preset reference temperature when it works. When the actual temperature deviates, the measurement result will be offset. Usually, a polynomial is used to describe this offset relationship.
To implement polynomial operations in computers, the Horner algorithm (also known as Qin Jiushao algorithm) is often used to simplify the execution steps, as follows
2 PX4 implementation
2.1 Calibration
Temperature rise, polynomial fitting calibration coefficient
if (_accel) { calibrators[num_calibrators] = new TemperatureCalibrationAccel(min_temp_rise, min_start_temp, max_start_temp); if (calibrators[num_calibrators]) { + + num_calibrators; } else { PX4_ERR("alloc failed"); } }
2.2 update
Combining the temperature measurement results to calculate the offset (taking addition as an example)
// calculate the offsets float delta_temp_2 = delta_temp * delta_temp; float delta_temp_3 = delta_temp_2 * delta_temp; for (uint8_t i = 0; i < 3; i ++ ) { offset[i] = coef.x0[i] + coef.x1[i] * delta_temp + coef.x2[i] * delta_temp_2 + coef.x3[i] * delta_temp_3; }
From the perspective of implementation, the process of designing filters, sensor calibration, and temperature compensation coefficient calibration are all raw data without any processing. The actual execution process is that the raw sensor data is first filtered, then corrected and temperature compensated, and finally used for upper-layer applications