blob: f3af9c5738881ec28b245797902783f1a8f26242 [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
James Kuszmaulb1e29372020-02-11 16:55:36 -08004#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmauld3f9eb22020-01-12 15:02:07 -08005#include "frc971/wpilib/imu_generated.h"
6#include "frc971/zeroing/averager.h"
7
8namespace frc971::zeroing {
9
10// This class handles processing IMU measurements and zeroing them once it is
11// able to do so.
12class ImuZeroer {
13 public:
James Kuszmaul30aca502020-01-19 15:05:33 -080014 // Average 0.5 seconds of data (assuming 2kHz sampling rate).
James Kuszmaul3e1bb272020-01-17 18:38:19 -080015 // TODO(james): Make the gyro zero in a constant amount of time, rather than a
16 // constant number of samples...
James Kuszmaul30aca502020-01-19 15:05:33 -080017 // TODO(james): Run average and GetRange calculations over every sample on
18 // every timestep, to provide consistent timing.
James Kuszmaul0a981402021-10-09 21:00:34 -070019 static constexpr size_t kSamplesToAverage = 200;
James Kuszmaulb1e29372020-02-11 16:55:36 -080020 static constexpr size_t kRequiredZeroPoints = 10;
James Kuszmauld3f9eb22020-01-12 15:02:07 -080021
22 ImuZeroer();
23 bool Zeroed() const;
24 bool Faulted() const;
James Kuszmaulb7f45bb2020-02-26 20:27:48 -080025 void InsertMeasurement(const IMUValues &values);
26 // PErforms the heavier-duty processing for managing zeroing.
27 void ProcessMeasurements();
28 void InsertAndProcessMeasurement(const IMUValues &values);
James Kuszmauld3f9eb22020-01-12 15:02:07 -080029 Eigen::Vector3d GyroOffset() const;
30 Eigen::Vector3d ZeroedGyro() const;
31 Eigen::Vector3d ZeroedAccel() const;
James Kuszmaulb1e29372020-02-11 16:55:36 -080032
33 flatbuffers::Offset<control_loops::drivetrain::ImuZeroerState> PopulateStatus(
34 flatbuffers::FlatBufferBuilder *fbb) const;
35
James Kuszmauld3f9eb22020-01-12 15:02:07 -080036 private:
37 // Max variation (difference between the maximum and minimum value) in a
38 // kSamplesToAverage range before we allow using the samples for zeroing.
39 // These values are currently based on looking at results from the ADIS16448.
James Kuszmaulb1e29372020-02-11 16:55:36 -080040 static constexpr double kGyroMaxVariation = 0.02; // rad / sec
41 // Maximum magnitude we allow the gyro zero to have--this is used to prevent
42 // us from zeroing the gyro if we just happen to be spinning at a very
43 // consistent non-zero rate. Currently this is only plausible in simulation.
44 static constexpr double kGyroMaxZeroingMagnitude = 0.1; // rad / sec
James Kuszmauld3f9eb22020-01-12 15:02:07 -080045 // Max variation in the range before we consider the accelerometer readings to
46 // be steady.
James Kuszmaulb1e29372020-02-11 16:55:36 -080047 static constexpr double kAccelMaxVariation = 0.05; // g's
James Kuszmauld3f9eb22020-01-12 15:02:07 -080048 // If we ever are able to rezero and get a zero that is more than
49 // kGyroFaultVariation away from the original zeroing, fault.
James Kuszmaulad2f8db2021-10-24 17:02:57 -070050 static constexpr double kGyroFaultVariation = 0.05; // rad / sec
James Kuszmauld3f9eb22020-01-12 15:02:07 -080051
52 bool GyroZeroReady() const;
53 bool AccelZeroReady() const;
54
55 Averager<double, kSamplesToAverage, 3> gyro_averager_;
56 // Averager for the accelerometer readings--we don't currently actually
57 // average the readings, but we do check that the accelerometer readings have
58 // stayed roughly constant during the calibration period.
59 Averager<double, kSamplesToAverage, 3> accel_averager_;
60 // The average zero position of the gyro.
61 Eigen::Vector3d gyro_average_;
James Kuszmaulb1e29372020-02-11 16:55:36 -080062 Eigen::Vector3d accel_average_;
James Kuszmauld3f9eb22020-01-12 15:02:07 -080063 Eigen::Vector3d last_gyro_sample_;
64 Eigen::Vector3d last_accel_sample_;
James Kuszmauld3f9eb22020-01-12 15:02:07 -080065 // Whether the zeroing has faulted at any point thus far.
66 bool faulted_ = false;
James Kuszmaulb1e29372020-02-11 16:55:36 -080067 size_t good_iters_ = 0;
68 size_t num_zeroes_ = 0;
James Kuszmauld3f9eb22020-01-12 15:02:07 -080069};
70
71} // namespace frc971::zeroing
72#endif // FRC971_ZEROING_IMU_ZEROER_H_