Sensor Scol plugin
Multi platform sensors for handled devices
gyroscopeBiasEstimator.cpp
1
2#include "tools/gyroscopeBiasEstimator.h"
3#include <algorithm>
4
5GyroscopeBiasEstimator::GyroscopeBiasEstimator()
6{
7 reset();
8}
9
10GyroscopeBiasEstimator::~GyroscopeBiasEstimator()
11{
12
13}
14
15void GyroscopeBiasEstimator::reset()
16{
17 smoothedGyroDiff = Vector3d();
18 smoothedAccelDiff = Vector3d();
19 accelLowPass = LowPassFilter(ACCEL_LOWPASS_FREQ);
20 gyroLowPass = LowPassFilter(GYRO_LOWPASS_FREQ);
21 gyroBiasLowPass = LowPassFilter(GYRO_BIAS_LOWPASS_FREQ);
22 isAccelStatic = IsStaticCounter(IS_STATIC_NUM_FRAMES_THRESHOLD);
23 isGyroStatic = IsStaticCounter(IS_STATIC_NUM_FRAMES_THRESHOLD);
24}
25
26void GyroscopeBiasEstimator::processGyroscope(Vector3d gyro, int64_t sensorTimestampNs)
27{
28 gyroLowPass.addSample(gyro, sensorTimestampNs);
29 Vector3d::sub(gyro, gyroLowPass.getFilteredData(), smoothedGyroDiff);
30
31 isGyroStatic.appendFrame(smoothedGyroDiff.length() < GYRO_DIFF_STATIC_THRESHOLD);
32
33 if ((isGyroStatic.isRecentlyStatic()) && (isAccelStatic.isRecentlyStatic()))
34 updateGyroBias(gyro, sensorTimestampNs);
35}
36
37void GyroscopeBiasEstimator::processAccelerometer(Vector3d accel, int64_t sensorTimestampNs)
38{
39 accelLowPass.addSample(accel, sensorTimestampNs);
40 Vector3d::sub(accel, accelLowPass.getFilteredData(), smoothedAccelDiff);
41
42 isAccelStatic.appendFrame(smoothedAccelDiff.length() < ACCEL_DIFF_STATIC_THRESHOLD);
43}
44
45void GyroscopeBiasEstimator::getGyroBias(Vector3d &result)
46{
47 if (gyroBiasLowPass.getNumSamples() < NUM_GYRO_BIAS_SAMPLES_THRESHOLD)
48 {
49 result.setZero();
50 }
51 else
52 {
53 result.set(gyroBiasLowPass.getFilteredData());
54 double rampUpRatio = std::min(1.0, double((gyroBiasLowPass.getNumSamples() - NUM_GYRO_BIAS_SAMPLES_THRESHOLD)) / NUM_GYRO_BIAS_SAMPLES_INITIAL_SMOOTHING);
55 result.scale(rampUpRatio);
56 }
57}
58
59void GyroscopeBiasEstimator::updateGyroBias(Vector3d gyro, int64_t sensorTimestampNs)
60{
61 if (gyro.length() >= GYRO_FOR_BIAS_THRESHOLD)
62 {
63 return;
64 }
65
66 double updateWeight = std::max(0.0, 1.0 - gyro.length() * 25.0);
67 updateWeight *= updateWeight;
68 gyroBiasLowPass.addWeightedSample(gyroLowPass.getFilteredData(), sensorTimestampNs, updateWeight);
69}