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 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 3 | #include <compare> |
| 4 | #include <cstdlib> |
| 5 | #include <memory> |
| 6 | #include <ratio> |
| 7 | |
| 8 | #include "Eigen/Dense" // IWYU pragma: keep |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 9 | #include "gtest/gtest.h" |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 10 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 11 | namespace aos::util::testing { |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 12 | |
| 13 | class TrapezoidProfileTest : public ::testing::Test { |
| 14 | public: |
| 15 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 16 | |
| 17 | protected: |
Austin Schuh | b3b799e | 2024-02-20 14:20:12 -0800 | [diff] [blame] | 18 | TrapezoidProfileTest() : profile_(kDeltaTime) { |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 19 | position_.setZero(); |
| 20 | profile_.set_maximum_acceleration(0.75); |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 21 | profile_.set_maximum_deceleration(0.75); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 22 | profile_.set_maximum_velocity(1.75); |
| 23 | } |
| 24 | |
| 25 | // Runs an iteration. |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 26 | void RunIteration(double goal_position, double goal_velocity) { |
| 27 | position_ = profile_.Update(goal_position, goal_velocity); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Austin Schuh | b3b799e | 2024-02-20 14:20:12 -0800 | [diff] [blame] | 30 | void RunFor(double goal_position, double goal_velocity, |
| 31 | std::chrono::nanoseconds duration) { |
| 32 | while (duration > std::chrono::nanoseconds(0)) { |
| 33 | position_ = profile_.Update(goal_position, goal_velocity); |
| 34 | duration -= kDeltaTime; |
| 35 | } |
| 36 | |
| 37 | ASSERT_EQ(duration.count(), 0); |
| 38 | } |
| 39 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 40 | const Eigen::Matrix<double, 2, 1> &position() { return position_; } |
| 41 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 42 | AsymmetricTrapezoidProfile profile_; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 43 | |
| 44 | ::testing::AssertionResult At(double position, double velocity) { |
brians | d412b3f | 2013-03-03 21:13:44 +0000 | [diff] [blame] | 45 | static const double kDoubleNear = 0.00001; |
| 46 | if (::std::abs(velocity - position_(1)) > kDoubleNear) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 47 | return ::testing::AssertionFailure() |
| 48 | << "velocity is " << position_(1) << " not " << velocity; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 49 | } |
brians | d412b3f | 2013-03-03 21:13:44 +0000 | [diff] [blame] | 50 | if (::std::abs(position - position_(0)) > kDoubleNear) { |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 51 | return ::testing::AssertionFailure() |
| 52 | << "position is " << position_(0) << " not " << position; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 53 | } |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 54 | return ::testing::AssertionSuccess() |
| 55 | << "at " << position << " moving at " << velocity; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | private: |
Austin Schuh | b3b799e | 2024-02-20 14:20:12 -0800 | [diff] [blame] | 59 | static constexpr ::std::chrono::nanoseconds kDeltaTime = |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 60 | ::std::chrono::milliseconds(10); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 61 | |
| 62 | Eigen::Matrix<double, 2, 1> position_; |
| 63 | }; |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 64 | |
Austin Schuh | b3b799e | 2024-02-20 14:20:12 -0800 | [diff] [blame] | 65 | constexpr ::std::chrono::nanoseconds TrapezoidProfileTest::kDeltaTime; |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 66 | |
| 67 | TEST_F(TrapezoidProfileTest, ReachesGoal) { |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 68 | RunFor(3, 0, std::chrono::milliseconds(4500)); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 69 | EXPECT_TRUE(At(3, 0)); |
| 70 | } |
| 71 | |
Austin Schuh | b3b799e | 2024-02-20 14:20:12 -0800 | [diff] [blame] | 72 | // Tests that decreasing the maximum velocity in the middle when it is already |
Ben Fredrickson | f33d653 | 2015-03-15 00:29:29 -0700 | [diff] [blame] | 73 | // moving faster than the new max is handled correctly. |
| 74 | TEST_F(TrapezoidProfileTest, ContinousUnderVelChange) { |
| 75 | profile_.set_maximum_velocity(1.75); |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 76 | RunFor(12.0, 0, std::chrono::milliseconds(10)); |
Ben Fredrickson | f33d653 | 2015-03-15 00:29:29 -0700 | [diff] [blame] | 77 | double last_pos = position()(0); |
| 78 | double last_vel = 1.75; |
| 79 | for (int i = 0; i < 1600; ++i) { |
| 80 | if (i == 400) { |
| 81 | profile_.set_maximum_velocity(0.75); |
| 82 | } |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 83 | RunFor(12.0, 0, std::chrono::milliseconds(10)); |
Ben Fredrickson | f33d653 | 2015-03-15 00:29:29 -0700 | [diff] [blame] | 84 | if (i >= 400) { |
| 85 | EXPECT_TRUE(::std::abs(last_pos - position()(0)) <= 1.75 * 0.01); |
| 86 | EXPECT_NEAR(last_vel, ::std::abs(last_pos - position()(0)), 0.0001); |
| 87 | } |
| 88 | last_vel = ::std::abs(last_pos - position()(0)); |
| 89 | last_pos = position()(0); |
| 90 | } |
| 91 | EXPECT_TRUE(At(12.0, 0)); |
| 92 | } |
| 93 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 94 | // There is some somewhat tricky code for dealing with going backwards. |
| 95 | TEST_F(TrapezoidProfileTest, Backwards) { |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 96 | RunFor(-2, 0, std::chrono::milliseconds(4000)); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 97 | EXPECT_TRUE(At(-2, 0)); |
| 98 | } |
| 99 | |
| 100 | TEST_F(TrapezoidProfileTest, SwitchGoalInMiddle) { |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 101 | RunFor(-2, 0, std::chrono::milliseconds(2000)); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 102 | EXPECT_FALSE(At(-2, 0)); |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 103 | RunFor(0, 0, std::chrono::milliseconds(5500)); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 104 | EXPECT_TRUE(At(0, 0)); |
| 105 | } |
| 106 | |
| 107 | // Checks to make sure that it hits top speed. |
| 108 | TEST_F(TrapezoidProfileTest, TopSpeed) { |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 109 | RunFor(4, 0, std::chrono::milliseconds(2000)); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 110 | EXPECT_NEAR(1.5, position()(1), 10e-5); |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 111 | RunFor(4, 0, std::chrono::milliseconds(20000)); |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 112 | EXPECT_TRUE(At(4, 0)); |
| 113 | } |
| 114 | |
Austin Schuh | 5d571d0 | 2017-02-11 12:28:17 -0800 | [diff] [blame] | 115 | // Tests that the position and velocity exactly match at the end. Some code we |
| 116 | // have assumes this to be true as a simplification. |
| 117 | TEST_F(TrapezoidProfileTest, ExactlyReachesGoal) { |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 118 | RunFor(1, 0, std::chrono::milliseconds(4500)); |
Austin Schuh | 5d571d0 | 2017-02-11 12:28:17 -0800 | [diff] [blame] | 119 | EXPECT_EQ(position()(1), 0.0); |
| 120 | EXPECT_EQ(position()(0), 1.0); |
| 121 | } |
| 122 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 123 | // Tests that we can move a goal without the trajectory teleporting. The goal |
| 124 | // needs to move to something we haven't already passed, but will blow by. |
Austin Schuh | b3b799e | 2024-02-20 14:20:12 -0800 | [diff] [blame] | 125 | TEST_F(TrapezoidProfileTest, MoveGoal) { |
| 126 | profile_.set_maximum_acceleration(2.0); |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 127 | profile_.set_maximum_deceleration(2.0); |
Austin Schuh | b3b799e | 2024-02-20 14:20:12 -0800 | [diff] [blame] | 128 | profile_.set_maximum_velocity(2.0); |
| 129 | |
| 130 | RunFor(5.0, 0, std::chrono::seconds(1)); |
| 131 | EXPECT_TRUE(At(1.0, 2.0)); |
| 132 | RunFor(5.0, 0, std::chrono::seconds(1)); |
| 133 | EXPECT_TRUE(At(3.0, 2.0)); |
| 134 | RunFor(3.5, 0, std::chrono::seconds(1)); |
| 135 | EXPECT_TRUE(At(4.0, 0.0)); |
| 136 | RunFor(3.5, 0, std::chrono::seconds(1)); |
| 137 | EXPECT_TRUE(At(3.5, 0.0)); |
| 138 | } |
| 139 | |
Austin Schuh | e197a96 | 2024-02-20 18:10:12 -0800 | [diff] [blame] | 140 | // Tests that we can move a goal back before where we currently are without |
| 141 | // teleporting. |
| 142 | TEST_F(TrapezoidProfileTest, MoveGoalFar) { |
| 143 | profile_.set_maximum_acceleration(2.0); |
| 144 | profile_.set_maximum_deceleration(2.0); |
| 145 | profile_.set_maximum_velocity(2.0); |
| 146 | |
| 147 | RunFor(5.0, 0, std::chrono::seconds(1)); |
| 148 | EXPECT_TRUE(At(1.0, 2.0)); |
| 149 | RunFor(5.0, 0, std::chrono::seconds(1)); |
| 150 | EXPECT_TRUE(At(3.0, 2.0)); |
| 151 | RunFor(2.5, 0, std::chrono::seconds(1)); |
| 152 | EXPECT_TRUE(At(4.0, 0.0)); |
| 153 | RunFor(2.5, 0, std::chrono::seconds(2)); |
| 154 | EXPECT_TRUE(At(2.5, 0.0)); |
| 155 | } |
| 156 | |
| 157 | // Tests that we can move a goal without the trajectory teleporting. The goal |
| 158 | // needs to move to something we haven't already passed, but will blow by. Do |
| 159 | // this one in the negative direction. |
| 160 | TEST_F(TrapezoidProfileTest, MoveGoalNegative) { |
| 161 | profile_.set_maximum_acceleration(2.0); |
| 162 | profile_.set_maximum_deceleration(2.0); |
| 163 | profile_.set_maximum_velocity(2.0); |
| 164 | |
| 165 | RunFor(-5.0, 0, std::chrono::seconds(1)); |
| 166 | EXPECT_TRUE(At(-1.0, -2.0)); |
| 167 | RunFor(-5.0, 0, std::chrono::seconds(1)); |
| 168 | EXPECT_TRUE(At(-3.0, -2.0)); |
| 169 | RunFor(-3.5, 0, std::chrono::seconds(1)); |
| 170 | EXPECT_TRUE(At(-4.0, 0.0)); |
| 171 | RunFor(-3.5, 0, std::chrono::seconds(1)); |
| 172 | EXPECT_TRUE(At(-3.5, 0.0)); |
| 173 | } |
| 174 | |
| 175 | // Tests that we can move a goal back before where we currently are without |
| 176 | // teleporting. Do this one in the negative direction. |
| 177 | TEST_F(TrapezoidProfileTest, MoveGoalNegativeFar) { |
| 178 | profile_.set_maximum_acceleration(2.0); |
| 179 | profile_.set_maximum_deceleration(2.0); |
| 180 | profile_.set_maximum_velocity(2.0); |
| 181 | |
| 182 | RunFor(-5.0, 0, std::chrono::seconds(1)); |
| 183 | EXPECT_TRUE(At(-1.0, -2.0)); |
| 184 | RunFor(-5.0, 0, std::chrono::seconds(1)); |
| 185 | EXPECT_TRUE(At(-3.0, -2.0)); |
| 186 | RunFor(-2.5, 0, std::chrono::seconds(1)); |
| 187 | EXPECT_TRUE(At(-4.0, 0.0)); |
| 188 | RunFor(-2.5, 0, std::chrono::seconds(2)); |
| 189 | EXPECT_TRUE(At(-2.5, 0.0)); |
| 190 | } |
| 191 | |
| 192 | // Tests that we can execute a profile with acceleration and deceleration not |
| 193 | // matching in magnitude. |
| 194 | TEST_F(TrapezoidProfileTest, AsymmetricAccelDecel) { |
| 195 | // Accelerates up until t=1. Will be at x=0.5 |
| 196 | profile_.set_maximum_acceleration(1.0); |
| 197 | // Decelerates in t=0.5 Will take x=0.25 |
| 198 | profile_.set_maximum_deceleration(2.0); |
| 199 | profile_.set_maximum_velocity(1.0); |
| 200 | |
| 201 | RunFor(1.75, 0, std::chrono::seconds(1)); |
| 202 | |
| 203 | EXPECT_TRUE(At(0.5, 1.0)); |
| 204 | |
| 205 | RunFor(1.75, 0, std::chrono::seconds(1)); |
| 206 | EXPECT_TRUE(At(1.5, 1.0)); |
| 207 | RunFor(1.75, 0, std::chrono::milliseconds(500)); |
| 208 | EXPECT_TRUE(At(1.75, 0.0)); |
| 209 | } |
| 210 | |
| 211 | // Tests that we can execute a profile with acceleration and deceleration not |
| 212 | // matching in magnitude, and hitting saturation. |
| 213 | TEST_F(TrapezoidProfileTest, AsymmetricAccelDecelUnconstrained) { |
| 214 | // Accelerates up until t=1. Will be at x=0.5 |
| 215 | profile_.set_maximum_acceleration(1.0); |
| 216 | // Decelerates in t=0.5 Will take x=0.25 |
| 217 | profile_.set_maximum_deceleration(2.0); |
| 218 | profile_.set_maximum_velocity(2.0); |
| 219 | |
| 220 | RunFor(0.75, 0, std::chrono::seconds(1)); |
| 221 | EXPECT_TRUE(At(0.5, 1.0)); |
| 222 | |
| 223 | RunFor(0.75, 0, std::chrono::milliseconds(500)); |
| 224 | EXPECT_TRUE(At(0.75, 0.0)); |
| 225 | } |
| 226 | |
| 227 | // Tests that we can execute a profile with acceleration and deceleration not |
| 228 | // matching in magnitude, and hitting saturation. |
| 229 | TEST_F(TrapezoidProfileTest, AsymmetricAccelDecelUnconstrainedNegative) { |
| 230 | // Accelerates up until t=1. Will be at x=0.5 |
| 231 | profile_.set_maximum_acceleration(1.0); |
| 232 | // Decelerates in t=0.5 Will take x=0.25 |
| 233 | profile_.set_maximum_deceleration(2.0); |
| 234 | profile_.set_maximum_velocity(2.0); |
| 235 | |
| 236 | RunFor(-0.75, 0, std::chrono::seconds(1)); |
| 237 | EXPECT_TRUE(At(-0.5, -1.0)); |
| 238 | |
| 239 | RunFor(-0.75, 0, std::chrono::milliseconds(500)); |
| 240 | EXPECT_TRUE(At(-0.75, 0.0)); |
| 241 | } |
| 242 | |
| 243 | // Tests that we can execute a profile with acceleration and deceleration not |
| 244 | // matching in magnitude when going in the negative direction. |
| 245 | TEST_F(TrapezoidProfileTest, AsymmetricAccelDecelNegative) { |
| 246 | // Accelerates up until t=1. Will be at x=0.5 |
| 247 | profile_.set_maximum_acceleration(1.0); |
| 248 | // Decelerates in t=0.5 Will take x=0.25 |
| 249 | profile_.set_maximum_deceleration(2.0); |
| 250 | profile_.set_maximum_velocity(1.0); |
| 251 | |
| 252 | RunFor(-1.75, 0, std::chrono::seconds(1)); |
| 253 | |
| 254 | EXPECT_TRUE(At(-0.5, -1.0)); |
| 255 | |
| 256 | RunFor(-1.75, 0, std::chrono::seconds(1)); |
| 257 | EXPECT_TRUE(At(-1.5, -1.0)); |
| 258 | RunFor(-1.75, 0, std::chrono::milliseconds(500)); |
| 259 | EXPECT_TRUE(At(-1.75, 0.0)); |
| 260 | } |
| 261 | |
| 262 | // Tests that we can move the goal when an asymmetric profile is executing. |
| 263 | TEST_F(TrapezoidProfileTest, AsymmetricAccelDecelMoveGoal) { |
| 264 | // Accelerates up until t=1. Will be at x=0.5 |
| 265 | profile_.set_maximum_acceleration(1.0); |
| 266 | // Decelerates in t=0.5 Will take x=0.25 |
| 267 | profile_.set_maximum_deceleration(2.0); |
| 268 | profile_.set_maximum_velocity(1.0); |
| 269 | |
| 270 | RunFor(1.75, 0, std::chrono::seconds(1)); |
| 271 | |
| 272 | EXPECT_TRUE(At(0.5, 1.0)); |
| 273 | |
| 274 | RunFor(1.75, 0, std::chrono::seconds(1)); |
| 275 | EXPECT_TRUE(At(1.5, 1.0)); |
| 276 | RunFor(1.6, 0, std::chrono::milliseconds(500)); |
| 277 | EXPECT_TRUE(At(1.75, 0.0)); |
| 278 | RunFor(1.6, 0, std::chrono::milliseconds(520)); |
| 279 | RunFor(1.6, 0, std::chrono::milliseconds(2500)); |
| 280 | EXPECT_TRUE(At(1.6, 0.0)); |
| 281 | } |
| 282 | |
| 283 | // Tests that we can move the goal when an asymmetric profile is executing in |
| 284 | // the negative direction. |
| 285 | TEST_F(TrapezoidProfileTest, AsymmetricAccelDecelMoveGoalFar) { |
| 286 | // Accelerates up until t=1. Will be at x=0.5 |
| 287 | profile_.set_maximum_acceleration(1.0); |
| 288 | // Decelerates in t=0.5 Will take x=0.25 |
| 289 | profile_.set_maximum_deceleration(2.0); |
| 290 | profile_.set_maximum_velocity(1.0); |
| 291 | |
| 292 | RunFor(1.75, 0, std::chrono::seconds(1)); |
| 293 | |
| 294 | EXPECT_TRUE(At(0.5, 1.0)); |
| 295 | |
| 296 | RunFor(1.75, 0, std::chrono::seconds(1)); |
| 297 | EXPECT_TRUE(At(1.5, 1.0)); |
| 298 | RunFor(1.0, 0, std::chrono::milliseconds(500)); |
| 299 | EXPECT_TRUE(At(1.75, 0.0)); |
| 300 | RunFor(1.0, 0, std::chrono::milliseconds(2500)); |
| 301 | EXPECT_TRUE(At(1.0, 0.0)); |
| 302 | } |
| 303 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 304 | } // namespace aos::util::testing |