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 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 22 | // Estimates the position with an incremental encoder with an index pulse and a |
| 23 | // potentiometer. |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 24 | class PotAndIndexPulseZeroingEstimator { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 25 | public: |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 26 | using Position = PotAndIndexPosition; |
| 27 | using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 28 | using State = EstimatorState; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 29 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 30 | PotAndIndexPulseZeroingEstimator( |
| 31 | const constants::PotAndIndexPulseZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 32 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 33 | // 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 Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 39 | // 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 Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 47 | // Returns true if the logic considers the corresponding mechanism to be |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 48 | // zeroed. It return false otherwise. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 49 | bool zeroed() const { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 50 | |
| 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 Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 54 | double position() const { return position_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 55 | |
| 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 Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 60 | // 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 Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 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 Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 70 | double offset_ratio_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 71 | return start_pos_samples_.size() / |
| 72 | static_cast<double>(constants_.average_filter_size); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 75 | // Returns true if the sample buffer is full. |
| 76 | bool offset_ready() const { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 77 | return start_pos_samples_.size() == constants_.average_filter_size; |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 80 | private: |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 81 | // 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 Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 86 | // The zeroing constants used to describe the configuration of the system. |
| 87 | const constants::PotAndIndexPulseZeroingConstants constants_; |
| 88 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 89 | // The estimated position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 90 | double position_; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 91 | // The unzeroed filtered position. |
| 92 | double filtered_position_ = 0.0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 93 | // The next position in 'start_pos_samples_' to be used to store the next |
| 94 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 95 | int samples_idx_; |
| 96 | // Last 'max_sample_count_' samples for start positions. |
| 97 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 98 | // The estimated starting position of the mechanism. We also call this the |
| 99 | // 'offset' in some contexts. |
| 100 | double start_pos_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 101 | // 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] | 102 | // after a reset. See `last_used_index_pulse_count_'. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 103 | 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 Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 106 | // 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 Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 111 | // Marker to track whether we're fully zeroed yet or not. |
| 112 | bool zeroed_; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 113 | // Marker to track whether an error has occurred. This gets reset to false |
| 114 | // whenever Reset() is called. |
| 115 | bool error_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 116 | // Stores the position "start_pos" variable the first time the program |
| 117 | // is zeroed. |
| 118 | double first_start_pos_; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 119 | }; |
| 120 | |
| 121 | // Estimates the position with an absolute encoder which also reports |
| 122 | // incremental counts, and a potentiometer. |
| 123 | class 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 Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 187 | // Populates an EstimatorState struct with information from the zeroing |
| 188 | // estimator. |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 189 | void PopulateEstimatorState(const PotAndIndexPulseZeroingEstimator &estimator, |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 190 | EstimatorState *state); |
| 191 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 192 | void PopulateEstimatorState(const PotAndAbsEncoderZeroingEstimator &estimator, |
| 193 | AbsoluteEstimatorState *state); |
| 194 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 195 | } // namespace zeroing |
| 196 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 197 | |
| 198 | #endif // FRC971_ZEROING_ZEROING_H_ |