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