blob: 1dfeff31fc6a7263c97ab765abf50ae7a1e8ef99 [file] [log] [blame]
briansf0165ca2013-03-02 06:17:47 +00001#include "gtest/gtest.h"
2
3#include "Eigen/Dense"
4
5#include "aos/common/util/trapezoid_profile.h"
6
7namespace aos {
8namespace util {
9namespace testing {
10
11class 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) {
briansd412b3f2013-03-03 21:13:44 +000034 static const double kDoubleNear = 0.00001;
35 if (::std::abs(velocity - position_(1)) > kDoubleNear) {
briansf0165ca2013-03-02 06:17:47 +000036 return ::testing::AssertionFailure() << "velocity is " << position_(1) <<
37 " not " << velocity;
38 }
briansd412b3f2013-03-03 21:13:44 +000039 if (::std::abs(position - position_(0)) > kDoubleNear) {
briansf0165ca2013-03-02 06:17:47 +000040 return ::testing::AssertionFailure() << "position is " << position_(0) <<
41 " not " << position;
42 }
43 return ::testing::AssertionSuccess() << "at " << position <<
44 " moving at " << velocity;
45 }
46
47 private:
48 static const time::Time delta_time;
49
50 Eigen::Matrix<double, 2, 1> position_;
51};
52const time::Time TrapezoidProfileTest::delta_time = time::Time::InSeconds(0.01);
53
54TEST_F(TrapezoidProfileTest, ReachesGoal) {
55 for (int i = 0; i < 450; ++i) {
56 RunIteration(3, 0);
57 }
58 EXPECT_TRUE(At(3, 0));
59}
60
Ben Fredricksonf33d6532015-03-15 00:29:29 -070061// Tests that decresing the maximum velocity in the middle when it is already
62// moving faster than the new max is handled correctly.
63TEST_F(TrapezoidProfileTest, ContinousUnderVelChange) {
64 profile_.set_maximum_velocity(1.75);
65 RunIteration(12.0, 0);
66 double last_pos = position()(0);
67 double last_vel = 1.75;
68 for (int i = 0; i < 1600; ++i) {
69 if (i == 400) {
70 profile_.set_maximum_velocity(0.75);
71 }
72 RunIteration(12.0, 0);
73 if (i >= 400) {
74 EXPECT_TRUE(::std::abs(last_pos - position()(0)) <= 1.75 * 0.01);
75 EXPECT_NEAR(last_vel, ::std::abs(last_pos - position()(0)), 0.0001);
76 }
77 last_vel = ::std::abs(last_pos - position()(0));
78 last_pos = position()(0);
79 }
80 EXPECT_TRUE(At(12.0, 0));
81}
82
briansf0165ca2013-03-02 06:17:47 +000083// There is some somewhat tricky code for dealing with going backwards.
84TEST_F(TrapezoidProfileTest, Backwards) {
85 for (int i = 0; i < 400; ++i) {
86 RunIteration(-2, 0);
87 }
88 EXPECT_TRUE(At(-2, 0));
89}
90
91TEST_F(TrapezoidProfileTest, SwitchGoalInMiddle) {
92 for (int i = 0; i < 200; ++i) {
93 RunIteration(-2, 0);
94 }
95 EXPECT_FALSE(At(-2, 0));
96 for (int i = 0; i < 550; ++i) {
97 RunIteration(0, 0);
98 }
99 EXPECT_TRUE(At(0, 0));
100}
101
102// Checks to make sure that it hits top speed.
103TEST_F(TrapezoidProfileTest, TopSpeed) {
104 for (int i = 0; i < 200; ++i) {
105 RunIteration(4, 0);
106 }
107 EXPECT_NEAR(1.5, position()(1), 10e-5);
108 for (int i = 0; i < 2000; ++i) {
109 RunIteration(4, 0);
110 }
111 EXPECT_TRUE(At(4, 0));
112}
113
114} // namespace testing
115} // namespace util
116} // namespace aos