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 | |
| 4 | #include <vector> |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 5 | #include "frc971/control_loops/control_loops.q.h" |
| 6 | #include "frc971/constants.h" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 7 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame^] | 8 | // TODO(pschrader): Support the ZeroingConstants::measured_index_position |
| 9 | // parameter. |
| 10 | // |
| 11 | // TODO(pschrader): Wait for an index pulse during zeroing. If we start up with |
| 12 | // a non-zero number of index pulses then the logic will use that pulse to |
| 13 | // compute the starting position (a/k/a the offset). |
| 14 | // |
| 15 | // TODO(pschrader): Create an error API to flag faults/errors etc.. |
| 16 | // |
| 17 | // TODO(pschrader): Flag an error if encoder index pulse is not n revolutions |
| 18 | // away from the last one (i.e. got extra counts from noise, etc..) |
| 19 | // |
| 20 | // TODO(pschrader): Flag error if the pot disagrees too much with the encoder |
| 21 | // after being zeroed. |
| 22 | // |
| 23 | // TODO(pschrader): Watch the offset over long periods of time and flag if it |
| 24 | // gets too far away from the initial value. |
| 25 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 26 | namespace frc971 { |
| 27 | namespace zeroing { |
| 28 | |
| 29 | // Estimates the position with encoder, |
| 30 | // the pot and the indices. |
| 31 | class ZeroingEstimator { |
| 32 | public: |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 33 | ZeroingEstimator(const constants::Values::ZeroingConstants &constants); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 34 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame^] | 35 | // Update the internal logic with the next sensor values. |
| 36 | void UpdateEstimate(const PotAndIndexPosition &info); |
| 37 | |
| 38 | // Reset the internal logic so it needs to be re-zeroed. |
| 39 | void Reset(); |
| 40 | |
| 41 | // Returns true if the logic considers the corresponding mechanism to be |
| 42 | // zeroed. It return false otherwise. For example, right after a call to |
| 43 | // Reset() this returns false. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 44 | bool zeroed() const { return zeroed_; } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame^] | 45 | |
| 46 | // Return the estimated position of the corresponding mechanism. This value |
| 47 | // is in SI units. For example, the estimator for the elevator would return a |
| 48 | // value in meters for the height relative to absolute zero. |
| 49 | double position() const { return pos_; } |
| 50 | |
| 51 | // Return the estimated starting position of the corresponding mechansim. In |
| 52 | // some contexts we refer to this as the "offset". |
| 53 | double offset() const { return start_pos_; } |
| 54 | |
| 55 | // Returns a number between 0 and 1 that represents the percentage of the |
| 56 | // samples being used in the moving average filter. A value of 0.0 means that |
| 57 | // no samples are being used. A value of 1.0 means that the filter is using |
| 58 | // as many samples as it has room for. For example, after a Reset() this |
| 59 | // value returns 0.0. As more samples get added with UpdateEstimate(...) the |
| 60 | // return value starts increasing to 1.0. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 61 | double offset_ratio_ready() const { |
| 62 | return start_pos_samples_.size() / static_cast<double>(max_sample_count_); |
| 63 | } |
| 64 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 65 | private: |
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_; |
| 81 | // Marker to track whether we're fully zeroed yet or not. |
| 82 | bool zeroed_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame^] | 85 | } // namespace zeroing |
| 86 | } // namespace frc971 |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 87 | |
| 88 | #endif // FRC971_ZEROING_ZEROING_H_ |