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 | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame^] | 13 | index_pulse_count_after_reset_ = 0; |
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; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 28 | void ZeroingEstimator::UpdateEstimate(const PotAndIndexPosition& info) { |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame^] | 29 | // We want to make sure that we encounter at least one index pulse while |
| 30 | // zeroing. So we take the index pulse count from the first sample after |
| 31 | // reset and wait for that count to change before we consider ourselves |
| 32 | // zeroed. |
| 33 | if (wait_for_index_pulse_) { |
| 34 | index_pulse_count_after_reset_ = info.index_pulses; |
| 35 | wait_for_index_pulse_ = false; |
| 36 | } |
| 37 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 38 | if (start_pos_samples_.size() < max_sample_count_) { |
| 39 | start_pos_samples_.push_back(info.pot - info.encoder); |
| 40 | } else { |
| 41 | start_pos_samples_[samples_idx_] = info.pot - info.encoder; |
| 42 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 43 | |
| 44 | // 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] | 45 | samples_idx_ = (samples_idx_ + 1) % max_sample_count_; |
| 46 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 47 | double sample_sum = 0.0; |
| 48 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 49 | for (size_t i = 0; i < start_pos_samples_.size(); ++i) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 50 | sample_sum += start_pos_samples_[i]; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Calculates the average of the starting position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 54 | double start_average = sample_sum / start_pos_samples_.size(); |
| 55 | |
| 56 | // If there are no index pulses to use or we don't have enough samples yet to |
| 57 | // have a well-filtered starting position then we use the filtered value as |
| 58 | // our best guess. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame^] | 59 | if (info.index_pulses == index_pulse_count_after_reset_ || |
| 60 | offset_ratio_ready() < 1.0) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 61 | start_pos_ = start_average; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 62 | } else { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 63 | // We calculate an aproximation of the value of the last index position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 64 | double index_pos = start_average + info.latched_encoder; |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 65 | // We round index_pos to the closest valid value of the index. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 66 | double accurate_index_pos = (round(index_pos / index_diff_)) * index_diff_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 67 | // Now we reverse the first calculation to the accurate start position. |
| 68 | start_pos_ = accurate_index_pos - info.latched_encoder; |
| 69 | |
| 70 | // Now that we have an accurate starting position we can consider ourselves |
| 71 | // zeroed. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 72 | zeroed_ = true; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 73 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 74 | |
| 75 | pos_ = start_pos_ + info.encoder; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 78 | } // namespace zeroing |
| 79 | } // namespace frc971 |