blob: 9c17371a906d5c0971f56429e9cc7a8437996d2a [file] [log] [blame]
Brian Silvermana57b7012020-03-11 20:19:23 -07001#ifndef FRC971_ZEROING_PULSE_INDEX_H_
2#define FRC971_ZEROING_PULSE_INDEX_H_
3
4#include "flatbuffers/flatbuffers.h"
5
6#include "frc971/zeroing/zeroing.h"
7
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08008namespace frc971::zeroing {
Brian Silvermana57b7012020-03-11 20:19:23 -07009
10// Zeros by seeing all the index pulses in the range of motion of the mechanism
11// and using that to figure out which index pulse is which.
12class PulseIndexZeroingEstimator
13 : public ZeroingEstimator<IndexPosition,
14 constants::EncoderPlusIndexZeroingConstants,
15 IndexEstimatorState> {
16 public:
17 explicit PulseIndexZeroingEstimator(const ZeroingConstants &constants)
18 : constants_(constants) {
19 Reset();
20 }
21
22 // Resets the internal logic so it needs to be re-zeroed.
23 void Reset() override;
24
25 bool zeroed() const override { return zeroed_; }
26
27 // It's as ready as it'll ever be...
28 bool offset_ready() const override { return true; }
29
30 double offset() const override { return offset_; }
31
32 bool error() const override { return error_; }
33
34 // Updates the internal logic with the next sensor values.
35 void UpdateEstimate(const IndexPosition &info) override;
36
37 // Returns information about our current state.
38 virtual flatbuffers::Offset<State> GetEstimatorState(
39 flatbuffers::FlatBufferBuilder *fbb) const override;
40
41 void TriggerError() override { error_ = true; }
42
43 private:
44 // Returns the current real position using the relative encoder offset.
45 double CalculateCurrentPosition(const IndexPosition &info);
46
47 // Sets the minimum and maximum index pulse position values.
48 void StoreIndexPulseMaxAndMin(const IndexPosition &info);
49
50 // Returns the number of index pulses we should have seen so far.
51 int IndexPulseCount() const;
52
53 // Contains the physical constants describing the system.
54 const ZeroingConstants constants_;
55
56 // The smallest position of all the index pulses.
57 double min_index_position_;
58 // The largest position of all the index pulses.
59 double max_index_position_;
60
61 // The estimated starting position of the mechanism.
62 double offset_;
63 // After a reset we keep track of the index pulse count with this. Only after
64 // the index pulse count changes (i.e. increments at least once or wraps
65 // around) will we consider the mechanism zeroed. We also use this to store
66 // the most recent `PotAndIndexPosition::index_pulses' value when the start
67 // position was calculated. It helps us calculate the start position only on
68 // index pulses to reject corrupted intermediate data.
69 uint32_t last_used_index_pulse_count_;
70
71 // True if we are fully zeroed.
72 bool zeroed_;
73 // Marker to track whether an error has occurred.
74 bool error_;
75
76 // The estimated position.
77 double position_;
78};
79
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080080} // namespace frc971::zeroing
Brian Silvermana57b7012020-03-11 20:19:23 -070081
82#endif // FRC971_ZEROING_PULSE_INDEX_H_