blob: bef6f2d47847b16fcb7027357545f7f7d54a20f1 [file] [log] [blame]
Brian Silvermana57b7012020-03-11 20:19:23 -07001#include "frc971/zeroing/pot_and_absolute_encoder.h"
2
3#include <cmath>
4#include <numeric>
5
Philipp Schrader790cb542023-07-05 21:06:52 -07006#include "glog/logging.h"
7
Ravago Jones726deb02021-05-29 14:36:43 -07008#include "aos/containers/error_list.h"
Brian Silvermana57b7012020-03-11 20:19:23 -07009#include "frc971/zeroing/wrap.h"
10
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace frc971::zeroing {
Brian Silvermana57b7012020-03-11 20:19:23 -070012
13PotAndAbsoluteEncoderZeroingEstimator::PotAndAbsoluteEncoderZeroingEstimator(
14 const constants::PotAndAbsoluteEncoderZeroingConstants &constants)
15 : constants_(constants), move_detector_(constants_.moving_buffer_size) {
16 relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size);
17 offset_samples_.reserve(constants_.average_filter_size);
18 Reset();
19}
20
21void PotAndAbsoluteEncoderZeroingEstimator::Reset() {
22 first_offset_ = 0.0;
23 pot_relative_encoder_offset_ = 0.0;
24 offset_ = 0.0;
25 samples_idx_ = 0;
26 filtered_position_ = 0.0;
27 position_ = 0.0;
28 zeroed_ = false;
29 nan_samples_ = 0;
30 relative_to_absolute_offset_samples_.clear();
31 offset_samples_.clear();
32 move_detector_.Reset();
33 error_ = false;
34}
35
36// So, this needs to be a multistep process. We need to first estimate the
37// offset between the absolute encoder and the relative encoder. That process
38// should get us an absolute number which is off by integer multiples of the
39// distance/rev. In parallel, we can estimate the offset between the pot and
40// encoder. When both estimates have converged, we can then compute the offset
41// in a cycle, and which cycle, which gives us the accurate global offset.
42//
43// It's tricky to compute the offset between the absolute and relative encoder.
44// We need to compute this inside 1 revolution. The easiest way to do this
45// would be to wrap the encoder, subtract the two of them, and then average the
46// result. That will struggle when they are off by PI. Instead, we need to
47// wrap the number to +- PI from the current averaged offset.
48//
49// To guard against the robot moving while updating estimates, buffer a number
50// of samples and check that the buffered samples are not different than the
51// zeroing threshold. At any point that the samples differ too much, do not
52// update estimates based on those samples.
53void PotAndAbsoluteEncoderZeroingEstimator::UpdateEstimate(
54 const PotAndAbsolutePosition &info) {
55 // Check for Abs Encoder NaN value that would mess up the rest of the zeroing
56 // code below. NaN values are given when the Absolute Encoder is disconnected.
57 if (::std::isnan(info.absolute_encoder())) {
58 if (zeroed_) {
59 VLOG(1) << "NAN on absolute encoder.";
Ravago Jones726deb02021-05-29 14:36:43 -070060 errors_.Set(ZeroingError::LOST_ABSOLUTE_ENCODER);
Brian Silvermana57b7012020-03-11 20:19:23 -070061 error_ = true;
62 } else {
63 ++nan_samples_;
Ravago Jones726deb02021-05-29 14:36:43 -070064 VLOG(1) << "NAN on absolute encoder while zeroing " << nan_samples_;
Brian Silvermana57b7012020-03-11 20:19:23 -070065 if (nan_samples_ >= constants_.average_filter_size) {
Ravago Jones726deb02021-05-29 14:36:43 -070066 errors_.Set(ZeroingError::LOST_ABSOLUTE_ENCODER);
Brian Silvermana57b7012020-03-11 20:19:23 -070067 error_ = true;
68 zeroed_ = true;
69 }
70 }
71 // Throw some dummy values in for now.
72 filtered_absolute_encoder_ = info.absolute_encoder();
73 filtered_position_ = pot_relative_encoder_offset_ + info.encoder();
74 position_ = offset_ + info.encoder();
75 return;
76 }
77
78 const bool moving = move_detector_.Update(info, constants_.moving_buffer_size,
79 constants_.zeroing_threshold);
80
81 if (!moving) {
82 const PositionStruct &sample = move_detector_.GetSample();
83
84 // Compute the average offset between the absolute encoder and relative
85 // encoder. If we have 0 samples, assume it is 0.
86 double average_relative_to_absolute_offset =
87 relative_to_absolute_offset_samples_.size() == 0
88 ? 0.0
89 : ::std::accumulate(relative_to_absolute_offset_samples_.begin(),
90 relative_to_absolute_offset_samples_.end(),
91 0.0) /
92 relative_to_absolute_offset_samples_.size();
93
94 const double adjusted_incremental_encoder =
95 sample.encoder + average_relative_to_absolute_offset;
96
97 // Now, compute the nearest absolute encoder value to the offset relative
98 // encoder position.
99 const double adjusted_absolute_encoder =
100 UnWrap(adjusted_incremental_encoder,
101 sample.absolute_encoder - constants_.measured_absolute_position,
102 constants_.one_revolution_distance);
103
104 // We can now compute the offset now that we have unwrapped the absolute
105 // encoder.
106 const double relative_to_absolute_offset =
107 adjusted_absolute_encoder - sample.encoder;
108
109 // Add the sample and update the average with the new reading.
110 const size_t relative_to_absolute_offset_samples_size =
111 relative_to_absolute_offset_samples_.size();
112 if (relative_to_absolute_offset_samples_size <
113 constants_.average_filter_size) {
114 average_relative_to_absolute_offset =
115 (average_relative_to_absolute_offset *
116 relative_to_absolute_offset_samples_size +
117 relative_to_absolute_offset) /
118 (relative_to_absolute_offset_samples_size + 1);
119
120 relative_to_absolute_offset_samples_.push_back(
121 relative_to_absolute_offset);
122 } else {
123 average_relative_to_absolute_offset -=
124 relative_to_absolute_offset_samples_[samples_idx_] /
125 relative_to_absolute_offset_samples_size;
126 relative_to_absolute_offset_samples_[samples_idx_] =
127 relative_to_absolute_offset;
128 average_relative_to_absolute_offset +=
129 relative_to_absolute_offset /
130 relative_to_absolute_offset_samples_size;
131 }
132
133 // Now compute the offset between the pot and relative encoder.
134 if (offset_samples_.size() < constants_.average_filter_size) {
135 offset_samples_.push_back(sample.pot - sample.encoder);
136 } else {
137 offset_samples_[samples_idx_] = sample.pot - sample.encoder;
138 }
139
140 // Drop the oldest sample when we run this function the next time around.
141 samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size;
142
143 pot_relative_encoder_offset_ =
144 ::std::accumulate(offset_samples_.begin(), offset_samples_.end(), 0.0) /
145 offset_samples_.size();
146
147 offset_ = UnWrap(sample.encoder + pot_relative_encoder_offset_,
148 average_relative_to_absolute_offset + sample.encoder,
149 constants_.one_revolution_distance) -
150 sample.encoder;
151
152 // Reverse the math for adjusted_absolute_encoder to compute the absolute
153 // encoder. Do this by taking the adjusted encoder, and then subtracting off
154 // the second argument above, and the value that was added by Wrap.
155 filtered_absolute_encoder_ =
156 ((sample.encoder + average_relative_to_absolute_offset) -
157 (-constants_.measured_absolute_position +
158 (adjusted_absolute_encoder -
159 (sample.absolute_encoder - constants_.measured_absolute_position))));
160
161 if (offset_ready()) {
162 if (!zeroed_) {
163 first_offset_ = offset_;
164 }
165
166 if (::std::abs(first_offset_ - offset_) >
167 constants_.allowable_encoder_error *
168 constants_.one_revolution_distance) {
169 VLOG(1) << "Offset moved too far. Initial: " << first_offset_
170 << ", current " << offset_ << ", allowable change: "
171 << constants_.allowable_encoder_error *
172 constants_.one_revolution_distance;
Ravago Jones726deb02021-05-29 14:36:43 -0700173 errors_.Set(ZeroingError::OFFSET_MOVED_TOO_FAR);
Brian Silvermana57b7012020-03-11 20:19:23 -0700174 error_ = true;
175 }
176
177 zeroed_ = true;
178 }
179 }
180
181 // Update the position.
182 filtered_position_ = pot_relative_encoder_offset_ + info.encoder();
183 position_ = offset_ + info.encoder();
184}
185
186flatbuffers::Offset<PotAndAbsoluteEncoderZeroingEstimator::State>
187PotAndAbsoluteEncoderZeroingEstimator::GetEstimatorState(
188 flatbuffers::FlatBufferBuilder *fbb) const {
Ravago Jones726deb02021-05-29 14:36:43 -0700189 flatbuffers::Offset<flatbuffers::Vector<ZeroingError>> errors_offset =
190 errors_.ToFlatbuffer(fbb);
191
Brian Silvermana57b7012020-03-11 20:19:23 -0700192 State::Builder builder(*fbb);
193 builder.add_error(error_);
194 builder.add_zeroed(zeroed_);
195 builder.add_position(position_);
196 builder.add_pot_position(filtered_position_);
197 builder.add_absolute_position(filtered_absolute_encoder_);
Ravago Jones726deb02021-05-29 14:36:43 -0700198 builder.add_errors(errors_offset);
Brian Silvermana57b7012020-03-11 20:19:23 -0700199 return builder.Finish();
200}
201
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800202} // namespace frc971::zeroing