blob: 7bf2b273d0fa936f4b0318f0a259476f52fa7e4a [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>
Austin Schuha8f88d42019-01-26 12:33:54 -08006#include <numeric>
Isaac Wilcove0851ffd2017-02-16 04:13:14 +00007#include <vector>
Adam Snaiderc4b3c192015-02-01 01:30:39 +00008
Austin Schuh5f01f152017-02-11 21:34:08 -08009#include "frc971/zeroing/wrap.h"
10
Adam Snaiderc4b3c192015-02-01 01:30:39 +000011namespace frc971 {
12namespace zeroing {
13
Tyler Chatowf8f03112017-02-05 14:31:34 -080014PotAndIndexPulseZeroingEstimator::PotAndIndexPulseZeroingEstimator(
Austin Schuh5f01f152017-02-11 21:34:08 -080015 const constants::PotAndIndexPulseZeroingConstants &constants)
16 : constants_(constants) {
17 start_pos_samples_.reserve(constants_.average_filter_size);
Adam Snaiderb4119252015-02-15 01:30:57 +000018 Reset();
Austin Schuh703b8d42015-02-01 14:56:34 -080019}
20
Tyler Chatowf8f03112017-02-05 14:31:34 -080021void PotAndIndexPulseZeroingEstimator::Reset() {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000022 samples_idx_ = 0;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000023 offset_ = 0;
Adam Snaiderb4119252015-02-15 01:30:57 +000024 start_pos_samples_.clear();
25 zeroed_ = false;
Philipp Schrader41d82912015-02-15 03:44:23 +000026 wait_for_index_pulse_ = true;
Philipp Schradere828be72015-02-15 07:07:37 +000027 last_used_index_pulse_count_ = 0;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000028 error_ = false;
29}
30
Tyler Chatowf8f03112017-02-05 14:31:34 -080031void PotAndIndexPulseZeroingEstimator::TriggerError() {
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000032 if (!error_) {
33 LOG(ERROR, "Manually triggered zeroing error.\n");
34 error_ = true;
35 }
Philipp Schradere828be72015-02-15 07:07:37 +000036}
37
Tyler Chatowf8f03112017-02-05 14:31:34 -080038double PotAndIndexPulseZeroingEstimator::CalculateStartPosition(
39 double start_average, double latched_encoder) const {
Philipp Schradere828be72015-02-15 07:07:37 +000040 // We calculate an aproximation of the value of the last index position.
41 // Also account for index pulses not lining up with integer multiples of the
42 // index_diff.
Austin Schuh5f01f152017-02-11 21:34:08 -080043 double index_pos =
44 start_average + latched_encoder - constants_.measured_index_position;
Philipp Schradere828be72015-02-15 07:07:37 +000045 // We round index_pos to the closest valid value of the index.
Austin Schuh5f01f152017-02-11 21:34:08 -080046 double accurate_index_pos = (round(index_pos / constants_.index_difference)) *
47 constants_.index_difference;
Philipp Schradere828be72015-02-15 07:07:37 +000048 // Now we reverse the first calculation to get the accurate start position.
Austin Schuh5f01f152017-02-11 21:34:08 -080049 return accurate_index_pos - latched_encoder +
50 constants_.measured_index_position;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000051}
52
Tyler Chatowf8f03112017-02-05 14:31:34 -080053void PotAndIndexPulseZeroingEstimator::UpdateEstimate(
54 const PotAndIndexPosition &info) {
Philipp Schrader41d82912015-02-15 03:44:23 +000055 // We want to make sure that we encounter at least one index pulse while
56 // zeroing. So we take the index pulse count from the first sample after
57 // reset and wait for that count to change before we consider ourselves
58 // zeroed.
59 if (wait_for_index_pulse_) {
Philipp Schradere828be72015-02-15 07:07:37 +000060 last_used_index_pulse_count_ = info.index_pulses;
Philipp Schrader41d82912015-02-15 03:44:23 +000061 wait_for_index_pulse_ = false;
62 }
63
Austin Schuh5f01f152017-02-11 21:34:08 -080064 if (start_pos_samples_.size() < constants_.average_filter_size) {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000065 start_pos_samples_.push_back(info.pot - info.encoder);
66 } else {
67 start_pos_samples_[samples_idx_] = info.pot - info.encoder;
68 }
Adam Snaiderb4119252015-02-15 01:30:57 +000069
70 // Drop the oldest sample when we run this function the next time around.
Austin Schuh5f01f152017-02-11 21:34:08 -080071 samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000072
Adam Snaiderb4119252015-02-15 01:30:57 +000073 double sample_sum = 0.0;
74
Adam Snaiderc4b3c192015-02-01 01:30:39 +000075 for (size_t i = 0; i < start_pos_samples_.size(); ++i) {
Adam Snaiderb4119252015-02-15 01:30:57 +000076 sample_sum += start_pos_samples_[i];
Adam Snaiderc4b3c192015-02-01 01:30:39 +000077 }
78
79 // Calculates the average of the starting position.
Adam Snaiderb4119252015-02-15 01:30:57 +000080 double start_average = sample_sum / start_pos_samples_.size();
81
82 // If there are no index pulses to use or we don't have enough samples yet to
83 // have a well-filtered starting position then we use the filtered value as
84 // our best guess.
Austin Schuh7485dbb2016-02-08 00:21:58 -080085 if (!zeroed_ &&
86 (info.index_pulses == last_used_index_pulse_count_ || !offset_ready())) {
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000087 offset_ = start_average;
Philipp Schradere828be72015-02-15 07:07:37 +000088 } else if (!zeroed_ || last_used_index_pulse_count_ != info.index_pulses) {
89 // Note the accurate start position and the current index pulse count so
90 // that we only run this logic once per index pulse. That should be more
91 // resilient to corrupted intermediate data.
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000092 offset_ = CalculateStartPosition(start_average, info.latched_encoder);
Philipp Schradere828be72015-02-15 07:07:37 +000093 last_used_index_pulse_count_ = info.index_pulses;
Austin Schuh7485dbb2016-02-08 00:21:58 -080094
95 // TODO(austin): Reject encoder positions which have x% error rather than
96 // rounding to the closest index pulse.
97
Adam Snaider3cd11c52015-02-16 02:16:09 +000098 // Save the first starting position.
99 if (!zeroed_) {
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000100 first_start_pos_ = offset_;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000101 LOG(INFO, "latching start position %f\n", first_start_pos_);
102 }
Adam Snaiderb4119252015-02-15 01:30:57 +0000103
104 // Now that we have an accurate starting position we can consider ourselves
105 // zeroed.
Austin Schuh703b8d42015-02-01 14:56:34 -0800106 zeroed_ = true;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000107 // Throw an error if first_start_pos is bigger/smaller than
Austin Schuh5f01f152017-02-11 21:34:08 -0800108 // constants_.allowable_encoder_error * index_diff + start_pos.
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000109 if (::std::abs(first_start_pos_ - offset_) >
Austin Schuh5f01f152017-02-11 21:34:08 -0800110 constants_.allowable_encoder_error * constants_.index_difference) {
Adam Snaider3cd11c52015-02-16 02:16:09 +0000111 if (!error_) {
112 LOG(ERROR,
113 "Encoder ticks out of range since last index pulse. first start "
Austin Schuh1c85bc82016-04-03 21:36:31 -0700114 "position: %f recent starting position: %f, allowable error: %f\n",
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000115 first_start_pos_, offset_,
Austin Schuh5f01f152017-02-11 21:34:08 -0800116 constants_.allowable_encoder_error * constants_.index_difference);
Adam Snaider3cd11c52015-02-16 02:16:09 +0000117 error_ = true;
118 }
119 }
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000120 }
Adam Snaiderb4119252015-02-15 01:30:57 +0000121
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000122 position_ = offset_ + info.encoder;
Austin Schuhbe133ed2016-03-11 21:23:34 -0800123 filtered_position_ = start_average + info.encoder;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000124}
125
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800126PotAndIndexPulseZeroingEstimator::State
127PotAndIndexPulseZeroingEstimator::GetEstimatorState() const {
128 State r;
129 r.error = error_;
130 r.zeroed = zeroed_;
131 r.position = position_;
132 r.pot_position = filtered_position_;
133 return r;
134}
135
Austin Schuh55934032017-03-11 12:45:27 -0800136HallEffectAndPositionZeroingEstimator::HallEffectAndPositionZeroingEstimator(
137 const ZeroingConstants &constants)
138 : constants_(constants) {
139 Reset();
140}
141
142void HallEffectAndPositionZeroingEstimator::Reset() {
143 offset_ = 0.0;
144 min_low_position_ = ::std::numeric_limits<double>::max();
145 max_low_position_ = ::std::numeric_limits<double>::lowest();
146 zeroed_ = false;
147 initialized_ = false;
148 last_used_posedge_count_ = 0;
149 cycles_high_ = 0;
150 high_long_enough_ = false;
151 first_start_pos_ = 0.0;
152 error_ = false;
153 current_ = 0.0;
154 first_start_pos_ = 0.0;
155}
156
157void HallEffectAndPositionZeroingEstimator::TriggerError() {
158 if (!error_) {
159 LOG(ERROR, "Manually triggered zeroing error.\n");
160 error_ = true;
161 }
162}
163
164void HallEffectAndPositionZeroingEstimator::StoreEncoderMaxAndMin(
165 const HallEffectAndPosition &info) {
166 // If we have a new posedge.
167 if (!info.current) {
168 if (last_hall_) {
Lee Mracek598a2452019-01-07 00:50:44 -0800169 min_low_position_ = max_low_position_ = info.encoder;
Austin Schuh55934032017-03-11 12:45:27 -0800170 } else {
Lee Mracek598a2452019-01-07 00:50:44 -0800171 min_low_position_ = ::std::min(min_low_position_, info.encoder);
172 max_low_position_ = ::std::max(max_low_position_, info.encoder);
Austin Schuh55934032017-03-11 12:45:27 -0800173 }
174 }
175 last_hall_ = info.current;
176}
177
178void HallEffectAndPositionZeroingEstimator::UpdateEstimate(
179 const HallEffectAndPosition &info) {
180 // We want to make sure that we encounter at least one posedge while zeroing.
181 // So we take the posedge count from the first sample after reset and wait for
182 // that count to change and for the hall effect to stay high before we
183 // consider ourselves zeroed.
184 if (!initialized_) {
185 last_used_posedge_count_ = info.posedge_count;
186 initialized_ = true;
187 last_hall_ = info.current;
188 }
189
190 StoreEncoderMaxAndMin(info);
191
192 if (info.current) {
193 cycles_high_++;
194 } else {
195 cycles_high_ = 0;
196 last_used_posedge_count_ = info.posedge_count;
197 }
198
199 high_long_enough_ = cycles_high_ >= constants_.hall_trigger_zeroing_length;
200
201 bool moving_backward = false;
202 if (constants_.zeroing_move_direction) {
Lee Mracek598a2452019-01-07 00:50:44 -0800203 moving_backward = info.encoder > min_low_position_;
Austin Schuh55934032017-03-11 12:45:27 -0800204 } else {
Lee Mracek598a2452019-01-07 00:50:44 -0800205 moving_backward = info.encoder < max_low_position_;
Austin Schuh55934032017-03-11 12:45:27 -0800206 }
207
208 // If there are no posedges to use or we don't have enough samples yet to
209 // have a well-filtered starting position then we use the filtered value as
210 // our best guess.
211 if (last_used_posedge_count_ != info.posedge_count && high_long_enough_ &&
212 moving_backward) {
213 // Note the offset and the current posedge count so that we only run this
214 // logic once per posedge. That should be more resilient to corrupted
215 // intermediate data.
216 offset_ = -info.posedge_value;
217 if (constants_.zeroing_move_direction) {
218 offset_ += constants_.lower_hall_position;
219 } else {
220 offset_ += constants_.upper_hall_position;
221 }
222 last_used_posedge_count_ = info.posedge_count;
223
224 // Save the first starting position.
225 if (!zeroed_) {
226 first_start_pos_ = offset_;
227 LOG(INFO, "latching start position %f\n", first_start_pos_);
228 }
229
230 // Now that we have an accurate starting position we can consider ourselves
231 // zeroed.
232 zeroed_ = true;
233 }
234
Lee Mracek598a2452019-01-07 00:50:44 -0800235 position_ = info.encoder - offset_;
Austin Schuh55934032017-03-11 12:45:27 -0800236}
237
238HallEffectAndPositionZeroingEstimator::State
239HallEffectAndPositionZeroingEstimator::GetEstimatorState() const {
240 State r;
241 r.error = error_;
242 r.zeroed = zeroed_;
243 r.encoder = position_;
244 r.high_long_enough = high_long_enough_;
245 r.offset = offset_;
246 return r;
247}
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800248
Austin Schuh72db9a12019-01-21 18:02:51 -0800249PotAndAbsoluteEncoderZeroingEstimator::PotAndAbsoluteEncoderZeroingEstimator(
Austin Schuh5f01f152017-02-11 21:34:08 -0800250 const constants::PotAndAbsoluteEncoderZeroingConstants &constants)
251 : constants_(constants) {
252 relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size);
253 offset_samples_.reserve(constants_.average_filter_size);
Austin Schuh409ffe02019-01-21 18:46:41 -0800254 buffered_samples_.reserve(constants_.moving_buffer_size);
Austin Schuh5f01f152017-02-11 21:34:08 -0800255 Reset();
256}
257
Austin Schuh72db9a12019-01-21 18:02:51 -0800258void PotAndAbsoluteEncoderZeroingEstimator::Reset() {
Austin Schuhddd08f82018-03-02 20:05:29 -0800259 first_offset_ = 0.0;
260 pot_relative_encoder_offset_ = 0.0;
261 offset_ = 0.0;
262 samples_idx_ = 0;
263 filtered_position_ = 0.0;
264 position_ = 0.0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800265 zeroed_ = false;
Austin Schuhddd08f82018-03-02 20:05:29 -0800266 nan_samples_ = 0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800267 relative_to_absolute_offset_samples_.clear();
268 offset_samples_.clear();
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800269 buffered_samples_.clear();
Brian Silvermana10d20a2017-02-19 14:28:53 -0800270 error_ = false;
Austin Schuh5f01f152017-02-11 21:34:08 -0800271}
272
273// So, this needs to be a multistep process. We need to first estimate the
274// offset between the absolute encoder and the relative encoder. That process
275// should get us an absolute number which is off by integer multiples of the
276// distance/rev. In parallel, we can estimate the offset between the pot and
277// encoder. When both estimates have converged, we can then compute the offset
278// in a cycle, and which cycle, which gives us the accurate global offset.
279//
280// It's tricky to compute the offset between the absolute and relative encoder.
281// We need to compute this inside 1 revolution. The easiest way to do this
282// would be to wrap the encoder, subtract the two of them, and then average the
283// result. That will struggle when they are off by PI. Instead, we need to
284// wrap the number to +- PI from the current averaged offset.
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800285//
286// To guard against the robot moving while updating estimates, buffer a number
287// of samples and check that the buffered samples are not different than the
288// zeroing threshold. At any point that the samples differ too much, do not
289// update estimates based on those samples.
Austin Schuh72db9a12019-01-21 18:02:51 -0800290void PotAndAbsoluteEncoderZeroingEstimator::UpdateEstimate(
Austin Schuh5f01f152017-02-11 21:34:08 -0800291 const PotAndAbsolutePosition &info) {
Neil Balch16275e32017-02-18 16:38:45 -0800292 // Check for Abs Encoder NaN value that would mess up the rest of the zeroing
293 // code below. NaN values are given when the Absolute Encoder is disconnected.
294 if (::std::isnan(info.absolute_encoder)) {
Austin Schuhddd08f82018-03-02 20:05:29 -0800295 if (zeroed_) {
296 LOG(ERROR, "NAN on absolute encoder\n");
297 error_ = true;
298 } else {
299 ++nan_samples_;
300 LOG(ERROR, "NAN on absolute encoder while zeroing %d\n",
301 static_cast<int>(nan_samples_));
302 if (nan_samples_ >= constants_.average_filter_size) {
303 error_ = true;
304 zeroed_ = true;
305 }
306 }
307 // Throw some dummy values in for now.
308 filtered_absolute_encoder_ = info.absolute_encoder;
309 filtered_position_ = pot_relative_encoder_offset_ + info.encoder;
310 position_ = offset_ + info.encoder;
Neil Balch16275e32017-02-18 16:38:45 -0800311 return;
312 }
313
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800314 bool moving = true;
315 if (buffered_samples_.size() < constants_.moving_buffer_size) {
316 // Not enough samples to start determining if the robot is moving or not,
317 // don't use the samples yet.
318 buffered_samples_.push_back(info);
Austin Schuh5f01f152017-02-11 21:34:08 -0800319 } else {
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800320 // Have enough samples to start determining if the robot is moving or not.
321 buffered_samples_[buffered_samples_idx_] = info;
Austin Schuhc8da03f2019-01-26 13:10:16 -0800322 const auto minmax_value = ::std::minmax_element(
323 buffered_samples_.begin(), buffered_samples_.end(),
324 [](const PotAndAbsolutePosition &left,
325 const PotAndAbsolutePosition &right) {
326 return left.encoder < right.encoder;
327 });
328 const double min_value = minmax_value.first->encoder;
329 const double max_value = minmax_value.second->encoder;
330
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800331 if (::std::abs(max_value - min_value) < constants_.zeroing_threshold) {
332 // Robot isn't moving, use middle sample to determine offsets.
333 moving = false;
334 }
335 }
336 buffered_samples_idx_ =
337 (buffered_samples_idx_ + 1) % constants_.moving_buffer_size;
338
339 if (!moving) {
340 // The robot is not moving, use the middle sample to determine offsets.
341 const int middle_index =
342 (buffered_samples_idx_ + (constants_.moving_buffer_size - 1) / 2) %
343 constants_.moving_buffer_size;
Austin Schuh409ffe02019-01-21 18:46:41 -0800344 const PotAndAbsolutePosition &sample = buffered_samples_[middle_index];
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800345
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800346 // Compute the average offset between the absolute encoder and relative
347 // encoder. If we have 0 samples, assume it is 0.
348 double average_relative_to_absolute_offset =
349 relative_to_absolute_offset_samples_.size() == 0
350 ? 0.0
Austin Schuha8f88d42019-01-26 12:33:54 -0800351 : ::std::accumulate(relative_to_absolute_offset_samples_.begin(),
352 relative_to_absolute_offset_samples_.end(),
353 0.0) /
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800354 relative_to_absolute_offset_samples_.size();
355
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800356 const double adjusted_incremental_encoder =
Austin Schuh409ffe02019-01-21 18:46:41 -0800357 sample.encoder + average_relative_to_absolute_offset;
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800358
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800359 // Now, compute the nearest absolute encoder value to the offset relative
360 // encoder position.
361 const double adjusted_absolute_encoder =
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800362 Wrap(adjusted_incremental_encoder,
Austin Schuh409ffe02019-01-21 18:46:41 -0800363 sample.absolute_encoder - constants_.measured_absolute_position,
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800364 constants_.one_revolution_distance);
365
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800366 // Reverse the math on the previous line to compute the absolute encoder.
367 // Do this by taking the adjusted encoder, and then subtracting off the
368 // second argument above, and the value that was added by Wrap.
369 filtered_absolute_encoder_ =
Austin Schuh409ffe02019-01-21 18:46:41 -0800370 ((sample.encoder + average_relative_to_absolute_offset) -
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800371 (-constants_.measured_absolute_position +
372 (adjusted_absolute_encoder -
Austin Schuh409ffe02019-01-21 18:46:41 -0800373 (sample.absolute_encoder - constants_.measured_absolute_position))));
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800374
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800375 const double relative_to_absolute_offset =
Austin Schuh409ffe02019-01-21 18:46:41 -0800376 adjusted_absolute_encoder - sample.encoder;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800377
378 // Add the sample and update the average with the new reading.
379 const size_t relative_to_absolute_offset_samples_size =
380 relative_to_absolute_offset_samples_.size();
381 if (relative_to_absolute_offset_samples_size <
382 constants_.average_filter_size) {
383 average_relative_to_absolute_offset =
384 (average_relative_to_absolute_offset *
385 relative_to_absolute_offset_samples_size +
386 relative_to_absolute_offset) /
387 (relative_to_absolute_offset_samples_size + 1);
388
389 relative_to_absolute_offset_samples_.push_back(
390 relative_to_absolute_offset);
391 } else {
392 average_relative_to_absolute_offset -=
393 relative_to_absolute_offset_samples_[samples_idx_] /
394 relative_to_absolute_offset_samples_size;
395 relative_to_absolute_offset_samples_[samples_idx_] =
396 relative_to_absolute_offset;
397 average_relative_to_absolute_offset +=
398 relative_to_absolute_offset /
399 relative_to_absolute_offset_samples_size;
400 }
401
402 // Now compute the offset between the pot and relative encoder.
403 if (offset_samples_.size() < constants_.average_filter_size) {
Austin Schuh409ffe02019-01-21 18:46:41 -0800404 offset_samples_.push_back(sample.pot - sample.encoder);
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800405 } else {
Austin Schuh409ffe02019-01-21 18:46:41 -0800406 offset_samples_[samples_idx_] = sample.pot - sample.encoder;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800407 }
408
409 // Drop the oldest sample when we run this function the next time around.
410 samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size;
411
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800412 pot_relative_encoder_offset_ =
Austin Schuha8f88d42019-01-26 12:33:54 -0800413 ::std::accumulate(offset_samples_.begin(), offset_samples_.end(), 0.0) /
414 offset_samples_.size();
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800415
Austin Schuh409ffe02019-01-21 18:46:41 -0800416 offset_ = Wrap(sample.encoder + pot_relative_encoder_offset_,
417 average_relative_to_absolute_offset + sample.encoder,
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800418 constants_.one_revolution_distance) -
Austin Schuh409ffe02019-01-21 18:46:41 -0800419 sample.encoder;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800420 if (offset_ready()) {
Brian Silvermana10d20a2017-02-19 14:28:53 -0800421 if (!zeroed_) {
422 first_offset_ = offset_;
423 }
424
425 if (::std::abs(first_offset_ - offset_) >
426 constants_.allowable_encoder_error *
427 constants_.one_revolution_distance) {
428 LOG(ERROR,
429 "Offset moved too far. Initial: %f, current %f, allowable change: "
430 "%f\n",
431 first_offset_, offset_, constants_.allowable_encoder_error *
432 constants_.one_revolution_distance);
433 error_ = true;
434 }
435
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800436 zeroed_ = true;
437 }
Austin Schuh5f01f152017-02-11 21:34:08 -0800438 }
439
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800440 // Update the position.
441 filtered_position_ = pot_relative_encoder_offset_ + info.encoder;
Austin Schuh5f01f152017-02-11 21:34:08 -0800442 position_ = offset_ + info.encoder;
443}
444
Austin Schuh72db9a12019-01-21 18:02:51 -0800445PotAndAbsoluteEncoderZeroingEstimator::State
446PotAndAbsoluteEncoderZeroingEstimator::GetEstimatorState() const {
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800447 State r;
448 r.error = error_;
449 r.zeroed = zeroed_;
450 r.position = position_;
451 r.pot_position = filtered_position_;
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800452 r.absolute_position = filtered_absolute_encoder_;
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800453 return r;
454}
455
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000456void PulseIndexZeroingEstimator::Reset() {
457 max_index_position_ = ::std::numeric_limits<double>::lowest();
458 min_index_position_ = ::std::numeric_limits<double>::max();
459 offset_ = 0;
460 last_used_index_pulse_count_ = 0;
461 zeroed_ = false;
462 error_ = false;
463}
464
465void PulseIndexZeroingEstimator::StoreIndexPulseMaxAndMin(
466 const IndexPosition &info) {
467 // If we have a new index pulse.
468 if (last_used_index_pulse_count_ != info.index_pulses) {
469 // If the latest pulses's position is outside the range we've currently
470 // seen, record it appropriately.
471 if (info.latched_encoder > max_index_position_) {
472 max_index_position_ = info.latched_encoder;
473 }
474 if (info.latched_encoder < min_index_position_) {
475 min_index_position_ = info.latched_encoder;
476 }
477 last_used_index_pulse_count_ = info.index_pulses;
478 }
479}
480
Brian Silvermanf37839c2017-02-19 18:07:15 -0800481int PulseIndexZeroingEstimator::IndexPulseCount() const {
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000482 if (min_index_position_ > max_index_position_) {
483 // This condition means we haven't seen a pulse yet.
484 return 0;
485 }
486
487 // Calculate the number of pulses encountered so far.
488 return 1 + static_cast<int>(
489 ::std::round((max_index_position_ - min_index_position_) /
490 constants_.index_difference));
491}
492
493void PulseIndexZeroingEstimator::UpdateEstimate(const IndexPosition &info) {
494 StoreIndexPulseMaxAndMin(info);
495 const int index_pulse_count = IndexPulseCount();
496 if (index_pulse_count > constants_.index_pulse_count) {
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000497 if (!error_) {
498 LOG(ERROR, "Got more index pulses than expected. Got %d expected %d.\n",
499 index_pulse_count, constants_.index_pulse_count);
500 error_ = true;
501 }
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000502 }
503
504 // TODO(austin): Detect if the encoder or index pulse is unplugged.
505 // TODO(austin): Detect missing counts.
506
507 if (index_pulse_count == constants_.index_pulse_count && !zeroed_) {
508 offset_ = constants_.measured_index_position -
509 constants_.known_index_pulse * constants_.index_difference -
510 min_index_position_;
511 zeroed_ = true;
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000512 } else if (zeroed_ && !error_) {
513 // Detect whether the index pulse is somewhere other than where we expect
514 // it to be. First we compute the position of the most recent index pulse.
515 double index_pulse_distance =
516 info.latched_encoder + offset_ - constants_.measured_index_position;
517 // Second we compute the position of the index pulse in terms of
518 // the index difference. I.e. if this index pulse is two pulses away from
519 // the index pulse that we know about then this number should be positive
520 // or negative two.
521 double relative_distance =
522 index_pulse_distance / constants_.index_difference;
523 // Now we compute how far away the measured index pulse is from the
524 // expected index pulse.
525 double error = relative_distance - ::std::round(relative_distance);
526 // This lets us check if the index pulse is within an acceptable error
527 // margin of where we expected it to be.
528 if (::std::abs(error) > constants_.allowable_encoder_error) {
529 LOG(ERROR,
530 "Encoder ticks out of range since last index pulse. known index "
531 "pulse: %f, expected index pulse: %f, actual index pulse: %f, "
532 "allowable error: %f\n",
533 constants_.measured_index_position,
534 round(relative_distance) * constants_.index_difference +
535 constants_.measured_index_position,
536 info.latched_encoder + offset_,
537 constants_.allowable_encoder_error * constants_.index_difference);
538 error_ = true;
539 }
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000540 }
Brian Silvermanf37839c2017-02-19 18:07:15 -0800541
542 position_ = info.encoder + offset_;
543}
544
545PulseIndexZeroingEstimator::State
546PulseIndexZeroingEstimator::GetEstimatorState() const {
547 State r;
548 r.error = error_;
549 r.zeroed = zeroed_;
550 r.position = position_;
551 r.min_index_position = min_index_position_;
552 r.max_index_position = max_index_position_;
553 r.index_pulses_seen = IndexPulseCount();
554 return r;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000555}
556
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000557} // namespace zeroing
558} // namespace frc971