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