blob: 819fad01fee22d37681ef9c0512ad3a1d465a279 [file] [log] [blame]
Adam Snaiderc4b3c192015-02-01 01:30:39 +00001#include <unistd.h>
2
3#include <memory>
4
5#include <random>
6
7#include "gtest/gtest.h"
Adam Snaiderc4b3c192015-02-01 01:30:39 +00008#include "frc971/zeroing/zeroing.h"
Adam Snaiderb4119252015-02-15 01:30:57 +00009#include "frc971/control_loops/control_loops.q.h"
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050010#include "aos/testing/test_shm.h"
Adam Snaiderc4b3c192015-02-01 01:30:39 +000011#include "aos/common/util/thread.h"
12#include "aos/common/die.h"
Adam Snaiderb4119252015-02-15 01:30:57 +000013#include "frc971/control_loops/position_sensor_sim.h"
Adam Snaiderc4b3c192015-02-01 01:30:39 +000014
15namespace frc971 {
16namespace zeroing {
17
Adam Snaiderb4119252015-02-15 01:30:57 +000018using control_loops::PositionSensorSimulator;
Tyler Chatowf8f03112017-02-05 14:31:34 -080019using constants::PotAndIndexPulseZeroingConstants;
Austin Schuh5f01f152017-02-11 21:34:08 -080020using constants::PotAndAbsoluteEncoderZeroingConstants;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000021using constants::EncoderPlusIndexZeroingConstants;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000022
Adam Snaiderb4119252015-02-15 01:30:57 +000023static const size_t kSampleSize = 30;
24static const double kAcceptableUnzeroedError = 0.2;
Adam Snaider3cd11c52015-02-16 02:16:09 +000025static const double kIndexErrorFraction = 0.3;
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -080026static const size_t kMovingBufferSize = 3;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000027
Adam Snaiderb4119252015-02-15 01:30:57 +000028class ZeroingTest : public ::testing::Test {
Adam Snaiderc4b3c192015-02-01 01:30:39 +000029 protected:
30 void SetUp() override { aos::SetDieTestMode(true); }
31
Tyler Chatowf8f03112017-02-05 14:31:34 -080032 void MoveTo(PositionSensorSimulator *simulator,
33 PotAndIndexPulseZeroingEstimator *estimator,
Adam Snaiderb4119252015-02-15 01:30:57 +000034 double new_position) {
Brian Silvermandc4eb102017-02-05 17:34:41 -080035 PotAndIndexPosition sensor_values;
Adam Snaiderb4119252015-02-15 01:30:57 +000036 simulator->MoveTo(new_position);
Brian Silvermandc4eb102017-02-05 17:34:41 -080037 simulator->GetSensorValues(&sensor_values);
38 estimator->UpdateEstimate(sensor_values);
Adam Snaiderb4119252015-02-15 01:30:57 +000039 }
Adam Snaiderc4b3c192015-02-01 01:30:39 +000040
Austin Schuh5f01f152017-02-11 21:34:08 -080041 void MoveTo(PositionSensorSimulator *simulator,
Neil Balch16275e32017-02-18 16:38:45 -080042 PotAndAbsEncoderZeroingEstimator *estimator,
43 double new_position) {
Austin Schuh5f01f152017-02-11 21:34:08 -080044 PotAndAbsolutePosition sensor_values_;
45 simulator->MoveTo(new_position);
46 simulator->GetSensorValues(&sensor_values_);
47 estimator->UpdateEstimate(sensor_values_);
48 }
Isaac Wilcove0851ffd2017-02-16 04:13:14 +000049
50 void MoveTo(PositionSensorSimulator *simulator,
51 PulseIndexZeroingEstimator *estimator, double new_position) {
52 IndexPosition sensor_values_;
53 simulator->MoveTo(new_position);
54 simulator->GetSensorValues(&sensor_values_);
55 estimator->UpdateEstimate(sensor_values_);
56 }
Austin Schuh5f01f152017-02-11 21:34:08 -080057
Austin Schuh55934032017-03-11 12:45:27 -080058 void MoveTo(PositionSensorSimulator *simulator,
59 HallEffectAndPositionZeroingEstimator *estimator,
60 double new_position) {
61 HallEffectAndPosition sensor_values_;
62 simulator->MoveTo(new_position);
63 simulator->GetSensorValues(&sensor_values_);
64 estimator->UpdateEstimate(sensor_values_);
65 }
66
Brian Silvermanf5f8d8e2015-12-06 18:39:12 -050067 ::aos::testing::TestSharedMemory my_shm_;
Adam Snaiderc4b3c192015-02-01 01:30:39 +000068};
69
Adam Snaiderb4119252015-02-15 01:30:57 +000070TEST_F(ZeroingTest, TestMovingAverageFilter) {
71 const double index_diff = 1.0;
72 PositionSensorSimulator sim(index_diff);
73 sim.Initialize(3.6 * index_diff, index_diff / 3.0);
Tyler Chatowf8f03112017-02-05 14:31:34 -080074 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +000075 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Adam Snaiderc4b3c192015-02-01 01:30:39 +000076
77 // The zeroing code is supposed to perform some filtering on the difference
78 // between the potentiometer value and the encoder value. We assume that 300
79 // samples are sufficient to have updated the filter.
80 for (int i = 0; i < 300; i++) {
Adam Snaiderb4119252015-02-15 01:30:57 +000081 MoveTo(&sim, &estimator, 3.3 * index_diff);
Adam Snaiderc4b3c192015-02-01 01:30:39 +000082 }
Brian Silvermanf37839c2017-02-19 18:07:15 -080083 ASSERT_NEAR(3.3 * index_diff, estimator.GetEstimatorState().position,
Adam Snaiderb4119252015-02-15 01:30:57 +000084 kAcceptableUnzeroedError * index_diff);
Adam Snaiderc4b3c192015-02-01 01:30:39 +000085
86 for (int i = 0; i < 300; i++) {
Adam Snaiderb4119252015-02-15 01:30:57 +000087 MoveTo(&sim, &estimator, 3.9 * index_diff);
Adam Snaiderc4b3c192015-02-01 01:30:39 +000088 }
Brian Silvermanf37839c2017-02-19 18:07:15 -080089 ASSERT_NEAR(3.9 * index_diff, estimator.GetEstimatorState().position,
Adam Snaiderb4119252015-02-15 01:30:57 +000090 kAcceptableUnzeroedError * index_diff);
Adam Snaiderc4b3c192015-02-01 01:30:39 +000091}
92
Adam Snaiderb4119252015-02-15 01:30:57 +000093TEST_F(ZeroingTest, NotZeroedBeforeEnoughSamplesCollected) {
94 double index_diff = 0.5;
95 double position = 3.6 * index_diff;
96 PositionSensorSimulator sim(index_diff);
97 sim.Initialize(position, index_diff / 3.0);
Tyler Chatowf8f03112017-02-05 14:31:34 -080098 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +000099 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Adam Snaiderb4119252015-02-15 01:30:57 +0000100
101 // Make sure that the zeroing code does not consider itself zeroed until we
102 // collect a good amount of samples. In this case we're waiting until the
103 // moving average filter is full.
104 for (unsigned int i = 0; i < kSampleSize - 1; i++) {
105 MoveTo(&sim, &estimator, position += index_diff);
106 ASSERT_FALSE(estimator.zeroed());
107 }
108
109 MoveTo(&sim, &estimator, position);
110 ASSERT_TRUE(estimator.zeroed());
111}
112
113TEST_F(ZeroingTest, TestLotsOfMovement) {
114 double index_diff = 1.0;
115 PositionSensorSimulator sim(index_diff);
116 sim.Initialize(3.6, index_diff / 3.0);
Tyler Chatowf8f03112017-02-05 14:31:34 -0800117 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000118 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000119
120 // The zeroing code is supposed to perform some filtering on the difference
121 // between the potentiometer value and the encoder value. We assume that 300
122 // samples are sufficient to have updated the filter.
123 for (int i = 0; i < 300; i++) {
Adam Snaiderb4119252015-02-15 01:30:57 +0000124 MoveTo(&sim, &estimator, 3.6);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000125 }
Brian Silvermanf37839c2017-02-19 18:07:15 -0800126 ASSERT_NEAR(3.6, estimator.GetEstimatorState().position,
127 kAcceptableUnzeroedError * index_diff);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000128
129 // With a single index pulse the zeroing estimator should be able to lock
130 // onto the true value of the position.
Adam Snaiderb4119252015-02-15 01:30:57 +0000131 MoveTo(&sim, &estimator, 4.01);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800132 ASSERT_NEAR(4.01, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000133
Adam Snaiderb4119252015-02-15 01:30:57 +0000134 MoveTo(&sim, &estimator, 4.99);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800135 ASSERT_NEAR(4.99, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000136
Adam Snaiderb4119252015-02-15 01:30:57 +0000137 MoveTo(&sim, &estimator, 3.99);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800138 ASSERT_NEAR(3.99, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000139
Adam Snaiderb4119252015-02-15 01:30:57 +0000140 MoveTo(&sim, &estimator, 3.01);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800141 ASSERT_NEAR(3.01, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000142
Adam Snaiderb4119252015-02-15 01:30:57 +0000143 MoveTo(&sim, &estimator, 13.55);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800144 ASSERT_NEAR(13.55, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000145}
146
Adam Snaiderb4119252015-02-15 01:30:57 +0000147TEST_F(ZeroingTest, TestDifferentIndexDiffs) {
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000148 double index_diff = 0.89;
Adam Snaiderb4119252015-02-15 01:30:57 +0000149 PositionSensorSimulator sim(index_diff);
150 sim.Initialize(3.5 * index_diff, index_diff / 3.0);
Tyler Chatowf8f03112017-02-05 14:31:34 -0800151 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000152 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000153
154 // The zeroing code is supposed to perform some filtering on the difference
155 // between the potentiometer value and the encoder value. We assume that 300
156 // samples are sufficient to have updated the filter.
157 for (int i = 0; i < 300; i++) {
Adam Snaiderb4119252015-02-15 01:30:57 +0000158 MoveTo(&sim, &estimator, 3.5 * index_diff);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000159 }
Brian Silvermanf37839c2017-02-19 18:07:15 -0800160 ASSERT_NEAR(3.5 * index_diff, estimator.GetEstimatorState().position,
Adam Snaiderb4119252015-02-15 01:30:57 +0000161 kAcceptableUnzeroedError * index_diff);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000162
163 // With a single index pulse the zeroing estimator should be able to lock
164 // onto the true value of the position.
Adam Snaiderb4119252015-02-15 01:30:57 +0000165 MoveTo(&sim, &estimator, 4.01);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800166 ASSERT_NEAR(4.01, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000167
Adam Snaiderb4119252015-02-15 01:30:57 +0000168 MoveTo(&sim, &estimator, 4.99);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800169 ASSERT_NEAR(4.99, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000170
Adam Snaiderb4119252015-02-15 01:30:57 +0000171 MoveTo(&sim, &estimator, 3.99);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800172 ASSERT_NEAR(3.99, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000173
Adam Snaiderb4119252015-02-15 01:30:57 +0000174 MoveTo(&sim, &estimator, 3.01);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800175 ASSERT_NEAR(3.01, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000176
Adam Snaiderb4119252015-02-15 01:30:57 +0000177 MoveTo(&sim, &estimator, 13.55);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800178 ASSERT_NEAR(13.55, estimator.GetEstimatorState().position, 0.001);
Adam Snaiderb4119252015-02-15 01:30:57 +0000179}
180
181TEST_F(ZeroingTest, TestPercentage) {
182 double index_diff = 0.89;
183 PositionSensorSimulator sim(index_diff);
184 sim.Initialize(3.5 * index_diff, index_diff / 3.0);
Tyler Chatowf8f03112017-02-05 14:31:34 -0800185 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000186 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Adam Snaiderb4119252015-02-15 01:30:57 +0000187
188 for (unsigned int i = 0; i < kSampleSize / 2; i++) {
189 MoveTo(&sim, &estimator, 3.5 * index_diff);
190 }
191 ASSERT_NEAR(0.5, estimator.offset_ratio_ready(), 0.001);
Austin Schuh7485dbb2016-02-08 00:21:58 -0800192 ASSERT_FALSE(estimator.offset_ready());
193
194 for (unsigned int i = 0; i < kSampleSize / 2; i++) {
195 MoveTo(&sim, &estimator, 3.5 * index_diff);
196 }
197 ASSERT_NEAR(1.0, estimator.offset_ratio_ready(), 0.001);
198 ASSERT_TRUE(estimator.offset_ready());
Adam Snaiderb4119252015-02-15 01:30:57 +0000199}
200
201TEST_F(ZeroingTest, TestOffset) {
202 double index_diff = 0.89;
203 PositionSensorSimulator sim(index_diff);
204 sim.Initialize(3.1 * index_diff, index_diff / 3.0);
Tyler Chatowf8f03112017-02-05 14:31:34 -0800205 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000206 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Adam Snaiderb4119252015-02-15 01:30:57 +0000207
Philipp Schrader41d82912015-02-15 03:44:23 +0000208 MoveTo(&sim, &estimator, 3.1 * index_diff);
209
Adam Snaiderb4119252015-02-15 01:30:57 +0000210 for (unsigned int i = 0; i < kSampleSize; i++) {
211 MoveTo(&sim, &estimator, 5.0 * index_diff);
212 }
Philipp Schrader41d82912015-02-15 03:44:23 +0000213
Adam Snaiderb4119252015-02-15 01:30:57 +0000214 ASSERT_NEAR(3.1 * index_diff, estimator.offset(), 0.001);
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000215}
216
Philipp Schrader41d82912015-02-15 03:44:23 +0000217TEST_F(ZeroingTest, WaitForIndexPulseAfterReset) {
218 double index_diff = 0.6;
219 PositionSensorSimulator sim(index_diff);
220 sim.Initialize(3.1 * index_diff, index_diff / 3.0);
Tyler Chatowf8f03112017-02-05 14:31:34 -0800221 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000222 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Philipp Schrader41d82912015-02-15 03:44:23 +0000223
224 // Make sure to fill up the averaging filter with samples.
225 for (unsigned int i = 0; i < kSampleSize; i++) {
226 MoveTo(&sim, &estimator, 3.1 * index_diff);
227 }
228
229 // Make sure we're not zeroed until we hit an index pulse.
230 ASSERT_FALSE(estimator.zeroed());
231
232 // Trigger an index pulse; we should now be zeroed.
233 MoveTo(&sim, &estimator, 4.5 * index_diff);
234 ASSERT_TRUE(estimator.zeroed());
235
236 // Reset the zeroing logic and supply a bunch of samples within the current
237 // index segment.
238 estimator.Reset();
239 for (unsigned int i = 0; i < kSampleSize; i++) {
240 MoveTo(&sim, &estimator, 4.2 * 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 another index pulse; we should be zeroed again.
247 MoveTo(&sim, &estimator, 3.1 * index_diff);
248 ASSERT_TRUE(estimator.zeroed());
249}
250
Philipp Schrader030ad182015-02-15 05:40:58 +0000251TEST_F(ZeroingTest, TestNonZeroIndexPulseOffsets) {
252 const double index_diff = 0.9;
253 const double known_index_pos = 3.5 * index_diff;
254 PositionSensorSimulator sim(index_diff);
255 sim.Initialize(3.3 * index_diff, index_diff / 3.0, known_index_pos);
Tyler Chatowf8f03112017-02-05 14:31:34 -0800256 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000257 kSampleSize, index_diff, known_index_pos, kIndexErrorFraction});
Philipp Schrader030ad182015-02-15 05:40:58 +0000258
259 // Make sure to fill up the averaging filter with samples.
260 for (unsigned int i = 0; i < kSampleSize; i++) {
261 MoveTo(&sim, &estimator, 3.3 * index_diff);
262 }
263
264 // Make sure we're not zeroed until we hit an index pulse.
265 ASSERT_FALSE(estimator.zeroed());
266
267 // Trigger an index pulse; we should now be zeroed.
268 MoveTo(&sim, &estimator, 3.7 * index_diff);
269 ASSERT_TRUE(estimator.zeroed());
270 ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset());
Brian Silvermanf37839c2017-02-19 18:07:15 -0800271 ASSERT_DOUBLE_EQ(3.7 * index_diff, estimator.GetEstimatorState().position);
Philipp Schrader030ad182015-02-15 05:40:58 +0000272
273 // Trigger one more index pulse and check the offset.
274 MoveTo(&sim, &estimator, 4.7 * index_diff);
275 ASSERT_DOUBLE_EQ(3.3 * index_diff, estimator.offset());
Brian Silvermanf37839c2017-02-19 18:07:15 -0800276 ASSERT_DOUBLE_EQ(4.7 * index_diff, estimator.GetEstimatorState().position);
Philipp Schrader030ad182015-02-15 05:40:58 +0000277}
278
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000279TEST_F(ZeroingTest, BasicErrorAPITest) {
280 const double index_diff = 1.0;
Tyler Chatowf8f03112017-02-05 14:31:34 -0800281 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000282 kSampleSize, index_diff, 0.0, kIndexErrorFraction});
Philipp Schrader53f4b6d2015-02-15 22:32:08 +0000283 PositionSensorSimulator sim(index_diff);
284 sim.Initialize(1.5 * index_diff, index_diff / 3.0, 0.0);
285
286 // Perform a simple move and make sure that no error occured.
287 MoveTo(&sim, &estimator, 3.5 * index_diff);
288 ASSERT_FALSE(estimator.error());
289
290 // Trigger an error and make sure it's reported.
291 estimator.TriggerError();
292 ASSERT_TRUE(estimator.error());
293
294 // Make sure that it can recover after a reset.
295 estimator.Reset();
296 ASSERT_FALSE(estimator.error());
297 MoveTo(&sim, &estimator, 4.5 * index_diff);
298 MoveTo(&sim, &estimator, 5.5 * index_diff);
299 ASSERT_FALSE(estimator.error());
300}
301
Brian Silvermana10d20a2017-02-19 14:28:53 -0800302// Tests that an error is detected when the starting position changes too much.
303TEST_F(ZeroingTest, TestIndexOffsetError) {
Adam Snaider3cd11c52015-02-16 02:16:09 +0000304 const double index_diff = 0.8;
305 const double known_index_pos = 2 * index_diff;
Austin Schuh5f01f152017-02-11 21:34:08 -0800306 const size_t sample_size = 30;
Adam Snaider3cd11c52015-02-16 02:16:09 +0000307 PositionSensorSimulator sim(index_diff);
308 sim.Initialize(10 * index_diff, index_diff / 3.0, known_index_pos);
Tyler Chatowf8f03112017-02-05 14:31:34 -0800309 PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
Adam Snaider3cd11c52015-02-16 02:16:09 +0000310 sample_size, index_diff, known_index_pos, kIndexErrorFraction});
311
Austin Schuh5f01f152017-02-11 21:34:08 -0800312 for (size_t i = 0; i < sample_size; i++) {
Adam Snaider3cd11c52015-02-16 02:16:09 +0000313 MoveTo(&sim, &estimator, 13 * index_diff);
314 }
315 MoveTo(&sim, &estimator, 8 * index_diff);
316
317 ASSERT_TRUE(estimator.zeroed());
318 ASSERT_FALSE(estimator.error());
319 sim.Initialize(9.0 * index_diff + 0.31 * index_diff, index_diff / 3.0,
320 known_index_pos);
321 MoveTo(&sim, &estimator, 9 * index_diff);
322 ASSERT_TRUE(estimator.zeroed());
323 ASSERT_TRUE(estimator.error());
324}
325
Austin Schuh5f01f152017-02-11 21:34:08 -0800326// Makes sure that using an absolute encoder lets us zero without moving.
327TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithoutMovement) {
328 const double index_diff = 1.0;
329 PositionSensorSimulator sim(index_diff);
330
331 const double start_pos = 2.1;
332 double measured_absolute_position = 0.3 * index_diff;
333
Brian Silvermana10d20a2017-02-19 14:28:53 -0800334 PotAndAbsoluteEncoderZeroingConstants constants{
335 kSampleSize, index_diff, measured_absolute_position,
336 0.1, kMovingBufferSize, kIndexErrorFraction};
Austin Schuh5f01f152017-02-11 21:34:08 -0800337
338 sim.Initialize(start_pos, index_diff / 3.0, 0.0,
339 constants.measured_absolute_position);
340
341 PotAndAbsEncoderZeroingEstimator estimator(constants);
342
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800343 for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) {
Austin Schuh5f01f152017-02-11 21:34:08 -0800344 MoveTo(&sim, &estimator, start_pos);
345 ASSERT_FALSE(estimator.zeroed());
346 }
347
348 MoveTo(&sim, &estimator, start_pos);
349 ASSERT_TRUE(estimator.zeroed());
350 EXPECT_DOUBLE_EQ(start_pos, estimator.offset());
351}
352
353// Makes sure that using an absolute encoder doesn't let us zero while moving.
354TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithMovement) {
355 const double index_diff = 1.0;
356 PositionSensorSimulator sim(index_diff);
357
358 const double start_pos = 10 * index_diff;
359 double measured_absolute_position = 0.3 * index_diff;
360
Brian Silvermana10d20a2017-02-19 14:28:53 -0800361 PotAndAbsoluteEncoderZeroingConstants constants{
362 kSampleSize, index_diff, measured_absolute_position,
363 0.1, kMovingBufferSize, kIndexErrorFraction};
Austin Schuh5f01f152017-02-11 21:34:08 -0800364
365 sim.Initialize(start_pos, index_diff / 3.0, 0.0,
366 constants.measured_absolute_position);
367
368 PotAndAbsEncoderZeroingEstimator estimator(constants);
369
Diana Vandenberg8fea6ea2017-02-18 17:24:45 -0800370 for (size_t i = 0; i < kSampleSize + kMovingBufferSize - 1; ++i) {
Austin Schuh5f01f152017-02-11 21:34:08 -0800371 MoveTo(&sim, &estimator, start_pos + i * index_diff);
372 ASSERT_FALSE(estimator.zeroed());
373 }
374 MoveTo(&sim, &estimator, start_pos + 10 * index_diff);
375
376 MoveTo(&sim, &estimator, start_pos);
377 ASSERT_FALSE(estimator.zeroed());
378}
Neil Balch16275e32017-02-18 16:38:45 -0800379
380// Makes sure we detect an error if the ZeroingEstimator gets sent a NaN.
381TEST_F(ZeroingTest, TestAbsoluteEncoderZeroingWithNaN) {
382 PotAndAbsoluteEncoderZeroingConstants constants{
Brian Silvermana10d20a2017-02-19 14:28:53 -0800383 kSampleSize, 1, 0.3, 0.1, kMovingBufferSize, kIndexErrorFraction};
Neil Balch16275e32017-02-18 16:38:45 -0800384
385 PotAndAbsEncoderZeroingEstimator estimator(constants);
386
387 PotAndAbsolutePosition sensor_values_;
388 sensor_values_.absolute_encoder = ::std::numeric_limits<double>::quiet_NaN();
389 sensor_values_.encoder = 0.0;
390 sensor_values_.pot = 0.0;
391 estimator.UpdateEstimate(sensor_values_);
392
393 ASSERT_TRUE(estimator.error());
394}
395
Brian Silvermana10d20a2017-02-19 14:28:53 -0800396// Tests that an error is detected when the starting position changes too much.
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000397TEST_F(ZeroingTest, TestRelativeEncoderZeroing) {
398 EncoderPlusIndexZeroingConstants constants;
399 constants.index_pulse_count = 3;
400 constants.index_difference = 10.0;
401 constants.measured_index_position = 20.0;
402 constants.known_index_pulse = 1;
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000403 constants.allowable_encoder_error = 0.01;
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000404
405 PositionSensorSimulator sim(constants.index_difference);
406
407 const double start_pos = 2.5 * constants.index_difference;
408
409 sim.Initialize(start_pos, constants.index_difference / 3.0,
410 constants.measured_index_position);
411
412 PulseIndexZeroingEstimator estimator(constants);
413
414 // Should not be zeroed when we stand still.
415 for (int i = 0; i < 300; ++i) {
416 MoveTo(&sim, &estimator, start_pos);
417 ASSERT_FALSE(estimator.zeroed());
418 }
419
420 // Move to 1.5 constants.index_difference and we should still not be zeroed.
421 MoveTo(&sim, &estimator, 1.5 * constants.index_difference);
422 ASSERT_FALSE(estimator.zeroed());
423
424 // Move to 0.5 constants.index_difference and we should still not be zeroed.
425 MoveTo(&sim, &estimator, 0.5 * constants.index_difference);
426 ASSERT_FALSE(estimator.zeroed());
427
428 // Move back to 1.5 constants.index_difference and we should still not be
429 // zeroed.
430 MoveTo(&sim, &estimator, 1.5 * constants.index_difference);
431 ASSERT_FALSE(estimator.zeroed());
432
433 // Move back to 2.5 constants.index_difference and we should still not be
434 // zeroed.
435 MoveTo(&sim, &estimator, 2.5 * constants.index_difference);
436 ASSERT_FALSE(estimator.zeroed());
437
438 // Move back to 3.5 constants.index_difference and we should now be zeroed.
439 MoveTo(&sim, &estimator, 3.5 * constants.index_difference);
440 ASSERT_TRUE(estimator.zeroed());
441
442 ASSERT_DOUBLE_EQ(start_pos, estimator.offset());
Brian Silvermanf37839c2017-02-19 18:07:15 -0800443 ASSERT_DOUBLE_EQ(3.5 * constants.index_difference,
444 estimator.GetEstimatorState().position);
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000445
446 MoveTo(&sim, &estimator, 0.5 * constants.index_difference);
Brian Silvermanf37839c2017-02-19 18:07:15 -0800447 ASSERT_DOUBLE_EQ(0.5 * constants.index_difference,
448 estimator.GetEstimatorState().position);
Isaac Wilcove0851ffd2017-02-16 04:13:14 +0000449}
450
Philipp Schrader3f5b6182017-03-25 22:36:37 +0000451// Tests that we can detect when an index pulse occurs where we didn't expect
452// it to for the PulseIndexZeroingEstimator.
453TEST_F(ZeroingTest, TestRelativeEncoderSlipping) {
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;
459 constants.allowable_encoder_error = 0.05;
460
461 PositionSensorSimulator sim(constants.index_difference);
462
463 const double start_pos =
464 constants.measured_index_position + 0.5 * constants.index_difference;
465
466 for (double direction : {1.0, -1.0}) {
467 sim.Initialize(start_pos, constants.index_difference / 3.0,
468 constants.measured_index_position);
469
470 PulseIndexZeroingEstimator estimator(constants);
471
472 // Zero the estimator.
473 MoveTo(&sim, &estimator, start_pos - 1 * constants.index_difference);
474 MoveTo(
475 &sim, &estimator,
476 start_pos - constants.index_pulse_count * constants.index_difference);
477 ASSERT_TRUE(estimator.zeroed());
478 ASSERT_FALSE(estimator.error());
479
480 // We have a 5% allowable error so we slip a little bit each time and make
481 // sure that the index pulses are still accepted.
482 for (double error = 0.00;
483 ::std::abs(error) < constants.allowable_encoder_error;
484 error += 0.01 * direction) {
485 sim.Initialize(start_pos, constants.index_difference / 3.0,
486 constants.measured_index_position +
487 error * constants.index_difference);
488 MoveTo(&sim, &estimator, start_pos - constants.index_difference);
489 EXPECT_FALSE(estimator.error());
490 }
491
492 // As soon as we hit cross the error margin, we should trigger an error.
493 sim.Initialize(start_pos, constants.index_difference / 3.0,
494 constants.measured_index_position +
495 constants.allowable_encoder_error * 1.1 *
496 constants.index_difference * direction);
497 MoveTo(&sim, &estimator, start_pos - constants.index_difference);
498 ASSERT_TRUE(estimator.error());
499 }
500}
501
Austin Schuh55934032017-03-11 12:45:27 -0800502// Tests that an error is detected when the starting position changes too much.
503TEST_F(ZeroingTest, TestHallEffectZeroing) {
504 constants::HallEffectZeroingConstants constants;
505 constants.lower_hall_position = 0.25;
506 constants.upper_hall_position = 0.75;
507 constants.index_difference = 1.0;
508 constants.hall_trigger_zeroing_length = 2;
509 constants.zeroing_move_direction = false;
510
511 PositionSensorSimulator sim(constants.index_difference);
512
513 const double start_pos = 1.0;
514
515 sim.InitializeHallEffectAndPosition(start_pos, constants.lower_hall_position,
516 constants.upper_hall_position);
517
518 HallEffectAndPositionZeroingEstimator estimator(constants);
519
520 // Should not be zeroed when we stand still.
521 for (int i = 0; i < 300; ++i) {
522 MoveTo(&sim, &estimator, start_pos);
523 ASSERT_FALSE(estimator.zeroed());
524 }
525
526 MoveTo(&sim, &estimator, 0.9);
527 ASSERT_FALSE(estimator.zeroed());
528
529 // Move to where the hall effect is triggered and make sure it becomes zeroed.
530 MoveTo(&sim, &estimator, 0.5);
531 EXPECT_FALSE(estimator.zeroed());
532 MoveTo(&sim, &estimator, 0.5);
533 ASSERT_TRUE(estimator.zeroed());
534
535 // Check that the offset is calculated correctly.
536 EXPECT_DOUBLE_EQ(-0.25, estimator.offset());
537
538 // Make sure triggering errors works.
539 estimator.TriggerError();
540 ASSERT_TRUE(estimator.error());
541
542 // Ensure resetting resets the state of the estimator.
543 estimator.Reset();
544 ASSERT_FALSE(estimator.zeroed());
545 ASSERT_FALSE(estimator.error());
546
547 // Make sure we don't become zeroed if the hall effect doesn't trigger for
548 // long enough.
549 MoveTo(&sim, &estimator, 0.9);
550 EXPECT_FALSE(estimator.zeroed());
551 MoveTo(&sim, &estimator, 0.5);
552 EXPECT_FALSE(estimator.zeroed());
553 MoveTo(&sim, &estimator, 0.9);
554 EXPECT_FALSE(estimator.zeroed());
555
556 // Make sure we can zero moving in the opposite direction as before and stay
557 // zeroed once the hall effect is no longer triggered.
558
559 MoveTo(&sim, &estimator, 0.0);
560 ASSERT_FALSE(estimator.zeroed());
561 MoveTo(&sim, &estimator, 0.4);
562 EXPECT_FALSE(estimator.zeroed());
563 MoveTo(&sim, &estimator, 0.6);
564 EXPECT_FALSE(estimator.zeroed());
565 MoveTo(&sim, &estimator, 0.9);
566 EXPECT_FALSE(estimator.zeroed());
567
568 // Check that the offset is calculated correctly.
569 EXPECT_DOUBLE_EQ(-0.75, estimator.offset());
570
571 // Make sure we don't zero if we start in the hall effect's range, before we
572 // reset, we also check that there were no errors.
573 MoveTo(&sim, &estimator, 0.5);
574 ASSERT_TRUE(estimator.zeroed());
575 ASSERT_FALSE(estimator.error());
576 estimator.Reset();
577 EXPECT_FALSE(estimator.zeroed());
578 MoveTo(&sim, &estimator, 0.5);
579 EXPECT_FALSE(estimator.zeroed());
580 MoveTo(&sim, &estimator, 0.5);
581 EXPECT_FALSE(estimator.zeroed());
582}
583
584
Adam Snaiderc4b3c192015-02-01 01:30:39 +0000585} // namespace zeroing
586} // namespace frc971