blob: 499f7d17b13b43588285cfdd446823f7a9099dd6 [file] [log] [blame]
Ravago Jonesea6464c2020-10-10 15:40:46 -07001#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
8#include "frc971/zeroing/zeroing.h"
9
10namespace frc971 {
11namespace zeroing {
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.
17class 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 Jones937587c2020-12-26 17:21:09 -080036 double offset() const override { return first_offset_; }
Ravago Jonesea6464c2020-10-10 15:40:46 -070037
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-ud53408e2021-10-21 19:43:58 -070051 protected:
Ravago Jonesea6464c2020-10-10 15:40:46 -070052 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-ud53408e2021-10-21 19:43:58 -070063 // 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 Jonesea6464c2020-10-10 15:40:46 -070069 // 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_;
76 // The first valid offset we recorded. This is only set after zeroed_ first
77 // changes to true.
78 double first_offset_;
79
80 // The filtered absolute encoder. This is used in the status for calibration.
81 double filtered_absolute_encoder_ = 0.0;
82
83 // The filtered single turn absolute encoder. This is used in the status for
84 // calibration.
85 double filtered_single_turn_absolute_encoder_ = 0.0;
86
87 // Samples of the offset needed to line the relative encoder up with the
88 // absolute encoder.
89 ::std::vector<double> relative_to_absolute_offset_samples_;
90
91 // Offset between the single turn absolute encoder and relative encoder
92 // position.
93 ::std::vector<double> offset_samples_;
94
95 MoveDetector<PositionStruct, AbsoluteAndAbsolutePosition> move_detector_;
96
97 // Estimated offset between the single turn encoder and relative encoder.
98 double single_turn_to_relative_encoder_offset_ = 0;
99 // Estimated start position of the mechanism
100 double offset_ = 0;
101 // The next position in 'relative_to_absolute_offset_samples_' and
102 // 'encoder_samples_' to be used to store the next sample.
103 int samples_idx_ = 0;
104
105 size_t nan_samples_ = 0;
106
107 // The unzeroed filtered position.
108 double filtered_position_ = 0.0;
109 // The filtered position.
110 double position_ = 0.0;
111};
112
113} // namespace zeroing
114} // namespace frc971
115
116#endif // FRC971_ZEROING_ABSOLUTE_AND_ABSOLUTE_ENCODER_H_