Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H |
| 20 | #define GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H |
| 21 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
Craig Tiller | 6d4340d | 2016-10-21 08:15:51 -0700 | [diff] [blame] | 26 | /* \file Simple PID controller. |
Craig Tiller | 57b7760 | 2016-11-04 13:48:49 -0700 | [diff] [blame] | 27 | Implements a proportional-integral-derivative controller. |
Craig Tiller | 6d4340d | 2016-10-21 08:15:51 -0700 | [diff] [blame] | 28 | 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 Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 33 | typedef struct { |
| 34 | double gain_p; |
| 35 | double gain_i; |
| 36 | double gain_d; |
Craig Tiller | 68c9dbe | 2017-01-03 11:31:34 -0800 | [diff] [blame] | 37 | double initial_control_value; |
| 38 | double min_control_value; |
| 39 | double max_control_value; |
| 40 | double integral_range; |
| 41 | } grpc_pid_controller_args; |
| 42 | |
| 43 | typedef struct { |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 44 | double last_error; |
| 45 | double error_integral; |
Craig Tiller | 95234e1 | 2017-01-03 10:48:10 -0800 | [diff] [blame] | 46 | double last_control_value; |
| 47 | double last_dc_dt; |
Craig Tiller | 68c9dbe | 2017-01-03 11:31:34 -0800 | [diff] [blame] | 48 | grpc_pid_controller_args args; |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 49 | } grpc_pid_controller; |
| 50 | |
Craig Tiller | 6d4340d | 2016-10-21 08:15:51 -0700 | [diff] [blame] | 51 | /** Initialize the controller */ |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 52 | void grpc_pid_controller_init(grpc_pid_controller *pid_controller, |
Craig Tiller | 68c9dbe | 2017-01-03 11:31:34 -0800 | [diff] [blame] | 53 | grpc_pid_controller_args args); |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 54 | |
Craig Tiller | 6d4340d | 2016-10-21 08:15:51 -0700 | [diff] [blame] | 55 | /** Reset the controller: useful when things have changed significantly */ |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 56 | void grpc_pid_controller_reset(grpc_pid_controller *pid_controller); |
| 57 | |
Craig Tiller | 6d4340d | 2016-10-21 08:15:51 -0700 | [diff] [blame] | 58 | /** Update the controller: given a current error estimate, and the time since |
Craig Tiller | 95234e1 | 2017-01-03 10:48:10 -0800 | [diff] [blame] | 59 | the last update, returns a new control value */ |
Craig Tiller | 6c7b6bf | 2016-10-20 15:03:38 -0700 | [diff] [blame] | 60 | double grpc_pid_controller_update(grpc_pid_controller *pid_controller, |
| 61 | double error, double dt); |
| 62 | |
Craig Tiller | 95234e1 | 2017-01-03 10:48:10 -0800 | [diff] [blame] | 63 | /** Returns the last control value calculated */ |
| 64 | double grpc_pid_controller_last(grpc_pid_controller *pid_controller); |
| 65 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 66 | #ifdef __cplusplus |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | #endif /* GRPC_CORE_LIB_TRANSPORT_PID_CONTROLLER_H */ |