blob: 0021e13d4f53bcbd7c4a724774c4a64dadd9b8cb [file] [log] [blame]
Brian Silvermana57b7012020-03-11 20:19:23 -07001#ifndef FRC971_ZEROING_ABSOLUTE_ENCODER_H_
2#define FRC971_ZEROING_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. The absolute encoder can't spin more than one
15// revolution.
16class AbsoluteEncoderZeroingEstimator
17 : public ZeroingEstimator<AbsolutePosition,
18 constants::AbsoluteEncoderZeroingConstants,
19 AbsoluteEncoderEstimatorState> {
20 public:
21 explicit AbsoluteEncoderZeroingEstimator(
22 const constants::AbsoluteEncoderZeroingConstants &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 AbsolutePosition &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 }
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 AbsolutePosition &position_buffer)
51 : absolute_encoder(position_buffer.absolute_encoder()),
52 encoder(position_buffer.encoder()) {}
53 double absolute_encoder;
54 double encoder;
55 };
56
57 // The zeroing constants used to describe the configuration of the system.
58 const constants::AbsoluteEncoderZeroingConstants constants_;
59
60 // True if the mechanism is zeroed.
61 bool zeroed_;
62 // Marker to track whether an error has occurred.
63 bool error_;
64 // The first valid offset we recorded. This is only set after zeroed_ first
65 // changes to true.
66 double first_offset_;
67
68 // The filtered absolute encoder. This is used in the status for calibration.
69 double filtered_absolute_encoder_ = 0.0;
70
71 // Samples of the offset needed to line the relative encoder up with the
72 // absolute encoder.
73 ::std::vector<double> relative_to_absolute_offset_samples_;
74
75 MoveDetector<PositionStruct, AbsolutePosition> move_detector_;
76
77 // Estimated start position of the mechanism
78 double offset_ = 0;
79 // The next position in 'relative_to_absolute_offset_samples_' and
80 // 'encoder_samples_' to be used to store the next sample.
81 int samples_idx_ = 0;
82
83 // Number of NANs we've seen in a row.
84 size_t nan_samples_ = 0;
85
86 // The filtered position.
87 double position_ = 0.0;
88};
89
90} // namespace zeroing
91} // namespace frc971
92
93#endif // FRC971_ZEROING_ABSOLUTE_ENCODER_H_