Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame^] | 1 | #ifndef FRC971_ZEROING_POT_AND_ABSOLUTE_ENCODER_H_ |
| 2 | #define FRC971_ZEROING_POT_AND_ABSOLUTE_ENCODER_H_ |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "flatbuffers/flatbuffers.h" |
| 7 | |
| 8 | #include "frc971/zeroing/zeroing.h" |
| 9 | |
| 10 | namespace frc971 { |
| 11 | namespace zeroing { |
| 12 | |
| 13 | // Estimates the position with an absolute encoder which also reports |
| 14 | // incremental counts, and a potentiometer. |
| 15 | class PotAndAbsoluteEncoderZeroingEstimator |
| 16 | : public ZeroingEstimator<PotAndAbsolutePosition, |
| 17 | constants::PotAndAbsoluteEncoderZeroingConstants, |
| 18 | PotAndAbsoluteEncoderEstimatorState> { |
| 19 | public: |
| 20 | explicit PotAndAbsoluteEncoderZeroingEstimator( |
| 21 | const constants::PotAndAbsoluteEncoderZeroingConstants &constants); |
| 22 | |
| 23 | // Resets the internal logic so it needs to be re-zeroed. |
| 24 | void Reset() override; |
| 25 | |
| 26 | // Updates the sensor values for the zeroing logic. |
| 27 | void UpdateEstimate(const PotAndAbsolutePosition &info) override; |
| 28 | |
| 29 | void TriggerError() override { error_ = true; } |
| 30 | |
| 31 | bool zeroed() const override { return zeroed_; } |
| 32 | |
| 33 | double offset() const override { return offset_; } |
| 34 | |
| 35 | bool error() const override { return error_; } |
| 36 | |
| 37 | // Returns true if the sample buffer is full. |
| 38 | bool offset_ready() const override { |
| 39 | return relative_to_absolute_offset_samples_.size() == |
| 40 | constants_.average_filter_size && |
| 41 | offset_samples_.size() == 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 PotAndAbsolutePosition &position_buffer) |
| 51 | : absolute_encoder(position_buffer.absolute_encoder()), |
| 52 | encoder(position_buffer.encoder()), |
| 53 | pot(position_buffer.pot()) {} |
| 54 | double absolute_encoder; |
| 55 | double encoder; |
| 56 | double pot; |
| 57 | }; |
| 58 | |
| 59 | // The zeroing constants used to describe the configuration of the system. |
| 60 | const constants::PotAndAbsoluteEncoderZeroingConstants constants_; |
| 61 | |
| 62 | // True if the mechanism is zeroed. |
| 63 | bool zeroed_; |
| 64 | // Marker to track whether an error has occurred. |
| 65 | bool error_; |
| 66 | // The first valid offset we recorded. This is only set after zeroed_ first |
| 67 | // changes to true. |
| 68 | double first_offset_; |
| 69 | |
| 70 | // The filtered absolute encoder. This is used in the status for calibration. |
| 71 | double filtered_absolute_encoder_ = 0.0; |
| 72 | |
| 73 | // Samples of the offset needed to line the relative encoder up with the |
| 74 | // absolute encoder. |
| 75 | ::std::vector<double> relative_to_absolute_offset_samples_; |
| 76 | // Offset between the Pot and Relative encoder position. |
| 77 | ::std::vector<double> offset_samples_; |
| 78 | |
| 79 | MoveDetector<PositionStruct, PotAndAbsolutePosition> move_detector_; |
| 80 | |
| 81 | // Estimated offset between the pot and relative encoder. |
| 82 | double pot_relative_encoder_offset_ = 0; |
| 83 | // Estimated start position of the mechanism |
| 84 | double offset_ = 0; |
| 85 | // The next position in 'relative_to_absolute_offset_samples_' and |
| 86 | // 'encoder_samples_' to be used to store the next sample. |
| 87 | int samples_idx_ = 0; |
| 88 | |
| 89 | size_t nan_samples_ = 0; |
| 90 | |
| 91 | // The unzeroed filtered position. |
| 92 | double filtered_position_ = 0.0; |
| 93 | // The filtered position. |
| 94 | double position_ = 0.0; |
| 95 | }; |
| 96 | |
| 97 | } // namespace zeroing |
| 98 | } // namespace frc971 |
| 99 | |
| 100 | #endif // FRC971_ZEROING_POT_AND_ABSOLUTE_ENCODER_H_ |