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" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 11 | #include "aos/common/util/thread.h" |
| 12 | #include "aos/common/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; |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 19 | using constants::PotAndIndexPulseZeroingConstants; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 20 | using constants::PotAndAbsoluteEncoderZeroingConstants; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 21 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 22 | static const size_t kSampleSize = 30; |
| 23 | static const double kAcceptableUnzeroedError = 0.2; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 24 | static const double kIndexErrorFraction = 0.3; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 25 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 26 | class ZeroingTest : public ::testing::Test { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 27 | protected: |
| 28 | void SetUp() override { aos::SetDieTestMode(true); } |
| 29 | |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 30 | void MoveTo(PositionSensorSimulator *simulator, |
| 31 | PotAndIndexPulseZeroingEstimator *estimator, |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 32 | double new_position) { |
Brian Silverman | dc4eb10 | 2017-02-05 17:34:41 -0800 | [diff] [blame] | 33 | PotAndIndexPosition sensor_values; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 34 | simulator->MoveTo(new_position); |
Brian Silverman | dc4eb10 | 2017-02-05 17:34:41 -0800 | [diff] [blame] | 35 | simulator->GetSensorValues(&sensor_values); |
| 36 | estimator->UpdateEstimate(sensor_values); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 37 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 38 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 39 | void MoveTo(PositionSensorSimulator *simulator, |
| 40 | PotAndAbsEncoderZeroingEstimator *estimator, double new_position) { |
| 41 | PotAndAbsolutePosition sensor_values_; |
| 42 | simulator->MoveTo(new_position); |
| 43 | simulator->GetSensorValues(&sensor_values_); |
| 44 | estimator->UpdateEstimate(sensor_values_); |
| 45 | } |
| 46 | // TODO(phil): Add new MoveTo overloads for different zeroing estimators. |
| 47 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 48 | ::aos::testing::TestSharedMemory my_shm_; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 51 | TEST_F(ZeroingTest, TestMovingAverageFilter) { |
| 52 | const double index_diff = 1.0; |
| 53 | PositionSensorSimulator sim(index_diff); |
| 54 | sim.Initialize(3.6 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 55 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 56 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 57 | |
| 58 | // The zeroing code is supposed to perform some filtering on the difference |
| 59 | // between the potentiometer value and the encoder value. We assume that 300 |
| 60 | // samples are sufficient to have updated the filter. |
| 61 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 62 | MoveTo(&sim, &estimator, 3.3 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 63 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 64 | ASSERT_NEAR(3.3 * index_diff, estimator.position(), |
| 65 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 66 | |
| 67 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 68 | MoveTo(&sim, &estimator, 3.9 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 69 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 70 | ASSERT_NEAR(3.9 * index_diff, estimator.position(), |
| 71 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 74 | TEST_F(ZeroingTest, NotZeroedBeforeEnoughSamplesCollected) { |
| 75 | double index_diff = 0.5; |
| 76 | double position = 3.6 * index_diff; |
| 77 | PositionSensorSimulator sim(index_diff); |
| 78 | sim.Initialize(position, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 79 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 80 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 81 | |
| 82 | // Make sure that the zeroing code does not consider itself zeroed until we |
| 83 | // collect a good amount of samples. In this case we're waiting until the |
| 84 | // moving average filter is full. |
| 85 | for (unsigned int i = 0; i < kSampleSize - 1; i++) { |
| 86 | MoveTo(&sim, &estimator, position += index_diff); |
| 87 | ASSERT_FALSE(estimator.zeroed()); |
| 88 | } |
| 89 | |
| 90 | MoveTo(&sim, &estimator, position); |
| 91 | ASSERT_TRUE(estimator.zeroed()); |
| 92 | } |
| 93 | |
| 94 | TEST_F(ZeroingTest, TestLotsOfMovement) { |
| 95 | double index_diff = 1.0; |
| 96 | PositionSensorSimulator sim(index_diff); |
| 97 | sim.Initialize(3.6, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 98 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 99 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 100 | |
| 101 | // The zeroing code is supposed to perform some filtering on the difference |
| 102 | // between the potentiometer value and the encoder value. We assume that 300 |
| 103 | // samples are sufficient to have updated the filter. |
| 104 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 105 | MoveTo(&sim, &estimator, 3.6); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 106 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 107 | ASSERT_NEAR(3.6, estimator.position(), kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 108 | |
| 109 | // With a single index pulse the zeroing estimator should be able to lock |
| 110 | // onto the true value of the position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 111 | MoveTo(&sim, &estimator, 4.01); |
| 112 | ASSERT_NEAR(4.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 113 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 114 | MoveTo(&sim, &estimator, 4.99); |
| 115 | ASSERT_NEAR(4.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 116 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 117 | MoveTo(&sim, &estimator, 3.99); |
| 118 | ASSERT_NEAR(3.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 119 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 120 | MoveTo(&sim, &estimator, 3.01); |
| 121 | ASSERT_NEAR(3.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 122 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 123 | MoveTo(&sim, &estimator, 13.55); |
| 124 | ASSERT_NEAR(13.55, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 127 | TEST_F(ZeroingTest, TestDifferentIndexDiffs) { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 128 | double index_diff = 0.89; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 129 | PositionSensorSimulator sim(index_diff); |
| 130 | sim.Initialize(3.5 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 131 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 132 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 133 | |
| 134 | // The zeroing code is supposed to perform some filtering on the difference |
| 135 | // between the potentiometer value and the encoder value. We assume that 300 |
| 136 | // samples are sufficient to have updated the filter. |
| 137 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 138 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 139 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 140 | ASSERT_NEAR(3.5 * index_diff, estimator.position(), |
| 141 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 142 | |
| 143 | // With a single index pulse the zeroing estimator should be able to lock |
| 144 | // onto the true value of the position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 145 | MoveTo(&sim, &estimator, 4.01); |
| 146 | ASSERT_NEAR(4.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 147 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 148 | MoveTo(&sim, &estimator, 4.99); |
| 149 | ASSERT_NEAR(4.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 150 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 151 | MoveTo(&sim, &estimator, 3.99); |
| 152 | ASSERT_NEAR(3.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 153 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 154 | MoveTo(&sim, &estimator, 3.01); |
| 155 | ASSERT_NEAR(3.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 156 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 157 | MoveTo(&sim, &estimator, 13.55); |
| 158 | ASSERT_NEAR(13.55, estimator.position(), 0.001); |
| 159 | } |
| 160 | |
| 161 | TEST_F(ZeroingTest, TestPercentage) { |
| 162 | double index_diff = 0.89; |
| 163 | PositionSensorSimulator sim(index_diff); |
| 164 | sim.Initialize(3.5 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 165 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 166 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 167 | |
| 168 | for (unsigned int i = 0; i < kSampleSize / 2; i++) { |
| 169 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 170 | } |
| 171 | ASSERT_NEAR(0.5, estimator.offset_ratio_ready(), 0.001); |
Austin Schuh | 7485dbb | 2016-02-08 00:21:58 -0800 | [diff] [blame] | 172 | ASSERT_FALSE(estimator.offset_ready()); |
| 173 | |
| 174 | for (unsigned int i = 0; i < kSampleSize / 2; i++) { |
| 175 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 176 | } |
| 177 | ASSERT_NEAR(1.0, estimator.offset_ratio_ready(), 0.001); |
| 178 | ASSERT_TRUE(estimator.offset_ready()); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | TEST_F(ZeroingTest, TestOffset) { |
| 182 | double index_diff = 0.89; |
| 183 | PositionSensorSimulator sim(index_diff); |
| 184 | sim.Initialize(3.1 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 185 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 186 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 187 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 188 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 189 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 190 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 191 | MoveTo(&sim, &estimator, 5.0 * index_diff); |
| 192 | } |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 193 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 194 | ASSERT_NEAR(3.1 * index_diff, estimator.offset(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 197 | TEST_F(ZeroingTest, WaitForIndexPulseAfterReset) { |
| 198 | double index_diff = 0.6; |
| 199 | PositionSensorSimulator sim(index_diff); |
| 200 | sim.Initialize(3.1 * index_diff, index_diff / 3.0); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 201 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 202 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 203 | |
| 204 | // Make sure to fill up the averaging filter with samples. |
| 205 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 206 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 207 | } |
| 208 | |
| 209 | // Make sure we're not zeroed until we hit an index pulse. |
| 210 | ASSERT_FALSE(estimator.zeroed()); |
| 211 | |
| 212 | // Trigger an index pulse; we should now be zeroed. |
| 213 | MoveTo(&sim, &estimator, 4.5 * index_diff); |
| 214 | ASSERT_TRUE(estimator.zeroed()); |
| 215 | |
| 216 | // Reset the zeroing logic and supply a bunch of samples within the current |
| 217 | // index segment. |
| 218 | estimator.Reset(); |
| 219 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 220 | MoveTo(&sim, &estimator, 4.2 * index_diff); |
| 221 | } |
| 222 | |
| 223 | // Make sure we're not zeroed until we hit an index pulse. |
| 224 | ASSERT_FALSE(estimator.zeroed()); |
| 225 | |
| 226 | // Trigger another index pulse; we should be zeroed again. |
| 227 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 228 | ASSERT_TRUE(estimator.zeroed()); |
| 229 | } |
| 230 | |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 231 | TEST_F(ZeroingTest, TestNonZeroIndexPulseOffsets) { |
| 232 | const double index_diff = 0.9; |
| 233 | const double known_index_pos = 3.5 * index_diff; |
| 234 | PositionSensorSimulator sim(index_diff); |
| 235 | 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] | 236 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 237 | kSampleSize, index_diff, known_index_pos, kIndexErrorFraction}); |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 238 | |
| 239 | // Make sure to fill up the averaging filter with samples. |
| 240 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 241 | MoveTo(&sim, &estimator, 3.3 * index_diff); |
| 242 | } |
| 243 | |
| 244 | // Make sure we're not zeroed until we hit an index pulse. |
| 245 | ASSERT_FALSE(estimator.zeroed()); |
| 246 | |
| 247 | // Trigger an index pulse; we should now be zeroed. |
| 248 | MoveTo(&sim, &estimator, 3.7 * index_diff); |
| 249 | ASSERT_TRUE(estimator.zeroed()); |
| 250 | ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset()); |
| 251 | ASSERT_DOUBLE_EQ(3.7 * index_diff, estimator.position()); |
| 252 | |
| 253 | // Trigger one more index pulse and check the offset. |
| 254 | MoveTo(&sim, &estimator, 4.7 * index_diff); |
| 255 | ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset()); |
| 256 | ASSERT_DOUBLE_EQ(4.7 * index_diff, estimator.position()); |
| 257 | } |
| 258 | |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 259 | TEST_F(ZeroingTest, BasicErrorAPITest) { |
| 260 | const double index_diff = 1.0; |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 261 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 262 | kSampleSize, index_diff, 0.0, kIndexErrorFraction}); |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame] | 263 | PositionSensorSimulator sim(index_diff); |
| 264 | sim.Initialize(1.5 * index_diff, index_diff / 3.0, 0.0); |
| 265 | |
| 266 | // Perform a simple move and make sure that no error occured. |
| 267 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 268 | ASSERT_FALSE(estimator.error()); |
| 269 | |
| 270 | // Trigger an error and make sure it's reported. |
| 271 | estimator.TriggerError(); |
| 272 | ASSERT_TRUE(estimator.error()); |
| 273 | |
| 274 | // Make sure that it can recover after a reset. |
| 275 | estimator.Reset(); |
| 276 | ASSERT_FALSE(estimator.error()); |
| 277 | MoveTo(&sim, &estimator, 4.5 * index_diff); |
| 278 | MoveTo(&sim, &estimator, 5.5 * index_diff); |
| 279 | ASSERT_FALSE(estimator.error()); |
| 280 | } |
| 281 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 282 | // I want to test that the the zeroing class can |
| 283 | // detect an error when the starting position |
| 284 | // changes too much. I do so by creating the |
| 285 | // simulator at an 'X' positon, making sure |
| 286 | // that the estimator is zeroed, and then |
| 287 | // initializing the simulator at another |
| 288 | // position. After making sure it's zeroed, |
| 289 | // if the error() function returns true, |
| 290 | // then, it works. |
| 291 | TEST_F(ZeroingTest, TestOffsetError) { |
| 292 | const double index_diff = 0.8; |
| 293 | const double known_index_pos = 2 * index_diff; |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 294 | const size_t sample_size = 30; |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 295 | PositionSensorSimulator sim(index_diff); |
| 296 | sim.Initialize(10 * index_diff, index_diff / 3.0, known_index_pos); |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 297 | PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{ |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 298 | sample_size, index_diff, known_index_pos, kIndexErrorFraction}); |
| 299 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 300 | for (size_t i = 0; i < sample_size; i++) { |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 301 | MoveTo(&sim, &estimator, 13 * index_diff); |
| 302 | } |
| 303 | MoveTo(&sim, &estimator, 8 * index_diff); |
| 304 | |
| 305 | ASSERT_TRUE(estimator.zeroed()); |
| 306 | ASSERT_FALSE(estimator.error()); |
| 307 | sim.Initialize(9.0 * index_diff + 0.31 * index_diff, index_diff / 3.0, |
| 308 | known_index_pos); |
| 309 | MoveTo(&sim, &estimator, 9 * index_diff); |
| 310 | ASSERT_TRUE(estimator.zeroed()); |
| 311 | ASSERT_TRUE(estimator.error()); |
| 312 | } |
| 313 | |
Austin Schuh | 5f01f15 | 2017-02-11 21:34:08 -0800 | [diff] [blame^] | 314 | // Makes sure that using an absolute encoder lets us zero without moving. |
| 315 | TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithoutMovement) { |
| 316 | const double index_diff = 1.0; |
| 317 | PositionSensorSimulator sim(index_diff); |
| 318 | |
| 319 | const double start_pos = 2.1; |
| 320 | double measured_absolute_position = 0.3 * index_diff; |
| 321 | |
| 322 | PotAndAbsoluteEncoderZeroingConstants constants{ |
| 323 | kSampleSize, index_diff, measured_absolute_position, 0.1}; |
| 324 | |
| 325 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 326 | constants.measured_absolute_position); |
| 327 | |
| 328 | PotAndAbsEncoderZeroingEstimator estimator(constants); |
| 329 | |
| 330 | for (size_t i = 0; i < kSampleSize - 1; ++i) { |
| 331 | MoveTo(&sim, &estimator, start_pos); |
| 332 | ASSERT_FALSE(estimator.zeroed()); |
| 333 | } |
| 334 | |
| 335 | MoveTo(&sim, &estimator, start_pos); |
| 336 | ASSERT_TRUE(estimator.zeroed()); |
| 337 | EXPECT_DOUBLE_EQ(start_pos, estimator.offset()); |
| 338 | } |
| 339 | |
| 340 | // Makes sure that using an absolute encoder doesn't let us zero while moving. |
| 341 | TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithMovement) { |
| 342 | const double index_diff = 1.0; |
| 343 | PositionSensorSimulator sim(index_diff); |
| 344 | |
| 345 | const double start_pos = 10 * index_diff; |
| 346 | double measured_absolute_position = 0.3 * index_diff; |
| 347 | |
| 348 | PotAndAbsoluteEncoderZeroingConstants constants{ |
| 349 | kSampleSize, index_diff, measured_absolute_position, 0.1}; |
| 350 | |
| 351 | sim.Initialize(start_pos, index_diff / 3.0, 0.0, |
| 352 | constants.measured_absolute_position); |
| 353 | |
| 354 | PotAndAbsEncoderZeroingEstimator estimator(constants); |
| 355 | |
| 356 | for (size_t i = 0; i < kSampleSize - 1; ++i) { |
| 357 | MoveTo(&sim, &estimator, start_pos + i * index_diff); |
| 358 | ASSERT_FALSE(estimator.zeroed()); |
| 359 | } |
| 360 | MoveTo(&sim, &estimator, start_pos + 10 * index_diff); |
| 361 | |
| 362 | MoveTo(&sim, &estimator, start_pos); |
| 363 | ASSERT_FALSE(estimator.zeroed()); |
| 364 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 365 | } // namespace zeroing |
| 366 | } // namespace frc971 |