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