pid_controller_test.cc 2.7 KB

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