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 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 22 | class ZeroingEstimator { |
| 23 | public: |
| 24 | virtual ~ZeroingEstimator(){} |
| 25 | |
| 26 | // Returns true if the logic considers the corresponding mechanism to be |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 27 | // zeroed. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 28 | virtual bool zeroed() const = 0; |
| 29 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 30 | // Returns the estimated position of the corresponding mechanism. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 31 | virtual double offset() const = 0; |
| 32 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 33 | // Returns the estimated starting position of the corresponding mechansim. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 34 | virtual double position() const = 0; |
| 35 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 36 | // Returns true if there has been an error. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 37 | virtual bool error() const = 0; |
| 38 | }; |
| 39 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 40 | // Estimates the position with an incremental encoder with an index pulse and a |
| 41 | // potentiometer. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 42 | class PotAndIndexPulseZeroingEstimator : public ZeroingEstimator { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 43 | public: |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 44 | using Position = PotAndIndexPosition; |
| 45 | using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 46 | using State = EstimatorState; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 47 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 48 | PotAndIndexPulseZeroingEstimator( |
| 49 | const constants::PotAndIndexPulseZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 50 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 51 | // Update the internal logic with the next sensor values. |
| 52 | void UpdateEstimate(const PotAndIndexPosition &info); |
| 53 | |
| 54 | // Reset the internal logic so it needs to be re-zeroed. |
| 55 | void Reset(); |
| 56 | |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 57 | // Manually trigger an internal error. This is used for testing the error |
| 58 | // logic. |
| 59 | void TriggerError(); |
| 60 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 61 | bool error() const override { return error_; } |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 62 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 63 | bool zeroed() const override { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 64 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 65 | double position() const override { return position_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 66 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 67 | double offset() const override { return offset_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 68 | |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 69 | // Return the estimated position of the corresponding mechanism not using the |
| 70 | // index pulse, even if one is available. |
| 71 | double filtered_position() const { return filtered_position_; } |
| 72 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 73 | // Returns a number between 0 and 1 that represents the percentage of the |
| 74 | // samples being used in the moving average filter. A value of 0.0 means that |
| 75 | // no samples are being used. A value of 1.0 means that the filter is using |
| 76 | // as many samples as it has room for. For example, after a Reset() this |
| 77 | // value returns 0.0. As more samples get added with UpdateEstimate(...) the |
| 78 | // return value starts increasing to 1.0. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 79 | double offset_ratio_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 80 | return start_pos_samples_.size() / |
| 81 | static_cast<double>(constants_.average_filter_size); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 84 | // Returns true if the sample buffer is full. |
| 85 | bool offset_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 86 | return start_pos_samples_.size() == constants_.average_filter_size; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 89 | private: |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 90 | // This function calculates the start position given the internal state and |
| 91 | // the provided `latched_encoder' value. |
| 92 | double CalculateStartPosition(double start_average, |
| 93 | double latched_encoder) const; |
| 94 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 95 | // The zeroing constants used to describe the configuration of the system. |
| 96 | const constants::PotAndIndexPulseZeroingConstants constants_; |
| 97 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 98 | // The estimated position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 99 | double position_; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 100 | // The unzeroed filtered position. |
| 101 | double filtered_position_ = 0.0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 102 | // The next position in 'start_pos_samples_' to be used to store the next |
| 103 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 104 | int samples_idx_; |
| 105 | // Last 'max_sample_count_' samples for start positions. |
| 106 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 107 | // The estimated starting position of the mechanism. We also call this the |
| 108 | // 'offset' in some contexts. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 109 | double offset_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 110 | // 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] | 111 | // after a reset. See `last_used_index_pulse_count_'. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 112 | bool wait_for_index_pulse_; |
| 113 | // After a reset we keep track of the index pulse count with this. Only after |
| 114 | // 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] | 115 | // around) will we consider the mechanism zeroed. We also use this to store |
| 116 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 117 | // position was calculated. It helps us calculate the start position only on |
| 118 | // index pulses to reject corrupted intermediate data. |
| 119 | uint32_t last_used_index_pulse_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 120 | // Marker to track whether we're fully zeroed yet or not. |
| 121 | bool zeroed_; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 122 | // Marker to track whether an error has occurred. This gets reset to false |
| 123 | // whenever Reset() is called. |
| 124 | bool error_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 125 | // Stores the position "start_pos" variable the first time the program |
| 126 | // is zeroed. |
| 127 | double first_start_pos_; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | // Estimates the position with an absolute encoder which also reports |
| 131 | // incremental counts, and a potentiometer. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 132 | class PotAndAbsEncoderZeroingEstimator : public ZeroingEstimator { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 133 | public: |
| 134 | using Position = PotAndAbsolutePosition; |
| 135 | using ZeroingConstants = constants::PotAndAbsoluteEncoderZeroingConstants; |
| 136 | using State = AbsoluteEstimatorState; |
| 137 | |
| 138 | PotAndAbsEncoderZeroingEstimator( |
| 139 | const constants::PotAndAbsoluteEncoderZeroingConstants &constants); |
| 140 | |
| 141 | // Resets the internal logic so it needs to be re-zeroed. |
| 142 | void Reset(); |
| 143 | |
| 144 | // Updates the sensor values for the zeroing logic. |
| 145 | void UpdateEstimate(const PotAndAbsolutePosition &info); |
| 146 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 147 | bool zeroed() const override { return zeroed_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 148 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 149 | double position() const override { return position_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 150 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame] | 151 | double offset() const override { return offset_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 152 | |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 153 | bool error() const override { return error_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 154 | |
| 155 | // Returns true if the sample buffer is full. |
| 156 | bool offset_ready() const { |
| 157 | return relative_to_absolute_offset_samples_.size() == |
| 158 | constants_.average_filter_size && |
| 159 | offset_samples_.size() == constants_.average_filter_size; |
| 160 | } |
| 161 | |
| 162 | // Return the estimated position of the corresponding mechanism not using the |
| 163 | // index pulse, even if one is available. |
| 164 | double filtered_position() const { return filtered_position_; } |
| 165 | |
| 166 | private: |
| 167 | // The zeroing constants used to describe the configuration of the system. |
| 168 | const constants::PotAndAbsoluteEncoderZeroingConstants constants_; |
| 169 | // True if the mechanism is zeroed. |
| 170 | bool zeroed_; |
| 171 | // Samples of the offset needed to line the relative encoder up with the |
| 172 | // absolute encoder. |
| 173 | ::std::vector<double> relative_to_absolute_offset_samples_; |
| 174 | // Offset between the Pot and Relative encoder position. |
| 175 | ::std::vector<double> offset_samples_; |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 176 | // Last moving_buffer_size position samples to be used to determine if the |
| 177 | // robot is moving. |
| 178 | ::std::vector<PotAndAbsolutePosition> buffered_samples_; |
| 179 | // Pointer to front of the buffered samples. |
| 180 | int buffered_samples_idx_ = 0; |
| 181 | // Estimated offset between the pot and relative encoder. |
| 182 | double pot_relative_encoder_offset_ = 0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 183 | // Estimated start position of the mechanism |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 184 | double offset_ = 0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 185 | // The next position in 'relative_to_absolute_offset_samples_' and |
| 186 | // 'encoder_samples_' to be used to store the next sample. |
| 187 | int samples_idx_; |
| 188 | // The unzeroed filtered position. |
| 189 | double filtered_position_ = 0.0; |
| 190 | // The filtered position. |
| 191 | double position_ = 0.0; |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 192 | // Whether or not there is an error in the estimate. |
| 193 | bool error_ = false; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 196 | |
| 197 | // Zeros by seeing all the index pulses in the range of motion of the mechanism |
| 198 | // and using that to figure out which index pulse is which. |
| 199 | class PulseIndexZeroingEstimator : public ZeroingEstimator { |
| 200 | public: |
| 201 | using Position = IndexPosition; |
| 202 | using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants; |
| 203 | using State = EstimatorState; |
| 204 | |
| 205 | PulseIndexZeroingEstimator( |
| 206 | const constants::EncoderPlusIndexZeroingConstants &constants) |
| 207 | : constants_(constants) { |
| 208 | Reset(); |
| 209 | } |
| 210 | |
| 211 | // Resets the internal logic so it needs to be re-zeroed. |
| 212 | void Reset(); |
| 213 | |
| 214 | bool zeroed() const override { return zeroed_; } |
| 215 | |
| 216 | double position() const override { |
| 217 | CHECK(zeroed_); |
| 218 | return position_; |
| 219 | } |
| 220 | |
| 221 | double offset() const override { return offset_; } |
| 222 | |
| 223 | bool error() const override { return error_; } |
| 224 | |
| 225 | // Updates the internal logic with the next sensor values. |
| 226 | void UpdateEstimate(const IndexPosition &info); |
| 227 | |
| 228 | private: |
| 229 | // Returns the current real position using the relative encoder offset. |
| 230 | double CalculateCurrentPosition(const IndexPosition &info); |
| 231 | |
| 232 | // Sets the minimum and maximum index pulse position values. |
| 233 | void StoreIndexPulseMaxAndMin(const IndexPosition &info); |
| 234 | |
| 235 | // Returns the number of index pulses we should have seen so far. |
| 236 | int IndexPulseCount(); |
| 237 | |
| 238 | // Contains the physical constants describing the system. |
| 239 | const constants::EncoderPlusIndexZeroingConstants constants_; |
| 240 | |
| 241 | // The smallest position of all the index pulses. |
| 242 | double min_index_position_; |
| 243 | // The largest position of all the index pulses. |
| 244 | double max_index_position_; |
| 245 | |
| 246 | // The estimated starting position of the mechanism. |
| 247 | double offset_; |
| 248 | // After a reset we keep track of the index pulse count with this. Only after |
| 249 | // the index pulse count changes (i.e. increments at least once or wraps |
| 250 | // around) will we consider the mechanism zeroed. We also use this to store |
| 251 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 252 | // position was calculated. It helps us calculate the start position only on |
| 253 | // index pulses to reject corrupted intermediate data. |
| 254 | uint32_t last_used_index_pulse_count_; |
| 255 | |
| 256 | // True if we are fully zeroed. |
| 257 | bool zeroed_; |
| 258 | // Marker to track whether an error has occurred. |
| 259 | bool error_; |
| 260 | |
| 261 | // The estimated position. |
| 262 | double position_; |
| 263 | }; |
| 264 | |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 265 | // Populates an EstimatorState struct with information from the zeroing |
| 266 | // estimator. |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 267 | void PopulateEstimatorState(const PotAndIndexPulseZeroingEstimator &estimator, |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 268 | EstimatorState *state); |
| 269 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 270 | void PopulateEstimatorState(const PotAndAbsEncoderZeroingEstimator &estimator, |
| 271 | AbsoluteEstimatorState *state); |
| 272 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 273 | } // namespace zeroing |
| 274 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 275 | |
| 276 | #endif // FRC971_ZEROING_ZEROING_H_ |