blob: 7005725fb65b2e89fc80a9ba7809007b2d942267 [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_) {
Lee Mracek598a2452019-01-07 00:50:44 -0800176 min_low_position_ = max_low_position_ = info.encoder;
Austin Schuh55934032017-03-11 12:45:27 -0800177 } else {
Lee Mracek598a2452019-01-07 00:50:44 -0800178 min_low_position_ = ::std::min(min_low_position_, info.encoder);
179 max_low_position_ = ::std::max(max_low_position_, info.encoder);
Austin Schuh55934032017-03-11 12:45:27 -0800180 }
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) {
Lee Mracek598a2452019-01-07 00:50:44 -0800210 moving_backward = info.encoder > min_low_position_;
Austin Schuh55934032017-03-11 12:45:27 -0800211 } else {
Lee Mracek598a2452019-01-07 00:50:44 -0800212 moving_backward = info.encoder < max_low_position_;
Austin Schuh55934032017-03-11 12:45:27 -0800213 }
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
Lee Mracek598a2452019-01-07 00:50:44 -0800242 position_ = info.encoder - offset_;
Austin Schuh55934032017-03-11 12:45:27 -0800243}
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 Schuh72db9a12019-01-21 18:02:51 -0800256PotAndAbsoluteEncoderZeroingEstimator::PotAndAbsoluteEncoderZeroingEstimator(
Austin Schuh5f01f152017-02-11 21:34:08 -0800257 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
Austin Schuh72db9a12019-01-21 18:02:51 -0800264void PotAndAbsoluteEncoderZeroingEstimator::Reset() {
Austin Schuhddd08f82018-03-02 20:05:29 -0800265 first_offset_ = 0.0;
266 pot_relative_encoder_offset_ = 0.0;
267 offset_ = 0.0;
268 samples_idx_ = 0;
269 filtered_position_ = 0.0;
270 position_ = 0.0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800271 zeroed_ = false;
Austin Schuhddd08f82018-03-02 20:05:29 -0800272 nan_samples_ = 0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800273 relative_to_absolute_offset_samples_.clear();
274 offset_samples_.clear();
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800275 buffered_samples_.clear();
Brian Silvermana10d20a2017-02-19 14:28:53 -0800276 error_ = false;
Austin Schuh5f01f152017-02-11 21:34:08 -0800277}
278
279// So, this needs to be a multistep process. We need to first estimate the
280// offset between the absolute encoder and the relative encoder. That process
281// should get us an absolute number which is off by integer multiples of the
282// distance/rev. In parallel, we can estimate the offset between the pot and
283// encoder. When both estimates have converged, we can then compute the offset
284// in a cycle, and which cycle, which gives us the accurate global offset.
285//
286// It's tricky to compute the offset between the absolute and relative encoder.
287// We need to compute this inside 1 revolution. The easiest way to do this
288// would be to wrap the encoder, subtract the two of them, and then average the
289// result. That will struggle when they are off by PI. Instead, we need to
290// wrap the number to +- PI from the current averaged offset.
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800291//
292// To guard against the robot moving while updating estimates, buffer a number
293// of samples and check that the buffered samples are not different than the
294// zeroing threshold. At any point that the samples differ too much, do not
295// update estimates based on those samples.
Austin Schuh72db9a12019-01-21 18:02:51 -0800296void PotAndAbsoluteEncoderZeroingEstimator::UpdateEstimate(
Austin Schuh5f01f152017-02-11 21:34:08 -0800297 const PotAndAbsolutePosition &info) {
Neil Balch16275e32017-02-18 16:38:45 -0800298 // Check for Abs Encoder NaN value that would mess up the rest of the zeroing
299 // code below. NaN values are given when the Absolute Encoder is disconnected.
300 if (::std::isnan(info.absolute_encoder)) {
Austin Schuhddd08f82018-03-02 20:05:29 -0800301 if (zeroed_) {
302 LOG(ERROR, "NAN on absolute encoder\n");
303 error_ = true;
304 } else {
305 ++nan_samples_;
306 LOG(ERROR, "NAN on absolute encoder while zeroing %d\n",
307 static_cast<int>(nan_samples_));
308 if (nan_samples_ >= constants_.average_filter_size) {
309 error_ = true;
310 zeroed_ = true;
311 }
312 }
313 // Throw some dummy values in for now.
314 filtered_absolute_encoder_ = info.absolute_encoder;
315 filtered_position_ = pot_relative_encoder_offset_ + info.encoder;
316 position_ = offset_ + info.encoder;
Neil Balch16275e32017-02-18 16:38:45 -0800317 return;
318 }
319
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800320 bool moving = true;
321 if (buffered_samples_.size() < constants_.moving_buffer_size) {
322 // Not enough samples to start determining if the robot is moving or not,
323 // don't use the samples yet.
324 buffered_samples_.push_back(info);
Austin Schuh5f01f152017-02-11 21:34:08 -0800325 } else {
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800326 // Have enough samples to start determining if the robot is moving or not.
327 buffered_samples_[buffered_samples_idx_] = info;
328 auto max_value =
329 ::std::max_element(buffered_samples_.begin(), buffered_samples_.end(),
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000330 compare_encoder)->encoder;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800331 auto min_value =
332 ::std::min_element(buffered_samples_.begin(), buffered_samples_.end(),
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000333 compare_encoder)->encoder;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800334 if (::std::abs(max_value - min_value) < constants_.zeroing_threshold) {
335 // Robot isn't moving, use middle sample to determine offsets.
336 moving = false;
337 }
338 }
339 buffered_samples_idx_ =
340 (buffered_samples_idx_ + 1) % constants_.moving_buffer_size;
341
342 if (!moving) {
343 // The robot is not moving, use the middle sample to determine offsets.
344 const int middle_index =
345 (buffered_samples_idx_ + (constants_.moving_buffer_size - 1) / 2) %
346 constants_.moving_buffer_size;
347
348 // Compute the sum of all the offset samples.
349 double relative_to_absolute_offset_sum = 0.0;
350 for (size_t i = 0; i < relative_to_absolute_offset_samples_.size(); ++i) {
351 relative_to_absolute_offset_sum +=
352 relative_to_absolute_offset_samples_[i];
353 }
354
355 // Compute the average offset between the absolute encoder and relative
356 // encoder. If we have 0 samples, assume it is 0.
357 double average_relative_to_absolute_offset =
358 relative_to_absolute_offset_samples_.size() == 0
359 ? 0.0
360 : relative_to_absolute_offset_sum /
361 relative_to_absolute_offset_samples_.size();
362
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800363 const double adjusted_incremental_encoder =
364 buffered_samples_[middle_index].encoder +
365 average_relative_to_absolute_offset;
366
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800367 // Now, compute the nearest absolute encoder value to the offset relative
368 // encoder position.
369 const double adjusted_absolute_encoder =
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800370 Wrap(adjusted_incremental_encoder,
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800371 buffered_samples_[middle_index].absolute_encoder -
372 constants_.measured_absolute_position,
373 constants_.one_revolution_distance);
374
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800375 // Reverse the math on the previous line to compute the absolute encoder.
376 // Do this by taking the adjusted encoder, and then subtracting off the
377 // second argument above, and the value that was added by Wrap.
378 filtered_absolute_encoder_ =
379 ((buffered_samples_[middle_index].encoder +
380 average_relative_to_absolute_offset) -
381 (-constants_.measured_absolute_position +
382 (adjusted_absolute_encoder -
383 (buffered_samples_[middle_index].absolute_encoder -
384 constants_.measured_absolute_position))));
385
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800386 const double relative_to_absolute_offset =
387 adjusted_absolute_encoder - buffered_samples_[middle_index].encoder;
388
389 // Add the sample and update the average with the new reading.
390 const size_t relative_to_absolute_offset_samples_size =
391 relative_to_absolute_offset_samples_.size();
392 if (relative_to_absolute_offset_samples_size <
393 constants_.average_filter_size) {
394 average_relative_to_absolute_offset =
395 (average_relative_to_absolute_offset *
396 relative_to_absolute_offset_samples_size +
397 relative_to_absolute_offset) /
398 (relative_to_absolute_offset_samples_size + 1);
399
400 relative_to_absolute_offset_samples_.push_back(
401 relative_to_absolute_offset);
402 } else {
403 average_relative_to_absolute_offset -=
404 relative_to_absolute_offset_samples_[samples_idx_] /
405 relative_to_absolute_offset_samples_size;
406 relative_to_absolute_offset_samples_[samples_idx_] =
407 relative_to_absolute_offset;
408 average_relative_to_absolute_offset +=
409 relative_to_absolute_offset /
410 relative_to_absolute_offset_samples_size;
411 }
412
413 // Now compute the offset between the pot and relative encoder.
414 if (offset_samples_.size() < constants_.average_filter_size) {
415 offset_samples_.push_back(buffered_samples_[middle_index].pot -
416 buffered_samples_[middle_index].encoder);
417 } else {
418 offset_samples_[samples_idx_] = buffered_samples_[middle_index].pot -
419 buffered_samples_[middle_index].encoder;
420 }
421
422 // Drop the oldest sample when we run this function the next time around.
423 samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size;
424
425 double pot_relative_encoder_offset_sum = 0.0;
426 for (size_t i = 0; i < offset_samples_.size(); ++i) {
427 pot_relative_encoder_offset_sum += offset_samples_[i];
428 }
429 pot_relative_encoder_offset_ =
430 pot_relative_encoder_offset_sum / offset_samples_.size();
431
432 offset_ = Wrap(buffered_samples_[middle_index].encoder +
433 pot_relative_encoder_offset_,
434 average_relative_to_absolute_offset +
435 buffered_samples_[middle_index].encoder,
436 constants_.one_revolution_distance) -
437 buffered_samples_[middle_index].encoder;
438 if (offset_ready()) {
Brian Silvermana10d20a2017-02-19 14:28:53 -0800439 if (!zeroed_) {
440 first_offset_ = offset_;
441 }
442
443 if (::std::abs(first_offset_ - offset_) >
444 constants_.allowable_encoder_error *
445 constants_.one_revolution_distance) {
446 LOG(ERROR,
447 "Offset moved too far. Initial: %f, current %f, allowable change: "
448 "%f\n",
449 first_offset_, offset_, constants_.allowable_encoder_error *
450 constants_.one_revolution_distance);
451 error_ = true;
452 }
453
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800454 zeroed_ = true;
455 }
Austin Schuh5f01f152017-02-11 21:34:08 -0800456 }
457
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800458 // Update the position.
459 filtered_position_ = pot_relative_encoder_offset_ + info.encoder;
Austin Schuh5f01f152017-02-11 21:34:08 -0800460 position_ = offset_ + info.encoder;
461}
462
Austin Schuh72db9a12019-01-21 18:02:51 -0800463PotAndAbsoluteEncoderZeroingEstimator::State
464PotAndAbsoluteEncoderZeroingEstimator::GetEstimatorState() const {
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800465 State r;
466 r.error = error_;
467 r.zeroed = zeroed_;
468 r.position = position_;
469 r.pot_position = filtered_position_;
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800470 r.absolute_position = filtered_absolute_encoder_;
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800471 return r;
472}
473
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000474void PulseIndexZeroingEstimator::Reset() {
475 max_index_position_ = ::std::numeric_limits<double>::lowest();
476 min_index_position_ = ::std::numeric_limits<double>::max();
477 offset_ = 0;
478 last_used_index_pulse_count_ = 0;
479 zeroed_ = false;
480 error_ = false;
481}
482
483void PulseIndexZeroingEstimator::StoreIndexPulseMaxAndMin(
484 const IndexPosition &info) {
485 // If we have a new index pulse.
486 if (last_used_index_pulse_count_ != info.index_pulses) {
487 // If the latest pulses's position is outside the range we've currently
488 // seen, record it appropriately.
489 if (info.latched_encoder > max_index_position_) {
490 max_index_position_ = info.latched_encoder;
491 }
492 if (info.latched_encoder < min_index_position_) {
493 min_index_position_ = info.latched_encoder;
494 }
495 last_used_index_pulse_count_ = info.index_pulses;
496 }
497}
498
Brian Silvermanf37839c2017-02-19 18:07:15 -0800499int PulseIndexZeroingEstimator::IndexPulseCount() const {
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000500 if (min_index_position_ > max_index_position_) {
501 // This condition means we haven't seen a pulse yet.
502 return 0;
503 }
504
505 // Calculate the number of pulses encountered so far.
506 return 1 + static_cast<int>(
507 ::std::round((max_index_position_ - min_index_position_) /
508 constants_.index_difference));
509}
510
511void PulseIndexZeroingEstimator::UpdateEstimate(const IndexPosition &info) {
512 StoreIndexPulseMaxAndMin(info);
513 const int index_pulse_count = IndexPulseCount();
514 if (index_pulse_count > constants_.index_pulse_count) {
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000515 if (!error_) {
516 LOG(ERROR, "Got more index pulses than expected. Got %d expected %d.\n",
517 index_pulse_count, constants_.index_pulse_count);
518 error_ = true;
519 }
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000520 }
521
522 // TODO(austin): Detect if the encoder or index pulse is unplugged.
523 // TODO(austin): Detect missing counts.
524
525 if (index_pulse_count == constants_.index_pulse_count && !zeroed_) {
526 offset_ = constants_.measured_index_position -
527 constants_.known_index_pulse * constants_.index_difference -
528 min_index_position_;
529 zeroed_ = true;
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000530 } else if (zeroed_ && !error_) {
531 // Detect whether the index pulse is somewhere other than where we expect
532 // it to be. First we compute the position of the most recent index pulse.
533 double index_pulse_distance =
534 info.latched_encoder + offset_ - constants_.measured_index_position;
535 // Second we compute the position of the index pulse in terms of
536 // the index difference. I.e. if this index pulse is two pulses away from
537 // the index pulse that we know about then this number should be positive
538 // or negative two.
539 double relative_distance =
540 index_pulse_distance / constants_.index_difference;
541 // Now we compute how far away the measured index pulse is from the
542 // expected index pulse.
543 double error = relative_distance - ::std::round(relative_distance);
544 // This lets us check if the index pulse is within an acceptable error
545 // margin of where we expected it to be.
546 if (::std::abs(error) > constants_.allowable_encoder_error) {
547 LOG(ERROR,
548 "Encoder ticks out of range since last index pulse. known index "
549 "pulse: %f, expected index pulse: %f, actual index pulse: %f, "
550 "allowable error: %f\n",
551 constants_.measured_index_position,
552 round(relative_distance) * constants_.index_difference +
553 constants_.measured_index_position,
554 info.latched_encoder + offset_,
555 constants_.allowable_encoder_error * constants_.index_difference);
556 error_ = true;
557 }
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000558 }
Brian Silvermanf37839c2017-02-19 18:07:15 -0800559
560 position_ = info.encoder + offset_;
561}
562
563PulseIndexZeroingEstimator::State
564PulseIndexZeroingEstimator::GetEstimatorState() const {
565 State r;
566 r.error = error_;
567 r.zeroed = zeroed_;
568 r.position = position_;
569 r.min_index_position = min_index_position_;
570 r.max_index_position = max_index_position_;
571 r.index_pulses_seen = IndexPulseCount();
572 return r;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000573}
574
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000575} // namespace zeroing
576} // namespace frc971