blob: 6c70bbe70d3d5bdf3fccfe0303760859d82e78c3 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001/**
2 * Copyright 2017 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pid.hpp"
18
19namespace ec
20{
21
22/********************************
23 * clamp
24 *
25 */
Patrick Venture5f59c0f2018-11-11 12:55:14 -080026static double clamp(double x, double min, double max)
Patrick Ventured8012182018-03-08 08:21:38 -080027{
28 if (x < min)
29 {
30 return min;
31 }
32 else if (x > max)
33 {
34 return max;
35 }
36 return x;
37}
38
39/********************************
40 * pid code
41 * Note: Codes assumes the ts field is non-zero
42 */
Patrick Venture5f59c0f2018-11-11 12:55:14 -080043double pid(pid_info_t* pidinfoptr, double input, double setpoint)
Patrick Ventured8012182018-03-08 08:21:38 -080044{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080045 double error;
Patrick Ventured8012182018-03-08 08:21:38 -080046
Patrick Venture5f59c0f2018-11-11 12:55:14 -080047 double p_term;
48 double i_term = 0.0f;
49 double ff_term = 0.0f;
Patrick Ventured8012182018-03-08 08:21:38 -080050
Patrick Venture5f59c0f2018-11-11 12:55:14 -080051 double output;
Patrick Ventured8012182018-03-08 08:21:38 -080052
53 // calculate P, I, D, FF
54
55 // Pid
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070056 error = setpoint - input;
Patrick Venture4b0df322019-02-11 09:04:57 -080057 p_term = pidinfoptr->proportionalCoeff * error;
Patrick Ventured8012182018-03-08 08:21:38 -080058
59 // pId
Patrick Venture4b0df322019-02-11 09:04:57 -080060 if (0.0f != pidinfoptr->integralCoeff)
Patrick Ventured8012182018-03-08 08:21:38 -080061 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070062 i_term = pidinfoptr->integral;
Patrick Venture4b0df322019-02-11 09:04:57 -080063 i_term += error * pidinfoptr->integralCoeff * pidinfoptr->ts;
64 i_term = clamp(i_term, pidinfoptr->integralLimit.min,
65 pidinfoptr->integralLimit.max);
Patrick Ventured8012182018-03-08 08:21:38 -080066 }
67
68 // FF
Patrick Venture4b0df322019-02-11 09:04:57 -080069 ff_term = (setpoint + pidinfoptr->feedFwdOffset) * pidinfoptr->feedFwdGain;
Patrick Ventured8012182018-03-08 08:21:38 -080070
71 output = p_term + i_term + ff_term;
Patrick Venture4b0df322019-02-11 09:04:57 -080072 output = clamp(output, pidinfoptr->outLim.min, pidinfoptr->outLim.max);
Patrick Ventured8012182018-03-08 08:21:38 -080073
74 // slew rate
75 // TODO(aarena) - Simplify logic as Andy suggested by creating dynamic
76 // out_lim_min/max that are affected by slew rate control and just clamping
77 // to those instead of effectively clamping twice.
78 if (pidinfoptr->initialized)
79 {
Patrick Venture4b0df322019-02-11 09:04:57 -080080 if (pidinfoptr->slewNeg != 0.0f)
Patrick Ventured8012182018-03-08 08:21:38 -080081 {
82 // Don't decrease too fast
Patrick Venture5f59c0f2018-11-11 12:55:14 -080083 double min_out =
Patrick Venture4b0df322019-02-11 09:04:57 -080084 pidinfoptr->lastOutput + pidinfoptr->slewNeg * pidinfoptr->ts;
Patrick Ventured8012182018-03-08 08:21:38 -080085 if (output < min_out)
86 {
87 output = min_out;
88 }
89 }
Patrick Venture4b0df322019-02-11 09:04:57 -080090 if (pidinfoptr->slewPos != 0.0f)
Patrick Ventured8012182018-03-08 08:21:38 -080091 {
92 // Don't increase too fast
Patrick Venture5f59c0f2018-11-11 12:55:14 -080093 double max_out =
Patrick Venture4b0df322019-02-11 09:04:57 -080094 pidinfoptr->lastOutput + pidinfoptr->slewPos * pidinfoptr->ts;
Patrick Ventured8012182018-03-08 08:21:38 -080095 if (output > max_out)
96 {
97 output = max_out;
98 }
99 }
100
Patrick Venture4b0df322019-02-11 09:04:57 -0800101 if (pidinfoptr->slewNeg != 0.0f || pidinfoptr->slewPos != 0.0f)
Patrick Ventured8012182018-03-08 08:21:38 -0800102 {
103 // Back calculate integral term for the cases where we limited the
104 // output
105 i_term = output - p_term;
106 }
107 }
108
109 // Clamp again because having limited the output may result in a
110 // larger integral term
Patrick Venture4b0df322019-02-11 09:04:57 -0800111 i_term = clamp(i_term, pidinfoptr->integralLimit.min,
112 pidinfoptr->integralLimit.max);
Patrick Ventured8012182018-03-08 08:21:38 -0800113 pidinfoptr->integral = i_term;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700114 pidinfoptr->initialized = true;
Patrick Venture4b0df322019-02-11 09:04:57 -0800115 pidinfoptr->lastOutput = output;
Patrick Ventured8012182018-03-08 08:21:38 -0800116
117 return output;
118}
119
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700120} // namespace ec