blob: 5332efe7e7d08afe679830f7f4c1a485cde507b4 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001/**
2 * Copyright 2017 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/* Configuration. */
Patrick Ventured8012182018-03-08 08:21:38 -080018#include "zone.hpp"
19
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020#include "conf.hpp"
21#include "pid/controller.hpp"
22#include "pid/ec/pid.hpp"
23#include "pid/fancontroller.hpp"
James Feist22c257a2018-08-31 14:07:12 -070024#include "pid/stepwisecontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025#include "pid/thermalcontroller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080026#include "pid/tuning.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070027
Patrick Ventured8012182018-03-08 08:21:38 -080028#include <algorithm>
29#include <chrono>
30#include <cstring>
31#include <fstream>
32#include <iostream>
Patrick Ventured8012182018-03-08 08:21:38 -080033#include <memory>
Josh Lehan55ccad62020-09-20 23:57:49 -070034#include <sstream>
Patrick Venture7a98c192020-08-12 08:35:16 -070035#include <string>
Patrick Ventured8012182018-03-08 08:21:38 -080036
Patrick Ventured8012182018-03-08 08:21:38 -080037using tstamp = std::chrono::high_resolution_clock::time_point;
38using namespace std::literals::chrono_literals;
39
Josh Lehan55ccad62020-09-20 23:57:49 -070040// Enforces minimum duration between events
41// Rreturns true if event should be allowed, false if disallowed
42bool allowThrottle(const tstamp& now, const std::chrono::seconds& pace)
43{
44 static tstamp then;
45 static bool first = true;
46
47 if (first)
48 {
49 // Special case initialization
50 then = now;
51 first = false;
52
53 // Initialization, always allow
54 return true;
55 }
56
57 auto elapsed = now - then;
58 if (elapsed < pace)
59 {
60 // Too soon since last time, disallow
61 return false;
62 }
63
64 // It has been long enough, allow
65 then = now;
66 return true;
67}
68
69namespace pid_control
70{
71
Patrick Venture597ebd62020-08-11 08:48:19 -070072double DbusPidZone::getMaxSetPointRequest(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080073{
Patrick Venturef7a2dd52019-07-16 14:31:13 -070074 return _maximumSetPoint;
Patrick Ventured8012182018-03-08 08:21:38 -080075}
76
Patrick Venture597ebd62020-08-11 08:48:19 -070077bool DbusPidZone::getManualMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080078{
79 return _manualMode;
80}
81
Patrick Venture597ebd62020-08-11 08:48:19 -070082void DbusPidZone::setManualMode(bool mode)
Patrick Ventured8012182018-03-08 08:21:38 -080083{
84 _manualMode = mode;
Josh Lehana4146eb2020-10-01 11:49:09 -070085
86 // If returning to automatic mode, need to restore PWM from PID loop
87 if (!mode)
88 {
89 _redundantWrite = true;
90 }
Patrick Ventured8012182018-03-08 08:21:38 -080091}
92
Patrick Venture597ebd62020-08-11 08:48:19 -070093bool DbusPidZone::getFailSafeMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080094{
95 // If any keys are present at least one sensor is in fail safe mode.
96 return !_failSafeSensors.empty();
97}
98
Josh Lehan3f0f7bc2023-02-13 01:45:29 -080099void DbusPidZone::markSensorMissing(const std::string& name)
100{
101 if (_missingAcceptable.find(name) != _missingAcceptable.end())
102 {
103 // Disallow sensors in MissingIsAcceptable list from causing failsafe
104 return;
105 }
106
107 _failSafeSensors.emplace(name);
108}
109
Patrick Venture597ebd62020-08-11 08:48:19 -0700110int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800111{
112 return _zoneId;
113}
114
Nirav Shahccc8bb62022-02-17 21:06:51 -0800115void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800116{
ykchiu7c6d35d2023-05-10 17:01:46 +0800117 /* exclude disabled pidloop from _maximumSetPoint calculation*/
118 if (!isPidProcessEnabled(name))
119 {
120 return;
121 }
122
Delphine CC Chiu97889632023-11-06 11:32:46 +0800123 auto profileName = name;
124 if (getAccSetPoint())
125 {
126 /*
127 * If the name of controller is Linear_Temp_CPU0.
128 * The profile name will be Temp_CPU0.
129 */
130 profileName = name.substr(name.find("_") + 1);
131 _SetPoints[profileName] += setPoint;
132 }
133 else
134 {
135 if (_SetPoints[profileName] < setPoint)
136 {
137 _SetPoints[profileName] = setPoint;
138 }
139 }
140
Nirav Shahccc8bb62022-02-17 21:06:51 -0800141 /*
142 * if there are multiple thermal controllers with the same
143 * value, pick the first one in the iterator
144 */
Delphine CC Chiu97889632023-11-06 11:32:46 +0800145 if (_maximumSetPoint < _SetPoints[profileName])
Nirav Shahccc8bb62022-02-17 21:06:51 -0800146 {
Delphine CC Chiu97889632023-11-06 11:32:46 +0800147 _maximumSetPoint = _SetPoints[profileName];
148 _maximumSetPointName = profileName;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800149 }
Patrick Ventured8012182018-03-08 08:21:38 -0800150}
151
Patrick Venture597ebd62020-08-11 08:48:19 -0700152void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800153{
154 _RPMCeilings.push_back(ceiling);
155}
156
Patrick Venture597ebd62020-08-11 08:48:19 -0700157void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800158{
159 _RPMCeilings.clear();
160}
161
Patrick Venture597ebd62020-08-11 08:48:19 -0700162void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800163{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700164 _SetPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800165 _maximumSetPoint = 0;
ykchiu7c6d35d2023-05-10 17:01:46 +0800166 _maximumSetPointName.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800167}
168
Patrick Venture597ebd62020-08-11 08:48:19 -0700169double DbusPidZone::getFailSafePercent(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800170{
171 return _failSafePercent;
172}
173
Nirav Shahccc8bb62022-02-17 21:06:51 -0800174double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800175{
James Feist3484bed2019-02-25 13:28:18 -0800176 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800177}
178
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800179uint64_t DbusPidZone::getCycleIntervalTime(void) const
180{
181 return _cycleTime.cycleIntervalTimeMS;
182}
183
184uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
185{
186 return _cycleTime.updateThermalsTimeMS;
187}
188
Patrick Venture597ebd62020-08-11 08:48:19 -0700189void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800190{
191 _fans.push_back(std::move(pid));
192}
193
Patrick Venture597ebd62020-08-11 08:48:19 -0700194void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800195{
196 _thermals.push_back(std::move(pid));
197}
198
Patrick Venture597ebd62020-08-11 08:48:19 -0700199double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800200{
Josh Lehanb3005752022-02-22 20:48:07 -0800201 return _cachedValuesByName.at(name).scaled;
202}
203
204ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
205{
Patrick Ventured8012182018-03-08 08:21:38 -0800206 return _cachedValuesByName.at(name);
207}
208
Josh Lehanb3005752022-02-22 20:48:07 -0800209void DbusPidZone::setOutputCache(std::string_view name,
210 const ValueCacheEntry& values)
211{
212 _cachedFanOutputs[std::string{name}] = values;
213}
214
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800215void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800216{
217 _fanInputs.push_back(fan);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800218
219 if (missingAcceptable)
220 {
221 _missingAcceptable.emplace(fan);
222 }
Patrick Ventured8012182018-03-08 08:21:38 -0800223}
224
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800225void DbusPidZone::addThermalInput(const std::string& therm,
226 bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800227{
Delphine CC Chiu97889632023-11-06 11:32:46 +0800228 /*
229 * One sensor may have stepwise and PID at the same time.
230 * Searching the sensor name before inserting it to avoid duplicated sensor
231 * names.
232 */
233 if (std::find(_thermalInputs.begin(), _thermalInputs.end(), therm) ==
234 _thermalInputs.end())
235 {
236 _thermalInputs.push_back(therm);
237 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800238
239 if (missingAcceptable)
240 {
241 _missingAcceptable.emplace(therm);
242 }
Patrick Ventured8012182018-03-08 08:21:38 -0800243}
244
Josh Lehan55ccad62020-09-20 23:57:49 -0700245// Updates desired RPM setpoint from optional text file
246// Returns true if rpmValue updated, false if left unchanged
247static bool fileParseRpm(const std::string& fileName, double& rpmValue)
248{
249 static constexpr std::chrono::seconds throttlePace{3};
250
251 std::string errText;
252
253 try
254 {
255 std::ifstream ifs;
256 ifs.open(fileName);
257 if (ifs)
258 {
259 int value;
260 ifs >> value;
261
262 if (value <= 0)
263 {
264 errText = "File content could not be parsed to a number";
265 }
266 else if (value <= 100)
267 {
268 errText = "File must contain RPM value, not PWM value";
269 }
270 else
271 {
272 rpmValue = static_cast<double>(value);
273 return true;
274 }
275 }
276 }
277 catch (const std::exception& e)
278 {
279 errText = "Exception: ";
280 errText += e.what();
281 }
282
283 // The file is optional, intentionally not an error if file not found
284 if (!(errText.empty()))
285 {
286 tstamp now = std::chrono::high_resolution_clock::now();
287 if (allowThrottle(now, throttlePace))
288 {
289 std::cerr << "Unable to read from '" << fileName << "': " << errText
290 << "\n";
291 }
292 }
293
294 return false;
295}
296
Patrick Venture597ebd62020-08-11 08:48:19 -0700297void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800298{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800299 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800300 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800301
James Feist608304d2019-02-25 10:01:42 -0800302 if (_RPMCeilings.size() > 0)
303 {
304 result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800305 // if Max set point is larger than the lowest ceiling, reset to lowest
306 // ceiling.
307 if (*result < _maximumSetPoint)
308 {
309 _maximumSetPoint = *result;
310 // When using lowest ceiling, controller name is ceiling.
311 _maximumSetPointName = "Ceiling";
312 }
James Feist608304d2019-02-25 10:01:42 -0800313 }
314
Patrick Ventured8012182018-03-08 08:21:38 -0800315 /*
Delphine CC Chiu97889632023-11-06 11:32:46 +0800316 * Combine the maximum SetPoint Name if the controllers have same profile
317 * name. e.g., PID_BB_INLET_TEMP_C + Stepwise_BB_INLET_TEMP_C.
318 */
319 if (getAccSetPoint())
320 {
321 auto profileName = _maximumSetPointName;
322 _maximumSetPointName = "";
323
324 for (auto& p : _thermals)
325 {
326 auto controllerID = p->getID();
327 auto found = controllerID.find(profileName);
328 if (found != std::string::npos)
329 {
330 if (_maximumSetPointName.empty())
331 {
332 _maximumSetPointName = controllerID;
333 }
334 else
335 {
336 _maximumSetPointName += " + " + controllerID;
337 }
338 }
339 }
340 }
341
342 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800343 * If the maximum RPM setpoint output is below the minimum RPM
344 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800345 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800346 if (minThermalThreshold >= _maximumSetPoint)
347 {
348 _maximumSetPoint = minThermalThreshold;
ykchiu7c6d35d2023-05-10 17:01:46 +0800349 _maximumSetPointName = "Minimum";
Nirav Shahccc8bb62022-02-17 21:06:51 -0800350 }
351 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
352 {
353 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
354 << _maximumSetPoint << " requested by "
355 << _maximumSetPointName;
356 for (const auto& sensor : _failSafeSensors)
357 {
358 if (sensor.find("Fan") == std::string::npos)
359 {
360 std::cerr << " " << sensor;
361 }
362 }
363 std::cerr << "\n";
364 _maximumSetPointNamePrev.assign(_maximumSetPointName);
365 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700366 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800367 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800368 /*
369 * We received no setpoints from thermal sensors.
370 * This is a case experienced during tuning where they only specify
371 * fan sensors and one large fan PID for all the fans.
372 */
373 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700374
Nirav Shahccc8bb62022-02-17 21:06:51 -0800375 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700376
377 // Allow per-zone setpoint files to override overall setpoint file
378 std::ostringstream zoneSuffix;
379 zoneSuffix << ".zone" << _zoneId;
380 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
381
Nirav Shahccc8bb62022-02-17 21:06:51 -0800382 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800383 }
Patrick Ventured8012182018-03-08 08:21:38 -0800384 return;
385}
386
Patrick Venture597ebd62020-08-11 08:48:19 -0700387void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800388{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700389 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800390 * epoch_ms,setpt,fan1,fan1_raw,fan1_pwm,fan1_pwm_raw,fan2,fan2_raw,fan2_pwm,fan2_pwm_raw,fanN,fanN_raw,fanN_pwm,fanN_pwm_raw,sensor1,sensor1_raw,sensor2,sensor2_raw,sensorN,sensorN_raw,failsafe
Patrick Venture5f02ad22018-04-24 10:18:40 -0700391 */
Patrick Ventured8012182018-03-08 08:21:38 -0800392
Nirav Shahccc8bb62022-02-17 21:06:51 -0800393 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800394
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700395 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800396 {
Josh Lehanb3005752022-02-22 20:48:07 -0800397 _log << "," << f << "," << f << "_raw";
398 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800399 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700400 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700401 {
Josh Lehanb3005752022-02-22 20:48:07 -0800402 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700403 }
Josh Lehanb3005752022-02-22 20:48:07 -0800404
Patrick Venture5f02ad22018-04-24 10:18:40 -0700405 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800406 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800407}
408
Patrick Venture7a98c192020-08-12 08:35:16 -0700409void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800410{
Patrick Venture7a98c192020-08-12 08:35:16 -0700411 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800412}
Patrick Ventured8012182018-03-08 08:21:38 -0800413
414/*
415 * TODO(venture) This is effectively updating the cache and should check if the
416 * values they're using to update it are new or old, or whatnot. For instance,
417 * if we haven't heard from the host in X time we need to detect this failure.
418 *
419 * I haven't decided if the Sensor should have a lastUpdated method or whether
420 * that should be for the ReadInterface or etc...
421 */
422
423/**
424 * We want the PID loop to run with values cached, so this will get all the
425 * fan tachs for the loop.
426 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700427void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800428{
429 /* TODO(venture): Should I just make _log point to /dev/null when logging
430 * is disabled? I think it's a waste to try and log things even if the
431 * data is just being dropped though.
432 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800433 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700434 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800435 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800436 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
437 now.time_since_epoch())
438 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700439 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800440 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800441 }
Patrick Ventured8012182018-03-08 08:21:38 -0800442
Tom Tungdf1f1832022-11-14 19:26:52 +0800443 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800444
Patrick Venturede79ee02019-05-08 14:50:00 -0700445 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700446 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800447 for (const auto& t : _thermalInputs)
448 {
Josh Lehanb3005752022-02-22 20:48:07 -0800449 const auto& v = _cachedValuesByName[t];
450 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800451 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700452 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700453
Patrick Ventured8012182018-03-08 08:21:38 -0800454 return;
455}
456
Patrick Venture597ebd62020-08-11 08:48:19 -0700457void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800458{
Tom Tungdf1f1832022-11-14 19:26:52 +0800459 processSensorInputs</* fanSensorLogging */ false>(
460 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800461
462 return;
463}
464
Patrick Venture597ebd62020-08-11 08:48:19 -0700465void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800466{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800467 auto nan = std::numeric_limits<double>::quiet_NaN();
468
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700469 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800470 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800471 _cachedValuesByName[f] = {nan, nan};
472 _cachedFanOutputs[f] = {nan, nan};
Will Liangded0ab52019-05-15 17:10:06 +0800473
474 // Start all fans in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800475 markSensorMissing(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800476 }
477
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700478 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800479 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800480 _cachedValuesByName[t] = {nan, nan};
Patrick Ventured8012182018-03-08 08:21:38 -0800481
482 // Start all sensors in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800483 markSensorMissing(t);
Patrick Ventured8012182018-03-08 08:21:38 -0800484 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800485 // Initialize Pid FailSafePercent
486 initPidFailSafePercent();
Patrick Ventured8012182018-03-08 08:21:38 -0800487}
488
Patrick Venture597ebd62020-08-11 08:48:19 -0700489void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800490{
491 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700492 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800493 {
Josh Lehanb3005752022-02-22 20:48:07 -0800494 std::cerr << name << ": " << value.scaled << " " << value.unscaled
495 << "\n";
496 }
497
498 std::cerr << "Fan outputs now: \n";
499 for (const auto& [name, value] : _cachedFanOutputs)
500 {
501 std::cerr << name << ": " << value.scaled << " " << value.unscaled
502 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800503 }
504}
505
Patrick Venture597ebd62020-08-11 08:48:19 -0700506void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800507{
508 for (auto& p : _fans)
509 {
James Feist22c257a2018-08-31 14:07:12 -0700510 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800511 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700512
513 if (_redundantWrite)
514 {
515 // This is only needed once
516 _redundantWrite = false;
517 }
Patrick Ventured8012182018-03-08 08:21:38 -0800518}
519
Patrick Venture597ebd62020-08-11 08:48:19 -0700520void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800521{
522 for (auto& p : _thermals)
523 {
James Feist22c257a2018-08-31 14:07:12 -0700524 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800525 }
526}
527
Patrick Venture597ebd62020-08-11 08:48:19 -0700528Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800529{
Patrick Venturefe75b192018-06-08 11:19:43 -0700530 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800531}
532
Josh Lehana4146eb2020-10-01 11:49:09 -0700533bool DbusPidZone::getRedundantWrite(void) const
534{
535 return _redundantWrite;
536}
537
Patrick Venture597ebd62020-08-11 08:48:19 -0700538bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800539{
540 std::cerr << "manual: " << value << std::endl;
541 setManualMode(value);
542 return ModeObject::manual(value);
543}
544
Patrick Venture597ebd62020-08-11 08:48:19 -0700545bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800546{
547 return getFailSafeMode();
548}
Patrick Venturea0764872020-08-08 07:48:43 -0700549
Harvey Wu37180062023-10-02 09:42:50 +0800550void DbusPidZone::addPidControlProcess(std::string name, std::string type,
551 double setpoint, sdbusplus::bus_t& bus,
ykchiu7c6d35d2023-05-10 17:01:46 +0800552 std::string objPath, bool defer)
553{
554 _pidsControlProcess[name] = std::make_unique<ProcessObject>(
555 bus, objPath.c_str(),
556 defer ? ProcessObject::action::defer_emit
557 : ProcessObject::action::emit_object_added);
558 // Default enable setting = true
559 _pidsControlProcess[name]->enabled(true);
Harvey Wu37180062023-10-02 09:42:50 +0800560 _pidsControlProcess[name]->setpoint(setpoint);
561
562 if (type == "temp")
563 {
564 _pidsControlProcess[name]->classType("Temperature");
565 }
566 else if (type == "margin")
567 {
568 _pidsControlProcess[name]->classType("Margin");
569 }
570 else if (type == "power")
571 {
572 _pidsControlProcess[name]->classType("Power");
573 }
574 else if (type == "powersum")
575 {
576 _pidsControlProcess[name]->classType("PowerSum");
577 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800578}
579
580bool DbusPidZone::isPidProcessEnabled(std::string name)
581{
582 return _pidsControlProcess[name]->enabled();
583}
584
ykchiu9fe3a3c2023-05-11 13:43:54 +0800585void DbusPidZone::initPidFailSafePercent(void)
586{
587 // Currently, find the max failsafe percent pwm settings from zone and
588 // controller, and assign it to zone failsafe percent.
589
590 _failSafePercent = _zoneFailSafePercent;
591 std::cerr << "zone: Zone" << _zoneId
592 << " zoneFailSafePercent: " << _zoneFailSafePercent << "\n";
593
594 for (const auto& [name, value] : _pidsFailSafePercent)
595 {
596 _failSafePercent = std::max(_failSafePercent, value);
597 std::cerr << "pid: " << name << " failSafePercent: " << value << "\n";
598 }
599
600 // when the final failsafe percent is zero , it indicate no failsafe
601 // percent is configured  , set it to 100% as the default setting.
602 if (_failSafePercent == 0)
603 {
604 _failSafePercent = 100;
605 }
606 std::cerr << "Final zone" << _zoneId
607 << " failSafePercent: " << _failSafePercent << "\n";
608}
609
610void DbusPidZone::addPidFailSafePercent(std::string name, double percent)
611{
612 _pidsFailSafePercent[name] = percent;
613}
614
Harvey Wucc0232a2023-02-09 14:58:55 +0800615std::string DbusPidZone::leader() const
616{
617 return _maximumSetPointName;
618}
619
Harvey Wu37180062023-10-02 09:42:50 +0800620void DbusPidZone::updateThermalPowerDebugInterface(std::string pidName,
621 std::string leader,
622 double input, double output)
623{
624 if (leader.empty())
625 {
626 _pidsControlProcess[pidName]->output(output);
627 }
628 else
629 {
630 _pidsControlProcess[pidName]->leader(leader);
631 _pidsControlProcess[pidName]->input(input);
632 }
633}
634
Delphine CC Chiu97889632023-11-06 11:32:46 +0800635bool DbusPidZone::getAccSetPoint(void) const
636{
637 return _accumulateSetPoint;
638}
639
Patrick Venturea0764872020-08-08 07:48:43 -0700640} // namespace pid_control