blob: 9d730f27f80206a6a054a61038cbb3605a2b0fa8 [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
James Kuszmaulec635d22023-08-12 18:39:24 -07008#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. The absolute encoder can't spin more than one
16// revolution.
17class AbsoluteEncoderZeroingEstimator
18 : public ZeroingEstimator<AbsolutePosition,
19 constants::AbsoluteEncoderZeroingConstants,
20 AbsoluteEncoderEstimatorState> {
21 public:
22 explicit AbsoluteEncoderZeroingEstimator(
23 const constants::AbsoluteEncoderZeroingConstants &constants);
24
25 // Resets the internal logic so it needs to be re-zeroed.
26 void Reset() override;
27
28 // Updates the sensor values for the zeroing logic.
29 void UpdateEstimate(const AbsolutePosition &info) override;
30
31 void TriggerError() override { error_ = true; }
32
33 bool zeroed() const override { return zeroed_; }
34
35 double offset() const override { return offset_; }
36
37 bool error() const override { return error_; }
38
39 // Returns true if the sample buffer is full.
40 bool offset_ready() const override {
41 return relative_to_absolute_offset_samples_.size() ==
42 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 AbsolutePosition &position_buffer)
52 : absolute_encoder(position_buffer.absolute_encoder()),
53 encoder(position_buffer.encoder()) {}
54 double absolute_encoder;
55 double encoder;
56 };
57
58 // The zeroing constants used to describe the configuration of the system.
59 const constants::AbsoluteEncoderZeroingConstants constants_;
60
61 // True if the mechanism is zeroed.
62 bool zeroed_;
63 // Marker to track whether an error has occurred.
64 bool error_;
65 // The first valid offset we recorded. This is only set after zeroed_ first
66 // changes to true.
67 double first_offset_;
68
69 // The filtered absolute encoder. This is used in the status for calibration.
70 double filtered_absolute_encoder_ = 0.0;
71
72 // Samples of the offset needed to line the relative encoder up with the
73 // absolute encoder.
74 ::std::vector<double> relative_to_absolute_offset_samples_;
75
76 MoveDetector<PositionStruct, AbsolutePosition> move_detector_;
77
78 // Estimated start position of the mechanism
79 double offset_ = 0;
80 // The next position in 'relative_to_absolute_offset_samples_' and
81 // 'encoder_samples_' to be used to store the next sample.
82 int samples_idx_ = 0;
83
84 // Number of NANs we've seen in a row.
85 size_t nan_samples_ = 0;
86
87 // The filtered position.
88 double position_ = 0.0;
Ravago Jones726deb02021-05-29 14:36:43 -070089
90 // Marker to track what kind of error has occured.
91 aos::ErrorList<ZeroingError> errors_;
Brian Silvermana57b7012020-03-11 20:19:23 -070092};
93
94} // namespace zeroing
95} // namespace frc971
96
97#endif // FRC971_ZEROING_ABSOLUTE_ENCODER_H_