blob: 9d7cea17528955cd60186b0cdca81bef48ef98cb [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 true if there has been an error.
Neil Balch1049be92017-02-15 23:20:49 -080034 virtual bool error() const = 0;
35};
36
Brian Silvermanab0b6772017-02-05 16:16:21 -080037// Estimates the position with an incremental encoder with an index pulse and a
38// potentiometer.
Neil Balch1049be92017-02-15 23:20:49 -080039class PotAndIndexPulseZeroingEstimator : public ZeroingEstimator {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000040 public:
Brian Silvermanab0b6772017-02-05 16:16:21 -080041 using Position = PotAndIndexPosition;
42 using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants;
Austin Schuh5f01f152017-02-11 21:34:08 -080043 using State = EstimatorState;
Brian Silvermanab0b6772017-02-05 16:16:21 -080044
Tyler Chatowf8f03112017-02-05 14:31:34 -080045 PotAndIndexPulseZeroingEstimator(
46 const constants::PotAndIndexPulseZeroingConstants &constants);
Austin Schuh703b8d42015-02-01 14:56:34 -080047
Adam Snaiderb4119252015-02-15 01:30:57 +000048 // Update the internal logic with the next sensor values.
49 void UpdateEstimate(const PotAndIndexPosition &info);
50
51 // Reset the internal logic so it needs to be re-zeroed.
52 void Reset();
53
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000054 // Manually trigger an internal error. This is used for testing the error
55 // logic.
56 void TriggerError();
57
Neil Balch1049be92017-02-15 23:20:49 -080058 bool error() const override { return error_; }
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000059
Neil Balch1049be92017-02-15 23:20:49 -080060 bool zeroed() const override { return zeroed_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000061
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000062 double offset() const override { return offset_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000063
64 // Returns a number between 0 and 1 that represents the percentage of the
65 // samples being used in the moving average filter. A value of 0.0 means that
66 // no samples are being used. A value of 1.0 means that the filter is using
67 // as many samples as it has room for. For example, after a Reset() this
68 // value returns 0.0. As more samples get added with UpdateEstimate(...) the
69 // return value starts increasing to 1.0.
Austin Schuh703b8d42015-02-01 14:56:34 -080070 double offset_ratio_ready() const {
Austin Schuh5f01f152017-02-11 21:34:08 -080071 return start_pos_samples_.size() /
72 static_cast<double>(constants_.average_filter_size);
Austin Schuh703b8d42015-02-01 14:56:34 -080073 }
74
Austin Schuh7485dbb2016-02-08 00:21:58 -080075 // Returns true if the sample buffer is full.
76 bool offset_ready() const {
Austin Schuh5f01f152017-02-11 21:34:08 -080077 return start_pos_samples_.size() == constants_.average_filter_size;
Austin Schuh7485dbb2016-02-08 00:21:58 -080078 }
79
Brian Silverman4f2e2ce2017-02-19 17:49:47 -080080 // Returns information about our current state.
81 State GetEstimatorState() const;
82
Adam Snaiderc4b3c192015-02-01 01:30:39 +000083 private:
Philipp Schradere828be72015-02-15 07:07:37 +000084 // This function calculates the start position given the internal state and
85 // the provided `latched_encoder' value.
86 double CalculateStartPosition(double start_average,
87 double latched_encoder) const;
88
Austin Schuh5f01f152017-02-11 21:34:08 -080089 // The zeroing constants used to describe the configuration of the system.
90 const constants::PotAndIndexPulseZeroingConstants constants_;
91
Adam Snaiderb4119252015-02-15 01:30:57 +000092 // The estimated position.
Austin Schuh5f01f152017-02-11 21:34:08 -080093 double position_;
Austin Schuhbe133ed2016-03-11 21:23:34 -080094 // The unzeroed filtered position.
95 double filtered_position_ = 0.0;
Adam Snaiderb4119252015-02-15 01:30:57 +000096 // The next position in 'start_pos_samples_' to be used to store the next
97 // sample.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000098 int samples_idx_;
99 // Last 'max_sample_count_' samples for start positions.
100 std::vector<double> start_pos_samples_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000101 // The estimated starting position of the mechanism. We also call this the
102 // 'offset' in some contexts.
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000103 double offset_;
Philipp Schrader41d82912015-02-15 03:44:23 +0000104 // Flag for triggering logic that takes note of the current index pulse count
Philipp Schradere828be72015-02-15 07:07:37 +0000105 // after a reset. See `last_used_index_pulse_count_'.
Philipp Schrader41d82912015-02-15 03:44:23 +0000106 bool wait_for_index_pulse_;
107 // After a reset we keep track of the index pulse count with this. Only after
108 // the index pulse count changes (i.e. increments at least once or wraps
Philipp Schradere828be72015-02-15 07:07:37 +0000109 // around) will we consider the mechanism zeroed. We also use this to store
110 // the most recent `PotAndIndexPosition::index_pulses' value when the start
111 // position was calculated. It helps us calculate the start position only on
112 // index pulses to reject corrupted intermediate data.
113 uint32_t last_used_index_pulse_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000114 // Marker to track whether we're fully zeroed yet or not.
115 bool zeroed_;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000116 // Marker to track whether an error has occurred. This gets reset to false
117 // whenever Reset() is called.
118 bool error_;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000119 // Stores the position "start_pos" variable the first time the program
120 // is zeroed.
121 double first_start_pos_;
Austin Schuh5f01f152017-02-11 21:34:08 -0800122};
123
Austin Schuh55934032017-03-11 12:45:27 -0800124// Estimates the position with an incremental encoder with an index pulse and a
125// potentiometer.
126class HallEffectAndPositionZeroingEstimator : public ZeroingEstimator {
127 public:
128 using Position = HallEffectAndPosition;
129 using ZeroingConstants = constants::HallEffectZeroingConstants;
130 using State = HallEffectAndPositionEstimatorState;
131
132 explicit HallEffectAndPositionZeroingEstimator(const ZeroingConstants &constants);
133
134 // Update the internal logic with the next sensor values.
135 void UpdateEstimate(const Position &info);
136
137 // Reset the internal logic so it needs to be re-zeroed.
138 void Reset();
139
140 // Manually trigger an internal error. This is used for testing the error
141 // logic.
142 void TriggerError();
143
144 bool error() const override { return error_; }
145
146 bool zeroed() const override { return zeroed_; }
147
148 double offset() const override { return offset_; }
149
150 // Returns information about our current state.
151 State GetEstimatorState() const;
152
153 private:
154 // Sets the minimum and maximum posedge position values.
155 void StoreEncoderMaxAndMin(const HallEffectAndPosition &info);
156
157 // The zeroing constants used to describe the configuration of the system.
158 const ZeroingConstants constants_;
159
160 // The estimated state of the hall effect.
161 double current_ = 0.0;
162 // The estimated position.
163 double position_ = 0.0;
164 // The smallest and largest positions of the last set of encoder positions
165 // while the hall effect was low.
166 double min_low_position_;
167 double max_low_position_;
168 // If we've seen the hall effect high for enough times without going low, then
169 // we can be sure it isn't a false positive.
170 bool high_long_enough_;
171 size_t cycles_high_;
172
173 bool last_hall_ = false;
174
175 // The estimated starting position of the mechanism. We also call this the
176 // 'offset' in some contexts.
177 double offset_;
178 // Flag for triggering logic that takes note of the current posedge count
179 // after a reset. See `last_used_posedge_count_'.
180 bool initialized_;
181 // After a reset we keep track of the posedge count with this. Only after the
182 // posedge count changes (i.e. increments at least once or wraps around) will
183 // we consider the mechanism zeroed. We also use this to store the most recent
184 // `HallEffectAndPosition::posedge_count' value when the start position
185 // was calculated. It helps us calculate the start position only on posedges
186 // to reject corrupted intermediate data.
187 int32_t last_used_posedge_count_;
188 // Marker to track whether we're fully zeroed yet or not.
189 bool zeroed_;
190 // Marker to track whether an error has occurred. This gets reset to false
191 // whenever Reset() is called.
192 bool error_ = false;
193 // Stores the position "start_pos" variable the first time the program
194 // is zeroed.
195 double first_start_pos_;
196};
197
Austin Schuh5f01f152017-02-11 21:34:08 -0800198// Estimates the position with an absolute encoder which also reports
199// incremental counts, and a potentiometer.
Neil Balch1049be92017-02-15 23:20:49 -0800200class PotAndAbsEncoderZeroingEstimator : public ZeroingEstimator {
Austin Schuh5f01f152017-02-11 21:34:08 -0800201 public:
202 using Position = PotAndAbsolutePosition;
203 using ZeroingConstants = constants::PotAndAbsoluteEncoderZeroingConstants;
204 using State = AbsoluteEstimatorState;
205
206 PotAndAbsEncoderZeroingEstimator(
207 const constants::PotAndAbsoluteEncoderZeroingConstants &constants);
208
209 // Resets the internal logic so it needs to be re-zeroed.
210 void Reset();
211
212 // Updates the sensor values for the zeroing logic.
213 void UpdateEstimate(const PotAndAbsolutePosition &info);
214
Neil Balch1049be92017-02-15 23:20:49 -0800215 bool zeroed() const override { return zeroed_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800216
Neil Balch1049be92017-02-15 23:20:49 -0800217 double offset() const override { return offset_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800218
Neil Balch16275e32017-02-18 16:38:45 -0800219 bool error() const override { return error_; }
Austin Schuh5f01f152017-02-11 21:34:08 -0800220
221 // Returns true if the sample buffer is full.
222 bool offset_ready() const {
223 return relative_to_absolute_offset_samples_.size() ==
224 constants_.average_filter_size &&
225 offset_samples_.size() == constants_.average_filter_size;
226 }
227
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800228 // Returns information about our current state.
229 State GetEstimatorState() const;
Austin Schuh5f01f152017-02-11 21:34:08 -0800230
231 private:
232 // The zeroing constants used to describe the configuration of the system.
233 const constants::PotAndAbsoluteEncoderZeroingConstants constants_;
Brian Silvermana10d20a2017-02-19 14:28:53 -0800234
Austin Schuh5f01f152017-02-11 21:34:08 -0800235 // True if the mechanism is zeroed.
236 bool zeroed_;
Brian Silvermana10d20a2017-02-19 14:28:53 -0800237 // Marker to track whether an error has occurred.
238 bool error_;
239 // The first valid offset we recorded. This is only set after zeroed_ first
240 // changes to true.
241 double first_offset_;
242
Austin Schuh0e1c2c62017-02-21 02:03:25 -0800243 // The filtered absolute encoder. This is used in the status for calibration.
244 double filtered_absolute_encoder_ = 0.0;
245
Austin Schuh5f01f152017-02-11 21:34:08 -0800246 // Samples of the offset needed to line the relative encoder up with the
247 // absolute encoder.
248 ::std::vector<double> relative_to_absolute_offset_samples_;
249 // Offset between the Pot and Relative encoder position.
250 ::std::vector<double> offset_samples_;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800251 // Last moving_buffer_size position samples to be used to determine if the
252 // robot is moving.
253 ::std::vector<PotAndAbsolutePosition> buffered_samples_;
254 // Pointer to front of the buffered samples.
255 int buffered_samples_idx_ = 0;
256 // Estimated offset between the pot and relative encoder.
257 double pot_relative_encoder_offset_ = 0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800258 // Estimated start position of the mechanism
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800259 double offset_ = 0;
Austin Schuh5f01f152017-02-11 21:34:08 -0800260 // The next position in 'relative_to_absolute_offset_samples_' and
261 // 'encoder_samples_' to be used to store the next sample.
262 int samples_idx_;
Brian Silvermana10d20a2017-02-19 14:28:53 -0800263
Austin Schuh5f01f152017-02-11 21:34:08 -0800264 // The unzeroed filtered position.
265 double filtered_position_ = 0.0;
266 // The filtered position.
267 double position_ = 0.0;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000268};
269
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000270
271// Zeros by seeing all the index pulses in the range of motion of the mechanism
272// and using that to figure out which index pulse is which.
273class PulseIndexZeroingEstimator : public ZeroingEstimator {
274 public:
275 using Position = IndexPosition;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800276 using ZeroingConstants = constants::EncoderPlusIndexZeroingConstants;
Brian Silvermanf37839c2017-02-19 18:07:15 -0800277 using State = IndexEstimatorState;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000278
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000279 PulseIndexZeroingEstimator(const ZeroingConstants &constants)
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000280 : constants_(constants) {
281 Reset();
282 }
283
284 // Resets the internal logic so it needs to be re-zeroed.
285 void Reset();
286
287 bool zeroed() const override { return zeroed_; }
288
Austin Schuh6a90cd92017-02-19 20:55:33 -0800289 // It's as ready as it'll ever be...
290 bool offset_ready() const { return true; }
291
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000292 double offset() const override { return offset_; }
293
294 bool error() const override { return error_; }
295
296 // Updates the internal logic with the next sensor values.
297 void UpdateEstimate(const IndexPosition &info);
298
Brian Silvermanf37839c2017-02-19 18:07:15 -0800299 // Returns information about our current state.
300 State GetEstimatorState() const;
301
Austin Schuhd93160a2017-03-05 01:00:54 -0800302 void TriggerError() { error_ = true; }
303
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000304 private:
305 // Returns the current real position using the relative encoder offset.
306 double CalculateCurrentPosition(const IndexPosition &info);
307
308 // Sets the minimum and maximum index pulse position values.
309 void StoreIndexPulseMaxAndMin(const IndexPosition &info);
310
311 // Returns the number of index pulses we should have seen so far.
Brian Silvermanf37839c2017-02-19 18:07:15 -0800312 int IndexPulseCount() const;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000313
314 // Contains the physical constants describing the system.
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000315 const ZeroingConstants constants_;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000316
317 // The smallest position of all the index pulses.
318 double min_index_position_;
319 // The largest position of all the index pulses.
320 double max_index_position_;
321
322 // The estimated starting position of the mechanism.
323 double offset_;
324 // After a reset we keep track of the index pulse count with this. Only after
325 // the index pulse count changes (i.e. increments at least once or wraps
326 // around) will we consider the mechanism zeroed. We also use this to store
327 // the most recent `PotAndIndexPosition::index_pulses' value when the start
328 // position was calculated. It helps us calculate the start position only on
329 // index pulses to reject corrupted intermediate data.
330 uint32_t last_used_index_pulse_count_;
331
332 // True if we are fully zeroed.
333 bool zeroed_;
334 // Marker to track whether an error has occurred.
335 bool error_;
336
337 // The estimated position.
338 double position_;
339};
340
Adam Snaiderb4119252015-02-15 01:30:57 +0000341} // namespace zeroing
342} // namespace frc971
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000343
344#endif // FRC971_ZEROING_ZEROING_H_