Ravago Jones | ea6464c | 2020-10-10 15:40:46 -0700 | [diff] [blame] | 1 | #ifndef frc971_zeroing_absolute_and_absolute_encoder_h_ |
| 2 | #define frc971_zeroing_absolute_and_absolute_encoder_h_ |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "flatbuffers/flatbuffers.h" |
| 7 | |
Ravago Jones | 726deb0 | 2021-05-29 14:36:43 -0700 | [diff] [blame] | 8 | #include "aos/containers/error_list.h" |
Ravago Jones | ea6464c | 2020-10-10 15:40:46 -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 { |
Ravago Jones | ea6464c | 2020-10-10 15:40:46 -0700 | [diff] [blame] | 12 | |
| 13 | // Estimates the position with an absolute encoder which also reports |
| 14 | // incremental counts and an absolute encoder that's not allowed to turn more |
| 15 | // than once. This is like the PotAndAbsoluteEncoderZeroingEstimator, but it |
| 16 | // uses the single turn absolute encoder instead of the potentiometer. |
| 17 | class AbsoluteAndAbsoluteEncoderZeroingEstimator |
| 18 | : public ZeroingEstimator< |
| 19 | AbsoluteAndAbsolutePosition, |
| 20 | constants::AbsoluteAndAbsoluteEncoderZeroingConstants, |
| 21 | AbsoluteAndAbsoluteEncoderEstimatorState> { |
| 22 | public: |
| 23 | explicit AbsoluteAndAbsoluteEncoderZeroingEstimator( |
| 24 | const constants::AbsoluteAndAbsoluteEncoderZeroingConstants &constants); |
| 25 | |
| 26 | // Resets the internal logic so it needs to be re-zeroed. |
| 27 | void Reset() override; |
| 28 | |
| 29 | // Updates the sensor values for the zeroing logic. |
| 30 | void UpdateEstimate(const AbsoluteAndAbsolutePosition &info) override; |
| 31 | |
| 32 | void TriggerError() override { error_ = true; } |
| 33 | |
| 34 | bool zeroed() const override { return zeroed_; } |
| 35 | |
Ravago Jones | 937587c | 2020-12-26 17:21:09 -0800 | [diff] [blame] | 36 | double offset() const override { return first_offset_; } |
Ravago Jones | ea6464c | 2020-10-10 15:40:46 -0700 | [diff] [blame] | 37 | |
| 38 | bool error() const override { return error_; } |
| 39 | |
| 40 | // Returns true if the sample buffer is full. |
| 41 | bool offset_ready() const override { |
| 42 | return relative_to_absolute_offset_samples_.size() == |
| 43 | constants_.average_filter_size && |
| 44 | offset_samples_.size() == constants_.average_filter_size; |
| 45 | } |
| 46 | |
| 47 | // Returns information about our current state. |
| 48 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 49 | flatbuffers::FlatBufferBuilder *fbb) const override; |
| 50 | |
milind-u | d53408e | 2021-10-21 19:43:58 -0700 | [diff] [blame] | 51 | protected: |
Ravago Jones | ea6464c | 2020-10-10 15:40:46 -0700 | [diff] [blame] | 52 | struct PositionStruct { |
| 53 | PositionStruct(const AbsoluteAndAbsolutePosition &position_buffer) |
| 54 | : single_turn_absolute_encoder( |
| 55 | position_buffer.single_turn_absolute_encoder()), |
| 56 | absolute_encoder(position_buffer.absolute_encoder()), |
| 57 | encoder(position_buffer.encoder()) {} |
| 58 | double single_turn_absolute_encoder; |
| 59 | double absolute_encoder; |
| 60 | double encoder; |
| 61 | }; |
| 62 | |
milind-u | d53408e | 2021-10-21 19:43:58 -0700 | [diff] [blame] | 63 | // Returns an adjusted single turn absolute encoder reading. |
| 64 | // Filled in by default but can be overriden. |
| 65 | virtual double AdjustedSingleTurnAbsoluteEncoder( |
| 66 | const PositionStruct &sample) const; |
| 67 | |
| 68 | private: |
Ravago Jones | ea6464c | 2020-10-10 15:40:46 -0700 | [diff] [blame] | 69 | // The zeroing constants used to describe the configuration of the system. |
| 70 | const constants::AbsoluteAndAbsoluteEncoderZeroingConstants constants_; |
| 71 | |
| 72 | // True if the mechanism is zeroed. |
| 73 | bool zeroed_; |
| 74 | // Marker to track whether an error has occurred. |
| 75 | bool error_; |
Ravago Jones | 726deb0 | 2021-05-29 14:36:43 -0700 | [diff] [blame] | 76 | // Marker to track what kind of error has occured. |
| 77 | aos::ErrorList<ZeroingError> errors_; |
Ravago Jones | ea6464c | 2020-10-10 15:40:46 -0700 | [diff] [blame] | 78 | // The first valid offset we recorded. This is only set after zeroed_ first |
| 79 | // changes to true. |
| 80 | double first_offset_; |
| 81 | |
| 82 | // The filtered absolute encoder. This is used in the status for calibration. |
| 83 | double filtered_absolute_encoder_ = 0.0; |
| 84 | |
| 85 | // The filtered single turn absolute encoder. This is used in the status for |
| 86 | // calibration. |
| 87 | double filtered_single_turn_absolute_encoder_ = 0.0; |
| 88 | |
| 89 | // Samples of the offset needed to line the relative encoder up with the |
| 90 | // absolute encoder. |
| 91 | ::std::vector<double> relative_to_absolute_offset_samples_; |
| 92 | |
| 93 | // Offset between the single turn absolute encoder and relative encoder |
| 94 | // position. |
| 95 | ::std::vector<double> offset_samples_; |
| 96 | |
| 97 | MoveDetector<PositionStruct, AbsoluteAndAbsolutePosition> move_detector_; |
| 98 | |
| 99 | // Estimated offset between the single turn encoder and relative encoder. |
| 100 | double single_turn_to_relative_encoder_offset_ = 0; |
| 101 | // Estimated start position of the mechanism |
| 102 | double offset_ = 0; |
| 103 | // The next position in 'relative_to_absolute_offset_samples_' and |
| 104 | // 'encoder_samples_' to be used to store the next sample. |
| 105 | int samples_idx_ = 0; |
| 106 | |
| 107 | size_t nan_samples_ = 0; |
| 108 | |
| 109 | // The unzeroed filtered position. |
| 110 | double filtered_position_ = 0.0; |
| 111 | // The filtered position. |
| 112 | double position_ = 0.0; |
| 113 | }; |
| 114 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 115 | } // namespace frc971::zeroing |
Ravago Jones | ea6464c | 2020-10-10 15:40:46 -0700 | [diff] [blame] | 116 | |
| 117 | #endif // FRC971_ZEROING_ABSOLUTE_AND_ABSOLUTE_ENCODER_H_ |