| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2019 Google Inc |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 3 | |
| 4 | #include "pid/buildjson.hpp" |
| 5 | |
| 6 | #include "conf.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 7 | #include "ec/stepwise.hpp" |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 8 | #include "util.hpp" |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 9 | |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 10 | #include <nlohmann/json.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 11 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 12 | #include <cstddef> |
| 13 | #include <cstdint> |
| Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 14 | #include <iostream> |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 15 | #include <limits> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 16 | #include <map> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <utility> |
| 19 | #include <vector> |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 20 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 21 | namespace pid_control |
| 22 | { |
| 23 | |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 24 | using json = nlohmann::json; |
| 25 | |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 26 | namespace conf |
| 27 | { |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 28 | |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 29 | void from_json(const json& j, conf::ControllerInfo& c) |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 30 | { |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 31 | std::vector<std::string> inputNames; |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 32 | std::vector<std::string> missingAcceptableNames; |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 33 | |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 34 | j.at("type").get_to(c.type); |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 35 | j.at("inputs").get_to(inputNames); |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 36 | j.at("setpoint").get_to(c.setpoint); |
| 37 | |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 38 | std::vector<double> inputTempToMargin; |
| 39 | |
| 40 | auto findTempToMargin = j.find("tempToMargin"); |
| 41 | if (findTempToMargin != j.end()) |
| 42 | { |
| 43 | findTempToMargin->get_to(inputTempToMargin); |
| 44 | } |
| 45 | |
| Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 46 | auto findMissingAcceptable = j.find("missingIsAcceptable"); |
| 47 | if (findMissingAcceptable != j.end()) |
| 48 | { |
| 49 | findMissingAcceptable->get_to(missingAcceptableNames); |
| 50 | } |
| 51 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 52 | c.inputs = |
| 53 | spliceInputs(inputNames, inputTempToMargin, missingAcceptableNames); |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 54 | |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 55 | /* TODO: We need to handle parsing other PID controller configurations. |
| 56 | * We can do that by checking for different keys and making the decision |
| 57 | * accordingly. |
| 58 | */ |
| 59 | auto p = j.at("pid"); |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 60 | |
| Delphine CC Chiu | 5d897e2 | 2024-06-04 13:33:22 +0800 | [diff] [blame] | 61 | auto checkHysterWithSetpt = p.find("checkHysteresisWithSetpoint"); |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 62 | auto positiveHysteresis = p.find("positiveHysteresis"); |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 63 | auto negativeHysteresis = p.find("negativeHysteresis"); |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 64 | auto derivativeCoeff = p.find("derivativeCoeff"); |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 65 | auto checkHysterWithSetptValue = false; |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 66 | auto positiveHysteresisValue = 0.0; |
| 67 | auto negativeHysteresisValue = 0.0; |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 68 | auto derivativeCoeffValue = 0.0; |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 69 | if (checkHysterWithSetpt != p.end()) |
| 70 | { |
| 71 | checkHysterWithSetpt->get_to(checkHysterWithSetptValue); |
| 72 | } |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 73 | if (positiveHysteresis != p.end()) |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 74 | { |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 75 | positiveHysteresis->get_to(positiveHysteresisValue); |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 76 | } |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 77 | if (negativeHysteresis != p.end()) |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 78 | { |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 79 | negativeHysteresis->get_to(negativeHysteresisValue); |
| 80 | } |
| 81 | if (derivativeCoeff != p.end()) |
| 82 | { |
| 83 | derivativeCoeff->get_to(derivativeCoeffValue); |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 86 | auto failSafePercent = j.find("FailSafePercent"); |
| 87 | auto failSafePercentValue = 0; |
| 88 | if (failSafePercent != j.end()) |
| 89 | { |
| 90 | failSafePercent->get_to(failSafePercentValue); |
| 91 | } |
| 92 | c.failSafePercent = failSafePercentValue; |
| 93 | |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 94 | if (c.type != "stepwise") |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 95 | { |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 96 | p.at("samplePeriod").get_to(c.pidInfo.ts); |
| 97 | p.at("proportionalCoeff").get_to(c.pidInfo.proportionalCoeff); |
| 98 | p.at("integralCoeff").get_to(c.pidInfo.integralCoeff); |
| 99 | p.at("feedFwdOffsetCoeff").get_to(c.pidInfo.feedFwdOffset); |
| 100 | p.at("feedFwdGainCoeff").get_to(c.pidInfo.feedFwdGain); |
| 101 | p.at("integralLimit_min").get_to(c.pidInfo.integralLimit.min); |
| 102 | p.at("integralLimit_max").get_to(c.pidInfo.integralLimit.max); |
| 103 | p.at("outLim_min").get_to(c.pidInfo.outLim.min); |
| 104 | p.at("outLim_max").get_to(c.pidInfo.outLim.max); |
| 105 | p.at("slewNeg").get_to(c.pidInfo.slewNeg); |
| 106 | p.at("slewPos").get_to(c.pidInfo.slewPos); |
| 107 | |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 108 | // Unlike other coefficients, treat derivativeCoeff as an optional |
| 109 | // parameter, as support for it is fairly new, to avoid breaking |
| 110 | // existing configurations in the field that predate it. |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 111 | c.pidInfo.positiveHysteresis = positiveHysteresisValue; |
| 112 | c.pidInfo.negativeHysteresis = negativeHysteresisValue; |
| Josh Lehan | c612c05 | 2022-12-12 09:56:47 -0800 | [diff] [blame] | 113 | c.pidInfo.derivativeCoeff = derivativeCoeffValue; |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 114 | c.pidInfo.checkHysterWithSetpt = checkHysterWithSetptValue; |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
| Hank Liou | 375f709 | 2019-03-29 20:15:42 +0800 | [diff] [blame] | 118 | p.at("samplePeriod").get_to(c.stepwiseInfo.ts); |
| 119 | p.at("isCeiling").get_to(c.stepwiseInfo.isCeiling); |
| 120 | |
| 121 | for (size_t i = 0; i < ec::maxStepwisePoints; i++) |
| 122 | { |
| 123 | c.stepwiseInfo.reading[i] = |
| 124 | std::numeric_limits<double>::quiet_NaN(); |
| 125 | c.stepwiseInfo.output[i] = std::numeric_limits<double>::quiet_NaN(); |
| 126 | } |
| 127 | |
| 128 | auto reading = p.find("reading"); |
| 129 | if (reading != p.end()) |
| 130 | { |
| 131 | auto r = p.at("reading"); |
| 132 | for (size_t i = 0; i < ec::maxStepwisePoints; i++) |
| 133 | { |
| 134 | auto n = r.find(std::to_string(i)); |
| 135 | if (n != r.end()) |
| 136 | { |
| 137 | r.at(std::to_string(i)).get_to(c.stepwiseInfo.reading[i]); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | auto output = p.find("output"); |
| 143 | if (output != p.end()) |
| 144 | { |
| 145 | auto o = p.at("output"); |
| 146 | for (size_t i = 0; i < ec::maxStepwisePoints; i++) |
| 147 | { |
| 148 | auto n = o.find(std::to_string(i)); |
| 149 | if (n != o.end()) |
| 150 | { |
| 151 | o.at(std::to_string(i)).get_to(c.stepwiseInfo.output[i]); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | c.stepwiseInfo.positiveHysteresis = positiveHysteresisValue; |
| 157 | c.stepwiseInfo.negativeHysteresis = negativeHysteresisValue; |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 160 | |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 161 | } // namespace conf |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 162 | |
| Harvey Wu | 239aa7d | 2022-11-18 08:43:34 +0800 | [diff] [blame] | 163 | inline void getCycleTimeSetting(const auto& zone, const int id, |
| 164 | const std::string& attributeName, |
| 165 | uint64_t& value) |
| 166 | { |
| 167 | auto findAttributeName = zone.find(attributeName); |
| 168 | if (findAttributeName != zone.end()) |
| 169 | { |
| 170 | uint64_t tmpAttributeValue = 0; |
| 171 | findAttributeName->get_to(tmpAttributeValue); |
| 172 | if (tmpAttributeValue >= 1) |
| 173 | { |
| 174 | value = tmpAttributeValue; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | std::cerr << "Zone " << id << ": " << attributeName |
| 179 | << " is invalid. Use default " << value << " ms\n"; |
| 180 | } |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | std::cerr << "Zone " << id << ": " << attributeName |
| 185 | << " cannot find setting. Use default " << value << " ms\n"; |
| 186 | } |
| 187 | } |
| 188 | |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 189 | std::pair<std::map<int64_t, conf::PIDConf>, std::map<int64_t, conf::ZoneConfig>> |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 190 | buildPIDsFromJson(const json& data) |
| 191 | { |
| 192 | // zone -> pids |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 193 | std::map<int64_t, conf::PIDConf> pidConfig; |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 194 | // zone -> configs |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 195 | std::map<int64_t, conf::ZoneConfig> zoneConfig; |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 196 | |
| 197 | /* TODO: if zones is empty, that's invalid. */ |
| 198 | auto zones = data["zones"]; |
| 199 | for (const auto& zone : zones) |
| 200 | { |
| 201 | int64_t id; |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 202 | conf::PIDConf thisZone; |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 203 | conf::ZoneConfig thisZoneConfig; |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 204 | |
| 205 | /* TODO: using at() throws a specific exception we can catch */ |
| 206 | id = zone["id"]; |
| James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 207 | thisZoneConfig.minThermalOutput = zone["minThermalOutput"]; |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 208 | thisZoneConfig.failsafePercent = zone["failsafePercent"]; |
| 209 | |
| Harvey Wu | 239aa7d | 2022-11-18 08:43:34 +0800 | [diff] [blame] | 210 | getCycleTimeSetting(zone, id, "cycleIntervalTimeMS", |
| 211 | thisZoneConfig.cycleTime.cycleIntervalTimeMS); |
| 212 | getCycleTimeSetting(zone, id, "updateThermalsTimeMS", |
| 213 | thisZoneConfig.cycleTime.updateThermalsTimeMS); |
| Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 214 | |
| Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 215 | bool accumulateSetPoint = false; |
| 216 | auto findAccSetPoint = zone.find("accumulateSetPoint"); |
| 217 | if (findAccSetPoint != zone.end()) |
| 218 | { |
| 219 | findAccSetPoint->get_to(accumulateSetPoint); |
| 220 | } |
| 221 | thisZoneConfig.accumulateSetPoint = accumulateSetPoint; |
| 222 | |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 223 | auto pids = zone["pids"]; |
| 224 | for (const auto& pid : pids) |
| 225 | { |
| 226 | auto name = pid["name"]; |
| James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 227 | auto item = pid.get<conf::ControllerInfo>(); |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 228 | |
| ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 229 | if (thisZone.find(name) != thisZone.end()) |
| 230 | { |
| 231 | std::cerr << "Warning: zone " << id |
| 232 | << " have the same pid name " << name << std::endl; |
| 233 | } |
| 234 | |
| Patrick Venture | d149172 | 2019-02-08 14:37:45 -0800 | [diff] [blame] | 235 | thisZone[name] = item; |
| 236 | } |
| 237 | |
| 238 | pidConfig[id] = thisZone; |
| 239 | zoneConfig[id] = thisZoneConfig; |
| 240 | } |
| 241 | |
| 242 | return std::make_pair(pidConfig, zoneConfig); |
| 243 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 244 | |
| 245 | } // namespace pid_control |