blob: 5f53e0d78a5ee65188d8d832b969811278e2ab1f [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).
James Kuszmaul3e1bb272020-01-17 18:38:19 -080014 // TODO(james): Make the gyro zero in a constant amount of time, rather than a
15 // constant number of samples...
James Kuszmauld3f9eb22020-01-12 15:02:07 -080016 static constexpr size_t kSamplesToAverage = 10000.0;
17
18 ImuZeroer();
19 bool Zeroed() const;
20 bool Faulted() const;
21 void ProcessMeasurement(const IMUValues &values);
22 Eigen::Vector3d GyroOffset() const;
23 Eigen::Vector3d ZeroedGyro() const;
24 Eigen::Vector3d ZeroedAccel() const;
25 private:
26 // Max variation (difference between the maximum and minimum value) in a
27 // kSamplesToAverage range before we allow using the samples for zeroing.
28 // These values are currently based on looking at results from the ADIS16448.
29 static constexpr double kGyroMaxVariation = 0.02; // rad / sec
30 // Max variation in the range before we consider the accelerometer readings to
31 // be steady.
32 static constexpr double kAccelMaxVariation = 0.02; // g's
33 // If we ever are able to rezero and get a zero that is more than
34 // kGyroFaultVariation away from the original zeroing, fault.
35 static constexpr double kGyroFaultVariation = 0.005; // rad / sec
36
37 bool GyroZeroReady() const;
38 bool AccelZeroReady() const;
39
40 Averager<double, kSamplesToAverage, 3> gyro_averager_;
41 // Averager for the accelerometer readings--we don't currently actually
42 // average the readings, but we do check that the accelerometer readings have
43 // stayed roughly constant during the calibration period.
44 Averager<double, kSamplesToAverage, 3> accel_averager_;
45 // The average zero position of the gyro.
46 Eigen::Vector3d gyro_average_;
47 Eigen::Vector3d last_gyro_sample_;
48 Eigen::Vector3d last_accel_sample_;
49 // Whether we have currently zeroed yet.
50 bool zeroed_ = false;
51 // Whether the zeroing has faulted at any point thus far.
52 bool faulted_ = false;
53};
54
55} // namespace frc971::zeroing
56#endif // FRC971_ZEROING_IMU_ZEROER_H_