Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame] | 1 | #include "frc971/zeroing/pulse_index.h" |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | #include "frc971/zeroing/zeroing_test.h" |
| 6 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 7 | namespace frc971::zeroing::testing { |
Brian Silverman | a57b701 | 2020-03-11 20:19:23 -0700 | [diff] [blame] | 8 | |
| 9 | using constants::EncoderPlusIndexZeroingConstants; |
| 10 | |
| 11 | class PulseIndexZeroingTest : public ZeroingTest { |
| 12 | protected: |
| 13 | void MoveTo(PositionSensorSimulator *simulator, |
| 14 | PulseIndexZeroingEstimator *estimator, double new_position) { |
| 15 | simulator->MoveTo(new_position); |
| 16 | FBB fbb; |
| 17 | estimator->UpdateEstimate( |
| 18 | *simulator->FillSensorValues<IndexPosition>(&fbb)); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | // Tests that an error is detected when the starting position changes too much. |
| 23 | TEST_F(PulseIndexZeroingTest, TestRelativeEncoderZeroing) { |
| 24 | EncoderPlusIndexZeroingConstants constants; |
| 25 | constants.index_pulse_count = 3; |
| 26 | constants.index_difference = 10.0; |
| 27 | constants.measured_index_position = 20.0; |
| 28 | constants.known_index_pulse = 1; |
| 29 | constants.allowable_encoder_error = 0.01; |
| 30 | |
| 31 | PositionSensorSimulator sim(constants.index_difference); |
| 32 | |
| 33 | const double start_pos = 2.5 * constants.index_difference; |
| 34 | |
| 35 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 36 | constants.measured_index_position); |
| 37 | |
| 38 | PulseIndexZeroingEstimator estimator(constants); |
| 39 | |
| 40 | // Should not be zeroed when we stand still. |
| 41 | for (int i = 0; i < 300; ++i) { |
| 42 | MoveTo(&sim, &estimator, start_pos); |
| 43 | ASSERT_FALSE(estimator.zeroed()); |
| 44 | } |
| 45 | |
| 46 | // Move to 1.5 constants.index_difference and we should still not be zeroed. |
| 47 | MoveTo(&sim, &estimator, 1.5 * constants.index_difference); |
| 48 | ASSERT_FALSE(estimator.zeroed()); |
| 49 | |
| 50 | // Move to 0.5 constants.index_difference and we should still not be zeroed. |
| 51 | MoveTo(&sim, &estimator, 0.5 * constants.index_difference); |
| 52 | ASSERT_FALSE(estimator.zeroed()); |
| 53 | |
| 54 | // Move back to 1.5 constants.index_difference and we should still not be |
| 55 | // zeroed. |
| 56 | MoveTo(&sim, &estimator, 1.5 * constants.index_difference); |
| 57 | ASSERT_FALSE(estimator.zeroed()); |
| 58 | |
| 59 | // Move back to 2.5 constants.index_difference and we should still not be |
| 60 | // zeroed. |
| 61 | MoveTo(&sim, &estimator, 2.5 * constants.index_difference); |
| 62 | ASSERT_FALSE(estimator.zeroed()); |
| 63 | |
| 64 | // Move back to 3.5 constants.index_difference and we should now be zeroed. |
| 65 | MoveTo(&sim, &estimator, 3.5 * constants.index_difference); |
| 66 | ASSERT_TRUE(estimator.zeroed()); |
| 67 | |
| 68 | ASSERT_DOUBLE_EQ(start_pos, estimator.offset()); |
| 69 | ASSERT_DOUBLE_EQ(3.5 * constants.index_difference, |
| 70 | GetEstimatorPosition(&estimator)); |
| 71 | |
| 72 | MoveTo(&sim, &estimator, 0.5 * constants.index_difference); |
| 73 | ASSERT_DOUBLE_EQ(0.5 * constants.index_difference, |
| 74 | GetEstimatorPosition(&estimator)); |
| 75 | } |
| 76 | |
| 77 | // Tests that we can detect when an index pulse occurs where we didn't expect |
| 78 | // it to for the PulseIndexZeroingEstimator. |
| 79 | TEST_F(PulseIndexZeroingTest, TestRelativeEncoderSlipping) { |
| 80 | EncoderPlusIndexZeroingConstants constants; |
| 81 | constants.index_pulse_count = 3; |
| 82 | constants.index_difference = 10.0; |
| 83 | constants.measured_index_position = 20.0; |
| 84 | constants.known_index_pulse = 1; |
| 85 | constants.allowable_encoder_error = 0.05; |
| 86 | |
| 87 | PositionSensorSimulator sim(constants.index_difference); |
| 88 | |
| 89 | const double start_pos = |
| 90 | constants.measured_index_position + 0.5 * constants.index_difference; |
| 91 | |
| 92 | for (double direction : {1.0, -1.0}) { |
| 93 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 94 | constants.measured_index_position); |
| 95 | |
| 96 | PulseIndexZeroingEstimator estimator(constants); |
| 97 | |
| 98 | // Zero the estimator. |
| 99 | MoveTo(&sim, &estimator, start_pos - 1 * constants.index_difference); |
| 100 | MoveTo( |
| 101 | &sim, &estimator, |
| 102 | start_pos - constants.index_pulse_count * constants.index_difference); |
| 103 | ASSERT_TRUE(estimator.zeroed()); |
| 104 | ASSERT_FALSE(estimator.error()); |
| 105 | |
| 106 | // We have a 5% allowable error so we slip a little bit each time and make |
| 107 | // sure that the index pulses are still accepted. |
| 108 | for (double error = 0.00; |
| 109 | ::std::abs(error) < constants.allowable_encoder_error; |
| 110 | error += 0.01 * direction) { |
| 111 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 112 | constants.measured_index_position + |
| 113 | error * constants.index_difference); |
| 114 | MoveTo(&sim, &estimator, start_pos - constants.index_difference); |
| 115 | EXPECT_FALSE(estimator.error()); |
| 116 | } |
| 117 | |
| 118 | // As soon as we hit cross the error margin, we should trigger an error. |
| 119 | sim.Initialize(start_pos, constants.index_difference / 3.0, |
| 120 | constants.measured_index_position + |
| 121 | constants.allowable_encoder_error * 1.1 * |
| 122 | constants.index_difference * direction); |
| 123 | MoveTo(&sim, &estimator, start_pos - constants.index_difference); |
| 124 | ASSERT_TRUE(estimator.error()); |
| 125 | } |
| 126 | } |
| 127 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 128 | } // namespace frc971::zeroing::testing |