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