Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 1 | #include "frc971/zeroing/zeroing.h" |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 2 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 3 | #include <algorithm> |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 4 | #include <cmath> |
| 5 | #include <limits> |
| 6 | #include <vector> |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 7 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 8 | #include "frc971/zeroing/wrap.h" |
| 9 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 10 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 11 | namespace frc971 { |
| 12 | namespace zeroing { |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 13 | namespace { |
| 14 | |
| 15 | bool compare_encoder(const PotAndAbsolutePosition &left, |
| 16 | const PotAndAbsolutePosition &right) { |
| 17 | return left.encoder < right.encoder; |
| 18 | } |
| 19 | |
| 20 | } // namespace |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 21 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 22 | void PopulateEstimatorState( |
| 23 | const zeroing::PotAndIndexPulseZeroingEstimator &estimator, |
| 24 | EstimatorState *state) { |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 25 | state->error = estimator.error(); |
| 26 | state->zeroed = estimator.zeroed(); |
| 27 | state->position = estimator.position(); |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 28 | state->pot_position = estimator.filtered_position(); |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 31 | void PopulateEstimatorState( |
| 32 | const zeroing::PotAndAbsEncoderZeroingEstimator &estimator, |
| 33 | AbsoluteEstimatorState *state) { |
| 34 | state->error = estimator.error(); |
| 35 | state->zeroed = estimator.zeroed(); |
| 36 | |
| 37 | state->position = estimator.position(); |
| 38 | state->pot_position = estimator.filtered_position(); |
| 39 | } |
| 40 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 41 | PotAndIndexPulseZeroingEstimator::PotAndIndexPulseZeroingEstimator( |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 42 | const constants::PotAndIndexPulseZeroingConstants &constants) |
| 43 | : constants_(constants) { |
| 44 | start_pos_samples_.reserve(constants_.average_filter_size); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 45 | Reset(); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 48 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 49 | void PotAndIndexPulseZeroingEstimator::Reset() { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 50 | samples_idx_ = 0; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 51 | offset_ = 0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 52 | start_pos_samples_.clear(); |
| 53 | zeroed_ = false; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 54 | wait_for_index_pulse_ = true; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 55 | last_used_index_pulse_count_ = 0; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 56 | first_start_pos_ = 0.0; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 57 | error_ = false; |
| 58 | } |
| 59 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 60 | void PotAndIndexPulseZeroingEstimator::TriggerError() { |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 61 | if (!error_) { |
| 62 | LOG(ERROR, "Manually triggered zeroing error.\n"); |
| 63 | error_ = true; |
| 64 | } |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 67 | double PotAndIndexPulseZeroingEstimator::CalculateStartPosition( |
| 68 | double start_average, double latched_encoder) const { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 69 | // We calculate an aproximation of the value of the last index position. |
| 70 | // Also account for index pulses not lining up with integer multiples of the |
| 71 | // index_diff. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 72 | double index_pos = |
| 73 | start_average + latched_encoder - constants_.measured_index_position; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 74 | // We round index_pos to the closest valid value of the index. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 75 | double accurate_index_pos = (round(index_pos / constants_.index_difference)) * |
| 76 | constants_.index_difference; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 77 | // Now we reverse the first calculation to get the accurate start position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 78 | return accurate_index_pos - latched_encoder + |
| 79 | constants_.measured_index_position; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 82 | void PotAndIndexPulseZeroingEstimator::UpdateEstimate( |
| 83 | const PotAndIndexPosition &info) { |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 84 | // We want to make sure that we encounter at least one index pulse while |
| 85 | // zeroing. So we take the index pulse count from the first sample after |
| 86 | // reset and wait for that count to change before we consider ourselves |
| 87 | // zeroed. |
| 88 | if (wait_for_index_pulse_) { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 89 | last_used_index_pulse_count_ = info.index_pulses; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 90 | wait_for_index_pulse_ = false; |
| 91 | } |
| 92 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 93 | if (start_pos_samples_.size() < constants_.average_filter_size) { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 94 | start_pos_samples_.push_back(info.pot - info.encoder); |
| 95 | } else { |
| 96 | start_pos_samples_[samples_idx_] = info.pot - info.encoder; |
| 97 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 98 | |
| 99 | // Drop the oldest sample when we run this function the next time around. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 100 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 101 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 102 | double sample_sum = 0.0; |
| 103 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 104 | for (size_t i = 0; i < start_pos_samples_.size(); ++i) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 105 | sample_sum += start_pos_samples_[i]; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Calculates the average of the starting position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 109 | double start_average = sample_sum / start_pos_samples_.size(); |
| 110 | |
| 111 | // If there are no index pulses to use or we don't have enough samples yet to |
| 112 | // have a well-filtered starting position then we use the filtered value as |
| 113 | // our best guess. |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 114 | if (!zeroed_ && |
| 115 | (info.index_pulses == last_used_index_pulse_count_ || !offset_ready())) { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 116 | offset_ = start_average; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 117 | } else if (!zeroed_ || last_used_index_pulse_count_ != info.index_pulses) { |
| 118 | // Note the accurate start position and the current index pulse count so |
| 119 | // that we only run this logic once per index pulse. That should be more |
| 120 | // resilient to corrupted intermediate data. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 121 | offset_ = CalculateStartPosition(start_average, info.latched_encoder); |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 122 | last_used_index_pulse_count_ = info.index_pulses; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 123 | |
| 124 | // TODO(austin): Reject encoder positions which have x% error rather than |
| 125 | // rounding to the closest index pulse. |
| 126 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 127 | // Save the first starting position. |
| 128 | if (!zeroed_) { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 129 | first_start_pos_ = offset_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 130 | LOG(INFO, "latching start position %f\n", first_start_pos_); |
| 131 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 132 | |
| 133 | // Now that we have an accurate starting position we can consider ourselves |
| 134 | // zeroed. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 135 | zeroed_ = true; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 136 | // Throw an error if first_start_pos is bigger/smaller than |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 137 | // constants_.allowable_encoder_error * index_diff + start_pos. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 138 | if (::std::abs(first_start_pos_ - offset_) > |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 139 | constants_.allowable_encoder_error * constants_.index_difference) { |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 140 | if (!error_) { |
| 141 | LOG(ERROR, |
| 142 | "Encoder ticks out of range since last index pulse. first start " |
Austin Schuh | 1c85bc8 | 2016-04-03 21:36:31 -0700 | [diff] [blame] | 143 | "position: %f recent starting position: %f, allowable error: %f\n", |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 144 | first_start_pos_, offset_, |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 145 | constants_.allowable_encoder_error * constants_.index_difference); |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 146 | error_ = true; |
| 147 | } |
| 148 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 149 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 150 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 151 | position_ = offset_ + info.encoder; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 152 | filtered_position_ = start_average + info.encoder; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 155 | PotAndAbsEncoderZeroingEstimator::PotAndAbsEncoderZeroingEstimator( |
| 156 | const constants::PotAndAbsoluteEncoderZeroingConstants &constants) |
| 157 | : constants_(constants) { |
| 158 | relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size); |
| 159 | offset_samples_.reserve(constants_.average_filter_size); |
| 160 | Reset(); |
| 161 | } |
| 162 | |
| 163 | void PotAndAbsEncoderZeroingEstimator::Reset() { |
| 164 | zeroed_ = false; |
| 165 | relative_to_absolute_offset_samples_.clear(); |
| 166 | offset_samples_.clear(); |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 167 | buffered_samples_.clear(); |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // So, this needs to be a multistep process. We need to first estimate the |
| 171 | // offset between the absolute encoder and the relative encoder. That process |
| 172 | // should get us an absolute number which is off by integer multiples of the |
| 173 | // distance/rev. In parallel, we can estimate the offset between the pot and |
| 174 | // encoder. When both estimates have converged, we can then compute the offset |
| 175 | // in a cycle, and which cycle, which gives us the accurate global offset. |
| 176 | // |
| 177 | // It's tricky to compute the offset between the absolute and relative encoder. |
| 178 | // We need to compute this inside 1 revolution. The easiest way to do this |
| 179 | // would be to wrap the encoder, subtract the two of them, and then average the |
| 180 | // result. That will struggle when they are off by PI. Instead, we need to |
| 181 | // wrap the number to +- PI from the current averaged offset. |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 182 | // |
| 183 | // To guard against the robot moving while updating estimates, buffer a number |
| 184 | // of samples and check that the buffered samples are not different than the |
| 185 | // zeroing threshold. At any point that the samples differ too much, do not |
| 186 | // update estimates based on those samples. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 187 | void PotAndAbsEncoderZeroingEstimator::UpdateEstimate( |
| 188 | const PotAndAbsolutePosition &info) { |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 189 | // Check for Abs Encoder NaN value that would mess up the rest of the zeroing |
| 190 | // code below. NaN values are given when the Absolute Encoder is disconnected. |
| 191 | if (::std::isnan(info.absolute_encoder)) { |
| 192 | error_ = true; |
| 193 | return; |
| 194 | } |
| 195 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 196 | bool moving = true; |
| 197 | if (buffered_samples_.size() < constants_.moving_buffer_size) { |
| 198 | // Not enough samples to start determining if the robot is moving or not, |
| 199 | // don't use the samples yet. |
| 200 | buffered_samples_.push_back(info); |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 201 | } else { |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 202 | // Have enough samples to start determining if the robot is moving or not. |
| 203 | buffered_samples_[buffered_samples_idx_] = info; |
| 204 | auto max_value = |
| 205 | ::std::max_element(buffered_samples_.begin(), buffered_samples_.end(), |
| 206 | compare_encoder) |
| 207 | ->encoder; |
| 208 | auto min_value = |
| 209 | ::std::min_element(buffered_samples_.begin(), buffered_samples_.end(), |
| 210 | compare_encoder) |
| 211 | ->encoder; |
| 212 | if (::std::abs(max_value - min_value) < constants_.zeroing_threshold) { |
| 213 | // Robot isn't moving, use middle sample to determine offsets. |
| 214 | moving = false; |
| 215 | } |
| 216 | } |
| 217 | buffered_samples_idx_ = |
| 218 | (buffered_samples_idx_ + 1) % constants_.moving_buffer_size; |
| 219 | |
| 220 | if (!moving) { |
| 221 | // The robot is not moving, use the middle sample to determine offsets. |
| 222 | const int middle_index = |
| 223 | (buffered_samples_idx_ + (constants_.moving_buffer_size - 1) / 2) % |
| 224 | constants_.moving_buffer_size; |
| 225 | |
| 226 | // Compute the sum of all the offset samples. |
| 227 | double relative_to_absolute_offset_sum = 0.0; |
| 228 | for (size_t i = 0; i < relative_to_absolute_offset_samples_.size(); ++i) { |
| 229 | relative_to_absolute_offset_sum += |
| 230 | relative_to_absolute_offset_samples_[i]; |
| 231 | } |
| 232 | |
| 233 | // Compute the average offset between the absolute encoder and relative |
| 234 | // encoder. If we have 0 samples, assume it is 0. |
| 235 | double average_relative_to_absolute_offset = |
| 236 | relative_to_absolute_offset_samples_.size() == 0 |
| 237 | ? 0.0 |
| 238 | : relative_to_absolute_offset_sum / |
| 239 | relative_to_absolute_offset_samples_.size(); |
| 240 | |
| 241 | // Now, compute the nearest absolute encoder value to the offset relative |
| 242 | // encoder position. |
| 243 | const double adjusted_absolute_encoder = |
| 244 | Wrap(buffered_samples_[middle_index].encoder + |
| 245 | average_relative_to_absolute_offset, |
| 246 | buffered_samples_[middle_index].absolute_encoder - |
| 247 | constants_.measured_absolute_position, |
| 248 | constants_.one_revolution_distance); |
| 249 | |
| 250 | const double relative_to_absolute_offset = |
| 251 | adjusted_absolute_encoder - buffered_samples_[middle_index].encoder; |
| 252 | |
| 253 | // Add the sample and update the average with the new reading. |
| 254 | const size_t relative_to_absolute_offset_samples_size = |
| 255 | relative_to_absolute_offset_samples_.size(); |
| 256 | if (relative_to_absolute_offset_samples_size < |
| 257 | constants_.average_filter_size) { |
| 258 | average_relative_to_absolute_offset = |
| 259 | (average_relative_to_absolute_offset * |
| 260 | relative_to_absolute_offset_samples_size + |
| 261 | relative_to_absolute_offset) / |
| 262 | (relative_to_absolute_offset_samples_size + 1); |
| 263 | |
| 264 | relative_to_absolute_offset_samples_.push_back( |
| 265 | relative_to_absolute_offset); |
| 266 | } else { |
| 267 | average_relative_to_absolute_offset -= |
| 268 | relative_to_absolute_offset_samples_[samples_idx_] / |
| 269 | relative_to_absolute_offset_samples_size; |
| 270 | relative_to_absolute_offset_samples_[samples_idx_] = |
| 271 | relative_to_absolute_offset; |
| 272 | average_relative_to_absolute_offset += |
| 273 | relative_to_absolute_offset / |
| 274 | relative_to_absolute_offset_samples_size; |
| 275 | } |
| 276 | |
| 277 | // Now compute the offset between the pot and relative encoder. |
| 278 | if (offset_samples_.size() < constants_.average_filter_size) { |
| 279 | offset_samples_.push_back(buffered_samples_[middle_index].pot - |
| 280 | buffered_samples_[middle_index].encoder); |
| 281 | } else { |
| 282 | offset_samples_[samples_idx_] = buffered_samples_[middle_index].pot - |
| 283 | buffered_samples_[middle_index].encoder; |
| 284 | } |
| 285 | |
| 286 | // Drop the oldest sample when we run this function the next time around. |
| 287 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
| 288 | |
| 289 | double pot_relative_encoder_offset_sum = 0.0; |
| 290 | for (size_t i = 0; i < offset_samples_.size(); ++i) { |
| 291 | pot_relative_encoder_offset_sum += offset_samples_[i]; |
| 292 | } |
| 293 | pot_relative_encoder_offset_ = |
| 294 | pot_relative_encoder_offset_sum / offset_samples_.size(); |
| 295 | |
| 296 | offset_ = Wrap(buffered_samples_[middle_index].encoder + |
| 297 | pot_relative_encoder_offset_, |
| 298 | average_relative_to_absolute_offset + |
| 299 | buffered_samples_[middle_index].encoder, |
| 300 | constants_.one_revolution_distance) - |
| 301 | buffered_samples_[middle_index].encoder; |
| 302 | if (offset_ready()) { |
| 303 | zeroed_ = true; |
| 304 | } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 307 | // Update the position. |
| 308 | filtered_position_ = pot_relative_encoder_offset_ + info.encoder; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 309 | position_ = offset_ + info.encoder; |
| 310 | } |
| 311 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame^] | 312 | void PulseIndexZeroingEstimator::Reset() { |
| 313 | max_index_position_ = ::std::numeric_limits<double>::lowest(); |
| 314 | min_index_position_ = ::std::numeric_limits<double>::max(); |
| 315 | offset_ = 0; |
| 316 | last_used_index_pulse_count_ = 0; |
| 317 | zeroed_ = false; |
| 318 | error_ = false; |
| 319 | } |
| 320 | |
| 321 | void PulseIndexZeroingEstimator::StoreIndexPulseMaxAndMin( |
| 322 | const IndexPosition &info) { |
| 323 | // If we have a new index pulse. |
| 324 | if (last_used_index_pulse_count_ != info.index_pulses) { |
| 325 | // If the latest pulses's position is outside the range we've currently |
| 326 | // seen, record it appropriately. |
| 327 | if (info.latched_encoder > max_index_position_) { |
| 328 | max_index_position_ = info.latched_encoder; |
| 329 | } |
| 330 | if (info.latched_encoder < min_index_position_) { |
| 331 | min_index_position_ = info.latched_encoder; |
| 332 | } |
| 333 | last_used_index_pulse_count_ = info.index_pulses; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | int PulseIndexZeroingEstimator::IndexPulseCount() { |
| 338 | if (min_index_position_ > max_index_position_) { |
| 339 | // This condition means we haven't seen a pulse yet. |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | // Calculate the number of pulses encountered so far. |
| 344 | return 1 + static_cast<int>( |
| 345 | ::std::round((max_index_position_ - min_index_position_) / |
| 346 | constants_.index_difference)); |
| 347 | } |
| 348 | |
| 349 | void PulseIndexZeroingEstimator::UpdateEstimate(const IndexPosition &info) { |
| 350 | StoreIndexPulseMaxAndMin(info); |
| 351 | const int index_pulse_count = IndexPulseCount(); |
| 352 | if (index_pulse_count > constants_.index_pulse_count) { |
| 353 | error_ = true; |
| 354 | } |
| 355 | |
| 356 | // TODO(austin): Detect if the encoder or index pulse is unplugged. |
| 357 | // TODO(austin): Detect missing counts. |
| 358 | |
| 359 | if (index_pulse_count == constants_.index_pulse_count && !zeroed_) { |
| 360 | offset_ = constants_.measured_index_position - |
| 361 | constants_.known_index_pulse * constants_.index_difference - |
| 362 | min_index_position_; |
| 363 | zeroed_ = true; |
| 364 | } |
| 365 | if (zeroed_) { |
| 366 | position_ = info.encoder + offset_; |
| 367 | } |
| 368 | } |
| 369 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 370 | } // namespace zeroing |
| 371 | } // namespace frc971 |