Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 1 | #include "frc971/zeroing/zeroing.h" |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 2 | |
Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 3 | #include <cmath> |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 4 | #include <vector> |
| 5 | |
| 6 | namespace frc971 { |
| 7 | namespace zeroing { |
| 8 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 9 | void PopulateEstimatorState(const zeroing::ZeroingEstimator& estimator, |
| 10 | EstimatorState* state) { |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 11 | state->error = estimator.error(); |
| 12 | state->zeroed = estimator.zeroed(); |
| 13 | state->position = estimator.position(); |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 14 | state->pot_position = estimator.filtered_position(); |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 15 | } |
| 16 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 17 | ZeroingEstimator::ZeroingEstimator( |
Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 18 | const constants::ZeroingConstants& constants) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 19 | index_diff_ = constants.index_difference; |
| 20 | max_sample_count_ = constants.average_filter_size; |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 21 | known_index_pos_ = constants.measured_index_position; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 22 | allowable_encoder_error_ = constants.allowable_encoder_error; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 23 | start_pos_samples_.reserve(max_sample_count_); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 24 | Reset(); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 27 | void ZeroingEstimator::Reset() { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 28 | samples_idx_ = 0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 29 | start_pos_ = 0; |
| 30 | start_pos_samples_.clear(); |
| 31 | zeroed_ = false; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 32 | wait_for_index_pulse_ = true; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 33 | last_used_index_pulse_count_ = 0; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 34 | first_start_pos_ = 0.0; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 35 | error_ = false; |
| 36 | } |
| 37 | |
| 38 | void ZeroingEstimator::TriggerError() { |
| 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 | |
| 45 | double ZeroingEstimator::CalculateStartPosition(double start_average, |
| 46 | double latched_encoder) const { |
| 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. |
| 50 | double index_pos = start_average + latched_encoder - known_index_pos_; |
| 51 | // We round index_pos to the closest valid value of the index. |
| 52 | double accurate_index_pos = (round(index_pos / index_diff_)) * index_diff_; |
| 53 | // Now we reverse the first calculation to get the accurate start position. |
| 54 | return accurate_index_pos - latched_encoder + known_index_pos_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 57 | void ZeroingEstimator::UpdateEstimate(const PotAndIndexPosition& info) { |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 58 | // We want to make sure that we encounter at least one index pulse while |
| 59 | // zeroing. So we take the index pulse count from the first sample after |
| 60 | // reset and wait for that count to change before we consider ourselves |
| 61 | // zeroed. |
| 62 | if (wait_for_index_pulse_) { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 63 | last_used_index_pulse_count_ = info.index_pulses; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 64 | wait_for_index_pulse_ = false; |
| 65 | } |
| 66 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 67 | if (start_pos_samples_.size() < max_sample_count_) { |
| 68 | start_pos_samples_.push_back(info.pot - info.encoder); |
| 69 | } else { |
| 70 | start_pos_samples_[samples_idx_] = info.pot - info.encoder; |
| 71 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 72 | |
| 73 | // Drop the oldest sample when we run this function the next time around. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 74 | samples_idx_ = (samples_idx_ + 1) % max_sample_count_; |
| 75 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 76 | double sample_sum = 0.0; |
| 77 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 78 | for (size_t i = 0; i < start_pos_samples_.size(); ++i) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 79 | sample_sum += start_pos_samples_[i]; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Calculates the average of the starting position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 83 | double start_average = sample_sum / start_pos_samples_.size(); |
| 84 | |
| 85 | // If there are no index pulses to use or we don't have enough samples yet to |
| 86 | // have a well-filtered starting position then we use the filtered value as |
| 87 | // our best guess. |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 88 | if (!zeroed_ && |
| 89 | (info.index_pulses == last_used_index_pulse_count_ || !offset_ready())) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 90 | start_pos_ = start_average; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 91 | } else if (!zeroed_ || last_used_index_pulse_count_ != info.index_pulses) { |
| 92 | // Note the accurate start position and the current index pulse count so |
| 93 | // that we only run this logic once per index pulse. That should be more |
| 94 | // resilient to corrupted intermediate data. |
| 95 | start_pos_ = CalculateStartPosition(start_average, info.latched_encoder); |
| 96 | last_used_index_pulse_count_ = info.index_pulses; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 97 | |
| 98 | // TODO(austin): Reject encoder positions which have x% error rather than |
| 99 | // rounding to the closest index pulse. |
| 100 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 101 | // Save the first starting position. |
| 102 | if (!zeroed_) { |
| 103 | first_start_pos_ = start_pos_; |
| 104 | LOG(INFO, "latching start position %f\n", first_start_pos_); |
| 105 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 106 | |
| 107 | // Now that we have an accurate starting position we can consider ourselves |
| 108 | // zeroed. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 109 | zeroed_ = true; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 110 | // Throw an error if first_start_pos is bigger/smaller than |
Brian Silverman | 852824a | 2016-05-15 23:03:01 -0700 | [diff] [blame^] | 111 | // allowable_encoder_error_ * index_diff + start_pos. |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 112 | if (::std::abs(first_start_pos_ - start_pos_) > |
| 113 | allowable_encoder_error_ * index_diff_) { |
| 114 | if (!error_) { |
| 115 | LOG(ERROR, |
| 116 | "Encoder ticks out of range since last index pulse. first start " |
Austin Schuh | 1c85bc8 | 2016-04-03 21:36:31 -0700 | [diff] [blame] | 117 | "position: %f recent starting position: %f, allowable error: %f\n", |
| 118 | first_start_pos_, start_pos_, |
| 119 | allowable_encoder_error_ * index_diff_); |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 120 | error_ = true; |
| 121 | } |
| 122 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 123 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 124 | |
| 125 | pos_ = start_pos_ + info.encoder; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 126 | filtered_position_ = start_average + info.encoder; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 129 | } // namespace zeroing |
| 130 | } // namespace frc971 |