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