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): Support the ZeroingConstants::measured_index_position |
| 11 | // parameter. |
| 12 | // |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 13 | // TODO(pschrader): Create an error API to flag faults/errors etc.. |
| 14 | // |
| 15 | // TODO(pschrader): Flag an error if encoder index pulse is not n revolutions |
| 16 | // away from the last one (i.e. got extra counts from noise, etc..) |
| 17 | // |
| 18 | // TODO(pschrader): Flag error if the pot disagrees too much with the encoder |
| 19 | // after being zeroed. |
| 20 | // |
| 21 | // TODO(pschrader): Watch the offset over long periods of time and flag if it |
| 22 | // gets too far away from the initial value. |
| 23 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 24 | namespace frc971 { |
| 25 | namespace zeroing { |
| 26 | |
| 27 | // Estimates the position with encoder, |
| 28 | // the pot and the indices. |
| 29 | class ZeroingEstimator { |
| 30 | public: |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 31 | ZeroingEstimator(const constants::Values::ZeroingConstants &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 | |
| 39 | // Returns true if the logic considers the corresponding mechanism to be |
| 40 | // zeroed. It return false otherwise. For example, right after a call to |
| 41 | // Reset() this returns false. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 42 | bool zeroed() const { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 43 | |
| 44 | // Return the estimated position of the corresponding mechanism. This value |
| 45 | // is in SI units. For example, the estimator for the elevator would return a |
| 46 | // value in meters for the height relative to absolute zero. |
| 47 | double position() const { return pos_; } |
| 48 | |
| 49 | // Return the estimated starting position of the corresponding mechansim. In |
| 50 | // some contexts we refer to this as the "offset". |
| 51 | double offset() const { return start_pos_; } |
| 52 | |
| 53 | // Returns a number between 0 and 1 that represents the percentage of the |
| 54 | // samples being used in the moving average filter. A value of 0.0 means that |
| 55 | // no samples are being used. A value of 1.0 means that the filter is using |
| 56 | // as many samples as it has room for. For example, after a Reset() this |
| 57 | // value returns 0.0. As more samples get added with UpdateEstimate(...) the |
| 58 | // return value starts increasing to 1.0. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 59 | double offset_ratio_ready() const { |
| 60 | return start_pos_samples_.size() / static_cast<double>(max_sample_count_); |
| 61 | } |
| 62 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 63 | private: |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 64 | // The estimated position. |
| 65 | double pos_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 66 | // The distance between two consecutive index positions. |
| 67 | double index_diff_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 68 | // The next position in 'start_pos_samples_' to be used to store the next |
| 69 | // sample. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 70 | int samples_idx_; |
| 71 | // Last 'max_sample_count_' samples for start positions. |
| 72 | std::vector<double> start_pos_samples_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 73 | // The number of the last samples of start position to consider in the |
| 74 | // estimation. |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 75 | size_t max_sample_count_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 76 | // The estimated starting position of the mechanism. We also call this the |
| 77 | // 'offset' in some contexts. |
| 78 | double start_pos_; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame^] | 79 | // Flag for triggering logic that takes note of the current index pulse count |
| 80 | // after a reset. See `index_pulse_count_after_reset_'. |
| 81 | bool wait_for_index_pulse_; |
| 82 | // After a reset we keep track of the index pulse count with this. Only after |
| 83 | // the index pulse count changes (i.e. increments at least once or wraps |
| 84 | // around) will we consider the mechanism zeroed. |
| 85 | uint32_t index_pulse_count_after_reset_; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 86 | // Marker to track whether we're fully zeroed yet or not. |
| 87 | bool zeroed_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 90 | } // namespace zeroing |
| 91 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 92 | |
| 93 | #endif // FRC971_ZEROING_ZEROING_H_ |