aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/transport/pid_controller.h
blob: 87e59a1a9049de8658c02b9b7175315b162db15b (plain)
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
/*
 *
 * Copyright 2016 gRPC authors.
 *
 * 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.
 *
 */

#ifndef GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H
#define GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H

#include <limits>

/* \file Simple PID controller.
   Implements a proportional-integral-derivative controller.
   Used when we want to iteratively control a variable to converge some other
   observed value to a 'set-point'.
   Gains can be set to adjust sensitivity to current error (p), the integral
   of error (i), and the derivative of error (d). */

namespace grpc_core {

class PidController {
 public:
  class Args {
   public:
    double gain_p() const { return gain_p_; }
    double gain_i() const { return gain_i_; }
    double gain_d() const { return gain_d_; }
    double initial_control_value() const { return initial_control_value_; }
    double min_control_value() const { return min_control_value_; }
    double max_control_value() const { return max_control_value_; }
    double integral_range() const { return integral_range_; }

    Args& set_gain_p(double gain_p) {
      gain_p_ = gain_p;
      return *this;
    }
    Args& set_gain_i(double gain_i) {
      gain_i_ = gain_i;
      return *this;
    }
    Args& set_gain_d(double gain_d) {
      gain_d_ = gain_d;
      return *this;
    }
    Args& set_initial_control_value(double initial_control_value) {
      initial_control_value_ = initial_control_value;
      return *this;
    }
    Args& set_min_control_value(double min_control_value) {
      min_control_value_ = min_control_value;
      return *this;
    }
    Args& set_max_control_value(double max_control_value) {
      max_control_value_ = max_control_value;
      return *this;
    }
    Args& set_integral_range(double integral_range) {
      integral_range_ = integral_range;
      return *this;
    }

   private:
    double gain_p_ = 0.0;
    double gain_i_ = 0.0;
    double gain_d_ = 0.0;
    double initial_control_value_ = 0.0;
    double min_control_value_ = std::numeric_limits<double>::min();
    double max_control_value_ = std::numeric_limits<double>::max();
    double integral_range_ = std::numeric_limits<double>::max();
  };

  explicit PidController(const Args& args);

  /// Reset the controller internal state: useful when the environment has
  /// changed significantly
  void Reset() {
    last_error_ = 0.0;
    last_dc_dt_ = 0.0;
    error_integral_ = 0.0;
  }

  /// Update the controller: given a current error estimate, and the time since
  /// the last update, returns a new control value
  double Update(double error, double dt);

  /// Returns the last control value calculated
  double last_control_value() const { return last_control_value_; }

  /// Returns the current error integral (mostly for testing)
  double error_integral() const { return error_integral_; }

 private:
  double last_error_ = 0.0;
  double error_integral_ = 0.0;
  double last_control_value_;
  double last_dc_dt_ = 0.0;
  const Args args_;
};

}  // namespace grpc_core

#endif /* GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H */