blob: 1872ad06949c27b63fa36765cde18656213c4972 [file] [log] [blame]
Adam Snaiderc4b3c192015-02-01 01:30:39 +00001#ifndef FRC971_ZEROING_ZEROING_H_
2#define FRC971_ZEROING_ZEROING_H_
3
Philipp Schrader41d82912015-02-15 03:44:23 +00004#include <cstdint>
Adam Snaiderc4b3c192015-02-01 01:30:39 +00005#include <vector>
Philipp Schrader41d82912015-02-15 03:44:23 +00006
Austin Schuh703b8d42015-02-01 14:56:34 -08007#include "frc971/control_loops/control_loops.q.h"
8#include "frc971/constants.h"
Adam Snaiderc4b3c192015-02-01 01:30:39 +00009
Adam Snaiderb4119252015-02-15 01:30:57 +000010// 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 Snaiderc4b3c192015-02-01 01:30:39 +000019namespace frc971 {
20namespace zeroing {
21
Brian Silvermanab0b6772017-02-05 16:16:21 -080022// Estimates the position with an incremental encoder with an index pulse and a
23// potentiometer.
Tyler Chatowf8f03112017-02-05 14:31:34 -080024class PotAndIndexPulseZeroingEstimator {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000025 public:
Brian Silvermanab0b6772017-02-05 16:16:21 -080026 using Position = PotAndIndexPosition;
27 using ZeroingConstants = constants::PotAndIndexPulseZeroingConstants;
28
Tyler Chatowf8f03112017-02-05 14:31:34 -080029 PotAndIndexPulseZeroingEstimator(
30 const constants::PotAndIndexPulseZeroingConstants &constants);
Austin Schuh703b8d42015-02-01 14:56:34 -080031
Adam Snaiderb4119252015-02-15 01:30:57 +000032 // Update the internal logic with the next sensor values.
33 void UpdateEstimate(const PotAndIndexPosition &info);
34
35 // Reset the internal logic so it needs to be re-zeroed.
36 void Reset();
37
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000038 // Manually trigger an internal error. This is used for testing the error
39 // logic.
40 void TriggerError();
41
42 // Returns true if an error has occurred, false otherwise. This gets reset to
43 // false when the Reset() function is called.
44 bool error() const { return error_; }
45
Adam Snaiderb4119252015-02-15 01:30:57 +000046 // Returns true if the logic considers the corresponding mechanism to be
47 // zeroed. It return false otherwise. For example, right after a call to
48 // Reset() this returns false.
Austin Schuh703b8d42015-02-01 14:56:34 -080049 bool zeroed() const { return zeroed_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000050
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.
54 double position() const { return pos_; }
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 Schuhbe133ed2016-03-11 21:23:34 -080060 // 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 Snaiderb4119252015-02-15 01:30:57 +000064 // 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 Schuh703b8d42015-02-01 14:56:34 -080070 double offset_ratio_ready() const {
71 return start_pos_samples_.size() / static_cast<double>(max_sample_count_);
72 }
73
Austin Schuh7485dbb2016-02-08 00:21:58 -080074 // Returns true if the sample buffer is full.
75 bool offset_ready() const {
76 return start_pos_samples_.size() == max_sample_count_;
77 }
78
Adam Snaiderc4b3c192015-02-01 01:30:39 +000079 private:
Philipp Schradere828be72015-02-15 07:07:37 +000080 // This function calculates the start position given the internal state and
81 // the provided `latched_encoder' value.
82 double CalculateStartPosition(double start_average,
83 double latched_encoder) const;
84
Adam Snaiderb4119252015-02-15 01:30:57 +000085 // The estimated position.
86 double pos_;
Austin Schuhbe133ed2016-03-11 21:23:34 -080087 // The unzeroed filtered position.
88 double filtered_position_ = 0.0;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000089 // The distance between two consecutive index positions.
90 double index_diff_;
Adam Snaiderb4119252015-02-15 01:30:57 +000091 // The next position in 'start_pos_samples_' to be used to store the next
92 // sample.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000093 int samples_idx_;
94 // Last 'max_sample_count_' samples for start positions.
95 std::vector<double> start_pos_samples_;
Adam Snaiderb4119252015-02-15 01:30:57 +000096 // The number of the last samples of start position to consider in the
97 // estimation.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000098 size_t max_sample_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +000099 // The estimated starting position of the mechanism. We also call this the
100 // 'offset' in some contexts.
101 double start_pos_;
Philipp Schrader030ad182015-02-15 05:40:58 +0000102 // The absolute position of any index pulse on the mechanism. This is used to
103 // account for the various ways the encoders get mounted into the robot.
104 double known_index_pos_;
Philipp Schrader41d82912015-02-15 03:44:23 +0000105 // Flag for triggering logic that takes note of the current index pulse count
Philipp Schradere828be72015-02-15 07:07:37 +0000106 // after a reset. See `last_used_index_pulse_count_'.
Philipp Schrader41d82912015-02-15 03:44:23 +0000107 bool wait_for_index_pulse_;
108 // After a reset we keep track of the index pulse count with this. Only after
109 // the index pulse count changes (i.e. increments at least once or wraps
Philipp Schradere828be72015-02-15 07:07:37 +0000110 // around) will we consider the mechanism zeroed. We also use this to store
111 // the most recent `PotAndIndexPosition::index_pulses' value when the start
112 // position was calculated. It helps us calculate the start position only on
113 // index pulses to reject corrupted intermediate data.
114 uint32_t last_used_index_pulse_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000115 // Marker to track whether we're fully zeroed yet or not.
116 bool zeroed_;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000117 // Marker to track whether an error has occurred. This gets reset to false
118 // whenever Reset() is called.
119 bool error_;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000120 // Stores the position "start_pos" variable the first time the program
121 // is zeroed.
122 double first_start_pos_;
Brian Silverman852824a2016-05-15 23:03:01 -0700123 // The fraction of index_diff (possibly greater than 1) after which an error
124 // is reported.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000125 double allowable_encoder_error_;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000126};
127
Daniel Pettiab274232015-02-16 19:15:34 -0800128// Populates an EstimatorState struct with information from the zeroing
129// estimator.
Tyler Chatowf8f03112017-02-05 14:31:34 -0800130void PopulateEstimatorState(const PotAndIndexPulseZeroingEstimator &estimator,
Daniel Pettiab274232015-02-16 19:15:34 -0800131 EstimatorState *state);
132
Adam Snaiderb4119252015-02-15 01:30:57 +0000133} // namespace zeroing
134} // namespace frc971
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000135
136#endif // FRC971_ZEROING_ZEROING_H_