blob: 5e700ee14b5a405bcc0535fefcb52e0b0ef8c1e3 [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
James Kuszmaulec635d22023-08-12 18:39:24 -07008#include "aos/containers/error_list.h"
James Kuszmaulbdc6a792023-08-12 16:29:38 -07009#include "frc971/zeroing/zeroing.h"
10
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080011namespace frc971::zeroing {
James Kuszmaulbdc6a792023-08-12 16:29:38 -070012
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
James Kuszmaulda040152024-10-29 22:20:03 -070050 void GetEstimatorState(AbsoluteEncoderEstimatorStateStatic *fbs) const;
51
James Kuszmaulbdc6a792023-08-12 16:29:38 -070052 private:
53 struct PositionStruct {
54 PositionStruct(const AbsolutePosition &position_buffer)
55 : absolute_encoder(position_buffer.absolute_encoder()),
56 encoder(position_buffer.encoder()) {}
57 double absolute_encoder;
58 double encoder;
59 };
60
61 // The zeroing constants used to describe the configuration of the system.
62 const constants::ContinuousAbsoluteEncoderZeroingConstants constants_;
63
64 // True if the mechanism is zeroed.
65 bool zeroed_;
66 // Marker to track whether an error has occurred.
67 bool error_;
68 // The first valid offset we recorded. This is only set after zeroed_ first
69 // changes to true.
70 double first_offset_;
71
72 // The filtered absolute encoder. This is used in the status for calibration.
73 double filtered_absolute_encoder_ = 0.0;
74
75 // Samples of the offset needed to line the relative encoder up with the
76 // absolute encoder.
77 ::std::vector<double> relative_to_absolute_offset_samples_;
78
79 MoveDetector<PositionStruct, AbsolutePosition> move_detector_;
80
81 // Estimated start position of the mechanism
82 double offset_ = 0;
83 // The next position in 'relative_to_absolute_offset_samples_' and
84 // 'encoder_samples_' to be used to store the next sample.
85 int samples_idx_ = 0;
86
87 // Number of NANs we've seen in a row.
88 size_t nan_samples_ = 0;
89
90 // The filtered position.
91 double position_ = 0.0;
92
93 // Marker to track what kind of error has occured.
94 aos::ErrorList<ZeroingError> errors_;
95};
96
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080097} // namespace frc971::zeroing
James Kuszmaulbdc6a792023-08-12 16:29:38 -070098
99#endif // FRC971_ZEROING_CONTINUOUS_ABSOLUTE_ENCODER_H_