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