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