blob: f6eddb5d86c2f0469ffab98109205829d58492be [file] [log] [blame]
Adam Snaiderc4b3c192015-02-01 01:30:39 +00001#include "frc971/zeroing/zeroing.h"
2#include <math.h>
3#include <vector>
4
5namespace frc971 {
6namespace zeroing {
7
Austin Schuh703b8d42015-02-01 14:56:34 -08008ZeroingEstimator::ZeroingEstimator(
9 const constants::Values::ZeroingConstants& constants) {
10 DoInit(constants.index_difference, constants.average_filter_size);
11}
12
Adam Snaiderc4b3c192015-02-01 01:30:39 +000013ZeroingEstimator::ZeroingEstimator(double index_difference,
14 size_t max_sample_count) {
Austin Schuh703b8d42015-02-01 14:56:34 -080015 DoInit(index_difference, max_sample_count);
16}
17
18void ZeroingEstimator::DoInit(double index_difference,
19 size_t max_sample_count) {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000020 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 Schuh703b8d42015-02-01 14:56:34 -080026void 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 Snaiderc4b3c192015-02-01 01:30:39 +000035void 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 Schuh703b8d42015-02-01 14:56:34 -080054 double pos;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000055 if (info.index_count == 0) {
Austin Schuh703b8d42015-02-01 14:56:34 -080056 pos = start_average + info.encoder;
57 zeroed_ = false;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000058 } else {
Austin Schuh703b8d42015-02-01 14:56:34 -080059 // We calculate an aproximation of the value of the last index position.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000060 double index_pos = start_average + info.index_encoder;
Austin Schuh703b8d42015-02-01 14:56:34 -080061 // We round index_pos to the closest valid value of the index.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000062 double accurate_index_pos = (round(index_pos / index_diff_)) * index_diff_;
Austin Schuh703b8d42015-02-01 14:56:34 -080063 // We use accurate_index_pos to calculate the position.
64 pos = accurate_index_pos + info.encoder - info.index_encoder;
65 zeroed_ = true;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000066 }
Austin Schuh703b8d42015-02-01 14:56:34 -080067 offset_ = pos - info.encoder;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000068}
69
Adam Snaiderc4b3c192015-02-01 01:30:39 +000070} // namespace zeroing
71} // namespace frc971