blob: 40b051966d5f09974f480248f2dde3ce61a0a062 [file] [log] [blame]
Ravago Jonesea6464c2020-10-10 15:40:46 -07001#include "frc971/zeroing/absolute_and_absolute_encoder.h"
2
3#include <cmath>
4#include <numeric>
5
6#include "glog/logging.h"
7
Ravago Jones937587c2020-12-26 17:21:09 -08008#include "aos/logging/logging.h"
Ravago Jonesea6464c2020-10-10 15:40:46 -07009#include "frc971/zeroing/wrap.h"
10
11namespace frc971 {
12namespace zeroing {
13
14AbsoluteAndAbsoluteEncoderZeroingEstimator::
15 AbsoluteAndAbsoluteEncoderZeroingEstimator(
16 const constants::AbsoluteAndAbsoluteEncoderZeroingConstants &constants)
17 : constants_(constants), move_detector_(constants_.moving_buffer_size) {
18 relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size);
19 offset_samples_.reserve(constants_.average_filter_size);
20 Reset();
21}
22
23void AbsoluteAndAbsoluteEncoderZeroingEstimator::Reset() {
24 first_offset_ = 0.0;
25 single_turn_to_relative_encoder_offset_ = 0.0;
26 offset_ = 0.0;
27 samples_idx_ = 0;
28 filtered_position_ = 0.0;
29 position_ = 0.0;
30 zeroed_ = false;
31 nan_samples_ = 0;
32 relative_to_absolute_offset_samples_.clear();
33 offset_samples_.clear();
34 move_detector_.Reset();
35 error_ = false;
36}
37
milind-ud53408e2021-10-21 19:43:58 -070038double
39AbsoluteAndAbsoluteEncoderZeroingEstimator::AdjustedSingleTurnAbsoluteEncoder(
40 const PositionStruct &sample) const {
41 return UnWrap(constants_.single_turn_middle_position,
42 sample.single_turn_absolute_encoder -
43 constants_.single_turn_measured_absolute_position,
44 constants_.single_turn_one_revolution_distance);
45}
46
Ravago Jonesea6464c2020-10-10 15:40:46 -070047// So, this needs to be a multistep process. We need to first estimate the
48// offset between the absolute encoder and the relative encoder. That process
49// should get us an absolute number which is off by integer multiples of the
50// distance/rev. In parallel, we can estimate the offset between the single
51// turn encoder and encoder. When both estimates have converged, we can then
52// compute the offset in a cycle, and which cycle, which gives us the accurate
53// global offset.
54//
55// It's tricky to compute the offset between the absolute and relative encoder.
56// We need to compute this inside 1 revolution. The easiest way to do this
57// would be to wrap the encoder, subtract the two of them, and then average the
58// result. That will struggle when they are off by PI. Instead, we need to
59// wrap the number to +- PI from the current averaged offset.
60//
61// To guard against the robot moving while updating estimates, buffer a number
62// of samples and check that the buffered samples are not different than the
63// zeroing threshold. At any point that the samples differ too much, do not
64// update estimates based on those samples.
65void AbsoluteAndAbsoluteEncoderZeroingEstimator::UpdateEstimate(
66 const AbsoluteAndAbsolutePosition &info) {
67 // Check for Abs Encoder NaN value that would mess up the rest of the zeroing
68 // code below. NaN values are given when the Absolute Encoder is disconnected.
Ravago Jones937587c2020-12-26 17:21:09 -080069 if (::std::isnan(info.absolute_encoder()) ||
70 ::std::isnan(info.single_turn_absolute_encoder())) {
Ravago Jonesea6464c2020-10-10 15:40:46 -070071 if (zeroed_) {
Ravago Jones937587c2020-12-26 17:21:09 -080072 VLOG(1) << "NAN on one of the absolute encoders.";
Ravago Jonesea6464c2020-10-10 15:40:46 -070073 error_ = true;
74 } else {
75 ++nan_samples_;
Ravago Jones937587c2020-12-26 17:21:09 -080076 VLOG(1) << "NAN on one of the absolute encoders while zeroing"
77 << nan_samples_;
Ravago Jonesea6464c2020-10-10 15:40:46 -070078 if (nan_samples_ >= constants_.average_filter_size) {
79 error_ = true;
80 zeroed_ = true;
81 }
82 }
83 // Throw some dummy values in for now.
84 filtered_absolute_encoder_ = info.absolute_encoder();
Ravago Jones937587c2020-12-26 17:21:09 -080085 filtered_single_turn_absolute_encoder_ =
86 info.single_turn_absolute_encoder();
Ravago Jonesea6464c2020-10-10 15:40:46 -070087 filtered_position_ =
88 single_turn_to_relative_encoder_offset_ + info.encoder();
89 position_ = offset_ + info.encoder();
90 return;
91 }
92
93 const bool moving = move_detector_.Update(info, constants_.moving_buffer_size,
94 constants_.zeroing_threshold);
95
96 if (!moving) {
97 const PositionStruct &sample = move_detector_.GetSample();
98
99 // Compute the average offset between the absolute encoder and relative
100 // encoder. If we have 0 samples, assume it is 0.
101 double average_relative_to_absolute_offset =
102 relative_to_absolute_offset_samples_.size() == 0
103 ? 0.0
104 : ::std::accumulate(relative_to_absolute_offset_samples_.begin(),
105 relative_to_absolute_offset_samples_.end(),
106 0.0) /
107 relative_to_absolute_offset_samples_.size();
108
109 const double adjusted_incremental_encoder =
110 sample.encoder + average_relative_to_absolute_offset;
111
112 // Now, compute the nearest absolute encoder value to the offset relative
113 // encoder position.
114 const double adjusted_absolute_encoder =
115 UnWrap(adjusted_incremental_encoder,
116 sample.absolute_encoder - constants_.measured_absolute_position,
117 constants_.one_revolution_distance);
118
119 // We can now compute the offset now that we have unwrapped the absolute
120 // encoder.
121 const double relative_to_absolute_offset =
122 adjusted_absolute_encoder - sample.encoder;
123
124 // Add the sample and update the average with the new reading.
125 const size_t relative_to_absolute_offset_samples_size =
126 relative_to_absolute_offset_samples_.size();
127 if (relative_to_absolute_offset_samples_size <
128 constants_.average_filter_size) {
129 average_relative_to_absolute_offset =
130 (average_relative_to_absolute_offset *
131 relative_to_absolute_offset_samples_size +
132 relative_to_absolute_offset) /
133 (relative_to_absolute_offset_samples_size + 1);
134
135 relative_to_absolute_offset_samples_.push_back(
136 relative_to_absolute_offset);
137 } else {
138 average_relative_to_absolute_offset -=
139 relative_to_absolute_offset_samples_[samples_idx_] /
140 relative_to_absolute_offset_samples_size;
141 relative_to_absolute_offset_samples_[samples_idx_] =
142 relative_to_absolute_offset;
143 average_relative_to_absolute_offset +=
144 relative_to_absolute_offset /
145 relative_to_absolute_offset_samples_size;
146 }
147
148 const double adjusted_single_turn_absolute_encoder =
milind-ud53408e2021-10-21 19:43:58 -0700149 AdjustedSingleTurnAbsoluteEncoder(sample);
Ravago Jonesea6464c2020-10-10 15:40:46 -0700150
151 // Now compute the offset between the pot and relative encoder.
152 if (offset_samples_.size() < constants_.average_filter_size) {
Ravago Jones937587c2020-12-26 17:21:09 -0800153 offset_samples_.push_back(sample.encoder -
154 adjusted_single_turn_absolute_encoder);
Ravago Jonesea6464c2020-10-10 15:40:46 -0700155 } else {
156 offset_samples_[samples_idx_] =
Ravago Jones937587c2020-12-26 17:21:09 -0800157 sample.encoder - adjusted_single_turn_absolute_encoder;
Ravago Jonesea6464c2020-10-10 15:40:46 -0700158 }
159
160 // Drop the oldest sample when we run this function the next time around.
161 samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size;
162
163 single_turn_to_relative_encoder_offset_ =
164 ::std::accumulate(offset_samples_.begin(), offset_samples_.end(), 0.0) /
165 offset_samples_.size();
166
Ravago Jones937587c2020-12-26 17:21:09 -0800167 offset_ = UnWrap(sample.encoder - single_turn_to_relative_encoder_offset_,
Ravago Jonesea6464c2020-10-10 15:40:46 -0700168 average_relative_to_absolute_offset + sample.encoder,
169 constants_.one_revolution_distance) -
170 sample.encoder;
171
172 // Reverse the math for adjusted_absolute_encoder to compute the absolute
173 // encoder. Do this by taking the adjusted encoder, and then subtracting off
174 // the second argument above, and the value that was added by Wrap.
175 filtered_absolute_encoder_ =
176 ((sample.encoder + average_relative_to_absolute_offset) -
177 (-constants_.measured_absolute_position +
178 (adjusted_absolute_encoder -
179 (sample.absolute_encoder - constants_.measured_absolute_position))));
180
Ravago Jones937587c2020-12-26 17:21:09 -0800181 const double what_Unwrap_added =
182 (adjusted_single_turn_absolute_encoder -
183 (sample.single_turn_absolute_encoder -
184 constants_.single_turn_measured_absolute_position));
185
Ravago Jonesea6464c2020-10-10 15:40:46 -0700186 // TODO(Ravago): this is impossible to read.
187 filtered_single_turn_absolute_encoder_ =
Ravago Jones937587c2020-12-26 17:21:09 -0800188 ((sample.encoder - single_turn_to_relative_encoder_offset_) -
Ravago Jonesea6464c2020-10-10 15:40:46 -0700189 (-constants_.single_turn_measured_absolute_position +
Ravago Jones937587c2020-12-26 17:21:09 -0800190 what_Unwrap_added));
191
192 /*
193 filtered_single_turn_absolute_encoder_ =
194 sample.encoder - single_turn_to_relative_encoder_offset_;
195 */
196
197 if (!zeroed_) {
198 first_offset_ = offset_;
199 }
Ravago Jonesea6464c2020-10-10 15:40:46 -0700200
201 if (offset_ready()) {
Ravago Jonesea6464c2020-10-10 15:40:46 -0700202 if (::std::abs(first_offset_ - offset_) >
203 constants_.allowable_encoder_error *
204 constants_.one_revolution_distance) {
Ravago Jones937587c2020-12-26 17:21:09 -0800205 AOS_LOG(INFO,
206 "Offset moved too far. Initial: %f, current %f, allowable "
207 "change: %f ",
208 first_offset_, offset_,
209 constants_.allowable_encoder_error *
210 constants_.one_revolution_distance);
Ravago Jonesea6464c2020-10-10 15:40:46 -0700211 error_ = true;
212 }
213
214 zeroed_ = true;
215 }
216 }
217
218 // Update the position.
Ravago Jones937587c2020-12-26 17:21:09 -0800219 position_ = first_offset_ + info.encoder();
Ravago Jonesea6464c2020-10-10 15:40:46 -0700220}
221
222flatbuffers::Offset<AbsoluteAndAbsoluteEncoderZeroingEstimator::State>
223AbsoluteAndAbsoluteEncoderZeroingEstimator::GetEstimatorState(
224 flatbuffers::FlatBufferBuilder *fbb) const {
225 State::Builder builder(*fbb);
226 builder.add_error(error_);
227 builder.add_zeroed(zeroed_);
228 builder.add_position(position_);
229 builder.add_absolute_position(filtered_absolute_encoder_);
230 builder.add_single_turn_absolute_position(
231 filtered_single_turn_absolute_encoder_);
232 return builder.Finish();
233}
234
235} // namespace zeroing
236} // namespace frc971