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): Create an error API to flag faults/errors etc.. |
| 11 | // |
| 12 | // TODO(pschrader): Flag an error if encoder index pulse is not n revolutions |
| 13 | // away from the last one (i.e. got extra counts from noise, etc..) |
| 14 | // |
| 15 | // TODO(pschrader): Flag error if the pot disagrees too much with the encoder |
| 16 | // after being zeroed. |
| 17 | // |
| 18 | // TODO(pschrader): Watch the offset over long periods of time and flag if it |
| 19 | // gets too far away from the initial value. |
| 20 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 21 | namespace frc971 { |
| 22 | namespace zeroing { |
| 23 | |
| 24 | // Estimates the position with encoder, |
| 25 | // the pot and the indices. |
| 26 | class ZeroingEstimator { |
| 27 | public: |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 28 | ZeroingEstimator(const constants::Values::ZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 29 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 30 | // Update the internal logic with the next sensor values. |
| 31 | void UpdateEstimate(const PotAndIndexPosition &info); |
| 32 | |
| 33 | // Reset the internal logic so it needs to be re-zeroed. |
| 34 | void Reset(); |
| 35 | |
| 36 | // Returns true if the logic considers the corresponding mechanism to be |
| 37 | // zeroed. It return false otherwise. For example, right after a call to |
| 38 | // Reset() this returns false. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 39 | bool zeroed() const { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 40 | |
| 41 | // Return the estimated position of the corresponding mechanism. This value |
| 42 | // is in SI units. For example, the estimator for the elevator would return a |
| 43 | // value in meters for the height relative to absolute zero. |
| 44 | double position() const { return pos_; } |
| 45 | |
| 46 | // Return the estimated starting position of the corresponding mechansim. In |
| 47 | // some contexts we refer to this as the "offset". |
| 48 | double offset() const { return start_pos_; } |
| 49 | |
| 50 | // Returns a number between 0 and 1 that represents the percentage of the |
| 51 | // samples being used in the moving average filter. A value of 0.0 means that |
| 52 | // no samples are being used. A value of 1.0 means that the filter is using |
| 53 | // as many samples as it has room for. For example, after a Reset() this |
| 54 | // value returns 0.0. As more samples get added with UpdateEstimate(...) the |
| 55 | // return value starts increasing to 1.0. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 56 | double offset_ratio_ready() const { |
| 57 | return start_pos_samples_.size() / static_cast<double>(max_sample_count_); |
| 58 | } |
| 59 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 60 | private: |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 61 | // This function calculates the start position given the internal state and |
| 62 | // the provided `latched_encoder' value. |
| 63 | double CalculateStartPosition(double start_average, |
| 64 | double latched_encoder) const; |
| 65 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 66 | // The estimated position. |
| 67 | double pos_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 68 | // The distance between two consecutive index positions. |
| 69 | double index_diff_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 70 | // The next position in 'start_pos_samples_' to be used to store the next |
| 71 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 72 | int samples_idx_; |
| 73 | // Last 'max_sample_count_' samples for start positions. |
| 74 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 75 | // The number of the last samples of start position to consider in the |
| 76 | // estimation. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 77 | size_t max_sample_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 78 | // The estimated starting position of the mechanism. We also call this the |
| 79 | // 'offset' in some contexts. |
| 80 | double start_pos_; |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 81 | // The absolute position of any index pulse on the mechanism. This is used to |
| 82 | // account for the various ways the encoders get mounted into the robot. |
| 83 | double known_index_pos_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 84 | // 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] | 85 | // after a reset. See `last_used_index_pulse_count_'. |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 86 | bool wait_for_index_pulse_; |
| 87 | // After a reset we keep track of the index pulse count with this. Only after |
| 88 | // 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] | 89 | // around) will we consider the mechanism zeroed. We also use this to store |
| 90 | // the most recent `PotAndIndexPosition::index_pulses' value when the start |
| 91 | // position was calculated. It helps us calculate the start position only on |
| 92 | // index pulses to reject corrupted intermediate data. |
| 93 | uint32_t last_used_index_pulse_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 94 | // Marker to track whether we're fully zeroed yet or not. |
| 95 | bool zeroed_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 98 | } // namespace zeroing |
| 99 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 100 | |
| 101 | #endif // FRC971_ZEROING_ZEROING_H_ |