Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1 | #include "aos/util/trapezoid_profile.h" |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 2 | |
| 3 | #include "Eigen/Dense" |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 4 | #include "gtest/gtest.h" |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 5 | |
| 6 | namespace aos { |
| 7 | namespace util { |
| 8 | namespace testing { |
| 9 | |
| 10 | class TrapezoidProfileTest : public ::testing::Test { |
| 11 | public: |
| 12 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 13 | |
| 14 | protected: |
| 15 | TrapezoidProfileTest() : profile_(delta_time) { |
| 16 | position_.setZero(); |
| 17 | profile_.set_maximum_acceleration(0.75); |
| 18 | profile_.set_maximum_velocity(1.75); |
| 19 | } |
| 20 | |
| 21 | // Runs an iteration. |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 22 | void RunIteration(double goal_position, double goal_velocity) { |
| 23 | position_ = profile_.Update(goal_position, goal_velocity); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | const Eigen::Matrix<double, 2, 1> &position() { return position_; } |
| 27 | |
| 28 | TrapezoidProfile profile_; |
| 29 | |
| 30 | ::testing::AssertionResult At(double position, double velocity) { |
brians | d412b3f | 2013-03-03 21:13:44 +0000 | [diff] [blame] | 31 | static const double kDoubleNear = 0.00001; |
| 32 | if (::std::abs(velocity - position_(1)) > kDoubleNear) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 33 | return ::testing::AssertionFailure() |
| 34 | << "velocity is " << position_(1) << " not " << velocity; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 35 | } |
brians | d412b3f | 2013-03-03 21:13:44 +0000 | [diff] [blame] | 36 | if (::std::abs(position - position_(0)) > kDoubleNear) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 37 | return ::testing::AssertionFailure() |
| 38 | << "position is " << position_(0) << " not " << position; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 39 | } |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 40 | return ::testing::AssertionSuccess() |
| 41 | << "at " << position << " moving at " << velocity; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | private: |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 45 | static constexpr ::std::chrono::nanoseconds delta_time = |
| 46 | ::std::chrono::milliseconds(10); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 47 | |
| 48 | Eigen::Matrix<double, 2, 1> position_; |
| 49 | }; |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 50 | |
| 51 | constexpr ::std::chrono::nanoseconds TrapezoidProfileTest::delta_time; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 52 | |
| 53 | TEST_F(TrapezoidProfileTest, ReachesGoal) { |
| 54 | for (int i = 0; i < 450; ++i) { |
| 55 | RunIteration(3, 0); |
| 56 | } |
| 57 | EXPECT_TRUE(At(3, 0)); |
| 58 | } |
| 59 | |
Ben Fredrickson | f33d653 | 2015-03-15 00:29:29 -0700 | [diff] [blame] | 60 | // Tests that decresing the maximum velocity in the middle when it is already |
| 61 | // moving faster than the new max is handled correctly. |
| 62 | TEST_F(TrapezoidProfileTest, ContinousUnderVelChange) { |
| 63 | profile_.set_maximum_velocity(1.75); |
| 64 | RunIteration(12.0, 0); |
| 65 | double last_pos = position()(0); |
| 66 | double last_vel = 1.75; |
| 67 | for (int i = 0; i < 1600; ++i) { |
| 68 | if (i == 400) { |
| 69 | profile_.set_maximum_velocity(0.75); |
| 70 | } |
| 71 | RunIteration(12.0, 0); |
| 72 | if (i >= 400) { |
| 73 | EXPECT_TRUE(::std::abs(last_pos - position()(0)) <= 1.75 * 0.01); |
| 74 | EXPECT_NEAR(last_vel, ::std::abs(last_pos - position()(0)), 0.0001); |
| 75 | } |
| 76 | last_vel = ::std::abs(last_pos - position()(0)); |
| 77 | last_pos = position()(0); |
| 78 | } |
| 79 | EXPECT_TRUE(At(12.0, 0)); |
| 80 | } |
| 81 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 82 | // There is some somewhat tricky code for dealing with going backwards. |
| 83 | TEST_F(TrapezoidProfileTest, Backwards) { |
| 84 | for (int i = 0; i < 400; ++i) { |
| 85 | RunIteration(-2, 0); |
| 86 | } |
| 87 | EXPECT_TRUE(At(-2, 0)); |
| 88 | } |
| 89 | |
| 90 | TEST_F(TrapezoidProfileTest, SwitchGoalInMiddle) { |
| 91 | for (int i = 0; i < 200; ++i) { |
| 92 | RunIteration(-2, 0); |
| 93 | } |
| 94 | EXPECT_FALSE(At(-2, 0)); |
| 95 | for (int i = 0; i < 550; ++i) { |
| 96 | RunIteration(0, 0); |
| 97 | } |
| 98 | EXPECT_TRUE(At(0, 0)); |
| 99 | } |
| 100 | |
| 101 | // Checks to make sure that it hits top speed. |
| 102 | TEST_F(TrapezoidProfileTest, TopSpeed) { |
| 103 | for (int i = 0; i < 200; ++i) { |
| 104 | RunIteration(4, 0); |
| 105 | } |
| 106 | EXPECT_NEAR(1.5, position()(1), 10e-5); |
| 107 | for (int i = 0; i < 2000; ++i) { |
| 108 | RunIteration(4, 0); |
| 109 | } |
| 110 | EXPECT_TRUE(At(4, 0)); |
| 111 | } |
| 112 | |
Austin Schuh | 5d571d0 | 2017-02-11 12:28:17 -0800 | [diff] [blame] | 113 | // Tests that the position and velocity exactly match at the end. Some code we |
| 114 | // have assumes this to be true as a simplification. |
| 115 | TEST_F(TrapezoidProfileTest, ExactlyReachesGoal) { |
| 116 | for (int i = 0; i < 450; ++i) { |
| 117 | RunIteration(1, 0); |
| 118 | } |
| 119 | EXPECT_EQ(position()(1), 0.0); |
| 120 | EXPECT_EQ(position()(0), 1.0); |
| 121 | } |
| 122 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 123 | } // namespace testing |
| 124 | } // namespace util |
| 125 | } // namespace aos |