blob: 4d36ca90ade8e832c49631441f0e25ec7720a766 [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
Brian Silvermanab0b6772017-02-05 16:16:21 -080022// Estimates the position with an incremental encoder with an index pulse and a
23// potentiometer.
Tyler Chatowf8f03112017-02-05 14:31:34 -080024class PotAndIndexPulseZeroingEstimator {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000025 public:
Brian Silvermanab0b6772017-02-05 16:16:21 -080026 using Position = PotAndIndexPosition;
27 using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants;
Austin Schuh5f01f152017-02-11 21:34:08 -080028 using State = EstimatorState;
Brian Silvermanab0b6772017-02-05 16:16:21 -080029
Tyler Chatowf8f03112017-02-05 14:31:34 -080030 PotAndIndexPulseZeroingEstimator(
31 const constants::PotAndIndexPulseZeroingConstants &constants);
Austin Schuh703b8d42015-02-01 14:56:34 -080032
Adam Snaiderb4119252015-02-15 01:30:57 +000033 // Update the internal logic with the next sensor values.
34 void UpdateEstimate(const PotAndIndexPosition &info);
35
36 // Reset the internal logic so it needs to be re-zeroed.
37 void Reset();
38
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000039 // Manually trigger an internal error. This is used for testing the error
40 // logic.
41 void TriggerError();
42
43 // Returns true if an error has occurred, false otherwise. This gets reset to
44 // false when the Reset() function is called.
45 bool error() const { return error_; }
46
Adam Snaiderb4119252015-02-15 01:30:57 +000047 // Returns true if the logic considers the corresponding mechanism to be
Austin Schuh5f01f152017-02-11 21:34:08 -080048 // zeroed. It return false otherwise.
Austin Schuh703b8d42015-02-01 14:56:34 -080049 bool zeroed() const { return zeroed_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000050
51 // Return the estimated position of the corresponding mechanism. This value
52 // is in SI units. For example, the estimator for the elevator would return a
53 // value in meters for the height relative to absolute zero.
Austin Schuh5f01f152017-02-11 21:34:08 -080054 double position() const { return position_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000055
56 // Return the estimated starting position of the corresponding mechansim. In
57 // some contexts we refer to this as the "offset".
58 double offset() const { return start_pos_; }
59
Austin Schuhbe133ed2016-03-11 21:23:34 -080060 // Return the estimated position of the corresponding mechanism not using the
61 // index pulse, even if one is available.
62 double filtered_position() const { return filtered_position_; }
63
Adam Snaiderb4119252015-02-15 01:30:57 +000064 // 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
Adam Snaiderc4b3c192015-02-01 01:30:39 +000080 private:
Philipp Schradere828be72015-02-15 07:07:37 +000081 // This function calculates the start position given the internal state and
82 // the provided `latched_encoder' value.
83 double CalculateStartPosition(double start_average,
84 double latched_encoder) const;
85
Austin Schuh5f01f152017-02-11 21:34:08 -080086 // The zeroing constants used to describe the configuration of the system.
87 const constants::PotAndIndexPulseZeroingConstants constants_;
88
Adam Snaiderb4119252015-02-15 01:30:57 +000089 // The estimated position.
Austin Schuh5f01f152017-02-11 21:34:08 -080090 double position_;
Austin Schuhbe133ed2016-03-11 21:23:34 -080091 // The unzeroed filtered position.
92 double filtered_position_ = 0.0;
Adam Snaiderb4119252015-02-15 01:30:57 +000093 // The next position in 'start_pos_samples_' to be used to store the next
94 // sample.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000095 int samples_idx_;
96 // Last 'max_sample_count_' samples for start positions.
97 std::vector<double> start_pos_samples_;
Adam Snaiderb4119252015-02-15 01:30:57 +000098 // The estimated starting position of the mechanism. We also call this the
99 // 'offset' in some contexts.
100 double start_pos_;
Philipp Schrader41d82912015-02-15 03:44:23 +0000101 // Flag for triggering logic that takes note of the current index pulse count
Philipp Schradere828be72015-02-15 07:07:37 +0000102 // after a reset. See `last_used_index_pulse_count_'.
Philipp Schrader41d82912015-02-15 03:44:23 +0000103 bool wait_for_index_pulse_;
104 // After a reset we keep track of the index pulse count with this. Only after
105 // the index pulse count changes (i.e. increments at least once or wraps
Philipp Schradere828be72015-02-15 07:07:37 +0000106 // around) will we consider the mechanism zeroed. We also use this to store
107 // the most recent `PotAndIndexPosition::index_pulses' value when the start
108 // position was calculated. It helps us calculate the start position only on
109 // index pulses to reject corrupted intermediate data.
110 uint32_t last_used_index_pulse_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000111 // Marker to track whether we're fully zeroed yet or not.
112 bool zeroed_;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000113 // Marker to track whether an error has occurred. This gets reset to false
114 // whenever Reset() is called.
115 bool error_;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000116 // Stores the position "start_pos" variable the first time the program
117 // is zeroed.
118 double first_start_pos_;
Austin Schuh5f01f152017-02-11 21:34:08 -0800119};
120
121// Estimates the position with an absolute encoder which also reports
122// incremental counts, and a potentiometer.
123class PotAndAbsEncoderZeroingEstimator {
124 public:
125 using Position = PotAndAbsolutePosition;
126 using ZeroingConstants = constants::PotAndAbsoluteEncoderZeroingConstants;
127 using State = AbsoluteEstimatorState;
128
129 PotAndAbsEncoderZeroingEstimator(
130 const constants::PotAndAbsoluteEncoderZeroingConstants &constants);
131
132 // Resets the internal logic so it needs to be re-zeroed.
133 void Reset();
134
135 // Updates the sensor values for the zeroing logic.
136 void UpdateEstimate(const PotAndAbsolutePosition &info);
137
138 // Returns true if the mechanism is zeroed, and false if it isn't.
139 bool zeroed() const { return zeroed_; }
140
141 // Return the estimated position of the corresponding mechanism. This value
142 // is in SI units. For example, the estimator for the elevator would return a
143 // value in meters for the height relative to absolute zero.
144 double position() const { return position_; }
145
146 // Return the estimated starting position of the corresponding mechansim. In
147 // some contexts we refer to this as the "offset".
148 double offset() const { return offset_; }
149
150 // Returns true if an error has occurred, false otherwise. This gets reset to
151 // false when the Reset() function is called.
152 // TODO(austin): Actually implement this.
153 bool error() const { return false; }
154
155 // Returns true if the sample buffer is full.
156 bool offset_ready() const {
157 return relative_to_absolute_offset_samples_.size() ==
158 constants_.average_filter_size &&
159 offset_samples_.size() == constants_.average_filter_size;
160 }
161
162 // Return the estimated position of the corresponding mechanism not using the
163 // index pulse, even if one is available.
164 double filtered_position() const { return filtered_position_; }
165
166 private:
167 // The zeroing constants used to describe the configuration of the system.
168 const constants::PotAndAbsoluteEncoderZeroingConstants constants_;
169 // True if the mechanism is zeroed.
170 bool zeroed_;
171 // Samples of the offset needed to line the relative encoder up with the
172 // absolute encoder.
173 ::std::vector<double> relative_to_absolute_offset_samples_;
174 // Offset between the Pot and Relative encoder position.
175 ::std::vector<double> offset_samples_;
176 // Estimated start position of the mechanism
177 double offset_;
178 // The next position in 'relative_to_absolute_offset_samples_' and
179 // 'encoder_samples_' to be used to store the next sample.
180 int samples_idx_;
181 // The unzeroed filtered position.
182 double filtered_position_ = 0.0;
183 // The filtered position.
184 double position_ = 0.0;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000185};
186
Daniel Pettiab274232015-02-16 19:15:34 -0800187// Populates an EstimatorState struct with information from the zeroing
188// estimator.
Tyler Chatowf8f03112017-02-05 14:31:34 -0800189void PopulateEstimatorState(const PotAndIndexPulseZeroingEstimator &estimator,
Daniel Pettiab274232015-02-16 19:15:34 -0800190 EstimatorState *state);
191
Austin Schuh5f01f152017-02-11 21:34:08 -0800192void PopulateEstimatorState(const PotAndAbsEncoderZeroingEstimator &estimator,
193 AbsoluteEstimatorState *state);
194
Adam Snaiderb4119252015-02-15 01:30:57 +0000195} // namespace zeroing
196} // namespace frc971
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000197
198#endif // FRC971_ZEROING_ZEROING_H_