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 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 3 | #include <math.h> |
| 4 | #include <vector> |
| 5 | |
| 6 | namespace frc971 { |
| 7 | namespace zeroing { |
| 8 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 9 | ZeroingEstimator::ZeroingEstimator( |
| 10 | const constants::Values::ZeroingConstants& constants) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 11 | index_diff_ = constants.index_difference; |
| 12 | max_sample_count_ = constants.average_filter_size; |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 13 | known_index_pos_ = constants.measured_index_position; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 14 | |
| 15 | start_pos_samples_.reserve(max_sample_count_); |
| 16 | |
| 17 | Reset(); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 18 | } |
| 19 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 20 | void ZeroingEstimator::Reset() { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 21 | samples_idx_ = 0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 22 | start_pos_ = 0; |
| 23 | start_pos_samples_.clear(); |
| 24 | zeroed_ = false; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 25 | wait_for_index_pulse_ = true; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame^] | 26 | last_used_index_pulse_count_ = 0; |
| 27 | } |
| 28 | |
| 29 | double ZeroingEstimator::CalculateStartPosition(double start_average, |
| 30 | double latched_encoder) const { |
| 31 | // We calculate an aproximation of the value of the last index position. |
| 32 | // Also account for index pulses not lining up with integer multiples of the |
| 33 | // index_diff. |
| 34 | double index_pos = start_average + latched_encoder - known_index_pos_; |
| 35 | // We round index_pos to the closest valid value of the index. |
| 36 | double accurate_index_pos = (round(index_pos / index_diff_)) * index_diff_; |
| 37 | // Now we reverse the first calculation to get the accurate start position. |
| 38 | return accurate_index_pos - latched_encoder + known_index_pos_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 41 | void ZeroingEstimator::UpdateEstimate(const PotAndIndexPosition& info) { |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 42 | // We want to make sure that we encounter at least one index pulse while |
| 43 | // zeroing. So we take the index pulse count from the first sample after |
| 44 | // reset and wait for that count to change before we consider ourselves |
| 45 | // zeroed. |
| 46 | if (wait_for_index_pulse_) { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame^] | 47 | last_used_index_pulse_count_ = info.index_pulses; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 48 | wait_for_index_pulse_ = false; |
| 49 | } |
| 50 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 51 | if (start_pos_samples_.size() < max_sample_count_) { |
| 52 | start_pos_samples_.push_back(info.pot - info.encoder); |
| 53 | } else { |
| 54 | start_pos_samples_[samples_idx_] = info.pot - info.encoder; |
| 55 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 56 | |
| 57 | // 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] | 58 | samples_idx_ = (samples_idx_ + 1) % max_sample_count_; |
| 59 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 60 | double sample_sum = 0.0; |
| 61 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 62 | for (size_t i = 0; i < start_pos_samples_.size(); ++i) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 63 | sample_sum += start_pos_samples_[i]; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Calculates the average of the starting position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 67 | double start_average = sample_sum / start_pos_samples_.size(); |
| 68 | |
| 69 | // If there are no index pulses to use or we don't have enough samples yet to |
| 70 | // have a well-filtered starting position then we use the filtered value as |
| 71 | // our best guess. |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame^] | 72 | if (!zeroed_ && (info.index_pulses == last_used_index_pulse_count_ || |
| 73 | offset_ratio_ready() < 1.0)) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 74 | start_pos_ = start_average; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame^] | 75 | } else if (!zeroed_ || last_used_index_pulse_count_ != info.index_pulses) { |
| 76 | // Note the accurate start position and the current index pulse count so |
| 77 | // that we only run this logic once per index pulse. That should be more |
| 78 | // resilient to corrupted intermediate data. |
| 79 | start_pos_ = CalculateStartPosition(start_average, info.latched_encoder); |
| 80 | last_used_index_pulse_count_ = info.index_pulses; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 81 | |
| 82 | // Now that we have an accurate starting position we can consider ourselves |
| 83 | // zeroed. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 84 | zeroed_ = true; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 85 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 86 | |
| 87 | pos_ = start_pos_ + info.encoder; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 90 | } // namespace zeroing |
| 91 | } // namespace frc971 |