blob: cb28fe472a5943965ce7375d3f029babd239cb1b [file] [log] [blame]
Brian Silvermana57b7012020-03-11 20:19:23 -07001#ifndef FRC971_ZEROING_POT_AND_INDEX_H_
2#define FRC971_ZEROING_POT_AND_INDEX_H_
3
4#include <vector>
5
6#include "flatbuffers/flatbuffers.h"
7
8#include "frc971/zeroing/zeroing.h"
9
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080010namespace frc971::zeroing {
Brian Silvermana57b7012020-03-11 20:19:23 -070011
12// Estimates the position with an incremental encoder with an index pulse and a
13// potentiometer.
14class PotAndIndexPulseZeroingEstimator
15 : public ZeroingEstimator<PotAndIndexPosition,
16 constants::PotAndIndexPulseZeroingConstants,
17 EstimatorState> {
18 public:
19 explicit PotAndIndexPulseZeroingEstimator(
20 const constants::PotAndIndexPulseZeroingConstants &constants);
21
22 // Update the internal logic with the next sensor values.
23 void UpdateEstimate(const PotAndIndexPosition &info) override;
24
25 // Reset the internal logic so it needs to be re-zeroed.
26 void Reset() override;
27
28 // Manually trigger an internal error. This is used for testing the error
29 // logic.
30 void TriggerError() override;
31
32 bool error() const override { return error_; }
33
34 bool zeroed() const override { return zeroed_; }
35
36 double offset() const override { return offset_; }
37
38 // Returns a number between 0 and 1 that represents the percentage of the
39 // samples being used in the moving average filter. A value of 0.0 means that
40 // no samples are being used. A value of 1.0 means that the filter is using
41 // as many samples as it has room for. For example, after a Reset() this
42 // value returns 0.0. As more samples get added with UpdateEstimate(...) the
43 // return value starts increasing to 1.0.
44 double offset_ratio_ready() const {
45 return start_pos_samples_.size() /
46 static_cast<double>(constants_.average_filter_size);
47 }
48
49 // Returns true if the sample buffer is full.
50 bool offset_ready() const override {
51 return start_pos_samples_.size() == constants_.average_filter_size;
52 }
53
54 // Returns information about our current state.
55 virtual flatbuffers::Offset<State> GetEstimatorState(
56 flatbuffers::FlatBufferBuilder *fbb) const override;
57
58 private:
59 // This function calculates the start position given the internal state and
60 // the provided `latched_encoder' value.
61 double CalculateStartPosition(double start_average,
62 double latched_encoder) const;
63
64 // The zeroing constants used to describe the configuration of the system.
65 const constants::PotAndIndexPulseZeroingConstants constants_;
66
67 // The estimated position.
68 double position_;
69 // The unzeroed filtered position.
70 double filtered_position_ = 0.0;
71 // The next position in 'start_pos_samples_' to be used to store the next
72 // sample.
73 int samples_idx_;
74 // Last 'max_sample_count_' samples for start positions.
75 std::vector<double> start_pos_samples_;
76 // The estimated starting position of the mechanism. We also call this the
77 // 'offset' in some contexts.
78 double offset_;
79 // Flag for triggering logic that takes note of the current index pulse count
80 // after a reset. See `last_used_index_pulse_count_'.
81 bool wait_for_index_pulse_;
82 // After a reset we keep track of the index pulse count with this. Only after
83 // the index pulse count changes (i.e. increments at least once or wraps
84 // around) will we consider the mechanism zeroed. We also use this to store
85 // the most recent `PotAndIndexPosition::index_pulses' value when the start
86 // position was calculated. It helps us calculate the start position only on
87 // index pulses to reject corrupted intermediate data.
88 uint32_t last_used_index_pulse_count_;
89 // Marker to track whether we're fully zeroed yet or not.
90 bool zeroed_;
91 // Marker to track whether an error has occurred. This gets reset to false
92 // whenever Reset() is called.
93 bool error_;
94 // Stores the position "start_pos" variable the first time the program
95 // is zeroed.
96 double first_start_pos_;
97};
98
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080099} // namespace frc971::zeroing
Brian Silvermana57b7012020-03-11 20:19:23 -0700100
101#endif // FRC971_ZEROING_POT_AND_INDEX_H_