blob: 17feabfd3935aea16355b141b872ace945a01c75 [file] [log] [blame]
Craig Tiller6c7b6bf2016-10-20 15:03:38 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Craig Tiller6c7b6bf2016-10-20 15:03:38 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller6c7b6bf2016-10-20 15:03:38 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070016 *
17 */
18
19#ifndef GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H
20#define GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H
21
Yash Tibrewala7e6d652017-09-20 18:56:37 -070022#ifdef __cplusplus
23extern "C" {
24#endif
25
Craig Tiller6d4340d2016-10-21 08:15:51 -070026/* \file Simple PID controller.
Craig Tiller57b77602016-11-04 13:48:49 -070027 Implements a proportional-integral-derivative controller.
Craig Tiller6d4340d2016-10-21 08:15:51 -070028 Used when we want to iteratively control a variable to converge some other
29 observed value to a 'set-point'.
30 Gains can be set to adjust sensitivity to current error (p), the integral
31 of error (i), and the derivative of error (d). */
32
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070033typedef struct {
34 double gain_p;
35 double gain_i;
36 double gain_d;
Craig Tiller68c9dbe2017-01-03 11:31:34 -080037 double initial_control_value;
38 double min_control_value;
39 double max_control_value;
40 double integral_range;
41} grpc_pid_controller_args;
42
43typedef struct {
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070044 double last_error;
45 double error_integral;
Craig Tiller95234e12017-01-03 10:48:10 -080046 double last_control_value;
47 double last_dc_dt;
Craig Tiller68c9dbe2017-01-03 11:31:34 -080048 grpc_pid_controller_args args;
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070049} grpc_pid_controller;
50
Craig Tiller6d4340d2016-10-21 08:15:51 -070051/** Initialize the controller */
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070052void grpc_pid_controller_init(grpc_pid_controller *pid_controller,
Craig Tiller68c9dbe2017-01-03 11:31:34 -080053 grpc_pid_controller_args args);
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070054
Craig Tiller6d4340d2016-10-21 08:15:51 -070055/** Reset the controller: useful when things have changed significantly */
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070056void grpc_pid_controller_reset(grpc_pid_controller *pid_controller);
57
Craig Tiller6d4340d2016-10-21 08:15:51 -070058/** Update the controller: given a current error estimate, and the time since
Craig Tiller95234e12017-01-03 10:48:10 -080059 the last update, returns a new control value */
Craig Tiller6c7b6bf2016-10-20 15:03:38 -070060double grpc_pid_controller_update(grpc_pid_controller *pid_controller,
61 double error, double dt);
62
Craig Tiller95234e12017-01-03 10:48:10 -080063/** Returns the last control value calculated */
64double grpc_pid_controller_last(grpc_pid_controller *pid_controller);
65
Yash Tibrewala7e6d652017-09-20 18:56:37 -070066#ifdef __cplusplus
67}
68#endif
69
70#endif /* GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H */