blob: 7fa2262e2e8beb1a902ebf7269377b6785e688a1 [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
22// Estimates the position with encoder,
23// the pot and the indices.
24class ZeroingEstimator {
25 public:
Austin Schuh703b8d42015-02-01 14:56:34 -080026 ZeroingEstimator(const constants::Values::ZeroingConstants &constants);
Austin Schuh703b8d42015-02-01 14:56:34 -080027
Adam Snaiderb4119252015-02-15 01:30:57 +000028 // Update the internal logic with the next sensor values.
29 void UpdateEstimate(const PotAndIndexPosition &info);
30
31 // Reset the internal logic so it needs to be re-zeroed.
32 void Reset();
33
Philipp Schrader53f4b6d2015-02-15 22:32:08 +000034 // Manually trigger an internal error. This is used for testing the error
35 // logic.
36 void TriggerError();
37
38 // Returns true if an error has occurred, false otherwise. This gets reset to
39 // false when the Reset() function is called.
40 bool error() const { return error_; }
41
Adam Snaiderb4119252015-02-15 01:30:57 +000042 // Returns true if the logic considers the corresponding mechanism to be
43 // zeroed. It return false otherwise. For example, right after a call to
44 // Reset() this returns false.
Austin Schuh703b8d42015-02-01 14:56:34 -080045 bool zeroed() const { return zeroed_; }
Adam Snaiderb4119252015-02-15 01:30:57 +000046
47 // Return the estimated position of the corresponding mechanism. This value
48 // is in SI units. For example, the estimator for the elevator would return a
49 // value in meters for the height relative to absolute zero.
50 double position() const { return pos_; }
51
52 // Return the estimated starting position of the corresponding mechansim. In
53 // some contexts we refer to this as the "offset".
54 double offset() const { return start_pos_; }
55
56 // Returns a number between 0 and 1 that represents the percentage of the
57 // samples being used in the moving average filter. A value of 0.0 means that
58 // no samples are being used. A value of 1.0 means that the filter is using
59 // as many samples as it has room for. For example, after a Reset() this
60 // value returns 0.0. As more samples get added with UpdateEstimate(...) the
61 // return value starts increasing to 1.0.
Austin Schuh703b8d42015-02-01 14:56:34 -080062 double offset_ratio_ready() const {
63 return start_pos_samples_.size() / static_cast<double>(max_sample_count_);
64 }
65
Adam Snaiderc4b3c192015-02-01 01:30:39 +000066 private:
Philipp Schradere828be72015-02-15 07:07:37 +000067 // This function calculates the start position given the internal state and
68 // the provided `latched_encoder' value.
69 double CalculateStartPosition(double start_average,
70 double latched_encoder) const;
71
Adam Snaiderb4119252015-02-15 01:30:57 +000072 // The estimated position.
73 double pos_;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000074 // The distance between two consecutive index positions.
75 double index_diff_;
Adam Snaiderb4119252015-02-15 01:30:57 +000076 // The next position in 'start_pos_samples_' to be used to store the next
77 // sample.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000078 int samples_idx_;
79 // Last 'max_sample_count_' samples for start positions.
80 std::vector<double> start_pos_samples_;
Adam Snaiderb4119252015-02-15 01:30:57 +000081 // The number of the last samples of start position to consider in the
82 // estimation.
Adam Snaiderc4b3c192015-02-01 01:30:39 +000083 size_t max_sample_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +000084 // The estimated starting position of the mechanism. We also call this the
85 // 'offset' in some contexts.
86 double start_pos_;
Philipp Schrader030ad182015-02-15 05:40:58 +000087 // The absolute position of any index pulse on the mechanism. This is used to
88 // account for the various ways the encoders get mounted into the robot.
89 double known_index_pos_;
Philipp Schrader41d82912015-02-15 03:44:23 +000090 // Flag for triggering logic that takes note of the current index pulse count
Philipp Schradere828be72015-02-15 07:07:37 +000091 // after a reset. See `last_used_index_pulse_count_'.
Philipp Schrader41d82912015-02-15 03:44:23 +000092 bool wait_for_index_pulse_;
93 // After a reset we keep track of the index pulse count with this. Only after
94 // the index pulse count changes (i.e. increments at least once or wraps
Philipp Schradere828be72015-02-15 07:07:37 +000095 // around) will we consider the mechanism zeroed. We also use this to store
96 // the most recent `PotAndIndexPosition::index_pulses' value when the start
97 // position was calculated. It helps us calculate the start position only on
98 // index pulses to reject corrupted intermediate data.
99 uint32_t last_used_index_pulse_count_;
Adam Snaiderb4119252015-02-15 01:30:57 +0000100 // Marker to track whether we're fully zeroed yet or not.
101 bool zeroed_;
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000102 // Marker to track whether an error has occurred. This gets reset to false
103 // whenever Reset() is called.
104 bool error_;
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000105};
106
Daniel Pettiab274232015-02-16 19:15:34 -0800107// Populates an EstimatorState struct with information from the zeroing
108// estimator.
109void PopulateEstimatorState(const ZeroingEstimator &estimator,
110 EstimatorState *state);
111
Adam Snaiderb4119252015-02-15 01:30:57 +0000112} // namespace zeroing
113} // namespace frc971
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000114
115#endif // FRC971_ZEROING_ZEROING_H_