blob: 901905dd2dda5a84ae96e9961ae9134a13958670 [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
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800107 if (_sensorFailSafePercent[name] == 0)
108 {
109 _failSafeSensors[name] = _zoneFailSafePercent;
110 }
111 else
112 {
113 _failSafeSensors[name] = _sensorFailSafePercent[name];
114 }
115
116 if (debugEnabled)
117 {
118 std::cerr << "Sensor " << name << " marked missing\n";
119 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800120}
121
Patrick Venture597ebd62020-08-11 08:48:19 -0700122int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800123{
124 return _zoneId;
125}
126
Nirav Shahccc8bb62022-02-17 21:06:51 -0800127void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800128{
ykchiu7c6d35d2023-05-10 17:01:46 +0800129 /* exclude disabled pidloop from _maximumSetPoint calculation*/
130 if (!isPidProcessEnabled(name))
131 {
132 return;
133 }
134
Delphine CC Chiu97889632023-11-06 11:32:46 +0800135 auto profileName = name;
136 if (getAccSetPoint())
137 {
138 /*
139 * If the name of controller is Linear_Temp_CPU0.
140 * The profile name will be Temp_CPU0.
141 */
142 profileName = name.substr(name.find("_") + 1);
143 _SetPoints[profileName] += setPoint;
144 }
145 else
146 {
147 if (_SetPoints[profileName] < setPoint)
148 {
149 _SetPoints[profileName] = setPoint;
150 }
151 }
152
Nirav Shahccc8bb62022-02-17 21:06:51 -0800153 /*
154 * if there are multiple thermal controllers with the same
155 * value, pick the first one in the iterator
156 */
Delphine CC Chiu97889632023-11-06 11:32:46 +0800157 if (_maximumSetPoint < _SetPoints[profileName])
Nirav Shahccc8bb62022-02-17 21:06:51 -0800158 {
Delphine CC Chiu97889632023-11-06 11:32:46 +0800159 _maximumSetPoint = _SetPoints[profileName];
160 _maximumSetPointName = profileName;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800161 }
Patrick Ventured8012182018-03-08 08:21:38 -0800162}
163
Patrick Venture597ebd62020-08-11 08:48:19 -0700164void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800165{
166 _RPMCeilings.push_back(ceiling);
167}
168
Patrick Venture597ebd62020-08-11 08:48:19 -0700169void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800170{
171 _RPMCeilings.clear();
172}
173
Patrick Venture597ebd62020-08-11 08:48:19 -0700174void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800175{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700176 _SetPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800177 _maximumSetPoint = 0;
ykchiu7c6d35d2023-05-10 17:01:46 +0800178 _maximumSetPointName.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800179}
180
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800181double DbusPidZone::getFailSafePercent(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800182{
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800183 std::map<std::string, double>::iterator maxData = std::max_element(
184 _failSafeSensors.begin(), _failSafeSensors.end(),
185 [](const std::pair<std::string, double> firstData,
186 const std::pair<std::string, double> secondData) {
187 return firstData.second < secondData.second;
188 });
189
190 // In dbus/dbusconfiguration.cpp, the default sensor failsafepercent is 0 if
191 // there is no setting in json.
192 // Therfore, if the max failsafe duty in _failSafeSensors is 0, set final
193 // failsafe duty to _zoneFailSafePercent.
194 if ((*maxData).second == 0)
195 {
196 return _zoneFailSafePercent;
197 }
198 else
199 {
200 return (*maxData).second;
201 }
Patrick Ventured8012182018-03-08 08:21:38 -0800202}
203
Nirav Shahccc8bb62022-02-17 21:06:51 -0800204double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800205{
James Feist3484bed2019-02-25 13:28:18 -0800206 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800207}
208
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800209uint64_t DbusPidZone::getCycleIntervalTime(void) const
210{
211 return _cycleTime.cycleIntervalTimeMS;
212}
213
214uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
215{
216 return _cycleTime.updateThermalsTimeMS;
217}
218
Patrick Venture597ebd62020-08-11 08:48:19 -0700219void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800220{
221 _fans.push_back(std::move(pid));
222}
223
Patrick Venture597ebd62020-08-11 08:48:19 -0700224void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800225{
226 _thermals.push_back(std::move(pid));
227}
228
Patrick Venture597ebd62020-08-11 08:48:19 -0700229double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800230{
Josh Lehanb3005752022-02-22 20:48:07 -0800231 return _cachedValuesByName.at(name).scaled;
232}
233
234ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
235{
Patrick Ventured8012182018-03-08 08:21:38 -0800236 return _cachedValuesByName.at(name);
237}
238
Josh Lehanb3005752022-02-22 20:48:07 -0800239void DbusPidZone::setOutputCache(std::string_view name,
240 const ValueCacheEntry& values)
241{
242 _cachedFanOutputs[std::string{name}] = values;
243}
244
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800245void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800246{
247 _fanInputs.push_back(fan);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800248
249 if (missingAcceptable)
250 {
251 _missingAcceptable.emplace(fan);
252 }
Patrick Ventured8012182018-03-08 08:21:38 -0800253}
254
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800255void DbusPidZone::addThermalInput(const std::string& therm,
256 bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800257{
Delphine CC Chiu97889632023-11-06 11:32:46 +0800258 /*
259 * One sensor may have stepwise and PID at the same time.
260 * Searching the sensor name before inserting it to avoid duplicated sensor
261 * names.
262 */
263 if (std::find(_thermalInputs.begin(), _thermalInputs.end(), therm) ==
264 _thermalInputs.end())
265 {
266 _thermalInputs.push_back(therm);
267 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800268
269 if (missingAcceptable)
270 {
271 _missingAcceptable.emplace(therm);
272 }
Patrick Ventured8012182018-03-08 08:21:38 -0800273}
274
Josh Lehan55ccad62020-09-20 23:57:49 -0700275// Updates desired RPM setpoint from optional text file
276// Returns true if rpmValue updated, false if left unchanged
277static bool fileParseRpm(const std::string& fileName, double& rpmValue)
278{
279 static constexpr std::chrono::seconds throttlePace{3};
280
281 std::string errText;
282
283 try
284 {
285 std::ifstream ifs;
286 ifs.open(fileName);
287 if (ifs)
288 {
289 int value;
290 ifs >> value;
291
292 if (value <= 0)
293 {
294 errText = "File content could not be parsed to a number";
295 }
296 else if (value <= 100)
297 {
298 errText = "File must contain RPM value, not PWM value";
299 }
300 else
301 {
302 rpmValue = static_cast<double>(value);
303 return true;
304 }
305 }
306 }
307 catch (const std::exception& e)
308 {
309 errText = "Exception: ";
310 errText += e.what();
311 }
312
313 // The file is optional, intentionally not an error if file not found
314 if (!(errText.empty()))
315 {
316 tstamp now = std::chrono::high_resolution_clock::now();
317 if (allowThrottle(now, throttlePace))
318 {
319 std::cerr << "Unable to read from '" << fileName << "': " << errText
320 << "\n";
321 }
322 }
323
324 return false;
325}
326
Patrick Venture597ebd62020-08-11 08:48:19 -0700327void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800328{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800329 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800330 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800331
James Feist608304d2019-02-25 10:01:42 -0800332 if (_RPMCeilings.size() > 0)
333 {
334 result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800335 // if Max set point is larger than the lowest ceiling, reset to lowest
336 // ceiling.
337 if (*result < _maximumSetPoint)
338 {
339 _maximumSetPoint = *result;
340 // When using lowest ceiling, controller name is ceiling.
341 _maximumSetPointName = "Ceiling";
342 }
James Feist608304d2019-02-25 10:01:42 -0800343 }
344
Patrick Ventured8012182018-03-08 08:21:38 -0800345 /*
Delphine CC Chiu97889632023-11-06 11:32:46 +0800346 * Combine the maximum SetPoint Name if the controllers have same profile
347 * name. e.g., PID_BB_INLET_TEMP_C + Stepwise_BB_INLET_TEMP_C.
348 */
349 if (getAccSetPoint())
350 {
351 auto profileName = _maximumSetPointName;
352 _maximumSetPointName = "";
353
354 for (auto& p : _thermals)
355 {
356 auto controllerID = p->getID();
357 auto found = controllerID.find(profileName);
358 if (found != std::string::npos)
359 {
360 if (_maximumSetPointName.empty())
361 {
362 _maximumSetPointName = controllerID;
363 }
364 else
365 {
366 _maximumSetPointName += " + " + controllerID;
367 }
368 }
369 }
370 }
371
372 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800373 * If the maximum RPM setpoint output is below the minimum RPM
374 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800375 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800376 if (minThermalThreshold >= _maximumSetPoint)
377 {
378 _maximumSetPoint = minThermalThreshold;
ykchiu7c6d35d2023-05-10 17:01:46 +0800379 _maximumSetPointName = "Minimum";
Nirav Shahccc8bb62022-02-17 21:06:51 -0800380 }
381 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
382 {
383 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
384 << _maximumSetPoint << " requested by "
385 << _maximumSetPointName;
386 for (const auto& sensor : _failSafeSensors)
387 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800388 if (sensor.first.find("Fan") == std::string::npos)
Nirav Shahccc8bb62022-02-17 21:06:51 -0800389 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800390 std::cerr << " " << sensor.first;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800391 }
392 }
393 std::cerr << "\n";
394 _maximumSetPointNamePrev.assign(_maximumSetPointName);
395 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700396 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800397 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800398 /*
399 * We received no setpoints from thermal sensors.
400 * This is a case experienced during tuning where they only specify
401 * fan sensors and one large fan PID for all the fans.
402 */
403 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700404
Nirav Shahccc8bb62022-02-17 21:06:51 -0800405 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700406
407 // Allow per-zone setpoint files to override overall setpoint file
408 std::ostringstream zoneSuffix;
409 zoneSuffix << ".zone" << _zoneId;
410 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
411
Nirav Shahccc8bb62022-02-17 21:06:51 -0800412 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800413 }
Patrick Ventured8012182018-03-08 08:21:38 -0800414 return;
415}
416
Patrick Venture597ebd62020-08-11 08:48:19 -0700417void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800418{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700419 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800420 * 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 -0700421 */
Patrick Ventured8012182018-03-08 08:21:38 -0800422
Nirav Shahccc8bb62022-02-17 21:06:51 -0800423 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800424
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700425 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800426 {
Josh Lehanb3005752022-02-22 20:48:07 -0800427 _log << "," << f << "," << f << "_raw";
428 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800429 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700430 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700431 {
Josh Lehanb3005752022-02-22 20:48:07 -0800432 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700433 }
Josh Lehanb3005752022-02-22 20:48:07 -0800434
Patrick Venture5f02ad22018-04-24 10:18:40 -0700435 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800436 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800437}
438
Patrick Venture7a98c192020-08-12 08:35:16 -0700439void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800440{
Patrick Venture7a98c192020-08-12 08:35:16 -0700441 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800442}
Patrick Ventured8012182018-03-08 08:21:38 -0800443
444/*
445 * TODO(venture) This is effectively updating the cache and should check if the
446 * values they're using to update it are new or old, or whatnot. For instance,
447 * if we haven't heard from the host in X time we need to detect this failure.
448 *
449 * I haven't decided if the Sensor should have a lastUpdated method or whether
450 * that should be for the ReadInterface or etc...
451 */
452
453/**
454 * We want the PID loop to run with values cached, so this will get all the
455 * fan tachs for the loop.
456 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700457void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800458{
459 /* TODO(venture): Should I just make _log point to /dev/null when logging
460 * is disabled? I think it's a waste to try and log things even if the
461 * data is just being dropped though.
462 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800463 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700464 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800465 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800466 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
467 now.time_since_epoch())
468 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700469 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800470 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800471 }
Patrick Ventured8012182018-03-08 08:21:38 -0800472
Tom Tungdf1f1832022-11-14 19:26:52 +0800473 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800474
Patrick Venturede79ee02019-05-08 14:50:00 -0700475 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700476 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800477 for (const auto& t : _thermalInputs)
478 {
Josh Lehanb3005752022-02-22 20:48:07 -0800479 const auto& v = _cachedValuesByName[t];
480 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800481 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700482 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700483
Patrick Ventured8012182018-03-08 08:21:38 -0800484 return;
485}
486
Patrick Venture597ebd62020-08-11 08:48:19 -0700487void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800488{
Tom Tungdf1f1832022-11-14 19:26:52 +0800489 processSensorInputs</* fanSensorLogging */ false>(
490 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800491
492 return;
493}
494
Patrick Venture597ebd62020-08-11 08:48:19 -0700495void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800496{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800497 auto nan = std::numeric_limits<double>::quiet_NaN();
498
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700499 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800500 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800501 _cachedValuesByName[f] = {nan, nan};
502 _cachedFanOutputs[f] = {nan, nan};
Will Liangded0ab52019-05-15 17:10:06 +0800503
504 // Start all fans in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800505 markSensorMissing(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800506 }
507
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700508 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800509 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800510 _cachedValuesByName[t] = {nan, nan};
Patrick Ventured8012182018-03-08 08:21:38 -0800511
512 // Start all sensors in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800513 markSensorMissing(t);
Patrick Ventured8012182018-03-08 08:21:38 -0800514 }
515}
516
Patrick Venture597ebd62020-08-11 08:48:19 -0700517void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800518{
519 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700520 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800521 {
Josh Lehanb3005752022-02-22 20:48:07 -0800522 std::cerr << name << ": " << value.scaled << " " << value.unscaled
523 << "\n";
524 }
525
526 std::cerr << "Fan outputs now: \n";
527 for (const auto& [name, value] : _cachedFanOutputs)
528 {
529 std::cerr << name << ": " << value.scaled << " " << value.unscaled
530 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800531 }
532}
533
Patrick Venture597ebd62020-08-11 08:48:19 -0700534void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800535{
536 for (auto& p : _fans)
537 {
James Feist22c257a2018-08-31 14:07:12 -0700538 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800539 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700540
541 if (_redundantWrite)
542 {
543 // This is only needed once
544 _redundantWrite = false;
545 }
Patrick Ventured8012182018-03-08 08:21:38 -0800546}
547
Patrick Venture597ebd62020-08-11 08:48:19 -0700548void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800549{
550 for (auto& p : _thermals)
551 {
James Feist22c257a2018-08-31 14:07:12 -0700552 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800553 }
554}
555
Patrick Venture597ebd62020-08-11 08:48:19 -0700556Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800557{
Patrick Venturefe75b192018-06-08 11:19:43 -0700558 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800559}
560
Josh Lehana4146eb2020-10-01 11:49:09 -0700561bool DbusPidZone::getRedundantWrite(void) const
562{
563 return _redundantWrite;
564}
565
Patrick Venture597ebd62020-08-11 08:48:19 -0700566bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800567{
568 std::cerr << "manual: " << value << std::endl;
569 setManualMode(value);
570 return ModeObject::manual(value);
571}
572
Patrick Venture597ebd62020-08-11 08:48:19 -0700573bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800574{
575 return getFailSafeMode();
576}
Patrick Venturea0764872020-08-08 07:48:43 -0700577
Harvey Wu37180062023-10-02 09:42:50 +0800578void DbusPidZone::addPidControlProcess(std::string name, std::string type,
579 double setpoint, sdbusplus::bus_t& bus,
ykchiu7c6d35d2023-05-10 17:01:46 +0800580 std::string objPath, bool defer)
581{
582 _pidsControlProcess[name] = std::make_unique<ProcessObject>(
583 bus, objPath.c_str(),
584 defer ? ProcessObject::action::defer_emit
585 : ProcessObject::action::emit_object_added);
586 // Default enable setting = true
587 _pidsControlProcess[name]->enabled(true);
Harvey Wu37180062023-10-02 09:42:50 +0800588 _pidsControlProcess[name]->setpoint(setpoint);
589
590 if (type == "temp")
591 {
592 _pidsControlProcess[name]->classType("Temperature");
593 }
594 else if (type == "margin")
595 {
596 _pidsControlProcess[name]->classType("Margin");
597 }
598 else if (type == "power")
599 {
600 _pidsControlProcess[name]->classType("Power");
601 }
602 else if (type == "powersum")
603 {
604 _pidsControlProcess[name]->classType("PowerSum");
605 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800606}
607
608bool DbusPidZone::isPidProcessEnabled(std::string name)
609{
610 return _pidsControlProcess[name]->enabled();
611}
612
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800613void DbusPidZone::addPidFailSafePercent(std::vector<std::string> inputs,
614 double percent)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800615{
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800616 for (const auto& sensorName : inputs)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800617 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800618 if (_sensorFailSafePercent.find(sensorName) !=
619 _sensorFailSafePercent.end())
620 {
621 _sensorFailSafePercent[sensorName] =
622 std::max(_sensorFailSafePercent[sensorName], percent);
623 if (debugEnabled)
624 {
625 std::cerr << "Sensor " << sensorName
626 << " failsafe percent updated to "
627 << _sensorFailSafePercent[sensorName] << "\n";
628 }
629 }
630 else
631 {
632 _sensorFailSafePercent[sensorName] = percent;
633 if (debugEnabled)
634 {
635 std::cerr << "Sensor " << sensorName
636 << " failsafe percent set to " << percent << "\n";
637 }
638 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800639 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800640}
641
Harvey Wucc0232a2023-02-09 14:58:55 +0800642std::string DbusPidZone::leader() const
643{
644 return _maximumSetPointName;
645}
646
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400647void DbusPidZone::updateThermalPowerDebugInterface(
648 std::string pidName, std::string leader, double input, double output)
Harvey Wu37180062023-10-02 09:42:50 +0800649{
650 if (leader.empty())
651 {
652 _pidsControlProcess[pidName]->output(output);
653 }
654 else
655 {
656 _pidsControlProcess[pidName]->leader(leader);
657 _pidsControlProcess[pidName]->input(input);
658 }
659}
660
Delphine CC Chiu97889632023-11-06 11:32:46 +0800661bool DbusPidZone::getAccSetPoint(void) const
662{
663 return _accumulateSetPoint;
664}
665
Patrick Venturea0764872020-08-08 07:48:43 -0700666} // namespace pid_control