Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 1 | #include "frc971/zeroing/zeroing.h" |
| 2 | #include <math.h> |
| 3 | #include <vector> |
| 4 | |
| 5 | namespace frc971 { |
| 6 | namespace zeroing { |
| 7 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 8 | ZeroingEstimator::ZeroingEstimator( |
| 9 | const constants::Values::ZeroingConstants& constants) { |
| 10 | DoInit(constants.index_difference, constants.average_filter_size); |
| 11 | } |
| 12 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 13 | ZeroingEstimator::ZeroingEstimator(double index_difference, |
| 14 | size_t max_sample_count) { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 15 | DoInit(index_difference, max_sample_count); |
| 16 | } |
| 17 | |
| 18 | void ZeroingEstimator::DoInit(double index_difference, |
| 19 | size_t max_sample_count) { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 20 | index_diff_ = index_difference; |
| 21 | samples_idx_ = 0; |
| 22 | max_sample_count_ = max_sample_count; |
| 23 | start_pos_samples_.reserve(max_sample_count); |
| 24 | } |
| 25 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 26 | void ZeroingEstimator::UpdateEstimate(const PotAndIndexPosition& info) { |
| 27 | ZeroingInfo zinfo; |
| 28 | zinfo.pot = info.pot; |
| 29 | zinfo.encoder = info.encoder; |
| 30 | zinfo.index_encoder = info.latched_encoder; |
| 31 | zinfo.index_count = info.index_pulses; |
| 32 | UpdateEstimate(zinfo); |
| 33 | } |
| 34 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 35 | void ZeroingEstimator::UpdateEstimate(const ZeroingInfo & info) { |
| 36 | if (start_pos_samples_.size() < max_sample_count_) { |
| 37 | start_pos_samples_.push_back(info.pot - info.encoder); |
| 38 | } else { |
| 39 | start_pos_samples_[samples_idx_] = info.pot - info.encoder; |
| 40 | } |
| 41 | samples_idx_ = (samples_idx_ + 1) % max_sample_count_; |
| 42 | |
| 43 | double start_average = 0.0; |
| 44 | for (size_t i = 0; i < start_pos_samples_.size(); ++i) { |
| 45 | start_average += start_pos_samples_[i]; |
| 46 | } |
| 47 | |
| 48 | // Calculates the average of the starting position. |
| 49 | start_average = start_average / start_pos_samples_.size(); |
| 50 | /* If the index_encoder is invalid, then we use |
| 51 | * the average of the starting position to |
| 52 | * calculate the position. |
| 53 | */ |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 54 | double pos; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 55 | if (info.index_count == 0) { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 56 | pos = start_average + info.encoder; |
| 57 | zeroed_ = false; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 58 | } else { |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 59 | // We calculate an aproximation of the value of the last index position. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 60 | double index_pos = start_average + info.index_encoder; |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 61 | // We round index_pos to the closest valid value of the index. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 62 | double accurate_index_pos = (round(index_pos / index_diff_)) * index_diff_; |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 63 | // We use accurate_index_pos to calculate the position. |
| 64 | pos = accurate_index_pos + info.encoder - info.index_encoder; |
| 65 | zeroed_ = true; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 66 | } |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame^] | 67 | offset_ = pos - info.encoder; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 70 | } // namespace zeroing |
| 71 | } // namespace frc971 |