blob: d250a50debc9e8fd00f07719ee81bfcb90c34564 [file] [log] [blame]
Brian Silvermana57b7012020-03-11 20:19:23 -07001#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"
Philipp Schrader790cb542023-07-05 21:06:52 -07007
8#include "aos/containers/error_list.h"
Brian Silvermana57b7012020-03-11 20:19:23 -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 a potentiometer.
16class PotAndAbsoluteEncoderZeroingEstimator
17 : public ZeroingEstimator<PotAndAbsolutePosition,
18 constants::PotAndAbsoluteEncoderZeroingConstants,
19 PotAndAbsoluteEncoderEstimatorState> {
20 public:
21 explicit PotAndAbsoluteEncoderZeroingEstimator(
22 const constants::PotAndAbsoluteEncoderZeroingConstants &constants);
23
24 // Resets the internal logic so it needs to be re-zeroed.
25 void Reset() override;
26
27 // Updates the sensor values for the zeroing logic.
28 void UpdateEstimate(const PotAndAbsolutePosition &info) override;
29
30 void TriggerError() override { error_ = true; }
31
32 bool zeroed() const override { return zeroed_; }
33
34 double offset() const override { return offset_; }
35
36 bool error() const override { return error_; }
37
38 // Returns true if the sample buffer is full.
39 bool offset_ready() const override {
40 return relative_to_absolute_offset_samples_.size() ==
41 constants_.average_filter_size &&
42 offset_samples_.size() == constants_.average_filter_size;
43 }
44
45 // Returns information about our current state.
46 virtual flatbuffers::Offset<State> GetEstimatorState(
47 flatbuffers::FlatBufferBuilder *fbb) const override;
48
49 private:
50 struct PositionStruct {
51 PositionStruct(const PotAndAbsolutePosition &position_buffer)
52 : absolute_encoder(position_buffer.absolute_encoder()),
53 encoder(position_buffer.encoder()),
54 pot(position_buffer.pot()) {}
55 double absolute_encoder;
56 double encoder;
57 double pot;
58 };
59
60 // The zeroing constants used to describe the configuration of the system.
61 const constants::PotAndAbsoluteEncoderZeroingConstants constants_;
62
63 // True if the mechanism is zeroed.
64 bool zeroed_;
65 // Marker to track whether an error has occurred.
66 bool error_;
67 // The first valid offset we recorded. This is only set after zeroed_ first
68 // changes to true.
69 double first_offset_;
70
71 // The filtered absolute encoder. This is used in the status for calibration.
72 double filtered_absolute_encoder_ = 0.0;
73
74 // Samples of the offset needed to line the relative encoder up with the
75 // absolute encoder.
76 ::std::vector<double> relative_to_absolute_offset_samples_;
77 // Offset between the Pot and Relative encoder position.
78 ::std::vector<double> offset_samples_;
79
80 MoveDetector<PositionStruct, PotAndAbsolutePosition> move_detector_;
81
82 // Estimated offset between the pot and relative encoder.
83 double pot_relative_encoder_offset_ = 0;
84 // Estimated start position of the mechanism
85 double offset_ = 0;
86 // The next position in 'relative_to_absolute_offset_samples_' and
87 // 'encoder_samples_' to be used to store the next sample.
88 int samples_idx_ = 0;
89
90 size_t nan_samples_ = 0;
91
92 // The unzeroed filtered position.
93 double filtered_position_ = 0.0;
94 // The filtered position.
95 double position_ = 0.0;
Ravago Jones726deb02021-05-29 14:36:43 -070096
97 // Marker to track what kind of error has occured.
98 aos::ErrorList<ZeroingError> errors_;
Brian Silvermana57b7012020-03-11 20:19:23 -070099};
100
101} // namespace zeroing
102} // namespace frc971
103
104#endif // FRC971_ZEROING_POT_AND_ABSOLUTE_ENCODER_H_