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