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