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