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 <float.h>
22 #include <math.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 
30 #include "src/core/lib/gpr/string.h"
31 #include "test/core/util/test_config.h"
32 
33 namespace grpc_core {
34 namespace testing {
35 
TEST(PidController,NoOp)36 TEST(PidController, NoOp) {
37   PidController pid(PidController::Args()
38                         .set_gain_p(1)
39                         .set_gain_i(1)
40                         .set_gain_d(1)
41                         .set_initial_control_value(1));
42 }
43 
44 struct SimpleConvergenceTestArgs {
45   double gain_p;
46   double gain_i;
47   double gain_d;
48   double dt;
49   double set_point;
50   double start;
51 };
52 
operator <<(std::ostream & out,SimpleConvergenceTestArgs args)53 std::ostream& operator<<(std::ostream& out, SimpleConvergenceTestArgs args) {
54   return out << "gain_p:" << args.gain_p << " gain_i:" << args.gain_i
55              << " gain_d:" << args.gain_d << " dt:" << args.dt
56              << " set_point:" << args.set_point << " start:" << args.start;
57 }
58 
59 class SimpleConvergenceTest
60     : public ::testing::TestWithParam<SimpleConvergenceTestArgs> {};
61 
TEST_P(SimpleConvergenceTest,Converges)62 TEST_P(SimpleConvergenceTest, Converges) {
63   PidController pid(PidController::Args()
64                         .set_gain_p(GetParam().gain_p)
65                         .set_gain_i(GetParam().gain_i)
66                         .set_gain_d(GetParam().gain_d)
67                         .set_initial_control_value(GetParam().start));
68 
69   for (int i = 0; i < 100000; i++) {
70     pid.Update(GetParam().set_point - pid.last_control_value(), GetParam().dt);
71   }
72 
73   EXPECT_LT(fabs(GetParam().set_point - pid.last_control_value()), 0.1);
74   if (GetParam().gain_i > 0) {
75     EXPECT_LT(fabs(pid.error_integral()), 0.1);
76   }
77 }
78 
79 INSTANTIATE_TEST_SUITE_P(
80     X, SimpleConvergenceTest,
81     ::testing::Values(SimpleConvergenceTestArgs{0.2, 0, 0, 1, 100, 0},
82                       SimpleConvergenceTestArgs{0.2, 0.1, 0, 1, 100, 0},
83                       SimpleConvergenceTestArgs{0.2, 0.1, 0.1, 1, 100, 0}));
84 
85 }  // namespace testing
86 }  // namespace grpc_core
87 
main(int argc,char ** argv)88 int main(int argc, char** argv) {
89   grpc::testing::TestEnvironment env(argc, argv);
90   ::testing::InitGoogleTest(&argc, argv);
91   return RUN_ALL_TESTS();
92 }
93