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 | |
| 22 | // Estimates the position with encoder, |
| 23 | // the pot and the indices. |
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: |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame^] | 26 | PotAndIndexPulseZeroingEstimator( |
| 27 | const constants::PotAndIndexPulseZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 28 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 29 | // Update the internal logic with the next sensor values. |
| 30 | void UpdateEstimate(const PotAndIndexPosition &info); |
| 31 | |
| 32 | // Reset the internal logic so it needs to be re-zeroed. |
| 33 | void Reset(); |
| 34 | |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 35 | // Manually trigger an internal error. This is used for testing the error |
| 36 | // logic. |
| 37 | void TriggerError(); |
| 38 | |
| 39 | // Returns true if an error has occurred, false otherwise. This gets reset to |
| 40 | // false when the Reset() function is called. |
| 41 | bool error() const { return error_; } |
| 42 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 43 | // Returns true if the logic considers the corresponding mechanism to be |
| 44 | // zeroed. It return false otherwise. For example, right after a call to |
| 45 | // Reset() this returns false. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 46 | bool zeroed() const { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 47 | |
| 48 | // Return the estimated position of the corresponding mechanism. This value |
| 49 | // is in SI units. For example, the estimator for the elevator would return a |
| 50 | // value in meters for the height relative to absolute zero. |
| 51 | double position() const { return pos_; } |
| 52 | |
| 53 | // Return the estimated starting position of the corresponding mechansim. In |
| 54 | // some contexts we refer to this as the "offset". |
| 55 | double offset() const { return start_pos_; } |
| 56 | |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 57 | // Return the estimated position of the corresponding mechanism not using the |
| 58 | // index pulse, even if one is available. |
| 59 | double filtered_position() const { return filtered_position_; } |
| 60 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 61 | // Returns a number between 0 and 1 that represents the percentage of the |
| 62 | // samples being used in the moving average filter. A value of 0.0 means that |
| 63 | // no samples are being used. A value of 1.0 means that the filter is using |
| 64 | // as many samples as it has room for. For example, after a Reset() this |
| 65 | // value returns 0.0. As more samples get added with UpdateEstimate(...) the |
| 66 | // return value starts increasing to 1.0. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 67 | double offset_ratio_ready() const { |
| 68 | return start_pos_samples_.size() / static_cast<double>(max_sample_count_); |
| 69 | } |
| 70 | |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 71 | // Returns true if the sample buffer is full. |
| 72 | bool offset_ready() const { |
| 73 | return start_pos_samples_.size() == max_sample_count_; |
| 74 | } |
| 75 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 76 | private: |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 77 | // This function calculates the start position given the internal state and |
| 78 | // the provided `latched_encoder' value. |
| 79 | double CalculateStartPosition(double start_average, |
| 80 | double latched_encoder) const; |
| 81 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 82 | // The estimated position. |
| 83 | double pos_; |
Austin Schuh | be133ed | 2016-03-11 21:23:34 -0800 | [diff] [blame] | 84 | // The unzeroed filtered position. |
| 85 | double filtered_position_ = 0.0; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 86 | // The distance between two consecutive index positions. |
| 87 | double index_diff_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 88 | // The next position in 'start_pos_samples_' to be used to store the next |
| 89 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 90 | int samples_idx_; |
| 91 | // Last 'max_sample_count_' samples for start positions. |
| 92 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 93 | // The number of the last samples of start position to consider in the |
| 94 | // estimation. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 95 | size_t max_sample_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 96 | // The estimated starting position of the mechanism. We also call this the |
| 97 | // 'offset' in some contexts. |
| 98 | double start_pos_; |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 99 | // The absolute position of any index pulse on the mechanism. This is used to |
| 100 | // account for the various ways the encoders get mounted into the robot. |
| 101 | double known_index_pos_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 102 | // 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] | 103 | // after a reset. See `last_used_index_pulse_count_'. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 104 | bool wait_for_index_pulse_; |
| 105 | // After a reset we keep track of the index pulse count with this. Only after |
| 106 | // 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] | 107 | // around) will we consider the mechanism zeroed. We also use this to store |
| 108 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 109 | // position was calculated. It helps us calculate the start position only on |
| 110 | // index pulses to reject corrupted intermediate data. |
| 111 | uint32_t last_used_index_pulse_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 112 | // Marker to track whether we're fully zeroed yet or not. |
| 113 | bool zeroed_; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 114 | // Marker to track whether an error has occurred. This gets reset to false |
| 115 | // whenever Reset() is called. |
| 116 | bool error_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 117 | // Stores the position "start_pos" variable the first time the program |
| 118 | // is zeroed. |
| 119 | double first_start_pos_; |
Brian Silverman | 852824a | 2016-05-15 23:03:01 -0700 | [diff] [blame] | 120 | // The fraction of index_diff (possibly greater than 1) after which an error |
| 121 | // is reported. |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 122 | double allowable_encoder_error_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 125 | // Populates an EstimatorState struct with information from the zeroing |
| 126 | // estimator. |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame^] | 127 | void PopulateEstimatorState(const PotAndIndexPulseZeroingEstimator &estimator, |
Daniel Petti | ab27423 | 2015-02-16 19:15:34 -0800 | [diff] [blame] | 128 | EstimatorState *state); |
| 129 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 130 | } // namespace zeroing |
| 131 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 132 | |
| 133 | #endif // FRC971_ZEROING_ZEROING_H_ |