blob: 57fabbfb6dd2cc83e9e5a5b2525a8155307ddce7 [file] [log] [blame]
Patrick Ventured1491722019-02-08 14:37:45 -08001/**
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 Lehan31058fd2023-01-13 11:06:16 -080020#include "util.hpp"
Patrick Ventured1491722019-02-08 14:37:45 -080021
Patrick Ventured1491722019-02-08 14:37:45 -080022#include <nlohmann/json.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070023
Bonnie Lo0e8fc392022-10-05 10:20:55 +080024#include <iostream>
Josh Lehan31058fd2023-01-13 11:06:16 -080025#include <limits>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070026#include <map>
Patrick Ventured1491722019-02-08 14:37:45 -080027#include <tuple>
28
Patrick Venturea0764872020-08-08 07:48:43 -070029namespace pid_control
30{
31
Patrick Ventured1491722019-02-08 14:37:45 -080032using json = nlohmann::json;
33
James Feistf81f2882019-02-26 11:26:36 -080034namespace conf
35{
Josh Lehan31058fd2023-01-13 11:06:16 -080036
James Feistf81f2882019-02-26 11:26:36 -080037void from_json(const json& j, conf::ControllerInfo& c)
Patrick Ventured1491722019-02-08 14:37:45 -080038{
Josh Lehan31058fd2023-01-13 11:06:16 -080039 std::vector<std::string> inputNames;
Josh Lehan3f0f7bc2023-02-13 01:45:29 -080040 std::vector<std::string> missingAcceptableNames;
Josh Lehan31058fd2023-01-13 11:06:16 -080041
Patrick Ventured1491722019-02-08 14:37:45 -080042 j.at("type").get_to(c.type);
Josh Lehan31058fd2023-01-13 11:06:16 -080043 j.at("inputs").get_to(inputNames);
Patrick Ventured1491722019-02-08 14:37:45 -080044 j.at("setpoint").get_to(c.setpoint);
45
Josh Lehan31058fd2023-01-13 11:06:16 -080046 std::vector<double> inputTempToMargin;
47
48 auto findTempToMargin = j.find("tempToMargin");
49 if (findTempToMargin != j.end())
50 {
51 findTempToMargin->get_to(inputTempToMargin);
52 }
53
Josh Lehan3f0f7bc2023-02-13 01:45:29 -080054 auto findMissingAcceptable = j.find("missingIsAcceptable");
55 if (findMissingAcceptable != j.end())
56 {
57 findMissingAcceptable->get_to(missingAcceptableNames);
58 }
59
60 c.inputs = spliceInputs(inputNames, inputTempToMargin,
61 missingAcceptableNames);
Josh Lehan31058fd2023-01-13 11:06:16 -080062
Patrick Ventured1491722019-02-08 14:37:45 -080063 /* TODO: We need to handle parsing other PID controller configurations.
64 * We can do that by checking for different keys and making the decision
65 * accordingly.
66 */
67 auto p = j.at("pid");
Patrick Ventured1491722019-02-08 14:37:45 -080068
Delphine CC Chiu97889632023-11-06 11:32:46 +080069 auto checkHysterWithSetpt = p.find("checkHysterWithSetpt");
Patrick Ventured1491722019-02-08 14:37:45 -080070 auto positiveHysteresis = p.find("positiveHysteresis");
Hank Liou375f7092019-03-29 20:15:42 +080071 auto negativeHysteresis = p.find("negativeHysteresis");
Josh Lehanc612c052022-12-12 09:56:47 -080072 auto derivativeCoeff = p.find("derivativeCoeff");
Delphine CC Chiu97889632023-11-06 11:32:46 +080073 auto checkHysterWithSetptValue = false;
Hank Liou375f7092019-03-29 20:15:42 +080074 auto positiveHysteresisValue = 0.0;
75 auto negativeHysteresisValue = 0.0;
Josh Lehanc612c052022-12-12 09:56:47 -080076 auto derivativeCoeffValue = 0.0;
Delphine CC Chiu97889632023-11-06 11:32:46 +080077 if (checkHysterWithSetpt != p.end())
78 {
79 checkHysterWithSetpt->get_to(checkHysterWithSetptValue);
80 }
Hank Liou375f7092019-03-29 20:15:42 +080081 if (positiveHysteresis != p.end())
Patrick Ventured1491722019-02-08 14:37:45 -080082 {
Josh Lehanc612c052022-12-12 09:56:47 -080083 positiveHysteresis->get_to(positiveHysteresisValue);
Patrick Ventured1491722019-02-08 14:37:45 -080084 }
Hank Liou375f7092019-03-29 20:15:42 +080085 if (negativeHysteresis != p.end())
Patrick Ventured1491722019-02-08 14:37:45 -080086 {
Josh Lehanc612c052022-12-12 09:56:47 -080087 negativeHysteresis->get_to(negativeHysteresisValue);
88 }
89 if (derivativeCoeff != p.end())
90 {
91 derivativeCoeff->get_to(derivativeCoeffValue);
Patrick Ventured1491722019-02-08 14:37:45 -080092 }
93
ykchiu9fe3a3c2023-05-11 13:43:54 +080094 auto failSafePercent = j.find("FailSafePercent");
95 auto failSafePercentValue = 0;
96 if (failSafePercent != j.end())
97 {
98 failSafePercent->get_to(failSafePercentValue);
99 }
100 c.failSafePercent = failSafePercentValue;
101
Hank Liou375f7092019-03-29 20:15:42 +0800102 if (c.type != "stepwise")
Patrick Ventured1491722019-02-08 14:37:45 -0800103 {
Hank Liou375f7092019-03-29 20:15:42 +0800104 p.at("samplePeriod").get_to(c.pidInfo.ts);
105 p.at("proportionalCoeff").get_to(c.pidInfo.proportionalCoeff);
106 p.at("integralCoeff").get_to(c.pidInfo.integralCoeff);
107 p.at("feedFwdOffsetCoeff").get_to(c.pidInfo.feedFwdOffset);
108 p.at("feedFwdGainCoeff").get_to(c.pidInfo.feedFwdGain);
109 p.at("integralLimit_min").get_to(c.pidInfo.integralLimit.min);
110 p.at("integralLimit_max").get_to(c.pidInfo.integralLimit.max);
111 p.at("outLim_min").get_to(c.pidInfo.outLim.min);
112 p.at("outLim_max").get_to(c.pidInfo.outLim.max);
113 p.at("slewNeg").get_to(c.pidInfo.slewNeg);
114 p.at("slewPos").get_to(c.pidInfo.slewPos);
115
Josh Lehanc612c052022-12-12 09:56:47 -0800116 // Unlike other coefficients, treat derivativeCoeff as an optional
117 // parameter, as support for it is fairly new, to avoid breaking
118 // existing configurations in the field that predate it.
Hank Liou375f7092019-03-29 20:15:42 +0800119 c.pidInfo.positiveHysteresis = positiveHysteresisValue;
120 c.pidInfo.negativeHysteresis = negativeHysteresisValue;
Josh Lehanc612c052022-12-12 09:56:47 -0800121 c.pidInfo.derivativeCoeff = derivativeCoeffValue;
Delphine CC Chiu97889632023-11-06 11:32:46 +0800122 c.pidInfo.checkHysterWithSetpt = checkHysterWithSetptValue;
Patrick Ventured1491722019-02-08 14:37:45 -0800123 }
124 else
125 {
Hank Liou375f7092019-03-29 20:15:42 +0800126 p.at("samplePeriod").get_to(c.stepwiseInfo.ts);
127 p.at("isCeiling").get_to(c.stepwiseInfo.isCeiling);
128
129 for (size_t i = 0; i < ec::maxStepwisePoints; i++)
130 {
131 c.stepwiseInfo.reading[i] =
132 std::numeric_limits<double>::quiet_NaN();
133 c.stepwiseInfo.output[i] = std::numeric_limits<double>::quiet_NaN();
134 }
135
136 auto reading = p.find("reading");
137 if (reading != p.end())
138 {
139 auto r = p.at("reading");
140 for (size_t i = 0; i < ec::maxStepwisePoints; i++)
141 {
142 auto n = r.find(std::to_string(i));
143 if (n != r.end())
144 {
145 r.at(std::to_string(i)).get_to(c.stepwiseInfo.reading[i]);
146 }
147 }
148 }
149
150 auto output = p.find("output");
151 if (output != p.end())
152 {
153 auto o = p.at("output");
154 for (size_t i = 0; i < ec::maxStepwisePoints; i++)
155 {
156 auto n = o.find(std::to_string(i));
157 if (n != o.end())
158 {
159 o.at(std::to_string(i)).get_to(c.stepwiseInfo.output[i]);
160 }
161 }
162 }
163
164 c.stepwiseInfo.positiveHysteresis = positiveHysteresisValue;
165 c.stepwiseInfo.negativeHysteresis = negativeHysteresisValue;
Patrick Ventured1491722019-02-08 14:37:45 -0800166 }
167}
Josh Lehan31058fd2023-01-13 11:06:16 -0800168
James Feistf81f2882019-02-26 11:26:36 -0800169} // namespace conf
Patrick Ventured1491722019-02-08 14:37:45 -0800170
Harvey Wu239aa7d2022-11-18 08:43:34 +0800171inline void getCycleTimeSetting(const auto& zone, const int id,
172 const std::string& attributeName,
173 uint64_t& value)
174{
175 auto findAttributeName = zone.find(attributeName);
176 if (findAttributeName != zone.end())
177 {
178 uint64_t tmpAttributeValue = 0;
179 findAttributeName->get_to(tmpAttributeValue);
180 if (tmpAttributeValue >= 1)
181 {
182 value = tmpAttributeValue;
183 }
184 else
185 {
186 std::cerr << "Zone " << id << ": " << attributeName
187 << " is invalid. Use default " << value << " ms\n";
188 }
189 }
190 else
191 {
192 std::cerr << "Zone " << id << ": " << attributeName
193 << " cannot find setting. Use default " << value << " ms\n";
194 }
195}
196
Patrick Venture1df9e872020-10-08 15:35:01 -0700197std::pair<std::map<int64_t, conf::PIDConf>, std::map<int64_t, conf::ZoneConfig>>
Patrick Ventured1491722019-02-08 14:37:45 -0800198 buildPIDsFromJson(const json& data)
199{
200 // zone -> pids
James Feistf81f2882019-02-26 11:26:36 -0800201 std::map<int64_t, conf::PIDConf> pidConfig;
Patrick Ventured1491722019-02-08 14:37:45 -0800202 // zone -> configs
Patrick Venture1df9e872020-10-08 15:35:01 -0700203 std::map<int64_t, conf::ZoneConfig> zoneConfig;
Patrick Ventured1491722019-02-08 14:37:45 -0800204
205 /* TODO: if zones is empty, that's invalid. */
206 auto zones = data["zones"];
207 for (const auto& zone : zones)
208 {
209 int64_t id;
James Feistf81f2882019-02-26 11:26:36 -0800210 conf::PIDConf thisZone;
Patrick Venture1df9e872020-10-08 15:35:01 -0700211 conf::ZoneConfig thisZoneConfig;
Patrick Ventured1491722019-02-08 14:37:45 -0800212
213 /* TODO: using at() throws a specific exception we can catch */
214 id = zone["id"];
James Feist3484bed2019-02-25 13:28:18 -0800215 thisZoneConfig.minThermalOutput = zone["minThermalOutput"];
Patrick Ventured1491722019-02-08 14:37:45 -0800216 thisZoneConfig.failsafePercent = zone["failsafePercent"];
217
Harvey Wu239aa7d2022-11-18 08:43:34 +0800218 getCycleTimeSetting(zone, id, "cycleIntervalTimeMS",
219 thisZoneConfig.cycleTime.cycleIntervalTimeMS);
220 getCycleTimeSetting(zone, id, "updateThermalsTimeMS",
221 thisZoneConfig.cycleTime.updateThermalsTimeMS);
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800222
Delphine CC Chiu97889632023-11-06 11:32:46 +0800223 bool accumulateSetPoint = false;
224 auto findAccSetPoint = zone.find("accumulateSetPoint");
225 if (findAccSetPoint != zone.end())
226 {
227 findAccSetPoint->get_to(accumulateSetPoint);
228 }
229 thisZoneConfig.accumulateSetPoint = accumulateSetPoint;
230
Patrick Ventured1491722019-02-08 14:37:45 -0800231 auto pids = zone["pids"];
232 for (const auto& pid : pids)
233 {
234 auto name = pid["name"];
James Feistf81f2882019-02-26 11:26:36 -0800235 auto item = pid.get<conf::ControllerInfo>();
Patrick Ventured1491722019-02-08 14:37:45 -0800236
ykchiu7c6d35d2023-05-10 17:01:46 +0800237 if (thisZone.find(name) != thisZone.end())
238 {
239 std::cerr << "Warning: zone " << id
240 << " have the same pid name " << name << std::endl;
241 }
242
Patrick Ventured1491722019-02-08 14:37:45 -0800243 thisZone[name] = item;
244 }
245
246 pidConfig[id] = thisZone;
247 zoneConfig[id] = thisZoneConfig;
248 }
249
250 return std::make_pair(pidConfig, zoneConfig);
251}
Patrick Venturea0764872020-08-08 07:48:43 -0700252
253} // namespace pid_control