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