Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 1 | #include "frc971/zeroing/zeroing.h" |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 2 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 3 | #include <algorithm> |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 4 | #include <cmath> |
| 5 | #include <limits> |
Austin Schuh | a8f88d4 | 2019-01-26 12:33:54 -0800 | [diff] [blame] | 6 | #include <numeric> |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 7 | #include <vector> |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 8 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 9 | #include "frc971/zeroing/wrap.h" |
| 10 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "flatbuffers/flatbuffers.h" |
| 12 | #include "glog/logging.h" |
| 13 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 14 | namespace frc971 { |
| 15 | namespace zeroing { |
| 16 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 17 | PotAndIndexPulseZeroingEstimator::PotAndIndexPulseZeroingEstimator( |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 18 | const constants::PotAndIndexPulseZeroingConstants &constants) |
| 19 | : constants_(constants) { |
| 20 | start_pos_samples_.reserve(constants_.average_filter_size); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 21 | Reset(); |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 22 | } |
| 23 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 24 | void PotAndIndexPulseZeroingEstimator::Reset() { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 25 | samples_idx_ = 0; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 26 | offset_ = 0; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 27 | start_pos_samples_.clear(); |
| 28 | zeroed_ = false; |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 29 | wait_for_index_pulse_ = true; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 30 | last_used_index_pulse_count_ = 0; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 31 | error_ = false; |
| 32 | } |
| 33 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 34 | void PotAndIndexPulseZeroingEstimator::TriggerError() { |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 35 | if (!error_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 36 | VLOG(1) << "Manually triggered zeroing error."; |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 37 | error_ = true; |
| 38 | } |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 41 | double PotAndIndexPulseZeroingEstimator::CalculateStartPosition( |
| 42 | double start_average, double latched_encoder) const { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 43 | // We calculate an aproximation of the value of the last index position. |
| 44 | // Also account for index pulses not lining up with integer multiples of the |
| 45 | // index_diff. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 46 | double index_pos = |
| 47 | start_average + latched_encoder - constants_.measured_index_position; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 48 | // We round index_pos to the closest valid value of the index. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 49 | double accurate_index_pos = (round(index_pos / constants_.index_difference)) * |
| 50 | constants_.index_difference; |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 51 | // Now we reverse the first calculation to get the accurate start position. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 52 | return accurate_index_pos - latched_encoder + |
| 53 | constants_.measured_index_position; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 56 | void PotAndIndexPulseZeroingEstimator::UpdateEstimate( |
| 57 | const PotAndIndexPosition &info) { |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 58 | // We want to make sure that we encounter at least one index pulse while |
| 59 | // zeroing. So we take the index pulse count from the first sample after |
| 60 | // reset and wait for that count to change before we consider ourselves |
| 61 | // zeroed. |
| 62 | if (wait_for_index_pulse_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | last_used_index_pulse_count_ = info.index_pulses(); |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 64 | wait_for_index_pulse_ = false; |
| 65 | } |
| 66 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 67 | if (start_pos_samples_.size() < constants_.average_filter_size) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | start_pos_samples_.push_back(info.pot() - info.encoder()); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 69 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 70 | start_pos_samples_[samples_idx_] = info.pot() - info.encoder(); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 71 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 72 | |
| 73 | // Drop the oldest sample when we run this function the next time around. |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 74 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 75 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 76 | double sample_sum = 0.0; |
| 77 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 78 | for (size_t i = 0; i < start_pos_samples_.size(); ++i) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 79 | sample_sum += start_pos_samples_[i]; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Calculates the average of the starting position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 83 | double start_average = sample_sum / start_pos_samples_.size(); |
| 84 | |
| 85 | // If there are no index pulses to use or we don't have enough samples yet to |
| 86 | // have a well-filtered starting position then we use the filtered value as |
| 87 | // our best guess. |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 88 | if (!zeroed_ && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | (info.index_pulses() == last_used_index_pulse_count_ || !offset_ready())) { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 90 | offset_ = start_average; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | } else if (!zeroed_ || last_used_index_pulse_count_ != info.index_pulses()) { |
Philipp Schrader | e828be7 | 2015-02-15 07:07:37 +0000 | [diff] [blame] | 92 | // Note the accurate start position and the current index pulse count so |
| 93 | // that we only run this logic once per index pulse. That should be more |
| 94 | // resilient to corrupted intermediate data. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | offset_ = CalculateStartPosition(start_average, info.latched_encoder()); |
| 96 | last_used_index_pulse_count_ = info.index_pulses(); |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 97 | |
| 98 | // TODO(austin): Reject encoder positions which have x% error rather than |
| 99 | // rounding to the closest index pulse. |
| 100 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 101 | // Save the first starting position. |
| 102 | if (!zeroed_) { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 103 | first_start_pos_ = offset_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 104 | VLOG(2) << "latching start position" << first_start_pos_; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 105 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 106 | |
| 107 | // Now that we have an accurate starting position we can consider ourselves |
| 108 | // zeroed. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 109 | zeroed_ = true; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 110 | // Throw an error if first_start_pos is bigger/smaller than |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 111 | // constants_.allowable_encoder_error * index_diff + start_pos. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 112 | if (::std::abs(first_start_pos_ - offset_) > |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 113 | constants_.allowable_encoder_error * constants_.index_difference) { |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 114 | if (!error_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | VLOG(1) |
| 116 | << "Encoder ticks out of range since last index pulse. first start " |
| 117 | "position: " |
| 118 | << first_start_pos_ << " recent starting position: " << offset_ |
| 119 | << ", allowable error: " |
| 120 | << constants_.allowable_encoder_error * constants_.index_difference; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 121 | error_ = true; |
| 122 | } |
| 123 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 124 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 125 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 126 | position_ = offset_ + info.encoder(); |
| 127 | filtered_position_ = start_average + info.encoder(); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 130 | flatbuffers::Offset<PotAndIndexPulseZeroingEstimator::State> |
| 131 | PotAndIndexPulseZeroingEstimator::GetEstimatorState( |
| 132 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 133 | State::Builder builder(*fbb); |
| 134 | builder.add_error(error_); |
| 135 | builder.add_zeroed(zeroed_); |
| 136 | builder.add_position(position_); |
| 137 | builder.add_pot_position(filtered_position_); |
| 138 | return builder.Finish(); |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 141 | HallEffectAndPositionZeroingEstimator::HallEffectAndPositionZeroingEstimator( |
| 142 | const ZeroingConstants &constants) |
| 143 | : constants_(constants) { |
| 144 | Reset(); |
| 145 | } |
| 146 | |
| 147 | void HallEffectAndPositionZeroingEstimator::Reset() { |
| 148 | offset_ = 0.0; |
| 149 | min_low_position_ = ::std::numeric_limits<double>::max(); |
| 150 | max_low_position_ = ::std::numeric_limits<double>::lowest(); |
| 151 | zeroed_ = false; |
| 152 | initialized_ = false; |
| 153 | last_used_posedge_count_ = 0; |
| 154 | cycles_high_ = 0; |
| 155 | high_long_enough_ = false; |
| 156 | first_start_pos_ = 0.0; |
| 157 | error_ = false; |
| 158 | current_ = 0.0; |
| 159 | first_start_pos_ = 0.0; |
| 160 | } |
| 161 | |
| 162 | void HallEffectAndPositionZeroingEstimator::TriggerError() { |
| 163 | if (!error_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | VLOG(1) << "Manually triggered zeroing error.\n"; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 165 | error_ = true; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void HallEffectAndPositionZeroingEstimator::StoreEncoderMaxAndMin( |
| 170 | const HallEffectAndPosition &info) { |
| 171 | // If we have a new posedge. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 172 | if (!info.current()) { |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 173 | if (last_hall_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 174 | min_low_position_ = max_low_position_ = info.encoder(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 175 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | min_low_position_ = ::std::min(min_low_position_, info.encoder()); |
| 177 | max_low_position_ = ::std::max(max_low_position_, info.encoder()); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 180 | last_hall_ = info.current(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void HallEffectAndPositionZeroingEstimator::UpdateEstimate( |
| 184 | const HallEffectAndPosition &info) { |
| 185 | // We want to make sure that we encounter at least one posedge while zeroing. |
| 186 | // So we take the posedge count from the first sample after reset and wait for |
| 187 | // that count to change and for the hall effect to stay high before we |
| 188 | // consider ourselves zeroed. |
| 189 | if (!initialized_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 190 | last_used_posedge_count_ = info.posedge_count(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 191 | initialized_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 192 | last_hall_ = info.current(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | StoreEncoderMaxAndMin(info); |
| 196 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 197 | if (info.current()) { |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 198 | cycles_high_++; |
| 199 | } else { |
| 200 | cycles_high_ = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 201 | last_used_posedge_count_ = info.posedge_count(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | high_long_enough_ = cycles_high_ >= constants_.hall_trigger_zeroing_length; |
| 205 | |
| 206 | bool moving_backward = false; |
| 207 | if (constants_.zeroing_move_direction) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 208 | moving_backward = info.encoder() > min_low_position_; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 209 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 210 | moving_backward = info.encoder() < max_low_position_; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // If there are no posedges to use or we don't have enough samples yet to |
| 214 | // have a well-filtered starting position then we use the filtered value as |
| 215 | // our best guess. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 216 | if (last_used_posedge_count_ != info.posedge_count() && high_long_enough_ && |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 217 | moving_backward) { |
| 218 | // Note the offset and the current posedge count so that we only run this |
| 219 | // logic once per posedge. That should be more resilient to corrupted |
| 220 | // intermediate data. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 221 | offset_ = -info.posedge_value(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 222 | if (constants_.zeroing_move_direction) { |
| 223 | offset_ += constants_.lower_hall_position; |
| 224 | } else { |
| 225 | offset_ += constants_.upper_hall_position; |
| 226 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 227 | last_used_posedge_count_ = info.posedge_count(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 228 | |
| 229 | // Save the first starting position. |
| 230 | if (!zeroed_) { |
| 231 | first_start_pos_ = offset_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 232 | VLOG(2) << "latching start position" << first_start_pos_; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Now that we have an accurate starting position we can consider ourselves |
| 236 | // zeroed. |
| 237 | zeroed_ = true; |
| 238 | } |
| 239 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 240 | position_ = info.encoder() - offset_; |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 243 | flatbuffers::Offset<HallEffectAndPositionZeroingEstimator::State> |
| 244 | HallEffectAndPositionZeroingEstimator::GetEstimatorState( |
| 245 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 246 | State::Builder builder(*fbb); |
| 247 | builder.add_error(error_); |
| 248 | builder.add_zeroed(zeroed_); |
| 249 | builder.add_encoder(position_); |
| 250 | builder.add_high_long_enough(high_long_enough_); |
| 251 | builder.add_offset(offset_); |
| 252 | return builder.Finish(); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 253 | } |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 254 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 255 | PotAndAbsoluteEncoderZeroingEstimator::PotAndAbsoluteEncoderZeroingEstimator( |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 256 | const constants::PotAndAbsoluteEncoderZeroingConstants &constants) |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 257 | : constants_(constants), move_detector_(constants_.moving_buffer_size) { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 258 | relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size); |
| 259 | offset_samples_.reserve(constants_.average_filter_size); |
| 260 | Reset(); |
| 261 | } |
| 262 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 263 | void PotAndAbsoluteEncoderZeroingEstimator::Reset() { |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 264 | first_offset_ = 0.0; |
| 265 | pot_relative_encoder_offset_ = 0.0; |
| 266 | offset_ = 0.0; |
| 267 | samples_idx_ = 0; |
| 268 | filtered_position_ = 0.0; |
| 269 | position_ = 0.0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 270 | zeroed_ = false; |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 271 | nan_samples_ = 0; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 272 | relative_to_absolute_offset_samples_.clear(); |
| 273 | offset_samples_.clear(); |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 274 | move_detector_.Reset(); |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 275 | error_ = false; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // So, this needs to be a multistep process. We need to first estimate the |
| 279 | // offset between the absolute encoder and the relative encoder. That process |
| 280 | // should get us an absolute number which is off by integer multiples of the |
| 281 | // distance/rev. In parallel, we can estimate the offset between the pot and |
| 282 | // encoder. When both estimates have converged, we can then compute the offset |
| 283 | // in a cycle, and which cycle, which gives us the accurate global offset. |
| 284 | // |
| 285 | // It's tricky to compute the offset between the absolute and relative encoder. |
| 286 | // We need to compute this inside 1 revolution. The easiest way to do this |
| 287 | // would be to wrap the encoder, subtract the two of them, and then average the |
| 288 | // result. That will struggle when they are off by PI. Instead, we need to |
| 289 | // wrap the number to +- PI from the current averaged offset. |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 290 | // |
| 291 | // To guard against the robot moving while updating estimates, buffer a number |
| 292 | // of samples and check that the buffered samples are not different than the |
| 293 | // zeroing threshold. At any point that the samples differ too much, do not |
| 294 | // update estimates based on those samples. |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 295 | void PotAndAbsoluteEncoderZeroingEstimator::UpdateEstimate( |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 296 | const PotAndAbsolutePosition &info) { |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 297 | // Check for Abs Encoder NaN value that would mess up the rest of the zeroing |
| 298 | // code below. NaN values are given when the Absolute Encoder is disconnected. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 299 | if (::std::isnan(info.absolute_encoder())) { |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 300 | if (zeroed_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 301 | VLOG(1) << "NAN on absolute encoder."; |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 302 | error_ = true; |
| 303 | } else { |
| 304 | ++nan_samples_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 305 | VLOG(1) << "NAN on absolute encoder while zeroing" << nan_samples_; |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 306 | if (nan_samples_ >= constants_.average_filter_size) { |
| 307 | error_ = true; |
| 308 | zeroed_ = true; |
| 309 | } |
| 310 | } |
| 311 | // Throw some dummy values in for now. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 312 | filtered_absolute_encoder_ = info.absolute_encoder(); |
| 313 | filtered_position_ = pot_relative_encoder_offset_ + info.encoder(); |
| 314 | position_ = offset_ + info.encoder(); |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
Austin Schuh | 66c59ba | 2019-01-26 20:34:35 -0800 | [diff] [blame] | 318 | const bool moving = move_detector_.Update(info, constants_.moving_buffer_size, |
| 319 | constants_.zeroing_threshold); |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 320 | |
| 321 | if (!moving) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 322 | const PositionStruct &sample = move_detector_.GetSample(); |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 323 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 324 | // Compute the average offset between the absolute encoder and relative |
| 325 | // encoder. If we have 0 samples, assume it is 0. |
| 326 | double average_relative_to_absolute_offset = |
| 327 | relative_to_absolute_offset_samples_.size() == 0 |
| 328 | ? 0.0 |
Austin Schuh | a8f88d4 | 2019-01-26 12:33:54 -0800 | [diff] [blame] | 329 | : ::std::accumulate(relative_to_absolute_offset_samples_.begin(), |
| 330 | relative_to_absolute_offset_samples_.end(), |
| 331 | 0.0) / |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 332 | relative_to_absolute_offset_samples_.size(); |
| 333 | |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 334 | const double adjusted_incremental_encoder = |
Austin Schuh | 409ffe0 | 2019-01-21 18:46:41 -0800 | [diff] [blame] | 335 | sample.encoder + average_relative_to_absolute_offset; |
Austin Schuh | 0e1c2c6 | 2017-02-21 02:03:25 -0800 | [diff] [blame] | 336 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 337 | // Now, compute the nearest absolute encoder value to the offset relative |
| 338 | // encoder position. |
| 339 | const double adjusted_absolute_encoder = |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 340 | UnWrap(adjusted_incremental_encoder, |
| 341 | sample.absolute_encoder - constants_.measured_absolute_position, |
| 342 | constants_.one_revolution_distance); |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 343 | |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 344 | // We can now compute the offset now that we have unwrapped the absolute |
| 345 | // encoder. |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 346 | const double relative_to_absolute_offset = |
Austin Schuh | 409ffe0 | 2019-01-21 18:46:41 -0800 | [diff] [blame] | 347 | adjusted_absolute_encoder - sample.encoder; |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 348 | |
| 349 | // Add the sample and update the average with the new reading. |
| 350 | const size_t relative_to_absolute_offset_samples_size = |
| 351 | relative_to_absolute_offset_samples_.size(); |
| 352 | if (relative_to_absolute_offset_samples_size < |
| 353 | constants_.average_filter_size) { |
| 354 | average_relative_to_absolute_offset = |
| 355 | (average_relative_to_absolute_offset * |
| 356 | relative_to_absolute_offset_samples_size + |
| 357 | relative_to_absolute_offset) / |
| 358 | (relative_to_absolute_offset_samples_size + 1); |
| 359 | |
| 360 | relative_to_absolute_offset_samples_.push_back( |
| 361 | relative_to_absolute_offset); |
| 362 | } else { |
| 363 | average_relative_to_absolute_offset -= |
| 364 | relative_to_absolute_offset_samples_[samples_idx_] / |
| 365 | relative_to_absolute_offset_samples_size; |
| 366 | relative_to_absolute_offset_samples_[samples_idx_] = |
| 367 | relative_to_absolute_offset; |
| 368 | average_relative_to_absolute_offset += |
| 369 | relative_to_absolute_offset / |
| 370 | relative_to_absolute_offset_samples_size; |
| 371 | } |
| 372 | |
| 373 | // Now compute the offset between the pot and relative encoder. |
| 374 | if (offset_samples_.size() < constants_.average_filter_size) { |
Austin Schuh | 409ffe0 | 2019-01-21 18:46:41 -0800 | [diff] [blame] | 375 | offset_samples_.push_back(sample.pot - sample.encoder); |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 376 | } else { |
Austin Schuh | 409ffe0 | 2019-01-21 18:46:41 -0800 | [diff] [blame] | 377 | offset_samples_[samples_idx_] = sample.pot - sample.encoder; |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Drop the oldest sample when we run this function the next time around. |
| 381 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
| 382 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 383 | pot_relative_encoder_offset_ = |
Austin Schuh | a8f88d4 | 2019-01-26 12:33:54 -0800 | [diff] [blame] | 384 | ::std::accumulate(offset_samples_.begin(), offset_samples_.end(), 0.0) / |
| 385 | offset_samples_.size(); |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 386 | |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 387 | offset_ = UnWrap(sample.encoder + pot_relative_encoder_offset_, |
| 388 | average_relative_to_absolute_offset + sample.encoder, |
| 389 | constants_.one_revolution_distance) - |
Austin Schuh | 409ffe0 | 2019-01-21 18:46:41 -0800 | [diff] [blame] | 390 | sample.encoder; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 391 | |
| 392 | // Reverse the math for adjusted_absolute_encoder to compute the absolute |
| 393 | // encoder. Do this by taking the adjusted encoder, and then subtracting off |
| 394 | // the second argument above, and the value that was added by Wrap. |
| 395 | filtered_absolute_encoder_ = |
| 396 | ((sample.encoder + average_relative_to_absolute_offset) - |
| 397 | (-constants_.measured_absolute_position + |
| 398 | (adjusted_absolute_encoder - |
| 399 | (sample.absolute_encoder - constants_.measured_absolute_position)))); |
| 400 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 401 | if (offset_ready()) { |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 402 | if (!zeroed_) { |
| 403 | first_offset_ = offset_; |
| 404 | } |
| 405 | |
| 406 | if (::std::abs(first_offset_ - offset_) > |
| 407 | constants_.allowable_encoder_error * |
| 408 | constants_.one_revolution_distance) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 409 | VLOG(1) << "Offset moved too far. Initial: " << first_offset_ |
| 410 | << ", current " << offset_ << ", allowable change: " |
| 411 | << constants_.allowable_encoder_error * |
| 412 | constants_.one_revolution_distance; |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 413 | error_ = true; |
| 414 | } |
| 415 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 416 | zeroed_ = true; |
| 417 | } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 420 | // Update the position. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 421 | filtered_position_ = pot_relative_encoder_offset_ + info.encoder(); |
| 422 | position_ = offset_ + info.encoder(); |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 425 | flatbuffers::Offset<PotAndAbsoluteEncoderZeroingEstimator::State> |
| 426 | PotAndAbsoluteEncoderZeroingEstimator::GetEstimatorState( |
| 427 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 428 | State::Builder builder(*fbb); |
| 429 | builder.add_error(error_); |
| 430 | builder.add_zeroed(zeroed_); |
| 431 | builder.add_position(position_); |
| 432 | builder.add_pot_position(filtered_position_); |
| 433 | builder.add_absolute_position(filtered_absolute_encoder_); |
| 434 | return builder.Finish(); |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 437 | void PulseIndexZeroingEstimator::Reset() { |
| 438 | max_index_position_ = ::std::numeric_limits<double>::lowest(); |
| 439 | min_index_position_ = ::std::numeric_limits<double>::max(); |
| 440 | offset_ = 0; |
| 441 | last_used_index_pulse_count_ = 0; |
| 442 | zeroed_ = false; |
| 443 | error_ = false; |
| 444 | } |
| 445 | |
| 446 | void PulseIndexZeroingEstimator::StoreIndexPulseMaxAndMin( |
| 447 | const IndexPosition &info) { |
| 448 | // If we have a new index pulse. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 449 | if (last_used_index_pulse_count_ != info.index_pulses()) { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 450 | // If the latest pulses's position is outside the range we've currently |
| 451 | // seen, record it appropriately. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 452 | if (info.latched_encoder() > max_index_position_) { |
| 453 | max_index_position_ = info.latched_encoder(); |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 454 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | if (info.latched_encoder() < min_index_position_) { |
| 456 | min_index_position_ = info.latched_encoder(); |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 457 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 458 | last_used_index_pulse_count_ = info.index_pulses(); |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 462 | int PulseIndexZeroingEstimator::IndexPulseCount() const { |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 463 | if (min_index_position_ > max_index_position_) { |
| 464 | // This condition means we haven't seen a pulse yet. |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | // Calculate the number of pulses encountered so far. |
| 469 | return 1 + static_cast<int>( |
| 470 | ::std::round((max_index_position_ - min_index_position_) / |
| 471 | constants_.index_difference)); |
| 472 | } |
| 473 | |
| 474 | void PulseIndexZeroingEstimator::UpdateEstimate(const IndexPosition &info) { |
| 475 | StoreIndexPulseMaxAndMin(info); |
| 476 | const int index_pulse_count = IndexPulseCount(); |
| 477 | if (index_pulse_count > constants_.index_pulse_count) { |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 478 | if (!error_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 479 | VLOG(1) << "Got more index pulses than expected. Got " |
| 480 | << index_pulse_count << " expected " |
| 481 | << constants_.index_pulse_count; |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 482 | error_ = true; |
| 483 | } |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // TODO(austin): Detect if the encoder or index pulse is unplugged. |
| 487 | // TODO(austin): Detect missing counts. |
| 488 | |
| 489 | if (index_pulse_count == constants_.index_pulse_count && !zeroed_) { |
| 490 | offset_ = constants_.measured_index_position - |
| 491 | constants_.known_index_pulse * constants_.index_difference - |
| 492 | min_index_position_; |
| 493 | zeroed_ = true; |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 494 | } else if (zeroed_ && !error_) { |
| 495 | // Detect whether the index pulse is somewhere other than where we expect |
| 496 | // it to be. First we compute the position of the most recent index pulse. |
| 497 | double index_pulse_distance = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 498 | info.latched_encoder() + offset_ - constants_.measured_index_position; |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 499 | // Second we compute the position of the index pulse in terms of |
| 500 | // the index difference. I.e. if this index pulse is two pulses away from |
| 501 | // the index pulse that we know about then this number should be positive |
| 502 | // or negative two. |
| 503 | double relative_distance = |
| 504 | index_pulse_distance / constants_.index_difference; |
| 505 | // Now we compute how far away the measured index pulse is from the |
| 506 | // expected index pulse. |
| 507 | double error = relative_distance - ::std::round(relative_distance); |
| 508 | // This lets us check if the index pulse is within an acceptable error |
| 509 | // margin of where we expected it to be. |
| 510 | if (::std::abs(error) > constants_.allowable_encoder_error) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 511 | VLOG(1) |
| 512 | << "Encoder ticks out of range since last index pulse. known index " |
| 513 | "pulse: " |
| 514 | << constants_.measured_index_position << ", expected index pulse: " |
| 515 | << round(relative_distance) * constants_.index_difference + |
| 516 | constants_.measured_index_position |
| 517 | << ", actual index pulse: " << info.latched_encoder() + offset_ |
| 518 | << ", " |
| 519 | "allowable error: " |
| 520 | << constants_.allowable_encoder_error * constants_.index_difference; |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 521 | error_ = true; |
| 522 | } |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 523 | } |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 524 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 525 | position_ = info.encoder() + offset_; |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 528 | flatbuffers::Offset<PulseIndexZeroingEstimator::State> |
| 529 | PulseIndexZeroingEstimator::GetEstimatorState( |
| 530 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 531 | State::Builder builder(*fbb); |
| 532 | builder.add_error(error_); |
| 533 | builder.add_zeroed(zeroed_); |
| 534 | builder.add_position(position_); |
| 535 | builder.add_min_index_position(min_index_position_); |
| 536 | builder.add_max_index_position(max_index_position_); |
| 537 | builder.add_index_pulses_seen(IndexPulseCount()); |
| 538 | return builder.Finish(); |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 541 | AbsoluteEncoderZeroingEstimator::AbsoluteEncoderZeroingEstimator( |
| 542 | const constants::AbsoluteEncoderZeroingConstants &constants) |
| 543 | : constants_(constants), move_detector_(constants_.moving_buffer_size) { |
| 544 | relative_to_absolute_offset_samples_.reserve(constants_.average_filter_size); |
| 545 | Reset(); |
| 546 | } |
| 547 | |
| 548 | void AbsoluteEncoderZeroingEstimator::Reset() { |
| 549 | zeroed_ = false; |
| 550 | error_ = false; |
| 551 | first_offset_ = 0.0; |
| 552 | offset_ = 0.0; |
| 553 | samples_idx_ = 0; |
| 554 | position_ = 0.0; |
| 555 | nan_samples_ = 0; |
| 556 | relative_to_absolute_offset_samples_.clear(); |
| 557 | move_detector_.Reset(); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | // The math here is a bit backwards, but I think it'll be less error prone that |
| 562 | // way and more similar to the version with a pot as well. |
| 563 | // |
| 564 | // We start by unwrapping the absolute encoder using the relative encoder. This |
| 565 | // puts us in a non-wrapping space and lets us average a bit easier. From |
| 566 | // there, we can compute an offset and wrap ourselves back such that we stay |
| 567 | // close to the middle value. |
| 568 | // |
| 569 | // To guard against the robot moving while updating estimates, buffer a number |
| 570 | // of samples and check that the buffered samples are not different than the |
| 571 | // zeroing threshold. At any point that the samples differ too much, do not |
| 572 | // update estimates based on those samples. |
| 573 | void AbsoluteEncoderZeroingEstimator::UpdateEstimate( |
| 574 | const AbsolutePosition &info) { |
| 575 | // Check for Abs Encoder NaN value that would mess up the rest of the zeroing |
| 576 | // code below. NaN values are given when the Absolute Encoder is disconnected. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 577 | if (::std::isnan(info.absolute_encoder())) { |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 578 | if (zeroed_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 579 | VLOG(1) << "NAN on absolute encoder."; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 580 | error_ = true; |
| 581 | } else { |
| 582 | ++nan_samples_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 583 | VLOG(1) << "NAN on absolute encoder while zeroing " << nan_samples_; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 584 | if (nan_samples_ >= constants_.average_filter_size) { |
| 585 | error_ = true; |
| 586 | zeroed_ = true; |
| 587 | } |
| 588 | } |
| 589 | // Throw some dummy values in for now. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 590 | filtered_absolute_encoder_ = info.absolute_encoder(); |
| 591 | position_ = offset_ + info.encoder(); |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 592 | return; |
| 593 | } |
| 594 | |
| 595 | const bool moving = move_detector_.Update(info, constants_.moving_buffer_size, |
| 596 | constants_.zeroing_threshold); |
| 597 | |
| 598 | if (!moving) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 599 | const PositionStruct &sample = move_detector_.GetSample(); |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 600 | |
| 601 | // Compute the average offset between the absolute encoder and relative |
| 602 | // encoder. If we have 0 samples, assume it is 0. |
| 603 | double average_relative_to_absolute_offset = |
| 604 | relative_to_absolute_offset_samples_.size() == 0 |
| 605 | ? 0.0 |
| 606 | : ::std::accumulate(relative_to_absolute_offset_samples_.begin(), |
| 607 | relative_to_absolute_offset_samples_.end(), |
| 608 | 0.0) / |
| 609 | relative_to_absolute_offset_samples_.size(); |
| 610 | |
| 611 | // Now, compute the estimated absolute position using the previously |
| 612 | // estimated offset and the incremental encoder. |
| 613 | const double adjusted_incremental_encoder = |
| 614 | sample.encoder + average_relative_to_absolute_offset; |
| 615 | |
| 616 | // Now, compute the absolute encoder value nearest to the offset relative |
| 617 | // encoder position. |
| 618 | const double adjusted_absolute_encoder = |
| 619 | UnWrap(adjusted_incremental_encoder, |
| 620 | sample.absolute_encoder - constants_.measured_absolute_position, |
| 621 | constants_.one_revolution_distance); |
| 622 | |
| 623 | // We can now compute the offset now that we have unwrapped the absolute |
| 624 | // encoder. |
| 625 | const double relative_to_absolute_offset = |
| 626 | adjusted_absolute_encoder - sample.encoder; |
| 627 | |
| 628 | // Add the sample and update the average with the new reading. |
| 629 | const size_t relative_to_absolute_offset_samples_size = |
| 630 | relative_to_absolute_offset_samples_.size(); |
| 631 | if (relative_to_absolute_offset_samples_size < |
| 632 | constants_.average_filter_size) { |
| 633 | average_relative_to_absolute_offset = |
| 634 | (average_relative_to_absolute_offset * |
| 635 | relative_to_absolute_offset_samples_size + |
| 636 | relative_to_absolute_offset) / |
| 637 | (relative_to_absolute_offset_samples_size + 1); |
| 638 | |
| 639 | relative_to_absolute_offset_samples_.push_back( |
| 640 | relative_to_absolute_offset); |
| 641 | } else { |
| 642 | average_relative_to_absolute_offset -= |
| 643 | relative_to_absolute_offset_samples_[samples_idx_] / |
| 644 | relative_to_absolute_offset_samples_size; |
| 645 | relative_to_absolute_offset_samples_[samples_idx_] = |
| 646 | relative_to_absolute_offset; |
| 647 | average_relative_to_absolute_offset += |
| 648 | relative_to_absolute_offset / |
| 649 | relative_to_absolute_offset_samples_size; |
| 650 | } |
| 651 | |
| 652 | // Drop the oldest sample when we run this function the next time around. |
| 653 | samples_idx_ = (samples_idx_ + 1) % constants_.average_filter_size; |
| 654 | |
| 655 | // And our offset is the offset that gives us the position within +- ord/2 |
| 656 | // of the middle position. |
| 657 | offset_ = Wrap(constants_.middle_position, |
| 658 | average_relative_to_absolute_offset + sample.encoder, |
| 659 | constants_.one_revolution_distance) - |
| 660 | sample.encoder; |
| 661 | |
| 662 | // Reverse the math for adjusted_absolute_encoder to compute the absolute |
| 663 | // encoder. Do this by taking the adjusted encoder, and then subtracting off |
| 664 | // the second argument above, and the value that was added by Wrap. |
| 665 | filtered_absolute_encoder_ = |
| 666 | ((sample.encoder + average_relative_to_absolute_offset) - |
| 667 | (-constants_.measured_absolute_position + |
| 668 | (adjusted_absolute_encoder - |
| 669 | (sample.absolute_encoder - constants_.measured_absolute_position)))); |
| 670 | |
| 671 | if (offset_ready()) { |
| 672 | if (!zeroed_) { |
| 673 | first_offset_ = offset_; |
| 674 | } |
| 675 | |
| 676 | if (::std::abs(first_offset_ - offset_) > |
| 677 | constants_.allowable_encoder_error * |
| 678 | constants_.one_revolution_distance) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 679 | VLOG(1) << "Offset moved too far. Initial: " << first_offset_ |
| 680 | << ", current " << offset_ << ", allowable change: " |
| 681 | << constants_.allowable_encoder_error * |
| 682 | constants_.one_revolution_distance; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 683 | error_ = true; |
| 684 | } |
| 685 | |
| 686 | zeroed_ = true; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Update the position. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 691 | position_ = offset_ + info.encoder(); |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 694 | flatbuffers::Offset<AbsoluteEncoderZeroingEstimator::State> |
| 695 | AbsoluteEncoderZeroingEstimator::GetEstimatorState( |
| 696 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 697 | State::Builder builder(*fbb); |
| 698 | builder.add_error(error_); |
| 699 | builder.add_zeroed(zeroed_); |
| 700 | builder.add_position(position_); |
| 701 | builder.add_absolute_position(filtered_absolute_encoder_); |
| 702 | return builder.Finish(); |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 705 | } // namespace zeroing |
| 706 | } // namespace frc971 |