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