blob: 53b8e31e2c7dd3a933d90ff3dfb137eceeb9a22f [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
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000027 // zeroed.
Neil Balch1049be92017-02-15 23:20:49 -080028 virtual bool zeroed() const = 0;
29
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000030 // Returns the estimated position of the corresponding mechanism.
Neil Balch1049be92017-02-15 23:20:49 -080031 virtual double offset() const = 0;
32
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000033 // Returns the estimated starting position of the corresponding mechansim.
Neil Balch1049be92017-02-15 23:20:49 -080034 virtual double position() const = 0;
35
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000036 // Returns true if there has been an error.
Neil Balch1049be92017-02-15 23:20:49 -080037 virtual bool error() const = 0;
38};
39
Brian Silvermanab0b6772017-02-05 16:16:21 -080040// Estimates the position with an incremental encoder with an index pulse and a
41// potentiometer.
Neil Balch1049be92017-02-15 23:20:49 -080042class PotAndIndexPulseZeroingEstimator : public ZeroingEstimator {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000043 public:
Brian Silvermanab0b6772017-02-05 16:16:21 -080044 using Position = PotAndIndexPosition;
45 using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants;
Austin Schuh5f01f152017-02-11 21:34:08 -080046 using State = EstimatorState;
Brian Silvermanab0b6772017-02-05 16:16:21 -080047
Tyler Chatowf8f03112017-02-05 14:31:34 -080048 PotAndIndexPulseZeroingEstimator(
49 const constants::PotAndIndexPulseZeroingConstants &constants);
Austin Schuh703b8d42015-02-01 14:56:34 -080050
Adam Snaiderb4119252015-02-15 01:30:57 +000051 // Update the internal logic with the next sensor values.
52 void UpdateEstimate(const PotAndIndexPosition &info);
53
54 // Reset the internal logic so it needs to be re-zeroed.
55 void Reset();
56
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000057 // Manually trigger an internal error. This is used for testing the error
58 // logic.
59 void TriggerError();
60
Neil Balch1049be92017-02-15 23:20:49 -080061 bool error() const override { return error_; }
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000062
Neil Balch1049be92017-02-15 23:20:49 -080063 bool zeroed() const override { return zeroed_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000064
Neil Balch1049be92017-02-15 23:20:49 -080065 double position() const override { return position_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000066
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000067 double offset() const override { return offset_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000068
69 // Returns a number between 0 and 1 that represents the percentage of the
70 // samples being used in the moving average filter. A value of 0.0 means that
71 // no samples are being used. A value of 1.0 means that the filter is using
72 // as many samples as it has room for. For example, after a Reset() this
73 // value returns 0.0. As more samples get added with UpdateEstimate(...) the
74 // return value starts increasing to 1.0.
Austin Schuh703b8d42015-02-01 14:56:34 -080075 double offset_ratio_ready() const {
Austin Schuh5f01f152017-02-11 21:34:08 -080076 return start_pos_samples_.size() /
77 static_cast<double>(constants_.average_filter_size);
Austin Schuh703b8d42015-02-01 14:56:34 -080078 }
79
Austin Schuh7485dbb2016-02-08 00:21:58 -080080 // Returns true if the sample buffer is full.
81 bool offset_ready() const {
Austin Schuh5f01f152017-02-11 21:34:08 -080082 return start_pos_samples_.size() == constants_.average_filter_size;
Austin Schuh7485dbb2016-02-08 00:21:58 -080083 }
84
Brian Silverman4f2e2ce2017-02-19 17:49:47 -080085 // Returns information about our current state.
86 State GetEstimatorState() const;
87
Adam Snaiderc4b3c192015-02-01 01:30:39 +000088 private:
Philipp Schradere828be72015-02-15 07:07:37 +000089 // This function calculates the start position given the internal state and
90 // the provided `latched_encoder' value.
91 double CalculateStartPosition(double start_average,
92 double latched_encoder) const;
93
Austin Schuh5f01f152017-02-11 21:34:08 -080094 // The zeroing constants used to describe the configuration of the system.
95 const constants::PotAndIndexPulseZeroingConstants constants_;
96
Adam Snaiderb4119252015-02-15 01:30:57 +000097 // The estimated position.
Austin Schuh5f01f152017-02-11 21:34:08 -080098 double position_;
Austin Schuhbe133ed2016-03-11 21:23:34 -080099 // The unzeroed filtered position.
100 double filtered_position_ = 0.0;
Adam Snaiderb4119252015-02-15 01:30:57 +0000101 // The next position in 'start_pos_samples_' to be used to store the next
102 // sample.
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000103 int samples_idx_;
104 // Last 'max_sample_count_' samples for start positions.
105 std::vector<double> start_pos_samples_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000106 // The estimated starting position of the mechanism. We also call this the
107 // 'offset' in some contexts.
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000108 double offset_;
Philipp Schrader41d82912015-02-15 03:44:23 +0000109 // Flag for triggering logic that takes note of the current index pulse count
Philipp Schradere828be72015-02-15 07:07:37 +0000110 // after a reset. See `last_used_index_pulse_count_'.
Philipp Schrader41d82912015-02-15 03:44:23 +0000111 bool wait_for_index_pulse_;
112 // After a reset we keep track of the index pulse count with this. Only after
113 // the index pulse count changes (i.e. increments at least once or wraps
Philipp Schradere828be72015-02-15 07:07:37 +0000114 // around) will we consider the mechanism zeroed. We also use this to store
115 // the most recent `PotAndIndexPosition::index_pulses' value when the start
116 // position was calculated. It helps us calculate the start position only on
117 // index pulses to reject corrupted intermediate data.
118 uint32_t last_used_index_pulse_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000119 // Marker to track whether we're fully zeroed yet or not.
120 bool zeroed_;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000121 // Marker to track whether an error has occurred. This gets reset to false
122 // whenever Reset() is called.
123 bool error_;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000124 // Stores the position "start_pos" variable the first time the program
125 // is zeroed.
126 double first_start_pos_;
Austin Schuh5f01f152017-02-11 21:34:08 -0800127};
128
129// Estimates the position with an absolute encoder which also reports
130// incremental counts, and a potentiometer.
Neil Balch1049be92017-02-15 23:20:49 -0800131class PotAndAbsEncoderZeroingEstimator : public ZeroingEstimator {
Austin Schuh5f01f152017-02-11 21:34:08 -0800132 public:
133 using Position = PotAndAbsolutePosition;
134 using ZeroingConstants = constants::PotAndAbsoluteEncoderZeroingConstants;
135 using State = AbsoluteEstimatorState;
136
137 PotAndAbsEncoderZeroingEstimator(
138 const constants::PotAndAbsoluteEncoderZeroingConstants &constants);
139
140 // Resets the internal logic so it needs to be re-zeroed.
141 void Reset();
142
143 // Updates the sensor values for the zeroing logic.
144 void UpdateEstimate(const PotAndAbsolutePosition &info);
145
Neil Balch1049be92017-02-15 23:20:49 -0800146 bool zeroed() const override { return zeroed_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800147
Neil Balch1049be92017-02-15 23:20:49 -0800148 double position() const override { return position_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800149
Neil Balch1049be92017-02-15 23:20:49 -0800150 double offset() const override { return offset_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800151
Neil Balch16275e32017-02-18 16:38:45 -0800152 bool error() const override { return error_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800153
154 // Returns true if the sample buffer is full.
155 bool offset_ready() const {
156 return relative_to_absolute_offset_samples_.size() ==
157 constants_.average_filter_size &&
158 offset_samples_.size() == constants_.average_filter_size;
159 }
160
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800161 // Returns information about our current state.
162 State GetEstimatorState() const;
Austin Schuh5f01f152017-02-11 21:34:08 -0800163
164 private:
165 // The zeroing constants used to describe the configuration of the system.
166 const constants::PotAndAbsoluteEncoderZeroingConstants constants_;
167 // True if the mechanism is zeroed.
168 bool zeroed_;
169 // Samples of the offset needed to line the relative encoder up with the
170 // absolute encoder.
171 ::std::vector<double> relative_to_absolute_offset_samples_;
172 // Offset between the Pot and Relative encoder position.
173 ::std::vector<double> offset_samples_;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800174 // Last moving_buffer_size position samples to be used to determine if the
175 // robot is moving.
176 ::std::vector<PotAndAbsolutePosition> buffered_samples_;
177 // Pointer to front of the buffered samples.
178 int buffered_samples_idx_ = 0;
179 // Estimated offset between the pot and relative encoder.
180 double pot_relative_encoder_offset_ = 0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800181 // Estimated start position of the mechanism
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800182 double offset_ = 0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800183 // The next position in 'relative_to_absolute_offset_samples_' and
184 // 'encoder_samples_' to be used to store the next sample.
185 int samples_idx_;
186 // The unzeroed filtered position.
187 double filtered_position_ = 0.0;
188 // The filtered position.
189 double position_ = 0.0;
Neil Balch16275e32017-02-18 16:38:45 -0800190 // Whether or not there is an error in the estimate.
191 bool error_ = false;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000192};
193
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000194
195// Zeros by seeing all the index pulses in the range of motion of the mechanism
196// and using that to figure out which index pulse is which.
197class PulseIndexZeroingEstimator : public ZeroingEstimator {
198 public:
199 using Position = IndexPosition;
200 using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants;
201 using State = EstimatorState;
202
203 PulseIndexZeroingEstimator(
204 const constants::EncoderPlusIndexZeroingConstants &constants)
205 : constants_(constants) {
206 Reset();
207 }
208
209 // Resets the internal logic so it needs to be re-zeroed.
210 void Reset();
211
212 bool zeroed() const override { return zeroed_; }
213
214 double position() const override {
215 CHECK(zeroed_);
216 return position_;
217 }
218
219 double offset() const override { return offset_; }
220
221 bool error() const override { return error_; }
222
223 // Updates the internal logic with the next sensor values.
224 void UpdateEstimate(const IndexPosition &info);
225
226 private:
227 // Returns the current real position using the relative encoder offset.
228 double CalculateCurrentPosition(const IndexPosition &info);
229
230 // Sets the minimum and maximum index pulse position values.
231 void StoreIndexPulseMaxAndMin(const IndexPosition &info);
232
233 // Returns the number of index pulses we should have seen so far.
234 int IndexPulseCount();
235
236 // Contains the physical constants describing the system.
237 const constants::EncoderPlusIndexZeroingConstants constants_;
238
239 // The smallest position of all the index pulses.
240 double min_index_position_;
241 // The largest position of all the index pulses.
242 double max_index_position_;
243
244 // The estimated starting position of the mechanism.
245 double offset_;
246 // After a reset we keep track of the index pulse count with this. Only after
247 // the index pulse count changes (i.e. increments at least once or wraps
248 // around) will we consider the mechanism zeroed. We also use this to store
249 // the most recent `PotAndIndexPosition::index_pulses' value when the start
250 // position was calculated. It helps us calculate the start position only on
251 // index pulses to reject corrupted intermediate data.
252 uint32_t last_used_index_pulse_count_;
253
254 // True if we are fully zeroed.
255 bool zeroed_;
256 // Marker to track whether an error has occurred.
257 bool error_;
258
259 // The estimated position.
260 double position_;
261};
262
Adam Snaiderb4119252015-02-15 01:30:57 +0000263} // namespace zeroing
264} // namespace frc971
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000265
266#endif // FRC971_ZEROING_ZEROING_H_