blob: e95c2318c90aaefab1f206d88386c69162698a89 [file] [log] [blame]
James Kuszmauld3f9eb22020-01-12 15:02:07 -08001#ifndef FRC971_ZEROING_IMU_ZEROER_H_
2#define FRC971_ZEROING_IMU_ZEROER_H_
3
4#include "frc971/wpilib/imu_generated.h"
5#include "frc971/zeroing/averager.h"
6
7namespace frc971::zeroing {
8
9// This class handles processing IMU measurements and zeroing them once it is
10// able to do so.
11class ImuZeroer {
12 public:
13 // Average 5 seconds of data (assuming 2kHz sampling rate).
14 static constexpr size_t kSamplesToAverage = 10000.0;
15
16 ImuZeroer();
17 bool Zeroed() const;
18 bool Faulted() const;
19 void ProcessMeasurement(const IMUValues &values);
20 Eigen::Vector3d GyroOffset() const;
21 Eigen::Vector3d ZeroedGyro() const;
22 Eigen::Vector3d ZeroedAccel() const;
23 private:
24 // Max variation (difference between the maximum and minimum value) in a
25 // kSamplesToAverage range before we allow using the samples for zeroing.
26 // These values are currently based on looking at results from the ADIS16448.
27 static constexpr double kGyroMaxVariation = 0.02; // rad / sec
28 // Max variation in the range before we consider the accelerometer readings to
29 // be steady.
30 static constexpr double kAccelMaxVariation = 0.02; // g's
31 // If we ever are able to rezero and get a zero that is more than
32 // kGyroFaultVariation away from the original zeroing, fault.
33 static constexpr double kGyroFaultVariation = 0.005; // rad / sec
34
35 bool GyroZeroReady() const;
36 bool AccelZeroReady() const;
37
38 Averager<double, kSamplesToAverage, 3> gyro_averager_;
39 // Averager for the accelerometer readings--we don't currently actually
40 // average the readings, but we do check that the accelerometer readings have
41 // stayed roughly constant during the calibration period.
42 Averager<double, kSamplesToAverage, 3> accel_averager_;
43 // The average zero position of the gyro.
44 Eigen::Vector3d gyro_average_;
45 Eigen::Vector3d last_gyro_sample_;
46 Eigen::Vector3d last_accel_sample_;
47 // Whether we have currently zeroed yet.
48 bool zeroed_ = false;
49 // Whether the zeroing has faulted at any point thus far.
50 bool faulted_ = false;
51};
52
53} // namespace frc971::zeroing
54#endif // FRC971_ZEROING_IMU_ZEROER_H_