Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <memory> |
| 4 | |
| 5 | #include <random> |
| 6 | |
| 7 | #include "gtest/gtest.h" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 8 | #include "frc971/zeroing/zeroing.h" |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 9 | #include "frc971/control_loops/control_loops.q.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 10 | #include "aos/testing/test_shm.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/util/thread.h" |
| 12 | #include "aos/die.h" |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 13 | #include "frc971/control_loops/position_sensor_sim.h" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 14 | |
| 15 | namespace frc971 { |
| 16 | namespace zeroing { |
| 17 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 18 | using control_loops::PositionSensorSimulator; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 19 | using constants::AbsoluteEncoderZeroingConstants; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 20 | using constants::EncoderPlusIndexZeroingConstants; |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 21 | using constants::PotAndAbsoluteEncoderZeroingConstants; |
| 22 | using constants::PotAndIndexPulseZeroingConstants; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 23 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 24 | static const size_t kSampleSize = 30; |
| 25 | static const double kAcceptableUnzeroedError = 0.2; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 26 | static const double kIndexErrorFraction = 0.3; |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 27 | static const size_t kMovingBufferSize = 3; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 28 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 29 | class ZeroingTest : public ::testing::Test { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 30 | protected: |
| 31 | void SetUp() override { aos::SetDieTestMode(true); } |
| 32 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 33 | void MoveTo(PositionSensorSimulator *simulator, |
| 34 | PotAndIndexPulseZeroingEstimator *estimator, |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 35 | double new_position) { |
Brian Silverman | dc4eb10 | 2017-02-05 17:34:41 -0800 | [diff] [blame] | 36 | PotAndIndexPosition sensor_values; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 37 | simulator->MoveTo(new_position); |
Brian Silverman | dc4eb10 | 2017-02-05 17:34:41 -0800 | [diff] [blame] | 38 | simulator->GetSensorValues(&sensor_values); |
| 39 | estimator->UpdateEstimate(sensor_values); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 40 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 41 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 42 | void MoveTo(PositionSensorSimulator *simulator, |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 43 | AbsoluteEncoderZeroingEstimator *estimator, |
| 44 | double new_position) { |
| 45 | AbsolutePosition sensor_values_; |
| 46 | simulator->MoveTo(new_position); |
| 47 | simulator->GetSensorValues(&sensor_values_); |
| 48 | estimator->UpdateEstimate(sensor_values_); |
| 49 | } |
| 50 | |
| 51 | void MoveTo(PositionSensorSimulator *simulator, |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 52 | PotAndAbsoluteEncoderZeroingEstimator *estimator, |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 53 | double new_position) { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 54 | PotAndAbsolutePosition sensor_values_; |
| 55 | simulator->MoveTo(new_position); |
| 56 | simulator->GetSensorValues(&sensor_values_); |
| 57 | estimator->UpdateEstimate(sensor_values_); |
| 58 | } |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 59 | |
| 60 | void MoveTo(PositionSensorSimulator *simulator, |
| 61 | PulseIndexZeroingEstimator *estimator, double new_position) { |
| 62 | IndexPosition sensor_values_; |
| 63 | simulator->MoveTo(new_position); |
| 64 | simulator->GetSensorValues(&sensor_values_); |
| 65 | estimator->UpdateEstimate(sensor_values_); |
| 66 | } |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 67 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 68 | void MoveTo(PositionSensorSimulator *simulator, |
| 69 | HallEffectAndPositionZeroingEstimator *estimator, |
| 70 | double new_position) { |
| 71 | HallEffectAndPosition sensor_values_; |
| 72 | simulator->MoveTo(new_position); |
| 73 | simulator->GetSensorValues(&sensor_values_); |
| 74 | estimator->UpdateEstimate(sensor_values_); |
| 75 | } |
| 76 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 77 | ::aos::testing::TestSharedMemory my_shm_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 80 | TEST_F(ZeroingTest, TestMovingAverageFilter) { |
| 81 | const double index_diff = 1.0; |
| 82 | PositionSensorSimulator sim(index_diff); |
| 83 | sim.Initialize(3.6 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 84 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 85 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 86 | |
| 87 | // The zeroing code is supposed to perform some filtering on the difference |
| 88 | // between the potentiometer value and the encoder value. We assume that 300 |
| 89 | // samples are sufficient to have updated the filter. |
| 90 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 91 | MoveTo(&sim, &estimator, 3.3 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 92 | } |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 93 | ASSERT_NEAR(3.3 * index_diff, estimator.GetEstimatorState().position, |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 94 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 95 | |
| 96 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 97 | MoveTo(&sim, &estimator, 3.9 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 98 | } |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 99 | ASSERT_NEAR(3.9 * index_diff, estimator.GetEstimatorState().position, |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 100 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 103 | TEST_F(ZeroingTest, NotZeroedBeforeEnoughSamplesCollected) { |
| 104 | double index_diff = 0.5; |
| 105 | double position = 3.6 * index_diff; |
| 106 | PositionSensorSimulator sim(index_diff); |
| 107 | sim.Initialize(position, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 108 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 109 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 110 | |
| 111 | // Make sure that the zeroing code does not consider itself zeroed until we |
| 112 | // collect a good amount of samples. In this case we're waiting until the |
| 113 | // moving average filter is full. |
| 114 | for (unsigned int i = 0; i < kSampleSize - 1; i++) { |
| 115 | MoveTo(&sim, &estimator, position += index_diff); |
| 116 | ASSERT_FALSE(estimator.zeroed()); |
| 117 | } |
| 118 | |
| 119 | MoveTo(&sim, &estimator, position); |
| 120 | ASSERT_TRUE(estimator.zeroed()); |
| 121 | } |
| 122 | |
| 123 | TEST_F(ZeroingTest, TestLotsOfMovement) { |
| 124 | double index_diff = 1.0; |
| 125 | PositionSensorSimulator sim(index_diff); |
| 126 | sim.Initialize(3.6, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 127 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 128 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 129 | |
| 130 | // The zeroing code is supposed to perform some filtering on the difference |
| 131 | // between the potentiometer value and the encoder value. We assume that 300 |
| 132 | // samples are sufficient to have updated the filter. |
| 133 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 134 | MoveTo(&sim, &estimator, 3.6); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 135 | } |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 136 | ASSERT_NEAR(3.6, estimator.GetEstimatorState().position, |
| 137 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 138 | |
| 139 | // With a single index pulse the zeroing estimator should be able to lock |
| 140 | // onto the true value of the position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 141 | MoveTo(&sim, &estimator, 4.01); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 142 | ASSERT_NEAR(4.01, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 143 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 144 | MoveTo(&sim, &estimator, 4.99); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 145 | ASSERT_NEAR(4.99, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 146 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 147 | MoveTo(&sim, &estimator, 3.99); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 148 | ASSERT_NEAR(3.99, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 149 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 150 | MoveTo(&sim, &estimator, 3.01); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 151 | ASSERT_NEAR(3.01, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 152 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 153 | MoveTo(&sim, &estimator, 13.55); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 154 | ASSERT_NEAR(13.55, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 157 | TEST_F(ZeroingTest, TestDifferentIndexDiffs) { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 158 | double index_diff = 0.89; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 159 | PositionSensorSimulator sim(index_diff); |
| 160 | sim.Initialize(3.5 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 161 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 162 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 163 | |
| 164 | // The zeroing code is supposed to perform some filtering on the difference |
| 165 | // between the potentiometer value and the encoder value. We assume that 300 |
| 166 | // samples are sufficient to have updated the filter. |
| 167 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 168 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 169 | } |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 170 | ASSERT_NEAR(3.5 * index_diff, estimator.GetEstimatorState().position, |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 171 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 172 | |
| 173 | // With a single index pulse the zeroing estimator should be able to lock |
| 174 | // onto the true value of the position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 175 | MoveTo(&sim, &estimator, 4.01); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 176 | ASSERT_NEAR(4.01, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 177 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 178 | MoveTo(&sim, &estimator, 4.99); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 179 | ASSERT_NEAR(4.99, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 180 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 181 | MoveTo(&sim, &estimator, 3.99); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 182 | ASSERT_NEAR(3.99, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 183 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 184 | MoveTo(&sim, &estimator, 3.01); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 185 | ASSERT_NEAR(3.01, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 186 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 187 | MoveTo(&sim, &estimator, 13.55); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 188 | ASSERT_NEAR(13.55, estimator.GetEstimatorState().position, 0.001); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | TEST_F(ZeroingTest, TestPercentage) { |
| 192 | double index_diff = 0.89; |
| 193 | PositionSensorSimulator sim(index_diff); |
| 194 | sim.Initialize(3.5 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 195 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 196 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 197 | |
| 198 | for (unsigned int i = 0; i < kSampleSize / 2; i++) { |
| 199 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 200 | } |
| 201 | ASSERT_NEAR(0.5, estimator.offset_ratio_ready(), 0.001); |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 202 | ASSERT_FALSE(estimator.offset_ready()); |
| 203 | |
| 204 | for (unsigned int i = 0; i < kSampleSize / 2; i++) { |
| 205 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 206 | } |
| 207 | ASSERT_NEAR(1.0, estimator.offset_ratio_ready(), 0.001); |
| 208 | ASSERT_TRUE(estimator.offset_ready()); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TEST_F(ZeroingTest, TestOffset) { |
| 212 | double index_diff = 0.89; |
| 213 | PositionSensorSimulator sim(index_diff); |
| 214 | sim.Initialize(3.1 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 215 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 216 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 217 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 218 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 219 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 220 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 221 | MoveTo(&sim, &estimator, 5.0 * index_diff); |
| 222 | } |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 223 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 224 | ASSERT_NEAR(3.1 * index_diff, estimator.offset(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 227 | TEST_F(ZeroingTest, WaitForIndexPulseAfterReset) { |
| 228 | double index_diff = 0.6; |
| 229 | PositionSensorSimulator sim(index_diff); |
| 230 | sim.Initialize(3.1 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 231 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 232 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 233 | |
| 234 | // Make sure to fill up the averaging filter with samples. |
| 235 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 236 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 237 | } |
| 238 | |
| 239 | // Make sure we're not zeroed until we hit an index pulse. |
| 240 | ASSERT_FALSE(estimator.zeroed()); |
| 241 | |
| 242 | // Trigger an index pulse; we should now be zeroed. |
| 243 | MoveTo(&sim, &estimator, 4.5 * index_diff); |
| 244 | ASSERT_TRUE(estimator.zeroed()); |
| 245 | |
| 246 | // Reset the zeroing logic and supply a bunch of samples within the current |
| 247 | // index segment. |
| 248 | estimator.Reset(); |
| 249 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 250 | MoveTo(&sim, &estimator, 4.2 * index_diff); |
| 251 | } |
| 252 | |
| 253 | // Make sure we're not zeroed until we hit an index pulse. |
| 254 | ASSERT_FALSE(estimator.zeroed()); |
| 255 | |
| 256 | // Trigger another index pulse; we should be zeroed again. |
| 257 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 258 | ASSERT_TRUE(estimator.zeroed()); |
| 259 | } |
| 260 | |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 261 | TEST_F(ZeroingTest, TestNonZeroIndexPulseOffsets) { |
| 262 | const double index_diff = 0.9; |
| 263 | const double known_index_pos = 3.5 * index_diff; |
| 264 | PositionSensorSimulator sim(index_diff); |
| 265 | sim.Initialize(3.3 * index_diff, index_diff / 3.0, known_index_pos); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 266 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 267 | kSampleSize, index_diff, known_index_pos, kIndexErrorFraction}); |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 268 | |
| 269 | // Make sure to fill up the averaging filter with samples. |
| 270 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 271 | MoveTo(&sim, &estimator, 3.3 * index_diff); |
| 272 | } |
| 273 | |
| 274 | // Make sure we're not zeroed until we hit an index pulse. |
| 275 | ASSERT_FALSE(estimator.zeroed()); |
| 276 | |
| 277 | // Trigger an index pulse; we should now be zeroed. |
| 278 | MoveTo(&sim, &estimator, 3.7 * index_diff); |
| 279 | ASSERT_TRUE(estimator.zeroed()); |
| 280 | ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset()); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 281 | ASSERT_DOUBLE_EQ(3.7 * index_diff, estimator.GetEstimatorState().position); |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 282 | |
| 283 | // Trigger one more index pulse and check the offset. |
| 284 | MoveTo(&sim, &estimator, 4.7 * index_diff); |
| 285 | ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset()); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 286 | ASSERT_DOUBLE_EQ(4.7 * index_diff, estimator.GetEstimatorState().position); |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 289 | TEST_F(ZeroingTest, BasicErrorAPITest) { |
| 290 | const double index_diff = 1.0; |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 291 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 292 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 293 | PositionSensorSimulator sim(index_diff); |
| 294 | sim.Initialize(1.5 * index_diff, index_diff / 3.0, 0.0); |
| 295 | |
| 296 | // Perform a simple move and make sure that no error occured. |
| 297 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 298 | ASSERT_FALSE(estimator.error()); |
| 299 | |
| 300 | // Trigger an error and make sure it's reported. |
| 301 | estimator.TriggerError(); |
| 302 | ASSERT_TRUE(estimator.error()); |
| 303 | |
| 304 | // Make sure that it can recover after a reset. |
| 305 | estimator.Reset(); |
| 306 | ASSERT_FALSE(estimator.error()); |
| 307 | MoveTo(&sim, &estimator, 4.5 * index_diff); |
| 308 | MoveTo(&sim, &estimator, 5.5 * index_diff); |
| 309 | ASSERT_FALSE(estimator.error()); |
| 310 | } |
| 311 | |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 312 | // Tests that an error is detected when the starting position changes too much. |
| 313 | TEST_F(ZeroingTest, TestIndexOffsetError) { |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 314 | const double index_diff = 0.8; |
| 315 | const double known_index_pos = 2 * index_diff; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 316 | const size_t sample_size = 30; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 317 | PositionSensorSimulator sim(index_diff); |
| 318 | sim.Initialize(10 * index_diff, index_diff / 3.0, known_index_pos); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 319 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 320 | sample_size, index_diff, known_index_pos, kIndexErrorFraction}); |
| 321 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 322 | for (size_t i = 0; i < sample_size; i++) { |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 323 | MoveTo(&sim, &estimator, 13 * index_diff); |
| 324 | } |
| 325 | MoveTo(&sim, &estimator, 8 * index_diff); |
| 326 | |
| 327 | ASSERT_TRUE(estimator.zeroed()); |
| 328 | ASSERT_FALSE(estimator.error()); |
| 329 | sim.Initialize(9.0 * index_diff + 0.31 * index_diff, index_diff / 3.0, |
| 330 | known_index_pos); |
| 331 | MoveTo(&sim, &estimator, 9 * index_diff); |
| 332 | ASSERT_TRUE(estimator.zeroed()); |
| 333 | ASSERT_TRUE(estimator.error()); |
| 334 | } |
| 335 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 336 | // Makes sure that using an absolute encoder lets us zero without moving. |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 337 | TEST_F(ZeroingTest, TestPotAndAbsoluteEncoderZeroingWithoutMovement) { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 338 | const double index_diff = 1.0; |
| 339 | PositionSensorSimulator sim(index_diff); |
| 340 | |
| 341 | const double start_pos = 2.1; |
| 342 | double measured_absolute_position = 0.3 * index_diff; |
| 343 | |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 344 | PotAndAbsoluteEncoderZeroingConstants constants{ |
| 345 | kSampleSize, index_diff, measured_absolute_position, |
| 346 | 0.1, kMovingBufferSize, kIndexErrorFraction}; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 347 | |
| 348 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 349 | constants.measured_absolute_position); |
| 350 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 351 | PotAndAbsoluteEncoderZeroingEstimator estimator(constants); |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 352 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 353 | for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 354 | MoveTo(&sim, &estimator, start_pos); |
| 355 | ASSERT_FALSE(estimator.zeroed()); |
| 356 | } |
| 357 | |
| 358 | MoveTo(&sim, &estimator, start_pos); |
| 359 | ASSERT_TRUE(estimator.zeroed()); |
| 360 | EXPECT_DOUBLE_EQ(start_pos, estimator.offset()); |
| 361 | } |
| 362 | |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 363 | // Makes sure that we ignore a NAN if we get it, but will correctly zero |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 364 | // afterwards. |
| 365 | TEST_F(ZeroingTest, TestPotAndAbsoluteEncoderZeroingIgnoresNAN) { |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 366 | const double index_diff = 1.0; |
| 367 | PositionSensorSimulator sim(index_diff); |
| 368 | |
| 369 | const double start_pos = 2.1; |
| 370 | double measured_absolute_position = 0.3 * index_diff; |
| 371 | |
| 372 | PotAndAbsoluteEncoderZeroingConstants constants{ |
| 373 | kSampleSize, index_diff, measured_absolute_position, |
| 374 | 0.1, kMovingBufferSize, kIndexErrorFraction}; |
| 375 | |
| 376 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 377 | constants.measured_absolute_position); |
| 378 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 379 | PotAndAbsoluteEncoderZeroingEstimator estimator(constants); |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 380 | |
| 381 | // We tolerate a couple NANs before we start. |
| 382 | PotAndAbsolutePosition sensor_values; |
| 383 | sensor_values.absolute_encoder = ::std::numeric_limits<double>::quiet_NaN(); |
| 384 | sensor_values.encoder = 0.0; |
| 385 | sensor_values.pot = 0.0; |
| 386 | for (size_t i = 0; i < kSampleSize - 1; ++i) { |
| 387 | estimator.UpdateEstimate(sensor_values); |
| 388 | } |
| 389 | |
| 390 | for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) { |
| 391 | MoveTo(&sim, &estimator, start_pos); |
| 392 | ASSERT_FALSE(estimator.zeroed()); |
| 393 | } |
| 394 | |
| 395 | MoveTo(&sim, &estimator, start_pos); |
| 396 | ASSERT_TRUE(estimator.zeroed()); |
| 397 | EXPECT_DOUBLE_EQ(start_pos, estimator.offset()); |
| 398 | } |
| 399 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 400 | // Makes sure that using an absolute encoder doesn't let us zero while moving. |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 401 | TEST_F(ZeroingTest, TestPotAndAbsoluteEncoderZeroingWithMovement) { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 402 | const double index_diff = 1.0; |
| 403 | PositionSensorSimulator sim(index_diff); |
| 404 | |
| 405 | const double start_pos = 10 * index_diff; |
| 406 | double measured_absolute_position = 0.3 * index_diff; |
| 407 | |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 408 | PotAndAbsoluteEncoderZeroingConstants constants{ |
| 409 | kSampleSize, index_diff, measured_absolute_position, |
| 410 | 0.1, kMovingBufferSize, kIndexErrorFraction}; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 411 | |
| 412 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 413 | constants.measured_absolute_position); |
| 414 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 415 | PotAndAbsoluteEncoderZeroingEstimator estimator(constants); |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 416 | |
Diana Vandenberg | 8fea6ea | 2017-02-18 17:24:45 -0800 | [diff] [blame] | 417 | for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) { |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame] | 418 | MoveTo(&sim, &estimator, start_pos + i * index_diff); |
| 419 | ASSERT_FALSE(estimator.zeroed()); |
| 420 | } |
| 421 | MoveTo(&sim, &estimator, start_pos + 10 * index_diff); |
| 422 | |
| 423 | MoveTo(&sim, &estimator, start_pos); |
| 424 | ASSERT_FALSE(estimator.zeroed()); |
| 425 | } |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 426 | |
| 427 | // Makes sure we detect an error if the ZeroingEstimator gets sent a NaN. |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 428 | TEST_F(ZeroingTest, TestPotAndAbsoluteEncoderZeroingWithNaN) { |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 429 | PotAndAbsoluteEncoderZeroingConstants constants{ |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 430 | kSampleSize, 1, 0.3, 0.1, kMovingBufferSize, kIndexErrorFraction}; |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 431 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 432 | PotAndAbsoluteEncoderZeroingEstimator estimator(constants); |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 433 | |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 434 | PotAndAbsolutePosition sensor_values; |
| 435 | sensor_values.absolute_encoder = ::std::numeric_limits<double>::quiet_NaN(); |
| 436 | sensor_values.encoder = 0.0; |
| 437 | sensor_values.pot = 0.0; |
| 438 | // We tolerate a couple NANs before we start. |
| 439 | for (size_t i = 0; i < kSampleSize - 1; ++i) { |
| 440 | estimator.UpdateEstimate(sensor_values); |
| 441 | } |
| 442 | ASSERT_FALSE(estimator.error()); |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 443 | |
Austin Schuh | ddd08f8 | 2018-03-02 20:05:29 -0800 | [diff] [blame] | 444 | estimator.UpdateEstimate(sensor_values); |
Neil Balch | 16275e3 | 2017-02-18 16:38:45 -0800 | [diff] [blame] | 445 | ASSERT_TRUE(estimator.error()); |
| 446 | } |
| 447 | |
Brian Silverman | a10d20a | 2017-02-19 14:28:53 -0800 | [diff] [blame] | 448 | // Tests that an error is detected when the starting position changes too much. |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 449 | TEST_F(ZeroingTest, TestRelativeEncoderZeroing) { |
| 450 | EncoderPlusIndexZeroingConstants constants; |
| 451 | constants.index_pulse_count = 3; |
| 452 | constants.index_difference = 10.0; |
| 453 | constants.measured_index_position = 20.0; |
| 454 | constants.known_index_pulse = 1; |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 455 | constants.allowable_encoder_error = 0.01; |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 456 | |
| 457 | PositionSensorSimulator sim(constants.index_difference); |
| 458 | |
| 459 | const double start_pos = 2.5 * constants.index_difference; |
| 460 | |
| 461 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 462 | constants.measured_index_position); |
| 463 | |
| 464 | PulseIndexZeroingEstimator estimator(constants); |
| 465 | |
| 466 | // Should not be zeroed when we stand still. |
| 467 | for (int i = 0; i < 300; ++i) { |
| 468 | MoveTo(&sim, &estimator, start_pos); |
| 469 | ASSERT_FALSE(estimator.zeroed()); |
| 470 | } |
| 471 | |
| 472 | // Move to 1.5 constants.index_difference and we should still not be zeroed. |
| 473 | MoveTo(&sim, &estimator, 1.5 * constants.index_difference); |
| 474 | ASSERT_FALSE(estimator.zeroed()); |
| 475 | |
| 476 | // Move to 0.5 constants.index_difference and we should still not be zeroed. |
| 477 | MoveTo(&sim, &estimator, 0.5 * constants.index_difference); |
| 478 | ASSERT_FALSE(estimator.zeroed()); |
| 479 | |
| 480 | // Move back to 1.5 constants.index_difference and we should still not be |
| 481 | // zeroed. |
| 482 | MoveTo(&sim, &estimator, 1.5 * constants.index_difference); |
| 483 | ASSERT_FALSE(estimator.zeroed()); |
| 484 | |
| 485 | // Move back to 2.5 constants.index_difference and we should still not be |
| 486 | // zeroed. |
| 487 | MoveTo(&sim, &estimator, 2.5 * constants.index_difference); |
| 488 | ASSERT_FALSE(estimator.zeroed()); |
| 489 | |
| 490 | // Move back to 3.5 constants.index_difference and we should now be zeroed. |
| 491 | MoveTo(&sim, &estimator, 3.5 * constants.index_difference); |
| 492 | ASSERT_TRUE(estimator.zeroed()); |
| 493 | |
| 494 | ASSERT_DOUBLE_EQ(start_pos, estimator.offset()); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 495 | ASSERT_DOUBLE_EQ(3.5 * constants.index_difference, |
| 496 | estimator.GetEstimatorState().position); |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 497 | |
| 498 | MoveTo(&sim, &estimator, 0.5 * constants.index_difference); |
Brian Silverman | f37839c | 2017-02-19 18:07:15 -0800 | [diff] [blame] | 499 | ASSERT_DOUBLE_EQ(0.5 * constants.index_difference, |
| 500 | estimator.GetEstimatorState().position); |
Isaac Wilcove | 0851ffd | 2017-02-16 04:13:14 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Philipp Schrader | 3f5b618 | 2017-03-25 22:36:37 +0000 | [diff] [blame] | 503 | // Tests that we can detect when an index pulse occurs where we didn't expect |
| 504 | // it to for the PulseIndexZeroingEstimator. |
| 505 | TEST_F(ZeroingTest, TestRelativeEncoderSlipping) { |
| 506 | EncoderPlusIndexZeroingConstants constants; |
| 507 | constants.index_pulse_count = 3; |
| 508 | constants.index_difference = 10.0; |
| 509 | constants.measured_index_position = 20.0; |
| 510 | constants.known_index_pulse = 1; |
| 511 | constants.allowable_encoder_error = 0.05; |
| 512 | |
| 513 | PositionSensorSimulator sim(constants.index_difference); |
| 514 | |
| 515 | const double start_pos = |
| 516 | constants.measured_index_position + 0.5 * constants.index_difference; |
| 517 | |
| 518 | for (double direction : {1.0, -1.0}) { |
| 519 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 520 | constants.measured_index_position); |
| 521 | |
| 522 | PulseIndexZeroingEstimator estimator(constants); |
| 523 | |
| 524 | // Zero the estimator. |
| 525 | MoveTo(&sim, &estimator, start_pos - 1 * constants.index_difference); |
| 526 | MoveTo( |
| 527 | &sim, &estimator, |
| 528 | start_pos - constants.index_pulse_count * constants.index_difference); |
| 529 | ASSERT_TRUE(estimator.zeroed()); |
| 530 | ASSERT_FALSE(estimator.error()); |
| 531 | |
| 532 | // We have a 5% allowable error so we slip a little bit each time and make |
| 533 | // sure that the index pulses are still accepted. |
| 534 | for (double error = 0.00; |
| 535 | ::std::abs(error) < constants.allowable_encoder_error; |
| 536 | error += 0.01 * direction) { |
| 537 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 538 | constants.measured_index_position + |
| 539 | error * constants.index_difference); |
| 540 | MoveTo(&sim, &estimator, start_pos - constants.index_difference); |
| 541 | EXPECT_FALSE(estimator.error()); |
| 542 | } |
| 543 | |
| 544 | // As soon as we hit cross the error margin, we should trigger an error. |
| 545 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 546 | constants.measured_index_position + |
| 547 | constants.allowable_encoder_error * 1.1 * |
| 548 | constants.index_difference * direction); |
| 549 | MoveTo(&sim, &estimator, start_pos - constants.index_difference); |
| 550 | ASSERT_TRUE(estimator.error()); |
| 551 | } |
| 552 | } |
| 553 | |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 554 | // Test fixture for HallEffectAndPositionZeroingEstimator. |
| 555 | class HallEffectAndPositionZeroingEstimatorTest : public ZeroingTest { |
| 556 | public: |
| 557 | // The starting position of the system. |
| 558 | static constexpr double kStartPosition = 2.0; |
| 559 | |
| 560 | // Returns a reasonable set of test constants. |
| 561 | static constants::HallEffectZeroingConstants MakeConstants() { |
| 562 | constants::HallEffectZeroingConstants constants; |
| 563 | constants.lower_hall_position = 0.25; |
| 564 | constants.upper_hall_position = 0.75; |
| 565 | constants.index_difference = 1.0; |
| 566 | constants.hall_trigger_zeroing_length = 2; |
| 567 | constants.zeroing_move_direction = false; |
| 568 | return constants; |
| 569 | } |
| 570 | |
| 571 | HallEffectAndPositionZeroingEstimatorTest() |
| 572 | : constants_(MakeConstants()), sim_(constants_.index_difference) { |
| 573 | // Start the system out at the starting position. |
| 574 | sim_.InitializeHallEffectAndPosition(kStartPosition, |
| 575 | constants_.lower_hall_position, |
| 576 | constants_.upper_hall_position); |
| 577 | } |
| 578 | |
| 579 | protected: |
| 580 | // Constants, and the simulation using them. |
| 581 | const constants::HallEffectZeroingConstants constants_; |
| 582 | PositionSensorSimulator sim_; |
| 583 | }; |
| 584 | |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 585 | // Tests that an error is detected when the starting position changes too much. |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 586 | TEST_F(HallEffectAndPositionZeroingEstimatorTest, TestHallEffectZeroing) { |
| 587 | HallEffectAndPositionZeroingEstimator estimator(constants_); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 588 | |
| 589 | // Should not be zeroed when we stand still. |
| 590 | for (int i = 0; i < 300; ++i) { |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 591 | MoveTo(&sim_, &estimator, kStartPosition); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 592 | ASSERT_FALSE(estimator.zeroed()); |
| 593 | } |
| 594 | |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 595 | MoveTo(&sim_, &estimator, 1.9); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 596 | ASSERT_FALSE(estimator.zeroed()); |
| 597 | |
| 598 | // Move to where the hall effect is triggered and make sure it becomes zeroed. |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 599 | MoveTo(&sim_, &estimator, 1.5); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 600 | EXPECT_FALSE(estimator.zeroed()); |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 601 | MoveTo(&sim_, &estimator, 1.5); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 602 | ASSERT_TRUE(estimator.zeroed()); |
| 603 | |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 604 | // Check that the offset is calculated correctly. We should expect to read |
| 605 | // 0.5. Since the encoder is reading -0.5 right now, the offset needs to be |
| 606 | // 1. |
| 607 | EXPECT_DOUBLE_EQ(1.0, estimator.offset()); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 608 | |
| 609 | // Make sure triggering errors works. |
| 610 | estimator.TriggerError(); |
| 611 | ASSERT_TRUE(estimator.error()); |
| 612 | |
| 613 | // Ensure resetting resets the state of the estimator. |
| 614 | estimator.Reset(); |
| 615 | ASSERT_FALSE(estimator.zeroed()); |
| 616 | ASSERT_FALSE(estimator.error()); |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 617 | } |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 618 | |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 619 | // Tests that we don't zero on a too short pulse. |
| 620 | TEST_F(HallEffectAndPositionZeroingEstimatorTest, TestTooShortPulse) { |
| 621 | HallEffectAndPositionZeroingEstimator estimator(constants_); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 622 | |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 623 | // Trigger for 1 cycle. |
| 624 | MoveTo(&sim_, &estimator, 0.9); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 625 | EXPECT_FALSE(estimator.zeroed()); |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 626 | MoveTo(&sim_, &estimator, 0.5); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 627 | EXPECT_FALSE(estimator.zeroed()); |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 628 | MoveTo(&sim_, &estimator, 0.9); |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 629 | EXPECT_FALSE(estimator.zeroed()); |
| 630 | } |
| 631 | |
Austin Schuh | 1fe4fb4 | 2017-10-16 22:35:54 -0700 | [diff] [blame] | 632 | // Tests that we don't zero when we go the wrong direction. |
| 633 | TEST_F(HallEffectAndPositionZeroingEstimatorTest, TestWrongDirectionNoZero) { |
| 634 | HallEffectAndPositionZeroingEstimator estimator(constants_); |
| 635 | |
| 636 | // Pass through the sensor, lingering long enough that we should zero. |
| 637 | MoveTo(&sim_, &estimator, 0.0); |
| 638 | ASSERT_FALSE(estimator.zeroed()); |
| 639 | MoveTo(&sim_, &estimator, 0.4); |
| 640 | EXPECT_FALSE(estimator.zeroed()); |
| 641 | MoveTo(&sim_, &estimator, 0.6); |
| 642 | EXPECT_FALSE(estimator.zeroed()); |
| 643 | MoveTo(&sim_, &estimator, 0.7); |
| 644 | EXPECT_FALSE(estimator.zeroed()); |
| 645 | MoveTo(&sim_, &estimator, 0.9); |
| 646 | EXPECT_FALSE(estimator.zeroed()); |
| 647 | } |
| 648 | |
| 649 | // Make sure we don't zero if we start in the hall effect's range. |
| 650 | TEST_F(HallEffectAndPositionZeroingEstimatorTest, TestStartingOnNoZero) { |
| 651 | HallEffectAndPositionZeroingEstimator estimator(constants_); |
| 652 | MoveTo(&sim_, &estimator, 0.5); |
| 653 | estimator.Reset(); |
| 654 | |
| 655 | // Stay on the hall effect. We shouldn't zero. |
| 656 | EXPECT_FALSE(estimator.zeroed()); |
| 657 | MoveTo(&sim_, &estimator, 0.5); |
| 658 | EXPECT_FALSE(estimator.zeroed()); |
| 659 | MoveTo(&sim_, &estimator, 0.5); |
| 660 | EXPECT_FALSE(estimator.zeroed()); |
| 661 | |
| 662 | // Verify moving off the hall still doesn't zero us. |
| 663 | MoveTo(&sim_, &estimator, 0.0); |
| 664 | EXPECT_FALSE(estimator.zeroed()); |
| 665 | MoveTo(&sim_, &estimator, 0.0); |
| 666 | EXPECT_FALSE(estimator.zeroed()); |
| 667 | } |
Austin Schuh | 5593403 | 2017-03-11 12:45:27 -0800 | [diff] [blame] | 668 | |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame^] | 669 | // Makes sure that using an absolute encoder lets us zero without moving. |
| 670 | TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithoutMovement) { |
| 671 | const double index_diff = 1.0; |
| 672 | PositionSensorSimulator sim(index_diff); |
| 673 | |
| 674 | const double kMiddlePosition = 2.5; |
| 675 | const double start_pos = 2.1; |
| 676 | double measured_absolute_position = 0.3 * index_diff; |
| 677 | |
| 678 | AbsoluteEncoderZeroingConstants constants{ |
| 679 | kSampleSize, index_diff, measured_absolute_position, |
| 680 | kMiddlePosition, 0.1, kMovingBufferSize, |
| 681 | kIndexErrorFraction}; |
| 682 | |
| 683 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 684 | constants.measured_absolute_position); |
| 685 | |
| 686 | AbsoluteEncoderZeroingEstimator estimator(constants); |
| 687 | |
| 688 | for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) { |
| 689 | MoveTo(&sim, &estimator, start_pos); |
| 690 | ASSERT_FALSE(estimator.zeroed()); |
| 691 | } |
| 692 | |
| 693 | MoveTo(&sim, &estimator, start_pos); |
| 694 | ASSERT_TRUE(estimator.zeroed()); |
| 695 | EXPECT_DOUBLE_EQ(start_pos, estimator.offset()); |
| 696 | } |
| 697 | |
| 698 | // Makes sure that we ignore a NAN if we get it, but will correctly zero |
| 699 | // afterwards. |
| 700 | TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingIgnoresNAN) { |
| 701 | const double index_diff = 1.0; |
| 702 | PositionSensorSimulator sim(index_diff); |
| 703 | |
| 704 | const double start_pos = 2.1; |
| 705 | double measured_absolute_position = 0.3 * index_diff; |
| 706 | const double kMiddlePosition = 2.5; |
| 707 | |
| 708 | AbsoluteEncoderZeroingConstants constants{ |
| 709 | kSampleSize, index_diff, measured_absolute_position, |
| 710 | kMiddlePosition, 0.1, kMovingBufferSize, |
| 711 | kIndexErrorFraction}; |
| 712 | |
| 713 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 714 | constants.measured_absolute_position); |
| 715 | |
| 716 | AbsoluteEncoderZeroingEstimator estimator(constants); |
| 717 | |
| 718 | // We tolerate a couple NANs before we start. |
| 719 | AbsolutePosition sensor_values; |
| 720 | sensor_values.absolute_encoder = ::std::numeric_limits<double>::quiet_NaN(); |
| 721 | sensor_values.encoder = 0.0; |
| 722 | for (size_t i = 0; i < kSampleSize - 1; ++i) { |
| 723 | estimator.UpdateEstimate(sensor_values); |
| 724 | } |
| 725 | |
| 726 | for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) { |
| 727 | MoveTo(&sim, &estimator, start_pos); |
| 728 | ASSERT_FALSE(estimator.zeroed()); |
| 729 | } |
| 730 | |
| 731 | MoveTo(&sim, &estimator, start_pos); |
| 732 | ASSERT_TRUE(estimator.zeroed()); |
| 733 | EXPECT_DOUBLE_EQ(start_pos, estimator.offset()); |
| 734 | } |
| 735 | |
| 736 | // Makes sure that using an absolute encoder doesn't let us zero while moving. |
| 737 | TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithMovement) { |
| 738 | const double index_diff = 1.0; |
| 739 | PositionSensorSimulator sim(index_diff); |
| 740 | |
| 741 | const double start_pos = 10 * index_diff; |
| 742 | double measured_absolute_position = 0.3 * index_diff; |
| 743 | const double kMiddlePosition = 2.5; |
| 744 | |
| 745 | AbsoluteEncoderZeroingConstants constants{ |
| 746 | kSampleSize, index_diff, measured_absolute_position, |
| 747 | kMiddlePosition, 0.1, kMovingBufferSize, |
| 748 | kIndexErrorFraction}; |
| 749 | |
| 750 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 751 | constants.measured_absolute_position); |
| 752 | |
| 753 | AbsoluteEncoderZeroingEstimator estimator(constants); |
| 754 | |
| 755 | for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) { |
| 756 | MoveTo(&sim, &estimator, start_pos + i * index_diff); |
| 757 | ASSERT_FALSE(estimator.zeroed()); |
| 758 | } |
| 759 | MoveTo(&sim, &estimator, start_pos + 10 * index_diff); |
| 760 | |
| 761 | MoveTo(&sim, &estimator, start_pos); |
| 762 | ASSERT_FALSE(estimator.zeroed()); |
| 763 | } |
| 764 | |
| 765 | // Makes sure we detect an error if the ZeroingEstimator gets sent a NaN. |
| 766 | TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithNaN) { |
| 767 | AbsoluteEncoderZeroingConstants constants{ |
| 768 | kSampleSize, 1, 0.3, 1.0, 0.1, kMovingBufferSize, kIndexErrorFraction}; |
| 769 | |
| 770 | AbsoluteEncoderZeroingEstimator estimator(constants); |
| 771 | |
| 772 | AbsolutePosition sensor_values; |
| 773 | sensor_values.absolute_encoder = ::std::numeric_limits<double>::quiet_NaN(); |
| 774 | sensor_values.encoder = 0.0; |
| 775 | // We tolerate a couple NANs before we start. |
| 776 | for (size_t i = 0; i < kSampleSize - 1; ++i) { |
| 777 | estimator.UpdateEstimate(sensor_values); |
| 778 | } |
| 779 | ASSERT_FALSE(estimator.error()); |
| 780 | |
| 781 | estimator.UpdateEstimate(sensor_values); |
| 782 | ASSERT_TRUE(estimator.error()); |
| 783 | } |
| 784 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 785 | } // namespace zeroing |
| 786 | } // namespace frc971 |