Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | /** |
| 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 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 19 | #include "../tuning.hpp" |
| 20 | #include "logging.hpp" |
| 21 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 22 | namespace pid_control |
| 23 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 24 | namespace ec |
| 25 | { |
| 26 | |
| 27 | /******************************** |
| 28 | * clamp |
| 29 | * |
| 30 | */ |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 31 | static double clamp(double x, double min, double max) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 32 | { |
| 33 | if (x < min) |
| 34 | { |
| 35 | return min; |
| 36 | } |
| 37 | else if (x > max) |
| 38 | { |
| 39 | return max; |
| 40 | } |
| 41 | return x; |
| 42 | } |
| 43 | |
| 44 | /******************************** |
| 45 | * pid code |
| 46 | * Note: Codes assumes the ts field is non-zero |
| 47 | */ |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 48 | double pid(pid_info_t* pidinfoptr, double input, double setpoint, |
| 49 | const std::string* nameptr) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 50 | { |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 51 | if (nameptr) |
| 52 | { |
| 53 | if (!(pidinfoptr->initialized)) |
| 54 | { |
| 55 | LogInit(*nameptr, pidinfoptr); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | auto logPtr = nameptr ? LogPeek(*nameptr) : nullptr; |
| 60 | |
| 61 | PidCoreContext coreContext; |
| 62 | std::chrono::milliseconds msNow; |
| 63 | |
| 64 | if (logPtr) |
| 65 | { |
| 66 | msNow = LogTimestamp(); |
| 67 | } |
| 68 | |
| 69 | coreContext.input = input; |
| 70 | coreContext.setpoint = setpoint; |
| 71 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 72 | double error; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 73 | |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 74 | double proportionalTerm; |
| 75 | double integralTerm = 0.0f; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 76 | double derivativeTerm = 0.0f; |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 77 | double feedFwdTerm = 0.0f; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 78 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 79 | double output; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 80 | |
| 81 | // calculate P, I, D, FF |
| 82 | |
| 83 | // Pid |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 84 | error = setpoint - input; |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 85 | proportionalTerm = pidinfoptr->proportionalCoeff * error; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 86 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 87 | coreContext.error = error; |
| 88 | coreContext.proportionalTerm = proportionalTerm; |
| 89 | coreContext.integralTerm1 = 0.0; |
| 90 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 91 | // pId |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 92 | if (0.0f != pidinfoptr->integralCoeff) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 93 | { |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 94 | integralTerm = pidinfoptr->integral; |
| 95 | integralTerm += error * pidinfoptr->integralCoeff * pidinfoptr->ts; |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 96 | |
| 97 | coreContext.integralTerm1 = integralTerm; |
| 98 | |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 99 | integralTerm = clamp(integralTerm, pidinfoptr->integralLimit.min, |
| 100 | pidinfoptr->integralLimit.max); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 103 | coreContext.integralTerm2 = integralTerm; |
| 104 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 105 | // piD |
| 106 | derivativeTerm = pidinfoptr->derivativeCoeff * |
| 107 | ((error - pidinfoptr->lastError) / pidinfoptr->ts); |
| 108 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 109 | coreContext.derivativeTerm = derivativeTerm; |
| 110 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 111 | // FF |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 112 | feedFwdTerm = |
| 113 | (setpoint + pidinfoptr->feedFwdOffset) * pidinfoptr->feedFwdGain; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 114 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 115 | coreContext.feedFwdTerm = feedFwdTerm; |
| 116 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 117 | output = proportionalTerm + integralTerm + derivativeTerm + feedFwdTerm; |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 118 | |
| 119 | coreContext.output1 = output; |
| 120 | |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 121 | output = clamp(output, pidinfoptr->outLim.min, pidinfoptr->outLim.max); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 122 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 123 | coreContext.output2 = output; |
| 124 | |
| 125 | coreContext.minOut = 0.0; |
| 126 | coreContext.maxOut = 0.0; |
| 127 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 128 | // slew rate |
| 129 | // TODO(aarena) - Simplify logic as Andy suggested by creating dynamic |
Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 130 | // outLim_min/max that are affected by slew rate control and just clamping |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 131 | // to those instead of effectively clamping twice. |
| 132 | if (pidinfoptr->initialized) |
| 133 | { |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 134 | if (pidinfoptr->slewNeg != 0.0f) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 135 | { |
| 136 | // Don't decrease too fast |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 137 | double minOut = |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 138 | pidinfoptr->lastOutput + pidinfoptr->slewNeg * pidinfoptr->ts; |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 139 | |
| 140 | coreContext.minOut = minOut; |
| 141 | |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 142 | if (output < minOut) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 143 | { |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 144 | output = minOut; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 147 | if (pidinfoptr->slewPos != 0.0f) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 148 | { |
| 149 | // Don't increase too fast |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 150 | double maxOut = |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 151 | pidinfoptr->lastOutput + pidinfoptr->slewPos * pidinfoptr->ts; |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 152 | |
| 153 | coreContext.maxOut = maxOut; |
| 154 | |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 155 | if (output > maxOut) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 156 | { |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 157 | output = maxOut; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 161 | if (pidinfoptr->slewNeg != 0.0f || pidinfoptr->slewPos != 0.0f) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 162 | { |
| 163 | // Back calculate integral term for the cases where we limited the |
| 164 | // output |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 165 | integralTerm = output - proportionalTerm; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 169 | coreContext.output3 = output; |
| 170 | coreContext.integralTerm3 = integralTerm; |
| 171 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 172 | // Clamp again because having limited the output may result in a |
| 173 | // larger integral term |
Patrick Venture | a23468e | 2019-02-11 09:25:56 -0800 | [diff] [blame] | 174 | integralTerm = clamp(integralTerm, pidinfoptr->integralLimit.min, |
| 175 | pidinfoptr->integralLimit.max); |
| 176 | pidinfoptr->integral = integralTerm; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 177 | pidinfoptr->initialized = true; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 178 | pidinfoptr->lastError = error; |
Patrick Venture | 4b0df32 | 2019-02-11 09:04:57 -0800 | [diff] [blame] | 179 | pidinfoptr->lastOutput = output; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 180 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 181 | coreContext.integralTerm = pidinfoptr->integral; |
| 182 | coreContext.output = pidinfoptr->lastOutput; |
| 183 | |
| 184 | if (logPtr) |
| 185 | { |
| 186 | LogContext(*logPtr, msNow, coreContext); |
| 187 | } |
| 188 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 189 | return output; |
| 190 | } |
| 191 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 192 | } // namespace ec |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 193 | } // namespace pid_control |