Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame] | 1 | #ifndef FRC971_ZEROING_ABSOLUTE_ENCODER_H_ |
| 2 | #define FRC971_ZEROING_ABSOLUTE_ENCODER_H_ |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "flatbuffers/flatbuffers.h" |
| 7 | |
James Kuszmaul | ec635d2 | 2023-08-12 18:39:24 -0700 | [diff] [blame] | 8 | #include "aos/containers/error_list.h" |
Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame] | 9 | #include "frc971/zeroing/zeroing.h" |
| 10 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 11 | namespace frc971::zeroing { |
Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame] | 12 | |
| 13 | // Estimates the position with an absolute encoder which also reports |
| 14 | // incremental counts. The absolute encoder can't spin more than one |
| 15 | // revolution. |
| 16 | class AbsoluteEncoderZeroingEstimator |
| 17 | : public ZeroingEstimator<AbsolutePosition, |
| 18 | constants::AbsoluteEncoderZeroingConstants, |
| 19 | AbsoluteEncoderEstimatorState> { |
| 20 | public: |
| 21 | explicit AbsoluteEncoderZeroingEstimator( |
| 22 | const constants::AbsoluteEncoderZeroingConstants &constants); |
| 23 | |
| 24 | // Resets the internal logic so it needs to be re-zeroed. |
| 25 | void Reset() override; |
| 26 | |
| 27 | // Updates the sensor values for the zeroing logic. |
| 28 | void UpdateEstimate(const AbsolutePosition &info) override; |
| 29 | |
| 30 | void TriggerError() override { error_ = true; } |
| 31 | |
| 32 | bool zeroed() const override { return zeroed_; } |
| 33 | |
| 34 | double offset() const override { return offset_; } |
| 35 | |
| 36 | bool error() const override { return error_; } |
| 37 | |
| 38 | // Returns true if the sample buffer is full. |
| 39 | bool offset_ready() const override { |
| 40 | return relative_to_absolute_offset_samples_.size() == |
| 41 | constants_.average_filter_size; |
| 42 | } |
| 43 | |
| 44 | // Returns information about our current state. |
| 45 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 46 | flatbuffers::FlatBufferBuilder *fbb) const override; |
| 47 | |
| 48 | private: |
| 49 | struct PositionStruct { |
| 50 | PositionStruct(const AbsolutePosition &position_buffer) |
| 51 | : absolute_encoder(position_buffer.absolute_encoder()), |
| 52 | encoder(position_buffer.encoder()) {} |
| 53 | double absolute_encoder; |
| 54 | double encoder; |
| 55 | }; |
| 56 | |
| 57 | // The zeroing constants used to describe the configuration of the system. |
| 58 | const constants::AbsoluteEncoderZeroingConstants constants_; |
| 59 | |
| 60 | // True if the mechanism is zeroed. |
| 61 | bool zeroed_; |
| 62 | // Marker to track whether an error has occurred. |
| 63 | bool error_; |
| 64 | // The first valid offset we recorded. This is only set after zeroed_ first |
| 65 | // changes to true. |
| 66 | double first_offset_; |
| 67 | |
| 68 | // The filtered absolute encoder. This is used in the status for calibration. |
| 69 | double filtered_absolute_encoder_ = 0.0; |
| 70 | |
| 71 | // Samples of the offset needed to line the relative encoder up with the |
| 72 | // absolute encoder. |
| 73 | ::std::vector<double> relative_to_absolute_offset_samples_; |
| 74 | |
| 75 | MoveDetector<PositionStruct, AbsolutePosition> move_detector_; |
| 76 | |
| 77 | // Estimated start position of the mechanism |
| 78 | double offset_ = 0; |
| 79 | // The next position in 'relative_to_absolute_offset_samples_' and |
| 80 | // 'encoder_samples_' to be used to store the next sample. |
| 81 | int samples_idx_ = 0; |
| 82 | |
| 83 | // Number of NANs we've seen in a row. |
| 84 | size_t nan_samples_ = 0; |
| 85 | |
| 86 | // The filtered position. |
| 87 | double position_ = 0.0; |
Ravago Jones | 726deb0 | 2021-05-29 14:36:43 -0700 | [diff] [blame] | 88 | |
| 89 | // Marker to track what kind of error has occured. |
| 90 | aos::ErrorList<ZeroingError> errors_; |
Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 93 | } // namespace frc971::zeroing |
Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame] | 94 | |
| 95 | #endif // FRC971_ZEROING_ABSOLUTE_ENCODER_H_ |