Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include "PoseRateLimiter.h" |
| 20 | #include "QuaternionUtil.h" |
| 21 | #include "TestUtil.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace media { |
| 25 | namespace { |
| 26 | |
| 27 | using Eigen::Quaternionf; |
| 28 | using Eigen::Vector3f; |
| 29 | using Options = PoseRateLimiter::Options; |
| 30 | |
| 31 | TEST(PoseRateLimiter, Initial) { |
| 32 | Pose3f target({1, 2, 3}, Quaternionf::UnitRandom()); |
| 33 | PoseRateLimiter limiter(Options{.maxTranslationalVelocity = 10, .maxRotationalVelocity = 10}); |
| 34 | limiter.setTarget(target); |
| 35 | EXPECT_EQ(limiter.calculatePose(1000), target); |
| 36 | } |
| 37 | |
| 38 | TEST(PoseRateLimiter, UnlimitedZeroTime) { |
| 39 | Pose3f target1({1, 2, 3}, Quaternionf::UnitRandom()); |
| 40 | Pose3f target2({4, 5, 6}, Quaternionf::UnitRandom()); |
| 41 | PoseRateLimiter limiter(Options{}); |
| 42 | limiter.setTarget(target1); |
| 43 | EXPECT_EQ(limiter.calculatePose(0), target1); |
| 44 | limiter.setTarget(target2); |
| 45 | EXPECT_EQ(limiter.calculatePose(0), target2); |
| 46 | limiter.setTarget(target1); |
| 47 | EXPECT_EQ(limiter.calculatePose(0), target1); |
| 48 | } |
| 49 | |
| 50 | TEST(PoseRateLimiter, Limited) { |
| 51 | Pose3f pose1({1, 2, 3}, Quaternionf::Identity()); |
| 52 | Pose3f pose2({1, 2, 8}, rotateZ(M_PI * 5 / 8)); |
| 53 | PoseRateLimiter limiter(Options{.maxTranslationalVelocity = 1, .maxRotationalVelocity = 10}); |
| 54 | limiter.setTarget(pose2); |
| 55 | EXPECT_EQ(limiter.calculatePose(1000), pose2); |
| 56 | |
| 57 | // Rate limiting is inactive. Should track despite the violation. |
| 58 | limiter.setTarget(pose1); |
| 59 | EXPECT_EQ(limiter.calculatePose(1001), pose1); |
| 60 | |
| 61 | // Enable rate limiting and observe gradual motion from pose1 to pose2. |
| 62 | limiter.enable(); |
| 63 | limiter.setTarget(pose2); |
| 64 | EXPECT_EQ(limiter.calculatePose(1002), Pose3f({1, 2, 4}, rotateZ(M_PI * 1 / 8))); |
| 65 | limiter.setTarget(pose2); |
| 66 | EXPECT_EQ(limiter.calculatePose(1003), Pose3f({1, 2, 5}, rotateZ(M_PI * 2 / 8))); |
| 67 | // Skip a tick. |
| 68 | limiter.setTarget(pose2); |
| 69 | EXPECT_EQ(limiter.calculatePose(1005), Pose3f({1, 2, 7}, rotateZ(M_PI * 4 / 8))); |
| 70 | limiter.setTarget(pose2); |
| 71 | EXPECT_EQ(limiter.calculatePose(1006), pose2); |
| 72 | |
| 73 | // We reached the target, so rate limiting should now be disabled. |
| 74 | limiter.setTarget(pose1); |
| 75 | EXPECT_EQ(limiter.calculatePose(1007), pose1); |
| 76 | } |
| 77 | |
| 78 | TEST(PoseRateLimiter, Reset) { |
| 79 | Pose3f pose1({1, 2, 3}, Quaternionf::Identity()); |
| 80 | Pose3f pose2({1, 2, 8}, rotateZ(M_PI * 5 / 8)); |
| 81 | PoseRateLimiter limiter(Options{.maxTranslationalVelocity = 1, .maxRotationalVelocity = 10}); |
| 82 | limiter.setTarget(pose1); |
| 83 | EXPECT_EQ(limiter.calculatePose(1000), pose1); |
| 84 | |
| 85 | // Enable rate limiting and observe gradual motion from pose1 to pose2. |
| 86 | limiter.enable(); |
| 87 | limiter.setTarget(pose2); |
| 88 | EXPECT_EQ(limiter.calculatePose(1001), Pose3f({1, 2, 4}, rotateZ(M_PI * 1 / 8))); |
| 89 | |
| 90 | // Reset the pose and disable rate limiting. |
| 91 | limiter.reset(pose2); |
| 92 | EXPECT_EQ(limiter.calculatePose(1002), pose2); |
| 93 | |
| 94 | // Rate limiting should now be disabled. |
| 95 | limiter.setTarget(pose1); |
| 96 | EXPECT_EQ(limiter.calculatePose(1003), pose1); |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | } // namespace media |
| 101 | } // namespace android |