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