| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "src/core/lib/transport/pid_controller.h" |
| 20 |
|
| 21 |
#include <math.h> |
| 22 |
|
| 23 |
#include <ostream> |
| 24 |
|
| 25 |
#include "gtest/gtest.h" |
| 26 |
|
| 27 |
#include "test/core/util/test_config.h" |
| 28 |
|
| 29 |
namespace grpc_core { |
| 30 |
namespace testing { |
| 31 |
|
| 32 |
TEST(PidController, NoOp) { |
| 33 |
PidController pid(PidController::Args() |
| 34 |
.set_gain_p(1) |
| 35 |
.set_gain_i(1) |
| 36 |
.set_gain_d(1) |
| 37 |
.set_initial_control_value(1)); |
| 38 |
} |
| 39 |
|
| 40 |
struct SimpleConvergenceTestArgs { |
| 41 |
double gain_p; |
| 42 |
double gain_i; |
| 43 |
double gain_d; |
| 44 |
double dt; |
| 45 |
double set_point; |
| 46 |
double start; |
| 47 |
}; |
| 48 |
|
| 49 |
std::ostream& operator<<(std::ostream& out, SimpleConvergenceTestArgs args) { |
| 50 |
return out << "gain_p:" << args.gain_p << " gain_i:" << args.gain_i |
| 51 |
<< " gain_d:" << args.gain_d << " dt:" << args.dt |
| 52 |
<< " set_point:" << args.set_point << " start:" << args.start; |
| 53 |
} |
| 54 |
|
| 55 |
class SimpleConvergenceTest |
| 56 |
: public ::testing::TestWithParam<SimpleConvergenceTestArgs> {}; |
| 57 |
|
| 58 |
TEST_P(SimpleConvergenceTest, Converges) { |
| 59 |
PidController pid(PidController::Args() |
| 60 |
.set_gain_p(GetParam().gain_p) |
| 61 |
.set_gain_i(GetParam().gain_i) |
| 62 |
.set_gain_d(GetParam().gain_d) |
| 63 |
.set_initial_control_value(GetParam().start)); |
| 64 |
|
| 65 |
for (int i = 0; i < 100000; i++) { |
| 66 |
pid.Update(GetParam().set_point - pid.last_control_value(), GetParam().dt); |
| 67 |
} |
| 68 |
|
| 69 |
EXPECT_LT(fabs(GetParam().set_point - pid.last_control_value()), 0.1); |
| 70 |
if (GetParam().gain_i > 0) { |
| 71 |
EXPECT_LT(fabs(pid.error_integral()), 0.1); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
INSTANTIATE_TEST_SUITE_P( |
| 76 |
X, SimpleConvergenceTest, |
| 77 |
::testing::Values(SimpleConvergenceTestArgs{0.2, 0, 0, 1, 100, 0}, |
| 78 |
SimpleConvergenceTestArgs{0.2, 0.1, 0, 1, 100, 0}, |
| 79 |
SimpleConvergenceTestArgs{0.2, 0.1, 0.1, 1, 100, 0})); |
| 80 |
|
| 81 |
} // namespace testing |
| 82 |
} // namespace grpc_core |
| 83 |
|
| 84 |
int main(int argc, char** argv) { |
| 85 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 86 |
::testing::InitGoogleTest(&argc, argv); |
| 87 |
return RUN_ALL_TESTS(); |
| 88 |
} |