blob: d27931856041ebe18279227a76a6e20ceb860d33 [file] [log] [blame]
Adam Snaiderc4b3c192015-02-01 01:30:39 +00001#include "frc971/zeroing/zeroing.h"
Adam Snaiderb4119252015-02-15 01:30:57 +00002
Adam Snaiderc4b3c192015-02-01 01:30:39 +00003#include <math.h>
4#include <vector>
5
6namespace frc971 {
7namespace zeroing {
8
Austin Schuh703b8d42015-02-01 14:56:34 -08009ZeroingEstimator::ZeroingEstimator(
10 const constants::Values::ZeroingConstants& constants) {
Adam Snaiderb4119252015-02-15 01:30:57 +000011 index_diff_ = constants.index_difference;
12 max_sample_count_ = constants.average_filter_size;
Philipp Schrader41d82912015-02-15 03:44:23 +000013 index_pulse_count_after_reset_ = 0;
Adam Snaiderb4119252015-02-15 01:30:57 +000014
15 start_pos_samples_.reserve(max_sample_count_);
16
17 Reset();
Austin Schuh703b8d42015-02-01 14:56:34 -080018}
19
Adam Snaiderb4119252015-02-15 01:30:57 +000020void ZeroingEstimator::Reset() {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000021 samples_idx_ = 0;
Adam Snaiderb4119252015-02-15 01:30:57 +000022 start_pos_ = 0;
23 start_pos_samples_.clear();
24 zeroed_ = false;
Philipp Schrader41d82912015-02-15 03:44:23 +000025 wait_for_index_pulse_ = true;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000026}
27
Austin Schuh703b8d42015-02-01 14:56:34 -080028void ZeroingEstimator::UpdateEstimate(const PotAndIndexPosition& info) {
Philipp Schrader41d82912015-02-15 03:44:23 +000029 // 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 Snaiderc4b3c192015-02-01 01:30:39 +000038 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 Snaiderb4119252015-02-15 01:30:57 +000043
44 // Drop the oldest sample when we run this function the next time around.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000045 samples_idx_ = (samples_idx_ + 1) % max_sample_count_;
46
Adam Snaiderb4119252015-02-15 01:30:57 +000047 double sample_sum = 0.0;
48
Adam Snaiderc4b3c192015-02-01 01:30:39 +000049 for (size_t i = 0; i < start_pos_samples_.size(); ++i) {
Adam Snaiderb4119252015-02-15 01:30:57 +000050 sample_sum += start_pos_samples_[i];
Adam Snaiderc4b3c192015-02-01 01:30:39 +000051 }
52
53 // Calculates the average of the starting position.
Adam Snaiderb4119252015-02-15 01:30:57 +000054 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 Schrader41d82912015-02-15 03:44:23 +000059 if (info.index_pulses == index_pulse_count_after_reset_ ||
60 offset_ratio_ready() < 1.0) {
Adam Snaiderb4119252015-02-15 01:30:57 +000061 start_pos_ = start_average;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000062 } else {
Austin Schuh703b8d42015-02-01 14:56:34 -080063 // We calculate an aproximation of the value of the last index position.
Adam Snaiderb4119252015-02-15 01:30:57 +000064 double index_pos = start_average + info.latched_encoder;
Austin Schuh703b8d42015-02-01 14:56:34 -080065 // We round index_pos to the closest valid value of the index.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000066 double accurate_index_pos = (round(index_pos / index_diff_)) * index_diff_;
Adam Snaiderb4119252015-02-15 01:30:57 +000067 // 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 Schuh703b8d42015-02-01 14:56:34 -080072 zeroed_ = true;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000073 }
Adam Snaiderb4119252015-02-15 01:30:57 +000074
75 pos_ = start_pos_ + info.encoder;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000076}
77
Adam Snaiderc4b3c192015-02-01 01:30:39 +000078} // namespace zeroing
79} // namespace frc971