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