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 | |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 4 | #include <algorithm> |
| 5 | #include <cmath> |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 6 | #include <cstdint> |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 7 | #include <vector> |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 8 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 9 | #include "frc971/control_loops/control_loops_generated.h" |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 10 | #include "frc971/constants.h" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 11 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 12 | #include "flatbuffers/flatbuffers.h" |
| 13 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 14 | // TODO(pschrader): Flag an error if encoder index pulse is not n revolutions |
| 15 | // away from the last one (i.e. got extra counts from noise, etc..) |
| 16 | // |
| 17 | // TODO(pschrader): Flag error if the pot disagrees too much with the encoder |
| 18 | // after being zeroed. |
| 19 | // |
| 20 | // TODO(pschrader): Watch the offset over long periods of time and flag if it |
| 21 | // gets too far away from the initial value. |
| 22 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 23 | namespace frc971 { |
| 24 | namespace zeroing { |
| 25 | |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 26 | template <typename TPosition, typename TZeroingConstants, typename TState> |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 27 | class ZeroingEstimator { |
| 28 | public: |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 29 | using Position = TPosition; |
| 30 | using ZeroingConstants = TZeroingConstants; |
| 31 | using State = TState; |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 32 | virtual ~ZeroingEstimator(){} |
| 33 | |
| 34 | // Returns true if the logic considers the corresponding mechanism to be |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 35 | // zeroed. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 36 | virtual bool zeroed() const = 0; |
| 37 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 38 | // Returns the estimated position of the corresponding mechanism. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 39 | virtual double offset() const = 0; |
| 40 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 41 | // Returns true if there has been an error. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 42 | virtual bool error() const = 0; |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 43 | |
| 44 | // Returns true if an offset is ready. |
| 45 | virtual bool offset_ready() const = 0; |
| 46 | |
| 47 | // Triggers an internal error. This is used for testing the error |
| 48 | // logic. |
| 49 | virtual void TriggerError() = 0; |
| 50 | |
| 51 | // Resets the estimator, clearing error and zeroing states. |
| 52 | virtual void Reset() = 0; |
| 53 | |
| 54 | // Updates the internal logic with new sensor values |
| 55 | virtual void UpdateEstimate(const Position &) = 0; |
| 56 | |
| 57 | // Returns the state of the estimator |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 58 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 59 | flatbuffers::FlatBufferBuilder *fbb) const = 0; |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 62 | // Estimates the position with an incremental encoder with an index pulse and a |
| 63 | // potentiometer. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 64 | class PotAndIndexPulseZeroingEstimator : public ZeroingEstimator<PotAndIndexPosition, |
| 65 | constants::PotAndIndexPulseZeroingConstants, |
| 66 | EstimatorState> { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 67 | public: |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 68 | explicit PotAndIndexPulseZeroingEstimator( |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 69 | const constants::PotAndIndexPulseZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 70 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 71 | // Update the internal logic with the next sensor values. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 72 | void UpdateEstimate(const PotAndIndexPosition &info) override; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 73 | |
| 74 | // Reset the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 75 | void Reset() override; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 76 | |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 77 | // Manually trigger an internal error. This is used for testing the error |
| 78 | // logic. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 79 | void TriggerError() override; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 80 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 81 | bool error() const override { return error_; } |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 82 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 83 | bool zeroed() const override { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 84 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 85 | double offset() const override { return offset_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 86 | |
| 87 | // Returns a number between 0 and 1 that represents the percentage of the |
| 88 | // samples being used in the moving average filter. A value of 0.0 means that |
| 89 | // no samples are being used. A value of 1.0 means that the filter is using |
| 90 | // as many samples as it has room for. For example, after a Reset() this |
| 91 | // value returns 0.0. As more samples get added with UpdateEstimate(...) the |
| 92 | // return value starts increasing to 1.0. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 93 | double offset_ratio_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 94 | return start_pos_samples_.size() / |
| 95 | static_cast<double>(constants_.average_filter_size); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 98 | // Returns true if the sample buffer is full. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 99 | bool offset_ready() const override { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 100 | return start_pos_samples_.size() == constants_.average_filter_size; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 103 | // Returns information about our current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 104 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 105 | flatbuffers::FlatBufferBuilder *fbb) const override; |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 106 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 107 | private: |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 108 | // This function calculates the start position given the internal state and |
| 109 | // the provided `latched_encoder' value. |
| 110 | double CalculateStartPosition(double start_average, |
| 111 | double latched_encoder) const; |
| 112 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 113 | // The zeroing constants used to describe the configuration of the system. |
| 114 | const constants::PotAndIndexPulseZeroingConstants constants_; |
| 115 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 116 | // The estimated position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 117 | double position_; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 118 | // The unzeroed filtered position. |
| 119 | double filtered_position_ = 0.0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 120 | // The next position in 'start_pos_samples_' to be used to store the next |
| 121 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 122 | int samples_idx_; |
| 123 | // Last 'max_sample_count_' samples for start positions. |
| 124 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 125 | // The estimated starting position of the mechanism. We also call this the |
| 126 | // 'offset' in some contexts. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 127 | double offset_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 128 | // 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] | 129 | // after a reset. See `last_used_index_pulse_count_'. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 130 | bool wait_for_index_pulse_; |
| 131 | // After a reset we keep track of the index pulse count with this. Only after |
| 132 | // 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] | 133 | // around) will we consider the mechanism zeroed. We also use this to store |
| 134 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 135 | // position was calculated. It helps us calculate the start position only on |
| 136 | // index pulses to reject corrupted intermediate data. |
| 137 | uint32_t last_used_index_pulse_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 138 | // Marker to track whether we're fully zeroed yet or not. |
| 139 | bool zeroed_; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 140 | // Marker to track whether an error has occurred. This gets reset to false |
| 141 | // whenever Reset() is called. |
| 142 | bool error_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 143 | // Stores the position "start_pos" variable the first time the program |
| 144 | // is zeroed. |
| 145 | double first_start_pos_; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 148 | // Estimates the position with an incremental encoder and a hall effect sensor. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 149 | class HallEffectAndPositionZeroingEstimator |
| 150 | : public ZeroingEstimator<HallEffectAndPosition, |
| 151 | constants::HallEffectZeroingConstants, |
| 152 | HallEffectAndPositionEstimatorState> { |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 153 | public: |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 154 | explicit HallEffectAndPositionZeroingEstimator(const ZeroingConstants &constants); |
| 155 | |
| 156 | // Update the internal logic with the next sensor values. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 157 | void UpdateEstimate(const Position &info) override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 158 | |
| 159 | // Reset the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 160 | void Reset() override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 161 | |
| 162 | // Manually trigger an internal error. This is used for testing the error |
| 163 | // logic. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 164 | void TriggerError() override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 165 | |
| 166 | bool error() const override { return error_; } |
| 167 | |
| 168 | bool zeroed() const override { return zeroed_; } |
| 169 | |
| 170 | double offset() const override { return offset_; } |
| 171 | |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 172 | bool offset_ready() const override { return zeroed_; } |
| 173 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 174 | // Returns information about our current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 175 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 176 | flatbuffers::FlatBufferBuilder *fbb) const override; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 177 | |
| 178 | private: |
| 179 | // Sets the minimum and maximum posedge position values. |
| 180 | void StoreEncoderMaxAndMin(const HallEffectAndPosition &info); |
| 181 | |
| 182 | // The zeroing constants used to describe the configuration of the system. |
| 183 | const ZeroingConstants constants_; |
| 184 | |
| 185 | // The estimated state of the hall effect. |
| 186 | double current_ = 0.0; |
| 187 | // The estimated position. |
| 188 | double position_ = 0.0; |
| 189 | // The smallest and largest positions of the last set of encoder positions |
| 190 | // while the hall effect was low. |
| 191 | double min_low_position_; |
| 192 | double max_low_position_; |
| 193 | // If we've seen the hall effect high for enough times without going low, then |
| 194 | // we can be sure it isn't a false positive. |
| 195 | bool high_long_enough_; |
| 196 | size_t cycles_high_; |
| 197 | |
| 198 | bool last_hall_ = false; |
| 199 | |
| 200 | // The estimated starting position of the mechanism. We also call this the |
| 201 | // 'offset' in some contexts. |
| 202 | double offset_; |
| 203 | // Flag for triggering logic that takes note of the current posedge count |
| 204 | // after a reset. See `last_used_posedge_count_'. |
| 205 | bool initialized_; |
| 206 | // After a reset we keep track of the posedge count with this. Only after the |
| 207 | // posedge count changes (i.e. increments at least once or wraps around) will |
| 208 | // we consider the mechanism zeroed. We also use this to store the most recent |
| 209 | // `HallEffectAndPosition::posedge_count' value when the start position |
| 210 | // was calculated. It helps us calculate the start position only on posedges |
| 211 | // to reject corrupted intermediate data. |
| 212 | int32_t last_used_posedge_count_; |
| 213 | // Marker to track whether we're fully zeroed yet or not. |
| 214 | bool zeroed_; |
| 215 | // Marker to track whether an error has occurred. This gets reset to false |
| 216 | // whenever Reset() is called. |
| 217 | bool error_ = false; |
| 218 | // Stores the position "start_pos" variable the first time the program |
| 219 | // is zeroed. |
| 220 | double first_start_pos_; |
| 221 | }; |
| 222 | |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 223 | // Class to encapsulate the logic to decide when we are moving and which samples |
| 224 | // are safe to use. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 225 | template <typename Position, typename PositionBuffer> |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 226 | class MoveDetector { |
| 227 | public: |
| 228 | MoveDetector(size_t filter_size) { |
| 229 | buffered_samples_.reserve(filter_size); |
| 230 | Reset(); |
| 231 | } |
| 232 | |
| 233 | // Clears all the state. |
| 234 | void Reset() { |
| 235 | buffered_samples_.clear(); |
| 236 | buffered_samples_idx_ = 0; |
| 237 | } |
| 238 | |
| 239 | // Updates the detector with a new sample. Returns true if we are moving. |
| 240 | // buffer_size is the number of samples in the moving buffer, and |
| 241 | // zeroing_threshold is the max amount we can move within the period specified |
| 242 | // by buffer_size. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 243 | bool Update(const PositionBuffer &position_buffer, size_t buffer_size, |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 244 | double zeroing_threshold) { |
| 245 | bool moving = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 246 | Position position(position_buffer); |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 247 | if (buffered_samples_.size() < buffer_size) { |
| 248 | // Not enough samples to start determining if the robot is moving or not, |
| 249 | // don't use the samples yet. |
| 250 | buffered_samples_.push_back(position); |
| 251 | } else { |
| 252 | // Have enough samples to start determining if the robot is moving or not. |
| 253 | buffered_samples_[buffered_samples_idx_] = position; |
| 254 | const auto minmax_value = ::std::minmax_element( |
| 255 | buffered_samples_.begin(), buffered_samples_.end(), |
| 256 | [](const Position &left, const Position &right) { |
| 257 | return left.encoder < right.encoder; |
| 258 | }); |
| 259 | const double min_value = minmax_value.first->encoder; |
| 260 | const double max_value = minmax_value.second->encoder; |
| 261 | |
| 262 | if (::std::abs(max_value - min_value) < zeroing_threshold) { |
| 263 | // Robot isn't moving, use middle sample to determine offsets. |
| 264 | moving = false; |
| 265 | } |
| 266 | } |
| 267 | buffered_samples_idx_ = (buffered_samples_idx_ + 1) % buffer_size; |
| 268 | return moving; |
| 269 | } |
| 270 | |
| 271 | // Returns a safe sample if we aren't moving as reported by Update. |
| 272 | const Position &GetSample() const { |
| 273 | // The robot is not moving, use the middle sample to determine offsets. |
| 274 | // The middle sample makes it so that we don't use the samples from the |
| 275 | // beginning or end of periods when the robot is moving. |
| 276 | const int middle_index = |
| 277 | (buffered_samples_idx_ + (buffered_samples_.size() / 2)) % |
| 278 | buffered_samples_.size(); |
| 279 | return buffered_samples_[middle_index]; |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | // The last buffer_size samples. |
| 284 | ::std::vector<Position> buffered_samples_; |
| 285 | // The index to place the next sample in. |
| 286 | size_t buffered_samples_idx_; |
| 287 | }; |
| 288 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 289 | // Estimates the position with an absolute encoder which also reports |
| 290 | // incremental counts, and a potentiometer. |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 291 | class PotAndAbsoluteEncoderZeroingEstimator |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 292 | : public ZeroingEstimator<PotAndAbsolutePosition, |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 293 | constants::PotAndAbsoluteEncoderZeroingConstants, |
| 294 | PotAndAbsoluteEncoderEstimatorState> { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 295 | public: |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 296 | explicit PotAndAbsoluteEncoderZeroingEstimator( |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 297 | const constants::PotAndAbsoluteEncoderZeroingConstants &constants); |
| 298 | |
| 299 | // Resets the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 300 | void Reset() override; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 301 | |
| 302 | // Updates the sensor values for the zeroing logic. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 303 | void UpdateEstimate(const PotAndAbsolutePosition &info) override; |
| 304 | |
| 305 | void TriggerError() override { error_ = true; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 306 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 307 | bool zeroed() const override { return zeroed_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 308 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 309 | double offset() const override { return offset_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 310 | |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 311 | bool error() const override { return error_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 312 | |
| 313 | // Returns true if the sample buffer is full. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 314 | bool offset_ready() const override { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 315 | return relative_to_absolute_offset_samples_.size() == |
| 316 | constants_.average_filter_size && |
| 317 | offset_samples_.size() == constants_.average_filter_size; |
| 318 | } |
| 319 | |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 320 | // Returns information about our current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 321 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 322 | flatbuffers::FlatBufferBuilder *fbb) const override; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 323 | |
| 324 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 325 | struct PositionStruct { |
| 326 | PositionStruct(const PotAndAbsolutePosition &position_buffer) |
| 327 | : absolute_encoder(position_buffer.absolute_encoder()), |
| 328 | encoder(position_buffer.encoder()), |
| 329 | pot(position_buffer.pot()) {} |
| 330 | double absolute_encoder; |
| 331 | double encoder; |
| 332 | double pot; |
| 333 | }; |
| 334 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 335 | // The zeroing constants used to describe the configuration of the system. |
| 336 | const constants::PotAndAbsoluteEncoderZeroingConstants constants_; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 337 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 338 | // True if the mechanism is zeroed. |
| 339 | bool zeroed_; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 340 | // Marker to track whether an error has occurred. |
| 341 | bool error_; |
| 342 | // The first valid offset we recorded. This is only set after zeroed_ first |
| 343 | // changes to true. |
| 344 | double first_offset_; |
| 345 | |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 346 | // The filtered absolute encoder. This is used in the status for calibration. |
| 347 | double filtered_absolute_encoder_ = 0.0; |
| 348 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 349 | // Samples of the offset needed to line the relative encoder up with the |
| 350 | // absolute encoder. |
| 351 | ::std::vector<double> relative_to_absolute_offset_samples_; |
| 352 | // Offset between the Pot and Relative encoder position. |
| 353 | ::std::vector<double> offset_samples_; |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 354 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 355 | MoveDetector<PositionStruct, PotAndAbsolutePosition> move_detector_; |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 356 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 357 | // Estimated offset between the pot and relative encoder. |
| 358 | double pot_relative_encoder_offset_ = 0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 359 | // Estimated start position of the mechanism |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 360 | double offset_ = 0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 361 | // The next position in 'relative_to_absolute_offset_samples_' and |
| 362 | // 'encoder_samples_' to be used to store the next sample. |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 363 | int samples_idx_ = 0; |
| 364 | |
| 365 | size_t nan_samples_ = 0; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 366 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 367 | // The unzeroed filtered position. |
| 368 | double filtered_position_ = 0.0; |
| 369 | // The filtered position. |
| 370 | double position_ = 0.0; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 371 | }; |
| 372 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 373 | // Zeros by seeing all the index pulses in the range of motion of the mechanism |
| 374 | // and using that to figure out which index pulse is which. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 375 | class PulseIndexZeroingEstimator : public ZeroingEstimator<IndexPosition, |
| 376 | constants::EncoderPlusIndexZeroingConstants, |
| 377 | IndexEstimatorState> { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 378 | public: |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 379 | explicit PulseIndexZeroingEstimator(const ZeroingConstants &constants) |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 380 | : constants_(constants) { |
| 381 | Reset(); |
| 382 | } |
| 383 | |
| 384 | // Resets the internal logic so it needs to be re-zeroed. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 385 | void Reset() override; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 386 | |
| 387 | bool zeroed() const override { return zeroed_; } |
| 388 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 389 | // It's as ready as it'll ever be... |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 390 | bool offset_ready() const override { return true; } |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 391 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 392 | double offset() const override { return offset_; } |
| 393 | |
| 394 | bool error() const override { return error_; } |
| 395 | |
| 396 | // Updates the internal logic with the next sensor values. |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 397 | void UpdateEstimate(const IndexPosition &info) override; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 398 | |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 399 | // Returns information about our current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 400 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 401 | flatbuffers::FlatBufferBuilder *fbb) const override; |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 402 | |
Lee Mracek | cdd19ee | 2019-01-07 10:19:54 -0500 | [diff] [blame] | 403 | void TriggerError() override { error_ = true; } |
Austin Schuh | d93160a | 2017-03-05 01:00:54 -0800 | [diff] [blame] | 404 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 405 | private: |
| 406 | // Returns the current real position using the relative encoder offset. |
| 407 | double CalculateCurrentPosition(const IndexPosition &info); |
| 408 | |
| 409 | // Sets the minimum and maximum index pulse position values. |
| 410 | void StoreIndexPulseMaxAndMin(const IndexPosition &info); |
| 411 | |
| 412 | // Returns the number of index pulses we should have seen so far. |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 413 | int IndexPulseCount() const; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 414 | |
| 415 | // Contains the physical constants describing the system. |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 416 | const ZeroingConstants constants_; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 417 | |
| 418 | // The smallest position of all the index pulses. |
| 419 | double min_index_position_; |
| 420 | // The largest position of all the index pulses. |
| 421 | double max_index_position_; |
| 422 | |
| 423 | // The estimated starting position of the mechanism. |
| 424 | double offset_; |
| 425 | // After a reset we keep track of the index pulse count with this. Only after |
| 426 | // the index pulse count changes (i.e. increments at least once or wraps |
| 427 | // around) will we consider the mechanism zeroed. We also use this to store |
| 428 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 429 | // position was calculated. It helps us calculate the start position only on |
| 430 | // index pulses to reject corrupted intermediate data. |
| 431 | uint32_t last_used_index_pulse_count_; |
| 432 | |
| 433 | // True if we are fully zeroed. |
| 434 | bool zeroed_; |
| 435 | // Marker to track whether an error has occurred. |
| 436 | bool error_; |
| 437 | |
| 438 | // The estimated position. |
| 439 | double position_; |
| 440 | }; |
| 441 | |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 442 | // Estimates the position with an absolute encoder which also reports |
| 443 | // incremental counts. The absolute encoder can't spin more than one |
| 444 | // revolution. |
| 445 | class AbsoluteEncoderZeroingEstimator |
| 446 | : public ZeroingEstimator<AbsolutePosition, |
| 447 | constants::AbsoluteEncoderZeroingConstants, |
| 448 | AbsoluteEncoderEstimatorState> { |
| 449 | public: |
| 450 | explicit AbsoluteEncoderZeroingEstimator( |
| 451 | const constants::AbsoluteEncoderZeroingConstants &constants); |
| 452 | |
| 453 | // Resets the internal logic so it needs to be re-zeroed. |
| 454 | void Reset() override; |
| 455 | |
| 456 | // Updates the sensor values for the zeroing logic. |
| 457 | void UpdateEstimate(const AbsolutePosition &info) override; |
| 458 | |
| 459 | void TriggerError() override { error_ = true; } |
| 460 | |
| 461 | bool zeroed() const override { return zeroed_; } |
| 462 | |
| 463 | double offset() const override { return offset_; } |
| 464 | |
| 465 | bool error() const override { return error_; } |
| 466 | |
| 467 | // Returns true if the sample buffer is full. |
| 468 | bool offset_ready() const override { |
| 469 | return relative_to_absolute_offset_samples_.size() == |
| 470 | constants_.average_filter_size; |
| 471 | } |
| 472 | |
| 473 | // Returns information about our current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 474 | virtual flatbuffers::Offset<State> GetEstimatorState( |
| 475 | flatbuffers::FlatBufferBuilder *fbb) const override; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 476 | |
| 477 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 478 | struct PositionStruct { |
| 479 | PositionStruct(const AbsolutePosition &position_buffer) |
| 480 | : absolute_encoder(position_buffer.absolute_encoder()), |
| 481 | encoder(position_buffer.encoder()) {} |
| 482 | double absolute_encoder; |
| 483 | double encoder; |
| 484 | }; |
| 485 | |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 486 | // The zeroing constants used to describe the configuration of the system. |
| 487 | const constants::AbsoluteEncoderZeroingConstants constants_; |
| 488 | |
| 489 | // True if the mechanism is zeroed. |
| 490 | bool zeroed_; |
| 491 | // Marker to track whether an error has occurred. |
| 492 | bool error_; |
| 493 | // The first valid offset we recorded. This is only set after zeroed_ first |
| 494 | // changes to true. |
| 495 | double first_offset_; |
| 496 | |
| 497 | // The filtered absolute encoder. This is used in the status for calibration. |
| 498 | double filtered_absolute_encoder_ = 0.0; |
| 499 | |
| 500 | // Samples of the offset needed to line the relative encoder up with the |
| 501 | // absolute encoder. |
| 502 | ::std::vector<double> relative_to_absolute_offset_samples_; |
| 503 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 504 | MoveDetector<PositionStruct, AbsolutePosition> move_detector_; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 505 | |
| 506 | // Estimated start position of the mechanism |
| 507 | double offset_ = 0; |
| 508 | // The next position in 'relative_to_absolute_offset_samples_' and |
| 509 | // 'encoder_samples_' to be used to store the next sample. |
| 510 | int samples_idx_ = 0; |
| 511 | |
| 512 | // Number of NANs we've seen in a row. |
| 513 | size_t nan_samples_ = 0; |
| 514 | |
| 515 | // The filtered position. |
| 516 | double position_ = 0.0; |
| 517 | }; |
| 518 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 519 | } // namespace zeroing |
| 520 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 521 | |
| 522 | #endif // FRC971_ZEROING_ZEROING_H_ |