blob: c64f38f3d65f2a98ce6f0dafa4098b421fdd6370 [file] [log] [blame]
Adam Snaiderc4b3c192015-02-01 01:30:39 +00001#ifndef FRC971_ZEROING_ZEROING_H_
2#define FRC971_ZEROING_ZEROING_H_
3
Philipp Schrader41d82912015-02-15 03:44:23 +00004#include <cstdint>
Adam Snaiderc4b3c192015-02-01 01:30:39 +00005#include <vector>
Philipp Schrader41d82912015-02-15 03:44:23 +00006
Austin Schuh703b8d42015-02-01 14:56:34 -08007#include "frc971/control_loops/control_loops.q.h"
8#include "frc971/constants.h"
Adam Snaiderc4b3c192015-02-01 01:30:39 +00009
Adam Snaiderb4119252015-02-15 01:30:57 +000010// TODO(pschrader): Flag an error if encoder index pulse is not n revolutions
11// away from the last one (i.e. got extra counts from noise, etc..)
12//
13// TODO(pschrader): Flag error if the pot disagrees too much with the encoder
14// after being zeroed.
15//
16// TODO(pschrader): Watch the offset over long periods of time and flag if it
17// gets too far away from the initial value.
18
Adam Snaiderc4b3c192015-02-01 01:30:39 +000019namespace frc971 {
20namespace zeroing {
21
Neil Balch1049be92017-02-15 23:20:49 -080022class ZeroingEstimator {
23 public:
24 virtual ~ZeroingEstimator(){}
25
26 // Returns true if the logic considers the corresponding mechanism to be
27 // zeroed. It return false otherwise. For example, right after a call to
28 // Reset() this returns false.
29 virtual bool zeroed() const = 0;
30
31 // Returns the estimated starting position of the corresponding mechansim. In
32 // some contexts we refer to this as the "offset".
33 virtual double offset() const = 0;
34
35 // Returns the estimated position of the corresponding mechanism. This value
36 // is in SI units. For example, the estimator for the elevator would return a
37 // value in meters for the height relative to absolute zero.
38 virtual double position() const = 0;
39
40 // Returns true if an error has occurred, false otherwise. This gets reset to
41 // false when the Reset() function is called.
42 virtual bool error() const = 0;
43};
44
Brian Silvermanab0b6772017-02-05 16:16:21 -080045// Estimates the position with an incremental encoder with an index pulse and a
46// potentiometer.
Neil Balch1049be92017-02-15 23:20:49 -080047class PotAndIndexPulseZeroingEstimator : public ZeroingEstimator {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000048 public:
Brian Silvermanab0b6772017-02-05 16:16:21 -080049 using Position = PotAndIndexPosition;
50 using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants;
Austin Schuh5f01f152017-02-11 21:34:08 -080051 using State = EstimatorState;
Brian Silvermanab0b6772017-02-05 16:16:21 -080052
Tyler Chatowf8f03112017-02-05 14:31:34 -080053 PotAndIndexPulseZeroingEstimator(
54 const constants::PotAndIndexPulseZeroingConstants &constants);
Austin Schuh703b8d42015-02-01 14:56:34 -080055
Adam Snaiderb4119252015-02-15 01:30:57 +000056 // Update the internal logic with the next sensor values.
57 void UpdateEstimate(const PotAndIndexPosition &info);
58
59 // Reset the internal logic so it needs to be re-zeroed.
60 void Reset();
61
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000062 // Manually trigger an internal error. This is used for testing the error
63 // logic.
64 void TriggerError();
65
Neil Balch1049be92017-02-15 23:20:49 -080066 bool error() const override { return error_; }
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000067
Neil Balch1049be92017-02-15 23:20:49 -080068 bool zeroed() const override { return zeroed_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000069
Neil Balch1049be92017-02-15 23:20:49 -080070 double position() const override { return position_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000071
Neil Balch1049be92017-02-15 23:20:49 -080072 double offset() const override { return start_pos_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000073
Austin Schuhbe133ed2016-03-11 21:23:34 -080074 // Return the estimated position of the corresponding mechanism not using the
75 // index pulse, even if one is available.
76 double filtered_position() const { return filtered_position_; }
77
Adam Snaiderb4119252015-02-15 01:30:57 +000078 // Returns a number between 0 and 1 that represents the percentage of the
79 // samples being used in the moving average filter. A value of 0.0 means that
80 // no samples are being used. A value of 1.0 means that the filter is using
81 // as many samples as it has room for. For example, after a Reset() this
82 // value returns 0.0. As more samples get added with UpdateEstimate(...) the
83 // return value starts increasing to 1.0.
Austin Schuh703b8d42015-02-01 14:56:34 -080084 double offset_ratio_ready() const {
Austin Schuh5f01f152017-02-11 21:34:08 -080085 return start_pos_samples_.size() /
86 static_cast<double>(constants_.average_filter_size);
Austin Schuh703b8d42015-02-01 14:56:34 -080087 }
88
Austin Schuh7485dbb2016-02-08 00:21:58 -080089 // Returns true if the sample buffer is full.
90 bool offset_ready() const {
Austin Schuh5f01f152017-02-11 21:34:08 -080091 return start_pos_samples_.size() == constants_.average_filter_size;
Austin Schuh7485dbb2016-02-08 00:21:58 -080092 }
93
Adam Snaiderc4b3c192015-02-01 01:30:39 +000094 private:
Philipp Schradere828be72015-02-15 07:07:37 +000095 // This function calculates the start position given the internal state and
96 // the provided `latched_encoder' value.
97 double CalculateStartPosition(double start_average,
98 double latched_encoder) const;
99
Austin Schuh5f01f152017-02-11 21:34:08 -0800100 // The zeroing constants used to describe the configuration of the system.
101 const constants::PotAndIndexPulseZeroingConstants constants_;
102
Adam Snaiderb4119252015-02-15 01:30:57 +0000103 // The estimated position.
Austin Schuh5f01f152017-02-11 21:34:08 -0800104 double position_;
Austin Schuhbe133ed2016-03-11 21:23:34 -0800105 // The unzeroed filtered position.
106 double filtered_position_ = 0.0;
Adam Snaiderb4119252015-02-15 01:30:57 +0000107 // The next position in 'start_pos_samples_' to be used to store the next
108 // sample.
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000109 int samples_idx_;
110 // Last 'max_sample_count_' samples for start positions.
111 std::vector<double> start_pos_samples_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000112 // The estimated starting position of the mechanism. We also call this the
113 // 'offset' in some contexts.
114 double start_pos_;
Philipp Schrader41d82912015-02-15 03:44:23 +0000115 // Flag for triggering logic that takes note of the current index pulse count
Philipp Schradere828be72015-02-15 07:07:37 +0000116 // after a reset. See `last_used_index_pulse_count_'.
Philipp Schrader41d82912015-02-15 03:44:23 +0000117 bool wait_for_index_pulse_;
118 // After a reset we keep track of the index pulse count with this. Only after
119 // the index pulse count changes (i.e. increments at least once or wraps
Philipp Schradere828be72015-02-15 07:07:37 +0000120 // around) will we consider the mechanism zeroed. We also use this to store
121 // the most recent `PotAndIndexPosition::index_pulses' value when the start
122 // position was calculated. It helps us calculate the start position only on
123 // index pulses to reject corrupted intermediate data.
124 uint32_t last_used_index_pulse_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000125 // Marker to track whether we're fully zeroed yet or not.
126 bool zeroed_;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000127 // Marker to track whether an error has occurred. This gets reset to false
128 // whenever Reset() is called.
129 bool error_;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000130 // Stores the position "start_pos" variable the first time the program
131 // is zeroed.
132 double first_start_pos_;
Austin Schuh5f01f152017-02-11 21:34:08 -0800133};
134
135// Estimates the position with an absolute encoder which also reports
136// incremental counts, and a potentiometer.
Neil Balch1049be92017-02-15 23:20:49 -0800137class PotAndAbsEncoderZeroingEstimator : public ZeroingEstimator {
Austin Schuh5f01f152017-02-11 21:34:08 -0800138 public:
139 using Position = PotAndAbsolutePosition;
140 using ZeroingConstants = constants::PotAndAbsoluteEncoderZeroingConstants;
141 using State = AbsoluteEstimatorState;
142
143 PotAndAbsEncoderZeroingEstimator(
144 const constants::PotAndAbsoluteEncoderZeroingConstants &constants);
145
146 // Resets the internal logic so it needs to be re-zeroed.
147 void Reset();
148
149 // Updates the sensor values for the zeroing logic.
150 void UpdateEstimate(const PotAndAbsolutePosition &info);
151
Neil Balch1049be92017-02-15 23:20:49 -0800152 bool zeroed() const override { return zeroed_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800153
Neil Balch1049be92017-02-15 23:20:49 -0800154 double position() const override { return position_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800155
Neil Balch1049be92017-02-15 23:20:49 -0800156 double offset() const override { return offset_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800157
Austin Schuh5f01f152017-02-11 21:34:08 -0800158 // TODO(austin): Actually implement this.
Neil Balch1049be92017-02-15 23:20:49 -0800159 bool error() const override { return false; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800160
161 // Returns true if the sample buffer is full.
162 bool offset_ready() const {
163 return relative_to_absolute_offset_samples_.size() ==
164 constants_.average_filter_size &&
165 offset_samples_.size() == constants_.average_filter_size;
166 }
167
168 // Return the estimated position of the corresponding mechanism not using the
169 // index pulse, even if one is available.
170 double filtered_position() const { return filtered_position_; }
171
172 private:
173 // The zeroing constants used to describe the configuration of the system.
174 const constants::PotAndAbsoluteEncoderZeroingConstants constants_;
175 // True if the mechanism is zeroed.
176 bool zeroed_;
177 // Samples of the offset needed to line the relative encoder up with the
178 // absolute encoder.
179 ::std::vector<double> relative_to_absolute_offset_samples_;
180 // Offset between the Pot and Relative encoder position.
181 ::std::vector<double> offset_samples_;
182 // Estimated start position of the mechanism
183 double offset_;
184 // The next position in 'relative_to_absolute_offset_samples_' and
185 // 'encoder_samples_' to be used to store the next sample.
186 int samples_idx_;
187 // The unzeroed filtered position.
188 double filtered_position_ = 0.0;
189 // The filtered position.
190 double position_ = 0.0;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000191};
192
Daniel Pettiab274232015-02-16 19:15:34 -0800193// Populates an EstimatorState struct with information from the zeroing
194// estimator.
Tyler Chatowf8f03112017-02-05 14:31:34 -0800195void PopulateEstimatorState(const PotAndIndexPulseZeroingEstimator &estimator,
Daniel Pettiab274232015-02-16 19:15:34 -0800196 EstimatorState *state);
197
Austin Schuh5f01f152017-02-11 21:34:08 -0800198void PopulateEstimatorState(const PotAndAbsEncoderZeroingEstimator &estimator,
199 AbsoluteEstimatorState *state);
200
Adam Snaiderb4119252015-02-15 01:30:57 +0000201} // namespace zeroing
202} // namespace frc971
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000203
204#endif // FRC971_ZEROING_ZEROING_H_