Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 1 | #ifndef FRC971_ZEROING_ZEROING_H_ |
| 2 | #define FRC971_ZEROING_ZEROING_H_ |
| 3 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 4 | #include <cstdint> |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 5 | #include <vector> |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 6 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/control_loops.q.h" |
| 8 | #include "frc971/constants.h" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 9 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 10 | // TODO(pschrader): Flag an error if encoder index pulse is not n revolutions |
| 11 | // away from the last one (i.e. got extra counts from noise, etc..) |
| 12 | // |
| 13 | // TODO(pschrader): Flag error if the pot disagrees too much with the encoder |
| 14 | // after being zeroed. |
| 15 | // |
| 16 | // TODO(pschrader): Watch the offset over long periods of time and flag if it |
| 17 | // gets too far away from the initial value. |
| 18 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 19 | namespace frc971 { |
| 20 | namespace zeroing { |
| 21 | |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 22 | template <typename TPosition, typename TZeroingConstants, typename TState> |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 23 | class ZeroingEstimator { |
| 24 | public: |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 25 | using Position = TPosition; |
| 26 | using ZeroingConstants = TZeroingConstants; |
| 27 | using State = TState; |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 28 | virtual ~ZeroingEstimator(){} |
| 29 | |
| 30 | // Returns true if the logic considers the corresponding mechanism to be |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 31 | // zeroed. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 32 | virtual bool zeroed() const = 0; |
| 33 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 34 | // Returns the estimated position of the corresponding mechanism. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 35 | virtual double offset() const = 0; |
| 36 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 37 | // Returns true if there has been an error. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 38 | virtual bool error() const = 0; |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 39 | |
| 40 | // Returns true if an offset is ready. |
| 41 | virtual bool offset_ready() const = 0; |
| 42 | |
| 43 | // Triggers an internal error. This is used for testing the error |
| 44 | // logic. |
| 45 | virtual void TriggerError() = 0; |
| 46 | |
| 47 | // Resets the estimator, clearing error and zeroing states. |
| 48 | virtual void Reset() = 0; |
| 49 | |
| 50 | // Updates the internal logic with new sensor values |
| 51 | virtual void UpdateEstimate(const Position &) = 0; |
| 52 | |
| 53 | // Returns the state of the estimator |
| 54 | virtual State GetEstimatorState() const = 0; |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 57 | // Estimates the position with an incremental encoder with an index pulse and a |
| 58 | // potentiometer. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 59 | class PotAndIndexPulseZeroingEstimator : public ZeroingEstimator<PotAndIndexPosition, |
| 60 | constants::PotAndIndexPulseZeroingConstants, |
| 61 | EstimatorState> { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 62 | public: |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 63 | explicit PotAndIndexPulseZeroingEstimator( |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 64 | const constants::PotAndIndexPulseZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 65 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 66 | // Update the internal logic with the next sensor values. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 67 | void UpdateEstimate(const PotAndIndexPosition &info) override; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 68 | |
| 69 | // Reset the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 70 | void Reset() override; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 71 | |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 72 | // Manually trigger an internal error. This is used for testing the error |
| 73 | // logic. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 74 | void TriggerError() override; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 75 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 76 | bool error() const override { return error_; } |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 77 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 78 | bool zeroed() const override { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 79 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 80 | double offset() const override { return offset_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 81 | |
| 82 | // Returns a number between 0 and 1 that represents the percentage of the |
| 83 | // samples being used in the moving average filter. A value of 0.0 means that |
| 84 | // no samples are being used. A value of 1.0 means that the filter is using |
| 85 | // as many samples as it has room for. For example, after a Reset() this |
| 86 | // value returns 0.0. As more samples get added with UpdateEstimate(...) the |
| 87 | // return value starts increasing to 1.0. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 88 | double offset_ratio_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 89 | return start_pos_samples_.size() / |
| 90 | static_cast<double>(constants_.average_filter_size); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 93 | // Returns true if the sample buffer is full. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 94 | bool offset_ready() const override { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 95 | return start_pos_samples_.size() == constants_.average_filter_size; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 98 | // Returns information about our current state. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 99 | State GetEstimatorState() const override; |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 100 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 101 | private: |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 102 | // This function calculates the start position given the internal state and |
| 103 | // the provided `latched_encoder' value. |
| 104 | double CalculateStartPosition(double start_average, |
| 105 | double latched_encoder) const; |
| 106 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 107 | // The zeroing constants used to describe the configuration of the system. |
| 108 | const constants::PotAndIndexPulseZeroingConstants constants_; |
| 109 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 110 | // The estimated position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 111 | double position_; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 112 | // The unzeroed filtered position. |
| 113 | double filtered_position_ = 0.0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 114 | // The next position in 'start_pos_samples_' to be used to store the next |
| 115 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 116 | int samples_idx_; |
| 117 | // Last 'max_sample_count_' samples for start positions. |
| 118 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 119 | // The estimated starting position of the mechanism. We also call this the |
| 120 | // 'offset' in some contexts. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 121 | double offset_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 122 | // Flag for triggering logic that takes note of the current index pulse count |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 123 | // after a reset. See `last_used_index_pulse_count_'. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 124 | bool wait_for_index_pulse_; |
| 125 | // After a reset we keep track of the index pulse count with this. Only after |
| 126 | // the index pulse count changes (i.e. increments at least once or wraps |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 127 | // around) will we consider the mechanism zeroed. We also use this to store |
| 128 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 129 | // position was calculated. It helps us calculate the start position only on |
| 130 | // index pulses to reject corrupted intermediate data. |
| 131 | uint32_t last_used_index_pulse_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 132 | // Marker to track whether we're fully zeroed yet or not. |
| 133 | bool zeroed_; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 134 | // Marker to track whether an error has occurred. This gets reset to false |
| 135 | // whenever Reset() is called. |
| 136 | bool error_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 137 | // Stores the position "start_pos" variable the first time the program |
| 138 | // is zeroed. |
| 139 | double first_start_pos_; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 142 | // Estimates the position with an incremental encoder and a hall effect sensor. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 143 | class HallEffectAndPositionZeroingEstimator |
| 144 | : public ZeroingEstimator<HallEffectAndPosition, |
| 145 | constants::HallEffectZeroingConstants, |
| 146 | HallEffectAndPositionEstimatorState> { |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 147 | public: |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 148 | explicit HallEffectAndPositionZeroingEstimator(const ZeroingConstants &constants); |
| 149 | |
| 150 | // Update the internal logic with the next sensor values. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 151 | void UpdateEstimate(const Position &info) override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 152 | |
| 153 | // Reset the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 154 | void Reset() override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 155 | |
| 156 | // Manually trigger an internal error. This is used for testing the error |
| 157 | // logic. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 158 | void TriggerError() override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 159 | |
| 160 | bool error() const override { return error_; } |
| 161 | |
| 162 | bool zeroed() const override { return zeroed_; } |
| 163 | |
| 164 | double offset() const override { return offset_; } |
| 165 | |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 166 | bool offset_ready() const override { return zeroed_; } |
| 167 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 168 | // Returns information about our current state. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 169 | State GetEstimatorState() const override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 170 | |
| 171 | private: |
| 172 | // Sets the minimum and maximum posedge position values. |
| 173 | void StoreEncoderMaxAndMin(const HallEffectAndPosition &info); |
| 174 | |
| 175 | // The zeroing constants used to describe the configuration of the system. |
| 176 | const ZeroingConstants constants_; |
| 177 | |
| 178 | // The estimated state of the hall effect. |
| 179 | double current_ = 0.0; |
| 180 | // The estimated position. |
| 181 | double position_ = 0.0; |
| 182 | // The smallest and largest positions of the last set of encoder positions |
| 183 | // while the hall effect was low. |
| 184 | double min_low_position_; |
| 185 | double max_low_position_; |
| 186 | // If we've seen the hall effect high for enough times without going low, then |
| 187 | // we can be sure it isn't a false positive. |
| 188 | bool high_long_enough_; |
| 189 | size_t cycles_high_; |
| 190 | |
| 191 | bool last_hall_ = false; |
| 192 | |
| 193 | // The estimated starting position of the mechanism. We also call this the |
| 194 | // 'offset' in some contexts. |
| 195 | double offset_; |
| 196 | // Flag for triggering logic that takes note of the current posedge count |
| 197 | // after a reset. See `last_used_posedge_count_'. |
| 198 | bool initialized_; |
| 199 | // After a reset we keep track of the posedge count with this. Only after the |
| 200 | // posedge count changes (i.e. increments at least once or wraps around) will |
| 201 | // we consider the mechanism zeroed. We also use this to store the most recent |
| 202 | // `HallEffectAndPosition::posedge_count' value when the start position |
| 203 | // was calculated. It helps us calculate the start position only on posedges |
| 204 | // to reject corrupted intermediate data. |
| 205 | int32_t last_used_posedge_count_; |
| 206 | // Marker to track whether we're fully zeroed yet or not. |
| 207 | bool zeroed_; |
| 208 | // Marker to track whether an error has occurred. This gets reset to false |
| 209 | // whenever Reset() is called. |
| 210 | bool error_ = false; |
| 211 | // Stores the position "start_pos" variable the first time the program |
| 212 | // is zeroed. |
| 213 | double first_start_pos_; |
| 214 | }; |
| 215 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 216 | // Estimates the position with an absolute encoder which also reports |
| 217 | // incremental counts, and a potentiometer. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 218 | class PotAndAbsEncoderZeroingEstimator |
| 219 | : public ZeroingEstimator<PotAndAbsolutePosition, |
| 220 | constants::PotAndAbsoluteEncoderZeroingConstants, |
| 221 | AbsoluteEstimatorState> { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 222 | public: |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 223 | explicit PotAndAbsEncoderZeroingEstimator( |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 224 | const constants::PotAndAbsoluteEncoderZeroingConstants &constants); |
| 225 | |
| 226 | // Resets the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 227 | void Reset() override; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 228 | |
| 229 | // Updates the sensor values for the zeroing logic. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 230 | void UpdateEstimate(const PotAndAbsolutePosition &info) override; |
| 231 | |
| 232 | void TriggerError() override { error_ = true; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 233 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 234 | bool zeroed() const override { return zeroed_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 235 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 236 | double offset() const override { return offset_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 237 | |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 238 | bool error() const override { return error_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 239 | |
| 240 | // Returns true if the sample buffer is full. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 241 | bool offset_ready() const override { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 242 | return relative_to_absolute_offset_samples_.size() == |
| 243 | constants_.average_filter_size && |
| 244 | offset_samples_.size() == constants_.average_filter_size; |
| 245 | } |
| 246 | |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 247 | // Returns information about our current state. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 248 | State GetEstimatorState() const override; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 249 | |
| 250 | private: |
| 251 | // The zeroing constants used to describe the configuration of the system. |
| 252 | const constants::PotAndAbsoluteEncoderZeroingConstants constants_; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 253 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 254 | // True if the mechanism is zeroed. |
| 255 | bool zeroed_; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 256 | // Marker to track whether an error has occurred. |
| 257 | bool error_; |
| 258 | // The first valid offset we recorded. This is only set after zeroed_ first |
| 259 | // changes to true. |
| 260 | double first_offset_; |
| 261 | |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 262 | // The filtered absolute encoder. This is used in the status for calibration. |
| 263 | double filtered_absolute_encoder_ = 0.0; |
| 264 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 265 | // Samples of the offset needed to line the relative encoder up with the |
| 266 | // absolute encoder. |
| 267 | ::std::vector<double> relative_to_absolute_offset_samples_; |
| 268 | // Offset between the Pot and Relative encoder position. |
| 269 | ::std::vector<double> offset_samples_; |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 270 | // Last moving_buffer_size position samples to be used to determine if the |
| 271 | // robot is moving. |
| 272 | ::std::vector<PotAndAbsolutePosition> buffered_samples_; |
| 273 | // Pointer to front of the buffered samples. |
| 274 | int buffered_samples_idx_ = 0; |
| 275 | // Estimated offset between the pot and relative encoder. |
| 276 | double pot_relative_encoder_offset_ = 0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 277 | // Estimated start position of the mechanism |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 278 | double offset_ = 0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 279 | // The next position in 'relative_to_absolute_offset_samples_' and |
| 280 | // 'encoder_samples_' to be used to store the next sample. |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 281 | int samples_idx_ = 0; |
| 282 | |
| 283 | size_t nan_samples_ = 0; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 284 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 285 | // The unzeroed filtered position. |
| 286 | double filtered_position_ = 0.0; |
| 287 | // The filtered position. |
| 288 | double position_ = 0.0; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 291 | // Zeros by seeing all the index pulses in the range of motion of the mechanism |
| 292 | // and using that to figure out which index pulse is which. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 293 | class PulseIndexZeroingEstimator : public ZeroingEstimator<IndexPosition, |
| 294 | constants::EncoderPlusIndexZeroingConstants, |
| 295 | IndexEstimatorState> { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 296 | public: |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 297 | explicit PulseIndexZeroingEstimator(const ZeroingConstants &constants) |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 298 | : constants_(constants) { |
| 299 | Reset(); |
| 300 | } |
| 301 | |
| 302 | // Resets the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 303 | void Reset() override; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 304 | |
| 305 | bool zeroed() const override { return zeroed_; } |
| 306 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 307 | // It's as ready as it'll ever be... |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 308 | bool offset_ready() const override { return true; } |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 309 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 310 | double offset() const override { return offset_; } |
| 311 | |
| 312 | bool error() const override { return error_; } |
| 313 | |
| 314 | // Updates the internal logic with the next sensor values. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 315 | void UpdateEstimate(const IndexPosition &info) override; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 316 | |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 317 | // Returns information about our current state. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 318 | State GetEstimatorState() const override; |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 319 | |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame^] | 320 | void TriggerError() override { error_ = true; } |
Austin Schuh | d93160a | 2017-03-05 01:00:54 -0800 | [diff] [blame] | 321 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 322 | private: |
| 323 | // Returns the current real position using the relative encoder offset. |
| 324 | double CalculateCurrentPosition(const IndexPosition &info); |
| 325 | |
| 326 | // Sets the minimum and maximum index pulse position values. |
| 327 | void StoreIndexPulseMaxAndMin(const IndexPosition &info); |
| 328 | |
| 329 | // Returns the number of index pulses we should have seen so far. |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 330 | int IndexPulseCount() const; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 331 | |
| 332 | // Contains the physical constants describing the system. |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 333 | const ZeroingConstants constants_; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 334 | |
| 335 | // The smallest position of all the index pulses. |
| 336 | double min_index_position_; |
| 337 | // The largest position of all the index pulses. |
| 338 | double max_index_position_; |
| 339 | |
| 340 | // The estimated starting position of the mechanism. |
| 341 | double offset_; |
| 342 | // After a reset we keep track of the index pulse count with this. Only after |
| 343 | // the index pulse count changes (i.e. increments at least once or wraps |
| 344 | // around) will we consider the mechanism zeroed. We also use this to store |
| 345 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 346 | // position was calculated. It helps us calculate the start position only on |
| 347 | // index pulses to reject corrupted intermediate data. |
| 348 | uint32_t last_used_index_pulse_count_; |
| 349 | |
| 350 | // True if we are fully zeroed. |
| 351 | bool zeroed_; |
| 352 | // Marker to track whether an error has occurred. |
| 353 | bool error_; |
| 354 | |
| 355 | // The estimated position. |
| 356 | double position_; |
| 357 | }; |
| 358 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 359 | } // namespace zeroing |
| 360 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 361 | |
| 362 | #endif // FRC971_ZEROING_ZEROING_H_ |