1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <input/VelocityControl.h>
#include <limits>
#include <gtest/gtest.h>
#include <input/AccelerationCurve.h>
#include <utils/Timers.h>
namespace android {
namespace {
constexpr float EPSILON = 0.001;
constexpr float COUNTS_PER_MM = 800 / 25.4;
} // namespace
class CurvedVelocityControlTest : public testing::Test {
protected:
CurvedVelocityControl mCtrl;
void moveWithoutCheckingResult(nsecs_t eventTime, float deltaX, float deltaY) {
mCtrl.move(eventTime, &deltaX, &deltaY);
}
void moveAndCheckRatio(nsecs_t eventTime, const float deltaX, const float deltaY,
float expectedRatio) {
float newDeltaX = deltaX, newDeltaY = deltaY;
mCtrl.move(eventTime, &newDeltaX, &newDeltaY);
ASSERT_NEAR(expectedRatio * deltaX, newDeltaX, EPSILON)
<< "Expected ratio of " << expectedRatio << " in X, but actual ratio was "
<< newDeltaX / deltaX;
ASSERT_NEAR(expectedRatio * deltaY, newDeltaY, EPSILON)
<< "Expected ratio of " << expectedRatio << " in Y, but actual ratio was "
<< newDeltaY / deltaY;
}
};
TEST_F(CurvedVelocityControlTest, SegmentSelection) {
// To make the maths simple, use a "curve" that's actually just a sequence of steps.
mCtrl.setCurve({
{10, 2, 0},
{20, 3, 0},
{30, 4, 0},
{std::numeric_limits<double>::infinity(), 5, 0},
});
// Establish a velocity of 16 mm/s.
moveWithoutCheckingResult(0, 0, 0);
moveWithoutCheckingResult(10'000'000, 0.16 * COUNTS_PER_MM, 0);
moveWithoutCheckingResult(20'000'000, 0.16 * COUNTS_PER_MM, 0);
moveWithoutCheckingResult(30'000'000, 0.16 * COUNTS_PER_MM, 0);
ASSERT_NO_FATAL_FAILURE(
moveAndCheckRatio(40'000'000, 0.16 * COUNTS_PER_MM, 0, /*expectedRatio=*/3));
// Establish a velocity of 50 mm/s.
mCtrl.reset();
moveWithoutCheckingResult(100'000'000, 0, 0);
moveWithoutCheckingResult(110'000'000, 0.50 * COUNTS_PER_MM, 0);
moveWithoutCheckingResult(120'000'000, 0.50 * COUNTS_PER_MM, 0);
moveWithoutCheckingResult(130'000'000, 0.50 * COUNTS_PER_MM, 0);
ASSERT_NO_FATAL_FAILURE(
moveAndCheckRatio(140'000'000, 0.50 * COUNTS_PER_MM, 0, /*expectedRatio=*/5));
}
TEST_F(CurvedVelocityControlTest, RatioDefaultsToFirstSegmentWhenVelocityIsUnknown) {
mCtrl.setCurve({
{10, 3, 0},
{20, 2, 0},
{std::numeric_limits<double>::infinity(), 4, 0},
});
// Only send two moves, which won't be enough for VelocityTracker to calculate a velocity from.
moveWithoutCheckingResult(0, 0, 0);
ASSERT_NO_FATAL_FAILURE(
moveAndCheckRatio(10'000'000, 0.25 * COUNTS_PER_MM, 0, /*expectedRatio=*/3));
}
TEST_F(CurvedVelocityControlTest, VelocityCalculatedUsingBothAxes) {
mCtrl.setCurve({
{8.0, 3, 0},
{8.1, 2, 0},
{std::numeric_limits<double>::infinity(), 4, 0},
});
// Establish a velocity of 8.06 (= √65 = √(7²+4²)) mm/s between the two axes.
moveWithoutCheckingResult(0, 0, 0);
moveWithoutCheckingResult(10'000'000, 0.07 * COUNTS_PER_MM, 0.04 * COUNTS_PER_MM);
moveWithoutCheckingResult(20'000'000, 0.07 * COUNTS_PER_MM, 0.04 * COUNTS_PER_MM);
moveWithoutCheckingResult(30'000'000, 0.07 * COUNTS_PER_MM, 0.04 * COUNTS_PER_MM);
ASSERT_NO_FATAL_FAILURE(moveAndCheckRatio(40'000'000, 0.07 * COUNTS_PER_MM,
0.04 * COUNTS_PER_MM,
/*expectedRatio=*/2));
}
TEST_F(CurvedVelocityControlTest, ReciprocalTerm) {
mCtrl.setCurve({
{10, 2, 0},
{20, 3, -10},
{std::numeric_limits<double>::infinity(), 3, 0},
});
// Establish a velocity of 15 mm/s.
moveWithoutCheckingResult(0, 0, 0);
moveWithoutCheckingResult(10'000'000, 0, 0.15 * COUNTS_PER_MM);
moveWithoutCheckingResult(20'000'000, 0, 0.15 * COUNTS_PER_MM);
moveWithoutCheckingResult(30'000'000, 0, 0.15 * COUNTS_PER_MM);
// Expected ratio is 3 - 10 / 15 = 2.33333...
ASSERT_NO_FATAL_FAILURE(
moveAndCheckRatio(40'000'000, 0, 0.15 * COUNTS_PER_MM, /*expectedRatio=*/2.33333));
}
} // namespace android
|