Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 1 | #ifndef FRC971_ZEROING_ZEROING_H_ |
| 2 | #define FRC971_ZEROING_ZEROING_H_ |
| 3 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 4 | #include <cstdint> |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 5 | #include <vector> |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 6 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/control_loops.q.h" |
| 8 | #include "frc971/constants.h" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 9 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 10 | // 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 Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 19 | namespace frc971 { |
| 20 | namespace zeroing { |
| 21 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 22 | class 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 Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 45 | // Estimates the position with an incremental encoder with an index pulse and a |
| 46 | // potentiometer. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 47 | class PotAndIndexPulseZeroingEstimator : public ZeroingEstimator { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 48 | public: |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 49 | using Position = PotAndIndexPosition; |
| 50 | using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 51 | using State = EstimatorState; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 52 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 53 | PotAndIndexPulseZeroingEstimator( |
| 54 | const constants::PotAndIndexPulseZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 55 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 56 | // 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 Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 62 | // Manually trigger an internal error. This is used for testing the error |
| 63 | // logic. |
| 64 | void TriggerError(); |
| 65 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 66 | bool error() const override { return error_; } |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 67 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 68 | bool zeroed() const override { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 69 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 70 | double position() const override { return position_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 71 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 72 | double offset() const override { return start_pos_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 73 | |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 74 | // 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 Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 78 | // 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 Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 84 | double offset_ratio_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 85 | return start_pos_samples_.size() / |
| 86 | static_cast<double>(constants_.average_filter_size); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 89 | // Returns true if the sample buffer is full. |
| 90 | bool offset_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 91 | return start_pos_samples_.size() == constants_.average_filter_size; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 94 | private: |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 95 | // 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 Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 100 | // The zeroing constants used to describe the configuration of the system. |
| 101 | const constants::PotAndIndexPulseZeroingConstants constants_; |
| 102 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 103 | // The estimated position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 104 | double position_; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 105 | // The unzeroed filtered position. |
| 106 | double filtered_position_ = 0.0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 107 | // The next position in 'start_pos_samples_' to be used to store the next |
| 108 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 109 | int samples_idx_; |
| 110 | // Last 'max_sample_count_' samples for start positions. |
| 111 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 112 | // The estimated starting position of the mechanism. We also call this the |
| 113 | // 'offset' in some contexts. |
| 114 | double start_pos_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 115 | // Flag for triggering logic that takes note of the current index pulse count |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 116 | // after a reset. See `last_used_index_pulse_count_'. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 117 | 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 Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 120 | // 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 Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 125 | // Marker to track whether we're fully zeroed yet or not. |
| 126 | bool zeroed_; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 127 | // Marker to track whether an error has occurred. This gets reset to false |
| 128 | // whenever Reset() is called. |
| 129 | bool error_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 130 | // Stores the position "start_pos" variable the first time the program |
| 131 | // is zeroed. |
| 132 | double first_start_pos_; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | // Estimates the position with an absolute encoder which also reports |
| 136 | // incremental counts, and a potentiometer. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 137 | class PotAndAbsEncoderZeroingEstimator : public ZeroingEstimator { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 138 | 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 Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 152 | bool zeroed() const override { return zeroed_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 153 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 154 | double position() const override { return position_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 155 | |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 156 | double offset() const override { return offset_; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 157 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 158 | // TODO(austin): Actually implement this. |
Neil Balch | 1049be9 | 2017-02-15 23:20:49 -0800 | [diff] [blame^] | 159 | bool error() const override { return false; } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 160 | |
| 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 Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 193 | // Populates an EstimatorState struct with information from the zeroing |
| 194 | // estimator. |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 195 | void PopulateEstimatorState(const PotAndIndexPulseZeroingEstimator &estimator, |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 196 | EstimatorState *state); |
| 197 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 198 | void PopulateEstimatorState(const PotAndAbsEncoderZeroingEstimator &estimator, |
| 199 | AbsoluteEstimatorState *state); |
| 200 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 201 | } // namespace zeroing |
| 202 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 203 | |
| 204 | #endif // FRC971_ZEROING_ZEROING_H_ |