blob: dd2190da7a65b1ca9bf3e0c0ba1e0a09331cc8aa [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
51 private:
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
63 // The zeroing constants used to describe the configuration of the system.
64 const constants::AbsoluteAndAbsoluteEncoderZeroingConstants constants_;
65
66 // True if the mechanism is zeroed.
67 bool zeroed_;
68 // Marker to track whether an error has occurred.
69 bool error_;
70 // The first valid offset we recorded. This is only set after zeroed_ first
71 // changes to true.
72 double first_offset_;
73
74 // The filtered absolute encoder. This is used in the status for calibration.
75 double filtered_absolute_encoder_ = 0.0;
76
77 // The filtered single turn absolute encoder. This is used in the status for
78 // calibration.
79 double filtered_single_turn_absolute_encoder_ = 0.0;
80
81 // Samples of the offset needed to line the relative encoder up with the
82 // absolute encoder.
83 ::std::vector<double> relative_to_absolute_offset_samples_;
84
85 // Offset between the single turn absolute encoder and relative encoder
86 // position.
87 ::std::vector<double> offset_samples_;
88
89 MoveDetector<PositionStruct, AbsoluteAndAbsolutePosition> move_detector_;
90
91 // Estimated offset between the single turn encoder and relative encoder.
92 double single_turn_to_relative_encoder_offset_ = 0;
93 // Estimated start position of the mechanism
94 double offset_ = 0;
95 // The next position in 'relative_to_absolute_offset_samples_' and
96 // 'encoder_samples_' to be used to store the next sample.
97 int samples_idx_ = 0;
98
99 size_t nan_samples_ = 0;
100
101 // The unzeroed filtered position.
102 double filtered_position_ = 0.0;
103 // The filtered position.
104 double position_ = 0.0;
105};
106
107} // namespace zeroing
108} // namespace frc971
109
110#endif // FRC971_ZEROING_ABSOLUTE_AND_ABSOLUTE_ENCODER_H_