blob: d9fe3c070893485705e6dac08fc18a7ebee674f3 [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"
James Zheng6df8bb52024-11-27 23:38:47 +000021#include "failsafeloggers/failsafe_logger_utility.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070022#include "pid/controller.hpp"
23#include "pid/ec/pid.hpp"
24#include "pid/fancontroller.hpp"
James Feist22c257a2018-08-31 14:07:12 -070025#include "pid/stepwisecontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026#include "pid/thermalcontroller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080027#include "pid/tuning.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028
Patrick Ventured8012182018-03-08 08:21:38 -080029#include <algorithm>
30#include <chrono>
31#include <cstring>
32#include <fstream>
33#include <iostream>
Patrick Ventured8012182018-03-08 08:21:38 -080034#include <memory>
Josh Lehan55ccad62020-09-20 23:57:49 -070035#include <sstream>
Patrick Venture7a98c192020-08-12 08:35:16 -070036#include <string>
Patrick Ventured8012182018-03-08 08:21:38 -080037
Patrick Ventured8012182018-03-08 08:21:38 -080038using tstamp = std::chrono::high_resolution_clock::time_point;
39using namespace std::literals::chrono_literals;
40
Josh Lehan55ccad62020-09-20 23:57:49 -070041// Enforces minimum duration between events
42// Rreturns true if event should be allowed, false if disallowed
43bool allowThrottle(const tstamp& now, const std::chrono::seconds& pace)
44{
45 static tstamp then;
46 static bool first = true;
47
48 if (first)
49 {
50 // Special case initialization
51 then = now;
52 first = false;
53
54 // Initialization, always allow
55 return true;
56 }
57
58 auto elapsed = now - then;
59 if (elapsed < pace)
60 {
61 // Too soon since last time, disallow
62 return false;
63 }
64
65 // It has been long enough, allow
66 then = now;
67 return true;
68}
69
70namespace pid_control
71{
72
Patrick Venture597ebd62020-08-11 08:48:19 -070073double DbusPidZone::getMaxSetPointRequest(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080074{
Patrick Venturef7a2dd52019-07-16 14:31:13 -070075 return _maximumSetPoint;
Patrick Ventured8012182018-03-08 08:21:38 -080076}
77
Patrick Venture597ebd62020-08-11 08:48:19 -070078bool DbusPidZone::getManualMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080079{
80 return _manualMode;
81}
82
Patrick Venture597ebd62020-08-11 08:48:19 -070083void DbusPidZone::setManualMode(bool mode)
Patrick Ventured8012182018-03-08 08:21:38 -080084{
85 _manualMode = mode;
Josh Lehana4146eb2020-10-01 11:49:09 -070086
87 // If returning to automatic mode, need to restore PWM from PID loop
88 if (!mode)
89 {
90 _redundantWrite = true;
91 }
Patrick Ventured8012182018-03-08 08:21:38 -080092}
93
Patrick Venture597ebd62020-08-11 08:48:19 -070094bool DbusPidZone::getFailSafeMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080095{
96 // If any keys are present at least one sensor is in fail safe mode.
97 return !_failSafeSensors.empty();
98}
99
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800100void DbusPidZone::markSensorMissing(const std::string& name)
101{
102 if (_missingAcceptable.find(name) != _missingAcceptable.end())
103 {
104 // Disallow sensors in MissingIsAcceptable list from causing failsafe
James Zheng6df8bb52024-11-27 23:38:47 +0000105 outputFailsafeLogWithZone(_zoneId, this->getFailSafeMode(), name,
106 "The sensor is missing but is acceptable.");
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800107 return;
108 }
109
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800110 if (_sensorFailSafePercent[name] == 0)
111 {
112 _failSafeSensors[name] = _zoneFailSafePercent;
113 }
114 else
115 {
116 _failSafeSensors[name] = _sensorFailSafePercent[name];
117 }
118
119 if (debugEnabled)
120 {
121 std::cerr << "Sensor " << name << " marked missing\n";
122 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800123}
124
Patrick Venture597ebd62020-08-11 08:48:19 -0700125int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800126{
127 return _zoneId;
128}
129
Nirav Shahccc8bb62022-02-17 21:06:51 -0800130void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800131{
ykchiu7c6d35d2023-05-10 17:01:46 +0800132 /* exclude disabled pidloop from _maximumSetPoint calculation*/
133 if (!isPidProcessEnabled(name))
134 {
135 return;
136 }
137
Delphine CC Chiu97889632023-11-06 11:32:46 +0800138 auto profileName = name;
139 if (getAccSetPoint())
140 {
141 /*
142 * If the name of controller is Linear_Temp_CPU0.
143 * The profile name will be Temp_CPU0.
144 */
145 profileName = name.substr(name.find("_") + 1);
146 _SetPoints[profileName] += setPoint;
147 }
148 else
149 {
150 if (_SetPoints[profileName] < setPoint)
151 {
152 _SetPoints[profileName] = setPoint;
153 }
154 }
155
Nirav Shahccc8bb62022-02-17 21:06:51 -0800156 /*
157 * if there are multiple thermal controllers with the same
158 * value, pick the first one in the iterator
159 */
Delphine CC Chiu97889632023-11-06 11:32:46 +0800160 if (_maximumSetPoint < _SetPoints[profileName])
Nirav Shahccc8bb62022-02-17 21:06:51 -0800161 {
Delphine CC Chiu97889632023-11-06 11:32:46 +0800162 _maximumSetPoint = _SetPoints[profileName];
163 _maximumSetPointName = profileName;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800164 }
Patrick Ventured8012182018-03-08 08:21:38 -0800165}
166
Patrick Venture597ebd62020-08-11 08:48:19 -0700167void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800168{
169 _RPMCeilings.push_back(ceiling);
170}
171
Patrick Venture597ebd62020-08-11 08:48:19 -0700172void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800173{
174 _RPMCeilings.clear();
175}
176
Patrick Venture597ebd62020-08-11 08:48:19 -0700177void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800178{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700179 _SetPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800180 _maximumSetPoint = 0;
ykchiu7c6d35d2023-05-10 17:01:46 +0800181 _maximumSetPointName.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800182}
183
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800184double DbusPidZone::getFailSafePercent(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800185{
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800186 std::map<std::string, double>::iterator maxData = std::max_element(
187 _failSafeSensors.begin(), _failSafeSensors.end(),
188 [](const std::pair<std::string, double> firstData,
189 const std::pair<std::string, double> secondData) {
190 return firstData.second < secondData.second;
191 });
192
193 // In dbus/dbusconfiguration.cpp, the default sensor failsafepercent is 0 if
194 // there is no setting in json.
195 // Therfore, if the max failsafe duty in _failSafeSensors is 0, set final
196 // failsafe duty to _zoneFailSafePercent.
197 if ((*maxData).second == 0)
198 {
199 return _zoneFailSafePercent;
200 }
201 else
202 {
203 return (*maxData).second;
204 }
Patrick Ventured8012182018-03-08 08:21:38 -0800205}
206
Nirav Shahccc8bb62022-02-17 21:06:51 -0800207double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800208{
James Feist3484bed2019-02-25 13:28:18 -0800209 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800210}
211
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800212uint64_t DbusPidZone::getCycleIntervalTime(void) const
213{
214 return _cycleTime.cycleIntervalTimeMS;
215}
216
217uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
218{
219 return _cycleTime.updateThermalsTimeMS;
220}
221
Patrick Venture597ebd62020-08-11 08:48:19 -0700222void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800223{
224 _fans.push_back(std::move(pid));
225}
226
Patrick Venture597ebd62020-08-11 08:48:19 -0700227void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800228{
229 _thermals.push_back(std::move(pid));
230}
231
Patrick Venture597ebd62020-08-11 08:48:19 -0700232double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800233{
Josh Lehanb3005752022-02-22 20:48:07 -0800234 return _cachedValuesByName.at(name).scaled;
235}
236
237ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
238{
Patrick Ventured8012182018-03-08 08:21:38 -0800239 return _cachedValuesByName.at(name);
240}
241
Josh Lehanb3005752022-02-22 20:48:07 -0800242void DbusPidZone::setOutputCache(std::string_view name,
243 const ValueCacheEntry& values)
244{
245 _cachedFanOutputs[std::string{name}] = values;
246}
247
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800248void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800249{
250 _fanInputs.push_back(fan);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800251
252 if (missingAcceptable)
253 {
254 _missingAcceptable.emplace(fan);
255 }
Patrick Ventured8012182018-03-08 08:21:38 -0800256}
257
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800258void DbusPidZone::addThermalInput(const std::string& therm,
259 bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800260{
Delphine CC Chiu97889632023-11-06 11:32:46 +0800261 /*
262 * One sensor may have stepwise and PID at the same time.
263 * Searching the sensor name before inserting it to avoid duplicated sensor
264 * names.
265 */
266 if (std::find(_thermalInputs.begin(), _thermalInputs.end(), therm) ==
267 _thermalInputs.end())
268 {
269 _thermalInputs.push_back(therm);
270 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800271
272 if (missingAcceptable)
273 {
274 _missingAcceptable.emplace(therm);
275 }
Patrick Ventured8012182018-03-08 08:21:38 -0800276}
277
Josh Lehan55ccad62020-09-20 23:57:49 -0700278// Updates desired RPM setpoint from optional text file
279// Returns true if rpmValue updated, false if left unchanged
280static bool fileParseRpm(const std::string& fileName, double& rpmValue)
281{
282 static constexpr std::chrono::seconds throttlePace{3};
283
284 std::string errText;
285
286 try
287 {
288 std::ifstream ifs;
289 ifs.open(fileName);
290 if (ifs)
291 {
292 int value;
293 ifs >> value;
294
295 if (value <= 0)
296 {
297 errText = "File content could not be parsed to a number";
298 }
299 else if (value <= 100)
300 {
301 errText = "File must contain RPM value, not PWM value";
302 }
303 else
304 {
305 rpmValue = static_cast<double>(value);
306 return true;
307 }
308 }
309 }
310 catch (const std::exception& e)
311 {
312 errText = "Exception: ";
313 errText += e.what();
314 }
315
316 // The file is optional, intentionally not an error if file not found
317 if (!(errText.empty()))
318 {
319 tstamp now = std::chrono::high_resolution_clock::now();
320 if (allowThrottle(now, throttlePace))
321 {
322 std::cerr << "Unable to read from '" << fileName << "': " << errText
323 << "\n";
324 }
325 }
326
327 return false;
328}
329
Patrick Venture597ebd62020-08-11 08:48:19 -0700330void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800331{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800332 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800333 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800334
James Feist608304d2019-02-25 10:01:42 -0800335 if (_RPMCeilings.size() > 0)
336 {
337 result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800338 // if Max set point is larger than the lowest ceiling, reset to lowest
339 // ceiling.
340 if (*result < _maximumSetPoint)
341 {
342 _maximumSetPoint = *result;
343 // When using lowest ceiling, controller name is ceiling.
344 _maximumSetPointName = "Ceiling";
345 }
James Feist608304d2019-02-25 10:01:42 -0800346 }
347
Patrick Ventured8012182018-03-08 08:21:38 -0800348 /*
Delphine CC Chiu97889632023-11-06 11:32:46 +0800349 * Combine the maximum SetPoint Name if the controllers have same profile
350 * name. e.g., PID_BB_INLET_TEMP_C + Stepwise_BB_INLET_TEMP_C.
351 */
352 if (getAccSetPoint())
353 {
354 auto profileName = _maximumSetPointName;
355 _maximumSetPointName = "";
356
357 for (auto& p : _thermals)
358 {
359 auto controllerID = p->getID();
360 auto found = controllerID.find(profileName);
361 if (found != std::string::npos)
362 {
363 if (_maximumSetPointName.empty())
364 {
365 _maximumSetPointName = controllerID;
366 }
367 else
368 {
369 _maximumSetPointName += " + " + controllerID;
370 }
371 }
372 }
373 }
374
375 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800376 * If the maximum RPM setpoint output is below the minimum RPM
377 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800378 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800379 if (minThermalThreshold >= _maximumSetPoint)
380 {
381 _maximumSetPoint = minThermalThreshold;
ykchiu7c6d35d2023-05-10 17:01:46 +0800382 _maximumSetPointName = "Minimum";
Nirav Shahccc8bb62022-02-17 21:06:51 -0800383 }
384 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
385 {
386 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
387 << _maximumSetPoint << " requested by "
388 << _maximumSetPointName;
389 for (const auto& sensor : _failSafeSensors)
390 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800391 if (sensor.first.find("Fan") == std::string::npos)
Nirav Shahccc8bb62022-02-17 21:06:51 -0800392 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800393 std::cerr << " " << sensor.first;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800394 }
395 }
396 std::cerr << "\n";
397 _maximumSetPointNamePrev.assign(_maximumSetPointName);
398 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700399 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800400 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800401 /*
402 * We received no setpoints from thermal sensors.
403 * This is a case experienced during tuning where they only specify
404 * fan sensors and one large fan PID for all the fans.
405 */
406 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700407
Nirav Shahccc8bb62022-02-17 21:06:51 -0800408 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700409
410 // Allow per-zone setpoint files to override overall setpoint file
411 std::ostringstream zoneSuffix;
412 zoneSuffix << ".zone" << _zoneId;
413 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
414
Nirav Shahccc8bb62022-02-17 21:06:51 -0800415 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800416 }
Patrick Ventured8012182018-03-08 08:21:38 -0800417 return;
418}
419
Patrick Venture597ebd62020-08-11 08:48:19 -0700420void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800421{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700422 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800423 * 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 -0700424 */
Patrick Ventured8012182018-03-08 08:21:38 -0800425
Nirav Shahccc8bb62022-02-17 21:06:51 -0800426 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800427
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700428 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800429 {
Josh Lehanb3005752022-02-22 20:48:07 -0800430 _log << "," << f << "," << f << "_raw";
431 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800432 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700433 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700434 {
Josh Lehanb3005752022-02-22 20:48:07 -0800435 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700436 }
Josh Lehanb3005752022-02-22 20:48:07 -0800437
Patrick Venture5f02ad22018-04-24 10:18:40 -0700438 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800439 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800440}
441
Patrick Venture7a98c192020-08-12 08:35:16 -0700442void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800443{
Patrick Venture7a98c192020-08-12 08:35:16 -0700444 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800445}
Patrick Ventured8012182018-03-08 08:21:38 -0800446
447/*
448 * TODO(venture) This is effectively updating the cache and should check if the
449 * values they're using to update it are new or old, or whatnot. For instance,
450 * if we haven't heard from the host in X time we need to detect this failure.
451 *
452 * I haven't decided if the Sensor should have a lastUpdated method or whether
453 * that should be for the ReadInterface or etc...
454 */
455
456/**
457 * We want the PID loop to run with values cached, so this will get all the
458 * fan tachs for the loop.
459 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700460void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800461{
462 /* TODO(venture): Should I just make _log point to /dev/null when logging
463 * is disabled? I think it's a waste to try and log things even if the
464 * data is just being dropped though.
465 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800466 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700467 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800468 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800469 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
470 now.time_since_epoch())
471 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700472 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800473 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800474 }
Patrick Ventured8012182018-03-08 08:21:38 -0800475
Tom Tungdf1f1832022-11-14 19:26:52 +0800476 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800477
Patrick Venturede79ee02019-05-08 14:50:00 -0700478 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700479 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800480 for (const auto& t : _thermalInputs)
481 {
Josh Lehanb3005752022-02-22 20:48:07 -0800482 const auto& v = _cachedValuesByName[t];
483 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800484 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700485 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700486
Patrick Ventured8012182018-03-08 08:21:38 -0800487 return;
488}
489
Patrick Venture597ebd62020-08-11 08:48:19 -0700490void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800491{
Tom Tungdf1f1832022-11-14 19:26:52 +0800492 processSensorInputs</* fanSensorLogging */ false>(
493 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800494
495 return;
496}
497
Patrick Venture597ebd62020-08-11 08:48:19 -0700498void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800499{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800500 auto nan = std::numeric_limits<double>::quiet_NaN();
501
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700502 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800503 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800504 _cachedValuesByName[f] = {nan, nan};
505 _cachedFanOutputs[f] = {nan, nan};
Will Liangded0ab52019-05-15 17:10:06 +0800506
507 // Start all fans in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800508 markSensorMissing(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800509 }
510
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700511 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800512 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800513 _cachedValuesByName[t] = {nan, nan};
Patrick Ventured8012182018-03-08 08:21:38 -0800514
515 // Start all sensors in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800516 markSensorMissing(t);
Patrick Ventured8012182018-03-08 08:21:38 -0800517 }
518}
519
Patrick Venture597ebd62020-08-11 08:48:19 -0700520void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800521{
522 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700523 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800524 {
Josh Lehanb3005752022-02-22 20:48:07 -0800525 std::cerr << name << ": " << value.scaled << " " << value.unscaled
526 << "\n";
527 }
528
529 std::cerr << "Fan outputs now: \n";
530 for (const auto& [name, value] : _cachedFanOutputs)
531 {
532 std::cerr << name << ": " << value.scaled << " " << value.unscaled
533 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800534 }
535}
536
Patrick Venture597ebd62020-08-11 08:48:19 -0700537void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800538{
539 for (auto& p : _fans)
540 {
James Feist22c257a2018-08-31 14:07:12 -0700541 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800542 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700543
544 if (_redundantWrite)
545 {
546 // This is only needed once
547 _redundantWrite = false;
548 }
Patrick Ventured8012182018-03-08 08:21:38 -0800549}
550
Patrick Venture597ebd62020-08-11 08:48:19 -0700551void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800552{
553 for (auto& p : _thermals)
554 {
James Feist22c257a2018-08-31 14:07:12 -0700555 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800556 }
557}
558
Patrick Venture597ebd62020-08-11 08:48:19 -0700559Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800560{
Patrick Venturefe75b192018-06-08 11:19:43 -0700561 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800562}
563
James Zheng6df8bb52024-11-27 23:38:47 +0000564std::vector<std::string> DbusPidZone::getSensorNames(void)
565{
566 return _thermalInputs;
567}
568
Josh Lehana4146eb2020-10-01 11:49:09 -0700569bool DbusPidZone::getRedundantWrite(void) const
570{
571 return _redundantWrite;
572}
573
Patrick Venture597ebd62020-08-11 08:48:19 -0700574bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800575{
576 std::cerr << "manual: " << value << std::endl;
577 setManualMode(value);
578 return ModeObject::manual(value);
579}
580
Patrick Venture597ebd62020-08-11 08:48:19 -0700581bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800582{
583 return getFailSafeMode();
584}
Patrick Venturea0764872020-08-08 07:48:43 -0700585
Harvey Wu37180062023-10-02 09:42:50 +0800586void DbusPidZone::addPidControlProcess(std::string name, std::string type,
587 double setpoint, sdbusplus::bus_t& bus,
ykchiu7c6d35d2023-05-10 17:01:46 +0800588 std::string objPath, bool defer)
589{
590 _pidsControlProcess[name] = std::make_unique<ProcessObject>(
591 bus, objPath.c_str(),
592 defer ? ProcessObject::action::defer_emit
593 : ProcessObject::action::emit_object_added);
594 // Default enable setting = true
595 _pidsControlProcess[name]->enabled(true);
Harvey Wu37180062023-10-02 09:42:50 +0800596 _pidsControlProcess[name]->setpoint(setpoint);
597
598 if (type == "temp")
599 {
600 _pidsControlProcess[name]->classType("Temperature");
601 }
602 else if (type == "margin")
603 {
604 _pidsControlProcess[name]->classType("Margin");
605 }
606 else if (type == "power")
607 {
608 _pidsControlProcess[name]->classType("Power");
609 }
610 else if (type == "powersum")
611 {
612 _pidsControlProcess[name]->classType("PowerSum");
613 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800614}
615
616bool DbusPidZone::isPidProcessEnabled(std::string name)
617{
618 return _pidsControlProcess[name]->enabled();
619}
620
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800621void DbusPidZone::addPidFailSafePercent(std::vector<std::string> inputs,
622 double percent)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800623{
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800624 for (const auto& sensorName : inputs)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800625 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800626 if (_sensorFailSafePercent.find(sensorName) !=
627 _sensorFailSafePercent.end())
628 {
629 _sensorFailSafePercent[sensorName] =
630 std::max(_sensorFailSafePercent[sensorName], percent);
631 if (debugEnabled)
632 {
633 std::cerr << "Sensor " << sensorName
634 << " failsafe percent updated to "
635 << _sensorFailSafePercent[sensorName] << "\n";
636 }
637 }
638 else
639 {
640 _sensorFailSafePercent[sensorName] = percent;
641 if (debugEnabled)
642 {
643 std::cerr << "Sensor " << sensorName
644 << " failsafe percent set to " << percent << "\n";
645 }
646 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800647 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800648}
649
Harvey Wucc0232a2023-02-09 14:58:55 +0800650std::string DbusPidZone::leader() const
651{
652 return _maximumSetPointName;
653}
654
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400655void DbusPidZone::updateThermalPowerDebugInterface(
656 std::string pidName, std::string leader, double input, double output)
Harvey Wu37180062023-10-02 09:42:50 +0800657{
658 if (leader.empty())
659 {
660 _pidsControlProcess[pidName]->output(output);
661 }
662 else
663 {
664 _pidsControlProcess[pidName]->leader(leader);
665 _pidsControlProcess[pidName]->input(input);
666 }
667}
668
Delphine CC Chiu97889632023-11-06 11:32:46 +0800669bool DbusPidZone::getAccSetPoint(void) const
670{
671 return _accumulateSetPoint;
672}
673
Patrick Venturea0764872020-08-08 07:48:43 -0700674} // namespace pid_control