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 | |
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 | PotAndIndexPulseZeroingEstimator::PotAndIndexPulseZeroingEstimator( |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 22 | const constants::PotAndIndexPulseZeroingConstants &constants) |
| 23 | : constants_(constants) { |
| 24 | start_pos_samples_.reserve(constants_.average_filter_size); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 25 | Reset(); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 26 | } |
| 27 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 28 | void PotAndIndexPulseZeroingEstimator::Reset() { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 29 | samples_idx_ = 0; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 30 | offset_ = 0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 31 | start_pos_samples_.clear(); |
| 32 | zeroed_ = false; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 33 | wait_for_index_pulse_ = true; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 34 | last_used_index_pulse_count_ = 0; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 35 | error_ = false; |
| 36 | } |
| 37 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 38 | void PotAndIndexPulseZeroingEstimator::TriggerError() { |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 39 | if (!error_) { |
| 40 | LOG(ERROR, "Manually triggered zeroing error.\n"); |
| 41 | error_ = true; |
| 42 | } |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 45 | double PotAndIndexPulseZeroingEstimator::CalculateStartPosition( |
| 46 | double start_average, double latched_encoder) const { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 47 | // We calculate an aproximation of the value of the last index position. |
| 48 | // Also account for index pulses not lining up with integer multiples of the |
| 49 | // index_diff. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 50 | double index_pos = |
| 51 | start_average + latched_encoder - constants_.measured_index_position; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 52 | // We round index_pos to the closest valid value of the index. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 53 | double accurate_index_pos = (round(index_pos / constants_.index_difference)) * |
| 54 | constants_.index_difference; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 55 | // Now we reverse the first calculation to get the accurate start position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 56 | return accurate_index_pos - latched_encoder + |
| 57 | constants_.measured_index_position; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 60 | void PotAndIndexPulseZeroingEstimator::UpdateEstimate( |
| 61 | const PotAndIndexPosition &info) { |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 62 | // We want to make sure that we encounter at least one index pulse while |
| 63 | // zeroing. So we take the index pulse count from the first sample after |
| 64 | // reset and wait for that count to change before we consider ourselves |
| 65 | // zeroed. |
| 66 | if (wait_for_index_pulse_) { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 67 | last_used_index_pulse_count_ = info.index_pulses; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 68 | wait_for_index_pulse_ = false; |
| 69 | } |
| 70 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 71 | if (start_pos_samples_.size() < constants_.average_filter_size) { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 72 | start_pos_samples_.push_back(info.pot - info.encoder); |
| 73 | } else { |
| 74 | start_pos_samples_[samples_idx_] = info.pot - info.encoder; |
| 75 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 76 | |
| 77 | // 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] | 78 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 79 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 80 | double sample_sum = 0.0; |
| 81 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 82 | for (size_t i = 0; i < start_pos_samples_.size(); ++i) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 83 | sample_sum += start_pos_samples_[i]; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Calculates the average of the starting position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 87 | double start_average = sample_sum / start_pos_samples_.size(); |
| 88 | |
| 89 | // If there are no index pulses to use or we don't have enough samples yet to |
| 90 | // have a well-filtered starting position then we use the filtered value as |
| 91 | // our best guess. |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 92 | if (!zeroed_ && |
| 93 | (info.index_pulses == last_used_index_pulse_count_ || !offset_ready())) { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 94 | offset_ = start_average; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 95 | } else if (!zeroed_ || last_used_index_pulse_count_ != info.index_pulses) { |
| 96 | // Note the accurate start position and the current index pulse count so |
| 97 | // that we only run this logic once per index pulse. That should be more |
| 98 | // resilient to corrupted intermediate data. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 99 | offset_ = CalculateStartPosition(start_average, info.latched_encoder); |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 100 | last_used_index_pulse_count_ = info.index_pulses; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 101 | |
| 102 | // TODO(austin): Reject encoder positions which have x% error rather than |
| 103 | // rounding to the closest index pulse. |
| 104 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 105 | // Save the first starting position. |
| 106 | if (!zeroed_) { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 107 | first_start_pos_ = offset_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 108 | LOG(INFO, "latching start position %f\n", first_start_pos_); |
| 109 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 110 | |
| 111 | // Now that we have an accurate starting position we can consider ourselves |
| 112 | // zeroed. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 113 | zeroed_ = true; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 114 | // Throw an error if first_start_pos is bigger/smaller than |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 115 | // constants_.allowable_encoder_error * index_diff + start_pos. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 116 | if (::std::abs(first_start_pos_ - offset_) > |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 117 | constants_.allowable_encoder_error * constants_.index_difference) { |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 118 | if (!error_) { |
| 119 | LOG(ERROR, |
| 120 | "Encoder ticks out of range since last index pulse. first start " |
Austin Schuh | 1c85bc8 | 2016-04-03 21:36:31 -0700 | [diff] [blame] | 121 | "position: %f recent starting position: %f, allowable error: %f\n", |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 122 | first_start_pos_, offset_, |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 123 | constants_.allowable_encoder_error * constants_.index_difference); |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 124 | error_ = true; |
| 125 | } |
| 126 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 127 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 128 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 129 | position_ = offset_ + info.encoder; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 130 | filtered_position_ = start_average + info.encoder; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 133 | PotAndIndexPulseZeroingEstimator::State |
| 134 | PotAndIndexPulseZeroingEstimator::GetEstimatorState() const { |
| 135 | State r; |
| 136 | r.error = error_; |
| 137 | r.zeroed = zeroed_; |
| 138 | r.position = position_; |
| 139 | r.pot_position = filtered_position_; |
| 140 | return r; |
| 141 | } |
| 142 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 143 | HallEffectAndPositionZeroingEstimator::HallEffectAndPositionZeroingEstimator( |
| 144 | const ZeroingConstants &constants) |
| 145 | : constants_(constants) { |
| 146 | Reset(); |
| 147 | } |
| 148 | |
| 149 | void HallEffectAndPositionZeroingEstimator::Reset() { |
| 150 | offset_ = 0.0; |
| 151 | min_low_position_ = ::std::numeric_limits<double>::max(); |
| 152 | max_low_position_ = ::std::numeric_limits<double>::lowest(); |
| 153 | zeroed_ = false; |
| 154 | initialized_ = false; |
| 155 | last_used_posedge_count_ = 0; |
| 156 | cycles_high_ = 0; |
| 157 | high_long_enough_ = false; |
| 158 | first_start_pos_ = 0.0; |
| 159 | error_ = false; |
| 160 | current_ = 0.0; |
| 161 | first_start_pos_ = 0.0; |
| 162 | } |
| 163 | |
| 164 | void HallEffectAndPositionZeroingEstimator::TriggerError() { |
| 165 | if (!error_) { |
| 166 | LOG(ERROR, "Manually triggered zeroing error.\n"); |
| 167 | error_ = true; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void HallEffectAndPositionZeroingEstimator::StoreEncoderMaxAndMin( |
| 172 | const HallEffectAndPosition &info) { |
| 173 | // If we have a new posedge. |
| 174 | if (!info.current) { |
| 175 | if (last_hall_) { |
| 176 | min_low_position_ = max_low_position_ = info.position; |
| 177 | } else { |
| 178 | min_low_position_ = ::std::min(min_low_position_, info.position); |
| 179 | max_low_position_ = ::std::max(max_low_position_, info.position); |
| 180 | } |
| 181 | } |
| 182 | last_hall_ = info.current; |
| 183 | } |
| 184 | |
| 185 | void HallEffectAndPositionZeroingEstimator::UpdateEstimate( |
| 186 | const HallEffectAndPosition &info) { |
| 187 | // We want to make sure that we encounter at least one posedge while zeroing. |
| 188 | // So we take the posedge count from the first sample after reset and wait for |
| 189 | // that count to change and for the hall effect to stay high before we |
| 190 | // consider ourselves zeroed. |
| 191 | if (!initialized_) { |
| 192 | last_used_posedge_count_ = info.posedge_count; |
| 193 | initialized_ = true; |
| 194 | last_hall_ = info.current; |
| 195 | } |
| 196 | |
| 197 | StoreEncoderMaxAndMin(info); |
| 198 | |
| 199 | if (info.current) { |
| 200 | cycles_high_++; |
| 201 | } else { |
| 202 | cycles_high_ = 0; |
| 203 | last_used_posedge_count_ = info.posedge_count; |
| 204 | } |
| 205 | |
| 206 | high_long_enough_ = cycles_high_ >= constants_.hall_trigger_zeroing_length; |
| 207 | |
| 208 | bool moving_backward = false; |
| 209 | if (constants_.zeroing_move_direction) { |
| 210 | moving_backward = info.position > min_low_position_; |
| 211 | } else { |
| 212 | moving_backward = info.position < max_low_position_; |
| 213 | } |
| 214 | |
| 215 | // If there are no posedges to use or we don't have enough samples yet to |
| 216 | // have a well-filtered starting position then we use the filtered value as |
| 217 | // our best guess. |
| 218 | if (last_used_posedge_count_ != info.posedge_count && high_long_enough_ && |
| 219 | moving_backward) { |
| 220 | // Note the offset and the current posedge count so that we only run this |
| 221 | // logic once per posedge. That should be more resilient to corrupted |
| 222 | // intermediate data. |
| 223 | offset_ = -info.posedge_value; |
| 224 | if (constants_.zeroing_move_direction) { |
| 225 | offset_ += constants_.lower_hall_position; |
| 226 | } else { |
| 227 | offset_ += constants_.upper_hall_position; |
| 228 | } |
| 229 | last_used_posedge_count_ = info.posedge_count; |
| 230 | |
| 231 | // Save the first starting position. |
| 232 | if (!zeroed_) { |
| 233 | first_start_pos_ = offset_; |
| 234 | LOG(INFO, "latching start position %f\n", first_start_pos_); |
| 235 | } |
| 236 | |
| 237 | // Now that we have an accurate starting position we can consider ourselves |
| 238 | // zeroed. |
| 239 | zeroed_ = true; |
| 240 | } |
| 241 | |
| 242 | position_ = info.position - offset_; |
| 243 | } |
| 244 | |
| 245 | HallEffectAndPositionZeroingEstimator::State |
| 246 | HallEffectAndPositionZeroingEstimator::GetEstimatorState() const { |
| 247 | State r; |
| 248 | r.error = error_; |
| 249 | r.zeroed = zeroed_; |
| 250 | r.encoder = position_; |
| 251 | r.high_long_enough = high_long_enough_; |
| 252 | r.offset = offset_; |
| 253 | return r; |
| 254 | } |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 255 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 256 | PotAndAbsEncoderZeroingEstimator::PotAndAbsEncoderZeroingEstimator( |
| 257 | const constants::PotAndAbsoluteEncoderZeroingConstants &constants) |
| 258 | : constants_(constants) { |
| 259 | relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size); |
| 260 | offset_samples_.reserve(constants_.average_filter_size); |
| 261 | Reset(); |
| 262 | } |
| 263 | |
| 264 | void PotAndAbsEncoderZeroingEstimator::Reset() { |
| 265 | zeroed_ = false; |
| 266 | relative_to_absolute_offset_samples_.clear(); |
| 267 | offset_samples_.clear(); |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 268 | buffered_samples_.clear(); |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 269 | error_ = false; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // So, this needs to be a multistep process. We need to first estimate the |
| 273 | // offset between the absolute encoder and the relative encoder. That process |
| 274 | // should get us an absolute number which is off by integer multiples of the |
| 275 | // distance/rev. In parallel, we can estimate the offset between the pot and |
| 276 | // encoder. When both estimates have converged, we can then compute the offset |
| 277 | // in a cycle, and which cycle, which gives us the accurate global offset. |
| 278 | // |
| 279 | // It's tricky to compute the offset between the absolute and relative encoder. |
| 280 | // We need to compute this inside 1 revolution. The easiest way to do this |
| 281 | // would be to wrap the encoder, subtract the two of them, and then average the |
| 282 | // result. That will struggle when they are off by PI. Instead, we need to |
| 283 | // wrap the number to +- PI from the current averaged offset. |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 284 | // |
| 285 | // To guard against the robot moving while updating estimates, buffer a number |
| 286 | // of samples and check that the buffered samples are not different than the |
| 287 | // zeroing threshold. At any point that the samples differ too much, do not |
| 288 | // update estimates based on those samples. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 289 | void PotAndAbsEncoderZeroingEstimator::UpdateEstimate( |
| 290 | const PotAndAbsolutePosition &info) { |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 291 | // Check for Abs Encoder NaN value that would mess up the rest of the zeroing |
| 292 | // code below. NaN values are given when the Absolute Encoder is disconnected. |
| 293 | if (::std::isnan(info.absolute_encoder)) { |
| 294 | error_ = true; |
| 295 | return; |
| 296 | } |
| 297 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 298 | bool moving = true; |
| 299 | if (buffered_samples_.size() < constants_.moving_buffer_size) { |
| 300 | // Not enough samples to start determining if the robot is moving or not, |
| 301 | // don't use the samples yet. |
| 302 | buffered_samples_.push_back(info); |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 303 | } else { |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 304 | // Have enough samples to start determining if the robot is moving or not. |
| 305 | buffered_samples_[buffered_samples_idx_] = info; |
| 306 | auto max_value = |
| 307 | ::std::max_element(buffered_samples_.begin(), buffered_samples_.end(), |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame^] | 308 | compare_encoder)->encoder; |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 309 | auto min_value = |
| 310 | ::std::min_element(buffered_samples_.begin(), buffered_samples_.end(), |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame^] | 311 | compare_encoder)->encoder; |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 312 | if (::std::abs(max_value - min_value) < constants_.zeroing_threshold) { |
| 313 | // Robot isn't moving, use middle sample to determine offsets. |
| 314 | moving = false; |
| 315 | } |
| 316 | } |
| 317 | buffered_samples_idx_ = |
| 318 | (buffered_samples_idx_ + 1) % constants_.moving_buffer_size; |
| 319 | |
| 320 | if (!moving) { |
| 321 | // The robot is not moving, use the middle sample to determine offsets. |
| 322 | const int middle_index = |
| 323 | (buffered_samples_idx_ + (constants_.moving_buffer_size - 1) / 2) % |
| 324 | constants_.moving_buffer_size; |
| 325 | |
| 326 | // Compute the sum of all the offset samples. |
| 327 | double relative_to_absolute_offset_sum = 0.0; |
| 328 | for (size_t i = 0; i < relative_to_absolute_offset_samples_.size(); ++i) { |
| 329 | relative_to_absolute_offset_sum += |
| 330 | relative_to_absolute_offset_samples_[i]; |
| 331 | } |
| 332 | |
| 333 | // Compute the average offset between the absolute encoder and relative |
| 334 | // encoder. If we have 0 samples, assume it is 0. |
| 335 | double average_relative_to_absolute_offset = |
| 336 | relative_to_absolute_offset_samples_.size() == 0 |
| 337 | ? 0.0 |
| 338 | : relative_to_absolute_offset_sum / |
| 339 | relative_to_absolute_offset_samples_.size(); |
| 340 | |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 341 | const double adjusted_incremental_encoder = |
| 342 | buffered_samples_[middle_index].encoder + |
| 343 | average_relative_to_absolute_offset; |
| 344 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 345 | // Now, compute the nearest absolute encoder value to the offset relative |
| 346 | // encoder position. |
| 347 | const double adjusted_absolute_encoder = |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 348 | Wrap(adjusted_incremental_encoder, |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 349 | buffered_samples_[middle_index].absolute_encoder - |
| 350 | constants_.measured_absolute_position, |
| 351 | constants_.one_revolution_distance); |
| 352 | |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 353 | // Reverse the math on the previous line to compute the absolute encoder. |
| 354 | // Do this by taking the adjusted encoder, and then subtracting off the |
| 355 | // second argument above, and the value that was added by Wrap. |
| 356 | filtered_absolute_encoder_ = |
| 357 | ((buffered_samples_[middle_index].encoder + |
| 358 | average_relative_to_absolute_offset) - |
| 359 | (-constants_.measured_absolute_position + |
| 360 | (adjusted_absolute_encoder - |
| 361 | (buffered_samples_[middle_index].absolute_encoder - |
| 362 | constants_.measured_absolute_position)))); |
| 363 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 364 | const double relative_to_absolute_offset = |
| 365 | adjusted_absolute_encoder - buffered_samples_[middle_index].encoder; |
| 366 | |
| 367 | // Add the sample and update the average with the new reading. |
| 368 | const size_t relative_to_absolute_offset_samples_size = |
| 369 | relative_to_absolute_offset_samples_.size(); |
| 370 | if (relative_to_absolute_offset_samples_size < |
| 371 | constants_.average_filter_size) { |
| 372 | average_relative_to_absolute_offset = |
| 373 | (average_relative_to_absolute_offset * |
| 374 | relative_to_absolute_offset_samples_size + |
| 375 | relative_to_absolute_offset) / |
| 376 | (relative_to_absolute_offset_samples_size + 1); |
| 377 | |
| 378 | relative_to_absolute_offset_samples_.push_back( |
| 379 | relative_to_absolute_offset); |
| 380 | } else { |
| 381 | average_relative_to_absolute_offset -= |
| 382 | relative_to_absolute_offset_samples_[samples_idx_] / |
| 383 | relative_to_absolute_offset_samples_size; |
| 384 | relative_to_absolute_offset_samples_[samples_idx_] = |
| 385 | relative_to_absolute_offset; |
| 386 | average_relative_to_absolute_offset += |
| 387 | relative_to_absolute_offset / |
| 388 | relative_to_absolute_offset_samples_size; |
| 389 | } |
| 390 | |
| 391 | // Now compute the offset between the pot and relative encoder. |
| 392 | if (offset_samples_.size() < constants_.average_filter_size) { |
| 393 | offset_samples_.push_back(buffered_samples_[middle_index].pot - |
| 394 | buffered_samples_[middle_index].encoder); |
| 395 | } else { |
| 396 | offset_samples_[samples_idx_] = buffered_samples_[middle_index].pot - |
| 397 | buffered_samples_[middle_index].encoder; |
| 398 | } |
| 399 | |
| 400 | // Drop the oldest sample when we run this function the next time around. |
| 401 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
| 402 | |
| 403 | double pot_relative_encoder_offset_sum = 0.0; |
| 404 | for (size_t i = 0; i < offset_samples_.size(); ++i) { |
| 405 | pot_relative_encoder_offset_sum += offset_samples_[i]; |
| 406 | } |
| 407 | pot_relative_encoder_offset_ = |
| 408 | pot_relative_encoder_offset_sum / offset_samples_.size(); |
| 409 | |
| 410 | offset_ = Wrap(buffered_samples_[middle_index].encoder + |
| 411 | pot_relative_encoder_offset_, |
| 412 | average_relative_to_absolute_offset + |
| 413 | buffered_samples_[middle_index].encoder, |
| 414 | constants_.one_revolution_distance) - |
| 415 | buffered_samples_[middle_index].encoder; |
| 416 | if (offset_ready()) { |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 417 | if (!zeroed_) { |
| 418 | first_offset_ = offset_; |
| 419 | } |
| 420 | |
| 421 | if (::std::abs(first_offset_ - offset_) > |
| 422 | constants_.allowable_encoder_error * |
| 423 | constants_.one_revolution_distance) { |
| 424 | LOG(ERROR, |
| 425 | "Offset moved too far. Initial: %f, current %f, allowable change: " |
| 426 | "%f\n", |
| 427 | first_offset_, offset_, constants_.allowable_encoder_error * |
| 428 | constants_.one_revolution_distance); |
| 429 | error_ = true; |
| 430 | } |
| 431 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 432 | zeroed_ = true; |
| 433 | } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 436 | // Update the position. |
| 437 | filtered_position_ = pot_relative_encoder_offset_ + info.encoder; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 438 | position_ = offset_ + info.encoder; |
| 439 | } |
| 440 | |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 441 | PotAndAbsEncoderZeroingEstimator::State |
| 442 | PotAndAbsEncoderZeroingEstimator::GetEstimatorState() const { |
| 443 | State r; |
| 444 | r.error = error_; |
| 445 | r.zeroed = zeroed_; |
| 446 | r.position = position_; |
| 447 | r.pot_position = filtered_position_; |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 448 | r.absolute_position = filtered_absolute_encoder_; |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 449 | return r; |
| 450 | } |
| 451 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 452 | void PulseIndexZeroingEstimator::Reset() { |
| 453 | max_index_position_ = ::std::numeric_limits<double>::lowest(); |
| 454 | min_index_position_ = ::std::numeric_limits<double>::max(); |
| 455 | offset_ = 0; |
| 456 | last_used_index_pulse_count_ = 0; |
| 457 | zeroed_ = false; |
| 458 | error_ = false; |
| 459 | } |
| 460 | |
| 461 | void PulseIndexZeroingEstimator::StoreIndexPulseMaxAndMin( |
| 462 | const IndexPosition &info) { |
| 463 | // If we have a new index pulse. |
| 464 | if (last_used_index_pulse_count_ != info.index_pulses) { |
| 465 | // If the latest pulses's position is outside the range we've currently |
| 466 | // seen, record it appropriately. |
| 467 | if (info.latched_encoder > max_index_position_) { |
| 468 | max_index_position_ = info.latched_encoder; |
| 469 | } |
| 470 | if (info.latched_encoder < min_index_position_) { |
| 471 | min_index_position_ = info.latched_encoder; |
| 472 | } |
| 473 | last_used_index_pulse_count_ = info.index_pulses; |
| 474 | } |
| 475 | } |
| 476 | |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 477 | int PulseIndexZeroingEstimator::IndexPulseCount() const { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 478 | if (min_index_position_ > max_index_position_) { |
| 479 | // This condition means we haven't seen a pulse yet. |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | // Calculate the number of pulses encountered so far. |
| 484 | return 1 + static_cast<int>( |
| 485 | ::std::round((max_index_position_ - min_index_position_) / |
| 486 | constants_.index_difference)); |
| 487 | } |
| 488 | |
| 489 | void PulseIndexZeroingEstimator::UpdateEstimate(const IndexPosition &info) { |
| 490 | StoreIndexPulseMaxAndMin(info); |
| 491 | const int index_pulse_count = IndexPulseCount(); |
| 492 | if (index_pulse_count > constants_.index_pulse_count) { |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame^] | 493 | if (!error_) { |
| 494 | LOG(ERROR, "Got more index pulses than expected. Got %d expected %d.\n", |
| 495 | index_pulse_count, constants_.index_pulse_count); |
| 496 | error_ = true; |
| 497 | } |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // TODO(austin): Detect if the encoder or index pulse is unplugged. |
| 501 | // TODO(austin): Detect missing counts. |
| 502 | |
| 503 | if (index_pulse_count == constants_.index_pulse_count && !zeroed_) { |
| 504 | offset_ = constants_.measured_index_position - |
| 505 | constants_.known_index_pulse * constants_.index_difference - |
| 506 | min_index_position_; |
| 507 | zeroed_ = true; |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame^] | 508 | } else if (zeroed_ && !error_) { |
| 509 | // Detect whether the index pulse is somewhere other than where we expect |
| 510 | // it to be. First we compute the position of the most recent index pulse. |
| 511 | double index_pulse_distance = |
| 512 | info.latched_encoder + offset_ - constants_.measured_index_position; |
| 513 | // Second we compute the position of the index pulse in terms of |
| 514 | // the index difference. I.e. if this index pulse is two pulses away from |
| 515 | // the index pulse that we know about then this number should be positive |
| 516 | // or negative two. |
| 517 | double relative_distance = |
| 518 | index_pulse_distance / constants_.index_difference; |
| 519 | // Now we compute how far away the measured index pulse is from the |
| 520 | // expected index pulse. |
| 521 | double error = relative_distance - ::std::round(relative_distance); |
| 522 | // This lets us check if the index pulse is within an acceptable error |
| 523 | // margin of where we expected it to be. |
| 524 | if (::std::abs(error) > constants_.allowable_encoder_error) { |
| 525 | LOG(ERROR, |
| 526 | "Encoder ticks out of range since last index pulse. known index " |
| 527 | "pulse: %f, expected index pulse: %f, actual index pulse: %f, " |
| 528 | "allowable error: %f\n", |
| 529 | constants_.measured_index_position, |
| 530 | round(relative_distance) * constants_.index_difference + |
| 531 | constants_.measured_index_position, |
| 532 | info.latched_encoder + offset_, |
| 533 | constants_.allowable_encoder_error * constants_.index_difference); |
| 534 | error_ = true; |
| 535 | } |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 536 | } |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 537 | |
| 538 | position_ = info.encoder + offset_; |
| 539 | } |
| 540 | |
| 541 | PulseIndexZeroingEstimator::State |
| 542 | PulseIndexZeroingEstimator::GetEstimatorState() const { |
| 543 | State r; |
| 544 | r.error = error_; |
| 545 | r.zeroed = zeroed_; |
| 546 | r.position = position_; |
| 547 | r.min_index_position = min_index_position_; |
| 548 | r.max_index_position = max_index_position_; |
| 549 | r.index_pulses_seen = IndexPulseCount(); |
| 550 | return r; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 553 | } // namespace zeroing |
| 554 | } // namespace frc971 |