brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame^] | 1 | #include "gtest/gtest.h" |
| 2 | |
| 3 | #include "Eigen/Dense" |
| 4 | |
| 5 | #include "aos/common/util/trapezoid_profile.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace util { |
| 9 | namespace testing { |
| 10 | |
| 11 | class TrapezoidProfileTest : public ::testing::Test { |
| 12 | public: |
| 13 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 14 | |
| 15 | protected: |
| 16 | TrapezoidProfileTest() : profile_(delta_time) { |
| 17 | position_.setZero(); |
| 18 | profile_.set_maximum_acceleration(0.75); |
| 19 | profile_.set_maximum_velocity(1.75); |
| 20 | } |
| 21 | |
| 22 | // Runs an iteration. |
| 23 | void RunIteration(double goal_position, |
| 24 | double goal_velocity) { |
| 25 | position_ = profile_.Update(goal_position, |
| 26 | goal_velocity); |
| 27 | } |
| 28 | |
| 29 | const Eigen::Matrix<double, 2, 1> &position() { return position_; } |
| 30 | |
| 31 | TrapezoidProfile profile_; |
| 32 | |
| 33 | ::testing::AssertionResult At(double position, double velocity) { |
| 34 | if (velocity != position_(1)) { |
| 35 | return ::testing::AssertionFailure() << "velocity is " << position_(1) << |
| 36 | " not " << velocity; |
| 37 | } |
| 38 | if (position != position_(0)) { |
| 39 | return ::testing::AssertionFailure() << "position is " << position_(0) << |
| 40 | " not " << position; |
| 41 | } |
| 42 | return ::testing::AssertionSuccess() << "at " << position << |
| 43 | " moving at " << velocity; |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | static const time::Time delta_time; |
| 48 | |
| 49 | Eigen::Matrix<double, 2, 1> position_; |
| 50 | }; |
| 51 | const time::Time TrapezoidProfileTest::delta_time = time::Time::InSeconds(0.01); |
| 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 | |
| 60 | // There is some somewhat tricky code for dealing with going backwards. |
| 61 | TEST_F(TrapezoidProfileTest, Backwards) { |
| 62 | for (int i = 0; i < 400; ++i) { |
| 63 | RunIteration(-2, 0); |
| 64 | } |
| 65 | EXPECT_TRUE(At(-2, 0)); |
| 66 | } |
| 67 | |
| 68 | TEST_F(TrapezoidProfileTest, SwitchGoalInMiddle) { |
| 69 | for (int i = 0; i < 200; ++i) { |
| 70 | RunIteration(-2, 0); |
| 71 | } |
| 72 | EXPECT_FALSE(At(-2, 0)); |
| 73 | for (int i = 0; i < 550; ++i) { |
| 74 | RunIteration(0, 0); |
| 75 | } |
| 76 | EXPECT_TRUE(At(0, 0)); |
| 77 | } |
| 78 | |
| 79 | // Checks to make sure that it hits top speed. |
| 80 | TEST_F(TrapezoidProfileTest, TopSpeed) { |
| 81 | for (int i = 0; i < 200; ++i) { |
| 82 | RunIteration(4, 0); |
| 83 | } |
| 84 | EXPECT_NEAR(1.5, position()(1), 10e-5); |
| 85 | for (int i = 0; i < 2000; ++i) { |
| 86 | RunIteration(4, 0); |
| 87 | } |
| 88 | EXPECT_TRUE(At(4, 0)); |
| 89 | } |
| 90 | |
| 91 | } // namespace testing |
| 92 | } // namespace util |
| 93 | } // namespace aos |