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