James Kuszmaul | bdc6a79 | 2023-08-12 16:29:38 -0700 | [diff] [blame] | 1 | #include "frc971/zeroing/continuous_absolute_encoder.h" |
| 2 | |
| 3 | #include <cmath> |
| 4 | #include <numeric> |
| 5 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 6 | #include "absl/log/check.h" |
| 7 | #include "absl/log/log.h" |
James Kuszmaul | bdc6a79 | 2023-08-12 16:29:38 -0700 | [diff] [blame] | 8 | |
| 9 | #include "aos/containers/error_list.h" |
| 10 | #include "frc971/zeroing/wrap.h" |
| 11 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 12 | namespace frc971::zeroing { |
James Kuszmaul | bdc6a79 | 2023-08-12 16:29:38 -0700 | [diff] [blame] | 13 | |
| 14 | ContinuousAbsoluteEncoderZeroingEstimator:: |
| 15 | ContinuousAbsoluteEncoderZeroingEstimator( |
| 16 | const constants::ContinuousAbsoluteEncoderZeroingConstants &constants) |
| 17 | : constants_(constants), move_detector_(constants_.moving_buffer_size) { |
| 18 | relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size); |
| 19 | Reset(); |
| 20 | } |
| 21 | |
| 22 | void ContinuousAbsoluteEncoderZeroingEstimator::Reset() { |
| 23 | zeroed_ = false; |
| 24 | error_ = false; |
| 25 | first_offset_ = 0.0; |
| 26 | offset_ = 0.0; |
| 27 | samples_idx_ = 0; |
| 28 | position_ = 0.0; |
| 29 | nan_samples_ = 0; |
| 30 | relative_to_absolute_offset_samples_.clear(); |
| 31 | move_detector_.Reset(); |
| 32 | } |
| 33 | |
| 34 | // The math here is a bit backwards, but I think it'll be less error prone that |
| 35 | // way and more similar to the version with a pot as well. |
| 36 | // |
| 37 | // We start by unwrapping the absolute encoder using the relative encoder. This |
| 38 | // puts us in a non-wrapping space and lets us average a bit easier. From |
| 39 | // there, we can compute an offset and wrap ourselves back such that we stay |
| 40 | // close to the middle value. |
| 41 | // |
| 42 | // To guard against the robot moving while updating estimates, buffer a number |
| 43 | // of samples and check that the buffered samples are not different than the |
| 44 | // zeroing threshold. At any point that the samples differ too much, do not |
| 45 | // update estimates based on those samples. |
| 46 | void ContinuousAbsoluteEncoderZeroingEstimator::UpdateEstimate( |
| 47 | const AbsolutePosition &info) { |
| 48 | // Check for Abs Encoder NaN value that would mess up the rest of the zeroing |
| 49 | // code below. NaN values are given when the Absolute Encoder is disconnected. |
| 50 | if (::std::isnan(info.absolute_encoder())) { |
| 51 | if (zeroed_) { |
| 52 | VLOG(1) << "NAN on absolute encoder."; |
| 53 | errors_.Set(ZeroingError::LOST_ABSOLUTE_ENCODER); |
| 54 | error_ = true; |
| 55 | } else { |
| 56 | ++nan_samples_; |
| 57 | VLOG(1) << "NAN on absolute encoder while zeroing " << nan_samples_; |
| 58 | if (nan_samples_ >= constants_.average_filter_size) { |
| 59 | errors_.Set(ZeroingError::LOST_ABSOLUTE_ENCODER); |
| 60 | error_ = true; |
| 61 | zeroed_ = true; |
| 62 | } |
| 63 | } |
| 64 | // Throw some dummy values in for now. |
| 65 | filtered_absolute_encoder_ = info.absolute_encoder(); |
| 66 | position_ = offset_ + info.encoder(); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | const bool moving = move_detector_.Update(info, constants_.moving_buffer_size, |
| 71 | constants_.zeroing_threshold); |
| 72 | |
| 73 | if (!moving) { |
| 74 | const PositionStruct &sample = move_detector_.GetSample(); |
| 75 | |
| 76 | // adjusted_* numbers are nominally in the desired output frame. |
| 77 | const double adjusted_absolute_encoder = |
| 78 | sample.absolute_encoder - constants_.measured_absolute_position; |
| 79 | |
| 80 | // Note: If are are near the breakpoint of the absolute encoder, this number |
| 81 | // will be jitter between numbers that are ~one_revolution_distance apart. |
| 82 | // For that reason, we rewrap it so that we are not near that boundary. |
| 83 | const double relative_to_absolute_offset = |
| 84 | adjusted_absolute_encoder - sample.encoder; |
| 85 | |
| 86 | // To avoid the aforementioned jitter, choose a base value to use for |
| 87 | // wrapping. When we have no prior samples, just use the current offset. |
| 88 | // Otherwise, we use an arbitrary prior offset (the stored offsets will all |
| 89 | // already be wrapped). |
| 90 | const double relative_to_absolute_offset_wrap_base = |
| 91 | relative_to_absolute_offset_samples_.size() == 0 |
| 92 | ? relative_to_absolute_offset |
| 93 | : relative_to_absolute_offset_samples_[0]; |
| 94 | |
| 95 | const double relative_to_absolute_offset_wrapped = |
| 96 | UnWrap(relative_to_absolute_offset_wrap_base, |
| 97 | relative_to_absolute_offset, constants_.one_revolution_distance); |
| 98 | |
| 99 | const size_t relative_to_absolute_offset_samples_size = |
| 100 | relative_to_absolute_offset_samples_.size(); |
| 101 | if (relative_to_absolute_offset_samples_size < |
| 102 | constants_.average_filter_size) { |
| 103 | relative_to_absolute_offset_samples_.push_back( |
| 104 | relative_to_absolute_offset_wrapped); |
| 105 | } else { |
| 106 | relative_to_absolute_offset_samples_[samples_idx_] = |
| 107 | relative_to_absolute_offset_wrapped; |
| 108 | } |
| 109 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
| 110 | |
| 111 | // Compute the average offset between the absolute encoder and relative |
| 112 | // encoder. Because we just pushed a value, the size() will never be zero. |
| 113 | offset_ = |
| 114 | ::std::accumulate(relative_to_absolute_offset_samples_.begin(), |
| 115 | relative_to_absolute_offset_samples_.end(), 0.0) / |
| 116 | relative_to_absolute_offset_samples_.size(); |
| 117 | |
| 118 | // To provide a value that can be used to estimate the |
| 119 | // measured_absolute_position when zeroing, we just need to output the |
| 120 | // current absolute encoder value. We could make use of the averaging |
| 121 | // implicit in offset_ to reduce the noise on this slightly. |
| 122 | filtered_absolute_encoder_ = sample.absolute_encoder; |
| 123 | |
| 124 | if (offset_ready()) { |
| 125 | if (!zeroed_) { |
| 126 | first_offset_ = offset_; |
| 127 | } |
| 128 | |
| 129 | if (::std::abs(first_offset_ - offset_) > |
| 130 | constants_.allowable_encoder_error * |
| 131 | constants_.one_revolution_distance) { |
| 132 | VLOG(1) << "Offset moved too far. Initial: " << first_offset_ |
| 133 | << ", current " << offset_ << ", allowable change: " |
| 134 | << constants_.allowable_encoder_error * |
| 135 | constants_.one_revolution_distance; |
| 136 | errors_.Set(ZeroingError::OFFSET_MOVED_TOO_FAR); |
| 137 | error_ = true; |
| 138 | } |
| 139 | |
| 140 | zeroed_ = true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Update the position. Wrap it to reflect the fact that we do not have |
| 145 | // sufficient information to disambiguate which revolution we are on (also, |
| 146 | // since this value is primarily meant for debugging, this makes it easier to |
| 147 | // see that the device is actually at zero without having to divide by 2 * |
| 148 | // pi). |
| 149 | position_ = |
| 150 | Wrap(0.0, offset_ + info.encoder(), constants_.one_revolution_distance); |
| 151 | } |
| 152 | |
| 153 | flatbuffers::Offset<ContinuousAbsoluteEncoderZeroingEstimator::State> |
| 154 | ContinuousAbsoluteEncoderZeroingEstimator::GetEstimatorState( |
| 155 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 156 | flatbuffers::Offset<flatbuffers::Vector<ZeroingError>> errors_offset = |
| 157 | errors_.ToFlatbuffer(fbb); |
| 158 | |
| 159 | State::Builder builder(*fbb); |
| 160 | builder.add_error(error_); |
| 161 | builder.add_zeroed(zeroed_); |
| 162 | builder.add_position(position_); |
| 163 | builder.add_absolute_position(filtered_absolute_encoder_); |
| 164 | builder.add_errors(errors_offset); |
| 165 | return builder.Finish(); |
| 166 | } |
| 167 | |
James Kuszmaul | cdbdb9d | 2024-10-29 22:20:03 -0700 | [diff] [blame^] | 168 | void ContinuousAbsoluteEncoderZeroingEstimator::GetEstimatorState( |
| 169 | AbsoluteEncoderEstimatorStateStatic *fbs) const { |
| 170 | errors_.ToStaticFlatbuffer(fbs->add_errors()); |
| 171 | |
| 172 | fbs->set_error(error_); |
| 173 | fbs->set_zeroed(zeroed_); |
| 174 | fbs->set_position(position_); |
| 175 | fbs->set_absolute_position(filtered_absolute_encoder_); |
| 176 | } |
| 177 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 178 | } // namespace frc971::zeroing |