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