blob: d897fb5c87e1987bd3a477e2f42dbc32ea420891 [file] [log] [blame]
Adam Snaiderc4b3c192015-02-01 01:30:39 +00001#include "frc971/zeroing/zeroing.h"
Adam Snaiderb4119252015-02-15 01:30:57 +00002
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -08003#include <algorithm>
Isaac Wilcove0851ffd2017-02-16 04:13:14 +00004#include <cmath>
5#include <limits>
6#include <vector>
Adam Snaiderc4b3c192015-02-01 01:30:39 +00007
Austin Schuh5f01f152017-02-11 21:34:08 -08008#include "frc971/zeroing/wrap.h"
9
Adam Snaiderc4b3c192015-02-01 01:30:39 +000010namespace frc971 {
11namespace zeroing {
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -080012namespace {
13
14bool compare_encoder(const PotAndAbsolutePosition &left,
15 const PotAndAbsolutePosition &right) {
16 return left.encoder < right.encoder;
17}
18
19} // namespace
Adam Snaiderc4b3c192015-02-01 01:30:39 +000020
Tyler Chatowf8f03112017-02-05 14:31:34 -080021PotAndIndexPulseZeroingEstimator::PotAndIndexPulseZeroingEstimator(
Austin Schuh5f01f152017-02-11 21:34:08 -080022 const constants::PotAndIndexPulseZeroingConstants &constants)
23 : constants_(constants) {
24 start_pos_samples_.reserve(constants_.average_filter_size);
Adam Snaiderb4119252015-02-15 01:30:57 +000025 Reset();
Austin Schuh703b8d42015-02-01 14:56:34 -080026}
27
Tyler Chatowf8f03112017-02-05 14:31:34 -080028void PotAndIndexPulseZeroingEstimator::Reset() {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000029 samples_idx_ = 0;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000030 offset_ = 0;
Adam Snaiderb4119252015-02-15 01:30:57 +000031 start_pos_samples_.clear();
32 zeroed_ = false;
Philipp Schrader41d82912015-02-15 03:44:23 +000033 wait_for_index_pulse_ = true;
Philipp Schradere828be72015-02-15 07:07:37 +000034 last_used_index_pulse_count_ = 0;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000035 error_ = false;
36}
37
Tyler Chatowf8f03112017-02-05 14:31:34 -080038void PotAndIndexPulseZeroingEstimator::TriggerError() {
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000039 if (!error_) {
40 LOG(ERROR, "Manually triggered zeroing error.\n");
41 error_ = true;
42 }
Philipp Schradere828be72015-02-15 07:07:37 +000043}
44
Tyler Chatowf8f03112017-02-05 14:31:34 -080045double PotAndIndexPulseZeroingEstimator::CalculateStartPosition(
46 double start_average, double latched_encoder) const {
Philipp Schradere828be72015-02-15 07:07:37 +000047 // 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 Schuh5f01f152017-02-11 21:34:08 -080050 double index_pos =
51 start_average + latched_encoder - constants_.measured_index_position;
Philipp Schradere828be72015-02-15 07:07:37 +000052 // We round index_pos to the closest valid value of the index.
Austin Schuh5f01f152017-02-11 21:34:08 -080053 double accurate_index_pos = (round(index_pos / constants_.index_difference)) *
54 constants_.index_difference;
Philipp Schradere828be72015-02-15 07:07:37 +000055 // Now we reverse the first calculation to get the accurate start position.
Austin Schuh5f01f152017-02-11 21:34:08 -080056 return accurate_index_pos - latched_encoder +
57 constants_.measured_index_position;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000058}
59
Tyler Chatowf8f03112017-02-05 14:31:34 -080060void PotAndIndexPulseZeroingEstimator::UpdateEstimate(
61 const PotAndIndexPosition &info) {
Philipp Schrader41d82912015-02-15 03:44:23 +000062 // 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 Schradere828be72015-02-15 07:07:37 +000067 last_used_index_pulse_count_ = info.index_pulses;
Philipp Schrader41d82912015-02-15 03:44:23 +000068 wait_for_index_pulse_ = false;
69 }
70
Austin Schuh5f01f152017-02-11 21:34:08 -080071 if (start_pos_samples_.size() < constants_.average_filter_size) {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000072 start_pos_samples_.push_back(info.pot - info.encoder);
73 } else {
74 start_pos_samples_[samples_idx_] = info.pot - info.encoder;
75 }
Adam Snaiderb4119252015-02-15 01:30:57 +000076
77 // Drop the oldest sample when we run this function the next time around.
Austin Schuh5f01f152017-02-11 21:34:08 -080078 samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000079
Adam Snaiderb4119252015-02-15 01:30:57 +000080 double sample_sum = 0.0;
81
Adam Snaiderc4b3c192015-02-01 01:30:39 +000082 for (size_t i = 0; i < start_pos_samples_.size(); ++i) {
Adam Snaiderb4119252015-02-15 01:30:57 +000083 sample_sum += start_pos_samples_[i];
Adam Snaiderc4b3c192015-02-01 01:30:39 +000084 }
85
86 // Calculates the average of the starting position.
Adam Snaiderb4119252015-02-15 01:30:57 +000087 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 Schuh7485dbb2016-02-08 00:21:58 -080092 if (!zeroed_ &&
93 (info.index_pulses == last_used_index_pulse_count_ || !offset_ready())) {
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000094 offset_ = start_average;
Philipp Schradere828be72015-02-15 07:07:37 +000095 } 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 Wilcove0851ffd2017-02-16 04:13:14 +000099 offset_ = CalculateStartPosition(start_average, info.latched_encoder);
Philipp Schradere828be72015-02-15 07:07:37 +0000100 last_used_index_pulse_count_ = info.index_pulses;
Austin Schuh7485dbb2016-02-08 00:21:58 -0800101
102 // TODO(austin): Reject encoder positions which have x% error rather than
103 // rounding to the closest index pulse.
104
Adam Snaider3cd11c52015-02-16 02:16:09 +0000105 // Save the first starting position.
106 if (!zeroed_) {
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000107 first_start_pos_ = offset_;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000108 LOG(INFO, "latching start position %f\n", first_start_pos_);
109 }
Adam Snaiderb4119252015-02-15 01:30:57 +0000110
111 // Now that we have an accurate starting position we can consider ourselves
112 // zeroed.
Austin Schuh703b8d42015-02-01 14:56:34 -0800113 zeroed_ = true;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000114 // Throw an error if first_start_pos is bigger/smaller than
Austin Schuh5f01f152017-02-11 21:34:08 -0800115 // constants_.allowable_encoder_error * index_diff + start_pos.
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000116 if (::std::abs(first_start_pos_ - offset_) >
Austin Schuh5f01f152017-02-11 21:34:08 -0800117 constants_.allowable_encoder_error * constants_.index_difference) {
Adam Snaider3cd11c52015-02-16 02:16:09 +0000118 if (!error_) {
119 LOG(ERROR,
120 "Encoder ticks out of range since last index pulse. first start "
Austin Schuh1c85bc82016-04-03 21:36:31 -0700121 "position: %f recent starting position: %f, allowable error: %f\n",
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000122 first_start_pos_, offset_,
Austin Schuh5f01f152017-02-11 21:34:08 -0800123 constants_.allowable_encoder_error * constants_.index_difference);
Adam Snaider3cd11c52015-02-16 02:16:09 +0000124 error_ = true;
125 }
126 }
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000127 }
Adam Snaiderb4119252015-02-15 01:30:57 +0000128
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000129 position_ = offset_ + info.encoder;
Austin Schuhbe133ed2016-03-11 21:23:34 -0800130 filtered_position_ = start_average + info.encoder;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000131}
132
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800133PotAndIndexPulseZeroingEstimator::State
134PotAndIndexPulseZeroingEstimator::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 Schuh55934032017-03-11 12:45:27 -0800143HallEffectAndPositionZeroingEstimator::HallEffectAndPositionZeroingEstimator(
144 const ZeroingConstants &constants)
145 : constants_(constants) {
146 Reset();
147}
148
149void 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
164void HallEffectAndPositionZeroingEstimator::TriggerError() {
165 if (!error_) {
166 LOG(ERROR, "Manually triggered zeroing error.\n");
167 error_ = true;
168 }
169}
170
171void 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
185void 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
245HallEffectAndPositionZeroingEstimator::State
246HallEffectAndPositionZeroingEstimator::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 Silverman4f2e2ce2017-02-19 17:49:47 -0800255
Austin Schuh5f01f152017-02-11 21:34:08 -0800256PotAndAbsEncoderZeroingEstimator::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
264void PotAndAbsEncoderZeroingEstimator::Reset() {
265 zeroed_ = false;
266 relative_to_absolute_offset_samples_.clear();
267 offset_samples_.clear();
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800268 buffered_samples_.clear();
Brian Silvermana10d20a2017-02-19 14:28:53 -0800269 error_ = false;
Austin Schuh5f01f152017-02-11 21:34:08 -0800270}
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 Vandenberg8fea6ea2017-02-18 17:24:45 -0800284//
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 Schuh5f01f152017-02-11 21:34:08 -0800289void PotAndAbsEncoderZeroingEstimator::UpdateEstimate(
290 const PotAndAbsolutePosition &info) {
Neil Balch16275e32017-02-18 16:38:45 -0800291 // 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 Vandenberg8fea6ea2017-02-18 17:24:45 -0800298 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 Schuh5f01f152017-02-11 21:34:08 -0800303 } else {
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800304 // 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 Schrader3f5b6182017-03-25 22:36:37 +0000308 compare_encoder)->encoder;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800309 auto min_value =
310 ::std::min_element(buffered_samples_.begin(), buffered_samples_.end(),
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000311 compare_encoder)->encoder;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800312 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 Schuh0e1c2c62017-02-21 02:03:25 -0800341 const double adjusted_incremental_encoder =
342 buffered_samples_[middle_index].encoder +
343 average_relative_to_absolute_offset;
344
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800345 // Now, compute the nearest absolute encoder value to the offset relative
346 // encoder position.
347 const double adjusted_absolute_encoder =
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800348 Wrap(adjusted_incremental_encoder,
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800349 buffered_samples_[middle_index].absolute_encoder -
350 constants_.measured_absolute_position,
351 constants_.one_revolution_distance);
352
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800353 // 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 Vandenberg8fea6ea2017-02-18 17:24:45 -0800364 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 Silvermana10d20a2017-02-19 14:28:53 -0800417 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 Vandenberg8fea6ea2017-02-18 17:24:45 -0800432 zeroed_ = true;
433 }
Austin Schuh5f01f152017-02-11 21:34:08 -0800434 }
435
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800436 // Update the position.
437 filtered_position_ = pot_relative_encoder_offset_ + info.encoder;
Austin Schuh5f01f152017-02-11 21:34:08 -0800438 position_ = offset_ + info.encoder;
439}
440
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800441PotAndAbsEncoderZeroingEstimator::State
442PotAndAbsEncoderZeroingEstimator::GetEstimatorState() const {
443 State r;
444 r.error = error_;
445 r.zeroed = zeroed_;
446 r.position = position_;
447 r.pot_position = filtered_position_;
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800448 r.absolute_position = filtered_absolute_encoder_;
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800449 return r;
450}
451
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000452void 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
461void 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 Silvermanf37839c2017-02-19 18:07:15 -0800477int PulseIndexZeroingEstimator::IndexPulseCount() const {
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000478 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
489void 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 Schrader3f5b6182017-03-25 22:36:37 +0000493 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 Wilcove0851ffd2017-02-16 04:13:14 +0000498 }
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 Schrader3f5b6182017-03-25 22:36:37 +0000508 } 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 Wilcove0851ffd2017-02-16 04:13:14 +0000536 }
Brian Silvermanf37839c2017-02-19 18:07:15 -0800537
538 position_ = info.encoder + offset_;
539}
540
541PulseIndexZeroingEstimator::State
542PulseIndexZeroingEstimator::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 Wilcove0851ffd2017-02-16 04:13:14 +0000551}
552
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000553} // namespace zeroing
554} // namespace frc971