blob: 922833103ce5728773088122a5dd041f7080695c [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;
Philipp Schrader030ad182015-02-15 05:40:58 +000014 known_index_pos_ = constants.measured_index_position;
Adam Snaiderb4119252015-02-15 01:30:57 +000015
16 start_pos_samples_.reserve(max_sample_count_);
17
18 Reset();
Austin Schuh703b8d42015-02-01 14:56:34 -080019}
20
Adam Snaiderb4119252015-02-15 01:30:57 +000021void ZeroingEstimator::Reset() {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000022 samples_idx_ = 0;
Adam Snaiderb4119252015-02-15 01:30:57 +000023 start_pos_ = 0;
24 start_pos_samples_.clear();
25 zeroed_ = false;
Philipp Schrader41d82912015-02-15 03:44:23 +000026 wait_for_index_pulse_ = true;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000027}
28
Austin Schuh703b8d42015-02-01 14:56:34 -080029void ZeroingEstimator::UpdateEstimate(const PotAndIndexPosition& info) {
Philipp Schrader41d82912015-02-15 03:44:23 +000030 // We want to make sure that we encounter at least one index pulse while
31 // zeroing. So we take the index pulse count from the first sample after
32 // reset and wait for that count to change before we consider ourselves
33 // zeroed.
34 if (wait_for_index_pulse_) {
35 index_pulse_count_after_reset_ = info.index_pulses;
36 wait_for_index_pulse_ = false;
37 }
38
Adam Snaiderc4b3c192015-02-01 01:30:39 +000039 if (start_pos_samples_.size() < max_sample_count_) {
40 start_pos_samples_.push_back(info.pot - info.encoder);
41 } else {
42 start_pos_samples_[samples_idx_] = info.pot - info.encoder;
43 }
Adam Snaiderb4119252015-02-15 01:30:57 +000044
45 // Drop the oldest sample when we run this function the next time around.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000046 samples_idx_ = (samples_idx_ + 1) % max_sample_count_;
47
Adam Snaiderb4119252015-02-15 01:30:57 +000048 double sample_sum = 0.0;
49
Adam Snaiderc4b3c192015-02-01 01:30:39 +000050 for (size_t i = 0; i < start_pos_samples_.size(); ++i) {
Adam Snaiderb4119252015-02-15 01:30:57 +000051 sample_sum += start_pos_samples_[i];
Adam Snaiderc4b3c192015-02-01 01:30:39 +000052 }
53
54 // Calculates the average of the starting position.
Adam Snaiderb4119252015-02-15 01:30:57 +000055 double start_average = sample_sum / start_pos_samples_.size();
56
57 // If there are no index pulses to use or we don't have enough samples yet to
58 // have a well-filtered starting position then we use the filtered value as
59 // our best guess.
Philipp Schrader41d82912015-02-15 03:44:23 +000060 if (info.index_pulses == index_pulse_count_after_reset_ ||
61 offset_ratio_ready() < 1.0) {
Adam Snaiderb4119252015-02-15 01:30:57 +000062 start_pos_ = start_average;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000063 } else {
Austin Schuh703b8d42015-02-01 14:56:34 -080064 // We calculate an aproximation of the value of the last index position.
Philipp Schrader030ad182015-02-15 05:40:58 +000065 // Also account for index pulses not lining up with integer multiples of
66 // the index_diff.
67 double index_pos = start_average + info.latched_encoder - known_index_pos_;
Austin Schuh703b8d42015-02-01 14:56:34 -080068 // We round index_pos to the closest valid value of the index.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000069 double accurate_index_pos = (round(index_pos / index_diff_)) * index_diff_;
Philipp Schrader030ad182015-02-15 05:40:58 +000070 // Now we reverse the first calculation to get the accurate start position.
71 start_pos_ = accurate_index_pos - info.latched_encoder + known_index_pos_;
Adam Snaiderb4119252015-02-15 01:30:57 +000072
73 // Now that we have an accurate starting position we can consider ourselves
74 // zeroed.
Austin Schuh703b8d42015-02-01 14:56:34 -080075 zeroed_ = true;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000076 }
Adam Snaiderb4119252015-02-15 01:30:57 +000077
78 pos_ = start_pos_ + info.encoder;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000079}
80
Adam Snaiderc4b3c192015-02-01 01:30:39 +000081} // namespace zeroing
82} // namespace frc971