blob: f03a4423548b61fb0d015bf26b2f520b043e45d9 [file] [log] [blame]
Brian Silvermana57b7012020-03-11 20:19:23 -07001#ifndef FRC971_ZEROING_HALL_EFFECT_AND_POSITION_H_
2#define FRC971_ZEROING_HALL_EFFECT_AND_POSITION_H_
3
4#include "flatbuffers/flatbuffers.h"
5
6#include "frc971/zeroing/zeroing.h"
7
8namespace frc971 {
9namespace zeroing {
10
11// Estimates the position with an incremental encoder and a hall effect sensor.
12class HallEffectAndPositionZeroingEstimator
13 : public ZeroingEstimator<HallEffectAndPosition,
14 constants::HallEffectZeroingConstants,
15 HallEffectAndPositionEstimatorState> {
16 public:
17 explicit HallEffectAndPositionZeroingEstimator(
18 const ZeroingConstants &constants);
19
20 // Update the internal logic with the next sensor values.
21 void UpdateEstimate(const Position &info) override;
22
23 // Reset the internal logic so it needs to be re-zeroed.
24 void Reset() override;
25
26 // Manually trigger an internal error. This is used for testing the error
27 // logic.
28 void TriggerError() override;
29
30 bool error() const override { return error_; }
31
32 bool zeroed() const override { return zeroed_; }
33
34 double offset() const override { return offset_; }
35
36 bool offset_ready() const override { return zeroed_; }
37
38 // Returns information about our current state.
39 virtual flatbuffers::Offset<State> GetEstimatorState(
40 flatbuffers::FlatBufferBuilder *fbb) const override;
41
42 private:
43 // Sets the minimum and maximum posedge position values.
44 void StoreEncoderMaxAndMin(const HallEffectAndPosition &info);
45
46 // The zeroing constants used to describe the configuration of the system.
47 const ZeroingConstants constants_;
48
49 // The estimated state of the hall effect.
50 double current_ = 0.0;
51 // The estimated position.
52 double position_ = 0.0;
53 // The smallest and largest positions of the last set of encoder positions
54 // while the hall effect was low.
55 double min_low_position_;
56 double max_low_position_;
57 // If we've seen the hall effect high for enough times without going low, then
58 // we can be sure it isn't a false positive.
59 bool high_long_enough_;
60 size_t cycles_high_;
61
62 bool last_hall_ = false;
63
64 // The estimated starting position of the mechanism. We also call this the
65 // 'offset' in some contexts.
66 double offset_;
67 // Flag for triggering logic that takes note of the current posedge count
68 // after a reset. See `last_used_posedge_count_'.
69 bool initialized_;
70 // After a reset we keep track of the posedge count with this. Only after the
71 // posedge count changes (i.e. increments at least once or wraps around) will
72 // we consider the mechanism zeroed. We also use this to store the most recent
73 // `HallEffectAndPosition::posedge_count' value when the start position
74 // was calculated. It helps us calculate the start position only on posedges
75 // to reject corrupted intermediate data.
76 int32_t last_used_posedge_count_;
77 // Marker to track whether we're fully zeroed yet or not.
78 bool zeroed_;
79 // Marker to track whether an error has occurred. This gets reset to false
80 // whenever Reset() is called.
81 bool error_ = false;
82 // Stores the position "start_pos" variable the first time the program
83 // is zeroed.
84 double first_start_pos_;
85};
86
87} // namespace zeroing
88} // namespace frc971
89
90#endif // FRC971_ZEROING_HALL_EFFECT_AND_POSITION_H_