blob: b912e84107606e363a7660972ec96796d091d4be [file] [log] [blame]
James Kuszmaulbdc6a792023-08-12 16:29:38 -07001#ifndef FRC971_ZEROING_CONTINUOUS_ABSOLUTE_ENCODER_H_
2#define FRC971_ZEROING_CONTINUOUS_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 spins continuously. The
14// absolute encoder must have a 1:1 ratio to the output.
15// The provided offset(), when added to incremental encoder, may return a value
16// outside of +/- pi. The user is responsible for handling wrapping.
17class ContinuousAbsoluteEncoderZeroingEstimator
18 : public ZeroingEstimator<
19 AbsolutePosition,
20 constants::ContinuousAbsoluteEncoderZeroingConstants,
21 AbsoluteEncoderEstimatorState> {
22 public:
23 explicit ContinuousAbsoluteEncoderZeroingEstimator(
24 const constants::ContinuousAbsoluteEncoderZeroingConstants &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 AbsolutePosition &info) override;
31
32 void TriggerError() override { error_ = true; }
33
34 bool zeroed() const override { return zeroed_; }
35
36 double offset() const override { return offset_; }
37
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 }
45
46 // Returns information about our current state.
47 virtual flatbuffers::Offset<State> GetEstimatorState(
48 flatbuffers::FlatBufferBuilder *fbb) const override;
49
50 private:
51 struct PositionStruct {
52 PositionStruct(const AbsolutePosition &position_buffer)
53 : absolute_encoder(position_buffer.absolute_encoder()),
54 encoder(position_buffer.encoder()) {}
55 double absolute_encoder;
56 double encoder;
57 };
58
59 // The zeroing constants used to describe the configuration of the system.
60 const constants::ContinuousAbsoluteEncoderZeroingConstants constants_;
61
62 // True if the mechanism is zeroed.
63 bool zeroed_;
64 // Marker to track whether an error has occurred.
65 bool error_;
66 // The first valid offset we recorded. This is only set after zeroed_ first
67 // changes to true.
68 double first_offset_;
69
70 // The filtered absolute encoder. This is used in the status for calibration.
71 double filtered_absolute_encoder_ = 0.0;
72
73 // Samples of the offset needed to line the relative encoder up with the
74 // absolute encoder.
75 ::std::vector<double> relative_to_absolute_offset_samples_;
76
77 MoveDetector<PositionStruct, AbsolutePosition> move_detector_;
78
79 // Estimated start position of the mechanism
80 double offset_ = 0;
81 // The next position in 'relative_to_absolute_offset_samples_' and
82 // 'encoder_samples_' to be used to store the next sample.
83 int samples_idx_ = 0;
84
85 // Number of NANs we've seen in a row.
86 size_t nan_samples_ = 0;
87
88 // The filtered position.
89 double position_ = 0.0;
90
91 // Marker to track what kind of error has occured.
92 aos::ErrorList<ZeroingError> errors_;
93};
94
95} // namespace zeroing
96} // namespace frc971
97
98#endif // FRC971_ZEROING_CONTINUOUS_ABSOLUTE_ENCODER_H_