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" |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 10 | #include "aos/common/queue_testutils.h" |
| 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; |
| 19 | using constants::Values; |
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 | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 23 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 24 | class ZeroingTest : public ::testing::Test { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 25 | protected: |
| 26 | void SetUp() override { aos::SetDieTestMode(true); } |
| 27 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 28 | void MoveTo(PositionSensorSimulator* simulator, ZeroingEstimator* estimator, |
| 29 | double new_position) { |
| 30 | PotAndIndexPosition sensor_values_; |
| 31 | simulator->MoveTo(new_position); |
| 32 | simulator->GetSensorValues(&sensor_values_); |
| 33 | estimator->UpdateEstimate(sensor_values_); |
| 34 | } |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 35 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 36 | aos::common::testing::GlobalCoreInstance my_core; |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 39 | TEST_F(ZeroingTest, TestMovingAverageFilter) { |
| 40 | const double index_diff = 1.0; |
| 41 | PositionSensorSimulator sim(index_diff); |
| 42 | sim.Initialize(3.6 * index_diff, index_diff / 3.0); |
| 43 | ZeroingEstimator estimator( |
| 44 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 45 | |
| 46 | // The zeroing code is supposed to perform some filtering on the difference |
| 47 | // between the potentiometer value and the encoder value. We assume that 300 |
| 48 | // samples are sufficient to have updated the filter. |
| 49 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 50 | MoveTo(&sim, &estimator, 3.3 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 51 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 52 | ASSERT_NEAR(3.3 * index_diff, estimator.position(), |
| 53 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 54 | |
| 55 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 56 | MoveTo(&sim, &estimator, 3.9 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 57 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 58 | ASSERT_NEAR(3.9 * index_diff, estimator.position(), |
| 59 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 62 | TEST_F(ZeroingTest, NotZeroedBeforeEnoughSamplesCollected) { |
| 63 | double index_diff = 0.5; |
| 64 | double position = 3.6 * index_diff; |
| 65 | PositionSensorSimulator sim(index_diff); |
| 66 | sim.Initialize(position, index_diff / 3.0); |
| 67 | ZeroingEstimator estimator( |
| 68 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
| 69 | |
| 70 | // Make sure that the zeroing code does not consider itself zeroed until we |
| 71 | // collect a good amount of samples. In this case we're waiting until the |
| 72 | // moving average filter is full. |
| 73 | for (unsigned int i = 0; i < kSampleSize - 1; i++) { |
| 74 | MoveTo(&sim, &estimator, position += index_diff); |
| 75 | ASSERT_FALSE(estimator.zeroed()); |
| 76 | } |
| 77 | |
| 78 | MoveTo(&sim, &estimator, position); |
| 79 | ASSERT_TRUE(estimator.zeroed()); |
| 80 | } |
| 81 | |
| 82 | TEST_F(ZeroingTest, TestLotsOfMovement) { |
| 83 | double index_diff = 1.0; |
| 84 | PositionSensorSimulator sim(index_diff); |
| 85 | sim.Initialize(3.6, index_diff / 3.0); |
| 86 | ZeroingEstimator estimator( |
| 87 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 88 | |
| 89 | // The zeroing code is supposed to perform some filtering on the difference |
| 90 | // between the potentiometer value and the encoder value. We assume that 300 |
| 91 | // samples are sufficient to have updated the filter. |
| 92 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 93 | MoveTo(&sim, &estimator, 3.6); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 94 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 95 | ASSERT_NEAR(3.6, estimator.position(), kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 96 | |
| 97 | // With a single index pulse the zeroing estimator should be able to lock |
| 98 | // onto the true value of the position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 99 | MoveTo(&sim, &estimator, 4.01); |
| 100 | ASSERT_NEAR(4.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 101 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 102 | MoveTo(&sim, &estimator, 4.99); |
| 103 | ASSERT_NEAR(4.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 104 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 105 | MoveTo(&sim, &estimator, 3.99); |
| 106 | ASSERT_NEAR(3.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 107 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 108 | MoveTo(&sim, &estimator, 3.01); |
| 109 | ASSERT_NEAR(3.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 110 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 111 | MoveTo(&sim, &estimator, 13.55); |
| 112 | ASSERT_NEAR(13.55, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 115 | TEST_F(ZeroingTest, TestDifferentIndexDiffs) { |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 116 | double index_diff = 0.89; |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 117 | PositionSensorSimulator sim(index_diff); |
| 118 | sim.Initialize(3.5 * index_diff, index_diff / 3.0); |
| 119 | ZeroingEstimator estimator( |
| 120 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 121 | |
| 122 | // The zeroing code is supposed to perform some filtering on the difference |
| 123 | // between the potentiometer value and the encoder value. We assume that 300 |
| 124 | // samples are sufficient to have updated the filter. |
| 125 | for (int i = 0; i < 300; i++) { |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 126 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 127 | } |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 128 | ASSERT_NEAR(3.5 * index_diff, estimator.position(), |
| 129 | kAcceptableUnzeroedError * index_diff); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 130 | |
| 131 | // With a single index pulse the zeroing estimator should be able to lock |
| 132 | // onto the true value of the position. |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 133 | MoveTo(&sim, &estimator, 4.01); |
| 134 | ASSERT_NEAR(4.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 135 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 136 | MoveTo(&sim, &estimator, 4.99); |
| 137 | ASSERT_NEAR(4.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 138 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 139 | MoveTo(&sim, &estimator, 3.99); |
| 140 | ASSERT_NEAR(3.99, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 141 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 142 | MoveTo(&sim, &estimator, 3.01); |
| 143 | ASSERT_NEAR(3.01, estimator.position(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 144 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 145 | MoveTo(&sim, &estimator, 13.55); |
| 146 | ASSERT_NEAR(13.55, estimator.position(), 0.001); |
| 147 | } |
| 148 | |
| 149 | TEST_F(ZeroingTest, TestPercentage) { |
| 150 | double index_diff = 0.89; |
| 151 | PositionSensorSimulator sim(index_diff); |
| 152 | sim.Initialize(3.5 * index_diff, index_diff / 3.0); |
| 153 | ZeroingEstimator estimator( |
| 154 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
| 155 | |
| 156 | for (unsigned int i = 0; i < kSampleSize / 2; i++) { |
| 157 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 158 | } |
| 159 | ASSERT_NEAR(0.5, estimator.offset_ratio_ready(), 0.001); |
| 160 | } |
| 161 | |
| 162 | TEST_F(ZeroingTest, TestOffset) { |
| 163 | double index_diff = 0.89; |
| 164 | PositionSensorSimulator sim(index_diff); |
| 165 | sim.Initialize(3.1 * index_diff, index_diff / 3.0); |
| 166 | ZeroingEstimator estimator( |
| 167 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
| 168 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 169 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 170 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 171 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 172 | MoveTo(&sim, &estimator, 5.0 * index_diff); |
| 173 | } |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 174 | |
Adam Snaider | b411925 | 2015-02-15 01:30:57 +0000 | [diff] [blame] | 175 | ASSERT_NEAR(3.1 * index_diff, estimator.offset(), 0.001); |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Philipp Schrader | 41d8291 | 2015-02-15 03:44:23 +0000 | [diff] [blame] | 178 | TEST_F(ZeroingTest, WaitForIndexPulseAfterReset) { |
| 179 | double index_diff = 0.6; |
| 180 | PositionSensorSimulator sim(index_diff); |
| 181 | sim.Initialize(3.1 * index_diff, index_diff / 3.0); |
| 182 | ZeroingEstimator estimator( |
| 183 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
| 184 | |
| 185 | // Make sure to fill up the averaging filter with samples. |
| 186 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 187 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 188 | } |
| 189 | |
| 190 | // Make sure we're not zeroed until we hit an index pulse. |
| 191 | ASSERT_FALSE(estimator.zeroed()); |
| 192 | |
| 193 | // Trigger an index pulse; we should now be zeroed. |
| 194 | MoveTo(&sim, &estimator, 4.5 * index_diff); |
| 195 | ASSERT_TRUE(estimator.zeroed()); |
| 196 | |
| 197 | // Reset the zeroing logic and supply a bunch of samples within the current |
| 198 | // index segment. |
| 199 | estimator.Reset(); |
| 200 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 201 | MoveTo(&sim, &estimator, 4.2 * index_diff); |
| 202 | } |
| 203 | |
| 204 | // Make sure we're not zeroed until we hit an index pulse. |
| 205 | ASSERT_FALSE(estimator.zeroed()); |
| 206 | |
| 207 | // Trigger another index pulse; we should be zeroed again. |
| 208 | MoveTo(&sim, &estimator, 3.1 * index_diff); |
| 209 | ASSERT_TRUE(estimator.zeroed()); |
| 210 | } |
| 211 | |
Philipp Schrader | 030ad18 | 2015-02-15 05:40:58 +0000 | [diff] [blame] | 212 | TEST_F(ZeroingTest, TestNonZeroIndexPulseOffsets) { |
| 213 | const double index_diff = 0.9; |
| 214 | const double known_index_pos = 3.5 * index_diff; |
| 215 | PositionSensorSimulator sim(index_diff); |
| 216 | sim.Initialize(3.3 * index_diff, index_diff / 3.0, known_index_pos); |
| 217 | ZeroingEstimator estimator( |
| 218 | Values::ZeroingConstants{kSampleSize, index_diff, known_index_pos}); |
| 219 | |
| 220 | // Make sure to fill up the averaging filter with samples. |
| 221 | for (unsigned int i = 0; i < kSampleSize; i++) { |
| 222 | MoveTo(&sim, &estimator, 3.3 * index_diff); |
| 223 | } |
| 224 | |
| 225 | // Make sure we're not zeroed until we hit an index pulse. |
| 226 | ASSERT_FALSE(estimator.zeroed()); |
| 227 | |
| 228 | // Trigger an index pulse; we should now be zeroed. |
| 229 | MoveTo(&sim, &estimator, 3.7 * index_diff); |
| 230 | ASSERT_TRUE(estimator.zeroed()); |
| 231 | ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset()); |
| 232 | ASSERT_DOUBLE_EQ(3.7 * index_diff, estimator.position()); |
| 233 | |
| 234 | // Trigger one more index pulse and check the offset. |
| 235 | MoveTo(&sim, &estimator, 4.7 * index_diff); |
| 236 | ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset()); |
| 237 | ASSERT_DOUBLE_EQ(4.7 * index_diff, estimator.position()); |
| 238 | } |
| 239 | |
Philipp Schrader | 53f4b6d | 2015-02-15 22:32:08 +0000 | [diff] [blame^] | 240 | TEST_F(ZeroingTest, BasicErrorAPITest) { |
| 241 | const double index_diff = 1.0; |
| 242 | ZeroingEstimator estimator( |
| 243 | Values::ZeroingConstants{kSampleSize, index_diff, 0.0}); |
| 244 | PositionSensorSimulator sim(index_diff); |
| 245 | sim.Initialize(1.5 * index_diff, index_diff / 3.0, 0.0); |
| 246 | |
| 247 | // Perform a simple move and make sure that no error occured. |
| 248 | MoveTo(&sim, &estimator, 3.5 * index_diff); |
| 249 | ASSERT_FALSE(estimator.error()); |
| 250 | |
| 251 | // Trigger an error and make sure it's reported. |
| 252 | estimator.TriggerError(); |
| 253 | ASSERT_TRUE(estimator.error()); |
| 254 | |
| 255 | // Make sure that it can recover after a reset. |
| 256 | estimator.Reset(); |
| 257 | ASSERT_FALSE(estimator.error()); |
| 258 | MoveTo(&sim, &estimator, 4.5 * index_diff); |
| 259 | MoveTo(&sim, &estimator, 5.5 * index_diff); |
| 260 | ASSERT_FALSE(estimator.error()); |
| 261 | } |
| 262 | |
Adam Snaider | c4b3c19 | 2015-02-01 01:30:39 +0000 | [diff] [blame] | 263 | } // namespace zeroing |
| 264 | } // namespace frc971 |