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