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
21 #include "media/QuaternionUtil.h"
22 #include "TestUtil.h"
23
24 namespace android {
25 namespace media {
26 namespace {
27
28 using Eigen::Quaternionf;
29 using Eigen::Vector3f;
30 using Options = PoseRateLimiter::Options;
31
TEST(PoseRateLimiter,Initial)32 TEST(PoseRateLimiter, Initial) {
33 Pose3f target({1, 2, 3}, Quaternionf::UnitRandom());
34 PoseRateLimiter limiter(Options{.maxTranslationalVelocity = 10, .maxRotationalVelocity = 10});
35 limiter.setTarget(target);
36 EXPECT_EQ(limiter.calculatePose(1000), target);
37 }
38
TEST(PoseRateLimiter,UnlimitedZeroTime)39 TEST(PoseRateLimiter, UnlimitedZeroTime) {
40 Pose3f target1({1, 2, 3}, Quaternionf::UnitRandom());
41 Pose3f target2({4, 5, 6}, Quaternionf::UnitRandom());
42 PoseRateLimiter limiter(Options{});
43 limiter.setTarget(target1);
44 EXPECT_EQ(limiter.calculatePose(0), target1);
45 limiter.setTarget(target2);
46 EXPECT_EQ(limiter.calculatePose(0), target2);
47 limiter.setTarget(target1);
48 EXPECT_EQ(limiter.calculatePose(0), target1);
49 }
50
TEST(PoseRateLimiter,Limited)51 TEST(PoseRateLimiter, Limited) {
52 Pose3f pose1({1, 2, 3}, Quaternionf::Identity());
53 Pose3f pose2({1, 2, 8}, rotateZ(M_PI * 5 / 8));
54 PoseRateLimiter limiter(Options{.maxTranslationalVelocity = 1, .maxRotationalVelocity = 10});
55 limiter.setTarget(pose2);
56 EXPECT_EQ(limiter.calculatePose(1000), pose2);
57
58 // Rate limiting is inactive. Should track despite the violation.
59 limiter.setTarget(pose1);
60 EXPECT_EQ(limiter.calculatePose(1001), pose1);
61
62 // Enable rate limiting and observe gradual motion from pose1 to pose2.
63 limiter.enable();
64 limiter.setTarget(pose2);
65 EXPECT_EQ(limiter.calculatePose(1002), Pose3f({1, 2, 4}, rotateZ(M_PI * 1 / 8)));
66 limiter.setTarget(pose2);
67 EXPECT_EQ(limiter.calculatePose(1003), Pose3f({1, 2, 5}, rotateZ(M_PI * 2 / 8)));
68 // Skip a tick.
69 limiter.setTarget(pose2);
70 EXPECT_EQ(limiter.calculatePose(1005), Pose3f({1, 2, 7}, rotateZ(M_PI * 4 / 8)));
71 limiter.setTarget(pose2);
72 EXPECT_EQ(limiter.calculatePose(1006), pose2);
73
74 // We reached the target, so rate limiting should now be disabled.
75 limiter.setTarget(pose1);
76 EXPECT_EQ(limiter.calculatePose(1007), pose1);
77 }
78
TEST(PoseRateLimiter,Reset)79 TEST(PoseRateLimiter, Reset) {
80 Pose3f pose1({1, 2, 3}, Quaternionf::Identity());
81 Pose3f pose2({1, 2, 8}, rotateZ(M_PI * 5 / 8));
82 PoseRateLimiter limiter(Options{.maxTranslationalVelocity = 1, .maxRotationalVelocity = 10});
83 limiter.setTarget(pose1);
84 EXPECT_EQ(limiter.calculatePose(1000), pose1);
85
86 // Enable rate limiting and observe gradual motion from pose1 to pose2.
87 limiter.enable();
88 limiter.setTarget(pose2);
89 EXPECT_EQ(limiter.calculatePose(1001), Pose3f({1, 2, 4}, rotateZ(M_PI * 1 / 8)));
90
91 // Reset the pose and disable rate limiting.
92 limiter.reset(pose2);
93 EXPECT_EQ(limiter.calculatePose(1002), pose2);
94
95 // Rate limiting should now be disabled.
96 limiter.setTarget(pose1);
97 EXPECT_EQ(limiter.calculatePose(1003), pose1);
98 }
99
100 } // namespace
101 } // namespace media
102 } // namespace android
103