blob: 0b4684142f1b0a2b7153ef1bc3e4ee8c48785a89 [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
Patrick Venture597ebd62020-08-11 08:48:19 -070099int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800100{
101 return _zoneId;
102}
103
Nirav Shahccc8bb62022-02-17 21:06:51 -0800104void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800105{
ykchiu7c6d35d2023-05-10 17:01:46 +0800106 /* exclude disabled pidloop from _maximumSetPoint calculation*/
107 if (!isPidProcessEnabled(name))
108 {
109 return;
110 }
111
Nirav Shahccc8bb62022-02-17 21:06:51 -0800112 _SetPoints.push_back(setPoint);
113 /*
114 * if there are multiple thermal controllers with the same
115 * value, pick the first one in the iterator
116 */
117 if (_maximumSetPoint < setPoint)
118 {
119 _maximumSetPoint = setPoint;
120 _maximumSetPointName = name;
121 }
Patrick Ventured8012182018-03-08 08:21:38 -0800122}
123
Patrick Venture597ebd62020-08-11 08:48:19 -0700124void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800125{
126 _RPMCeilings.push_back(ceiling);
127}
128
Patrick Venture597ebd62020-08-11 08:48:19 -0700129void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800130{
131 _RPMCeilings.clear();
132}
133
Patrick Venture597ebd62020-08-11 08:48:19 -0700134void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800135{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700136 _SetPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800137 _maximumSetPoint = 0;
ykchiu7c6d35d2023-05-10 17:01:46 +0800138 _maximumSetPointName.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800139}
140
Patrick Venture597ebd62020-08-11 08:48:19 -0700141double DbusPidZone::getFailSafePercent(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800142{
143 return _failSafePercent;
144}
145
Nirav Shahccc8bb62022-02-17 21:06:51 -0800146double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800147{
James Feist3484bed2019-02-25 13:28:18 -0800148 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800149}
150
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800151uint64_t DbusPidZone::getCycleIntervalTime(void) const
152{
153 return _cycleTime.cycleIntervalTimeMS;
154}
155
156uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
157{
158 return _cycleTime.updateThermalsTimeMS;
159}
160
Patrick Venture597ebd62020-08-11 08:48:19 -0700161void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800162{
163 _fans.push_back(std::move(pid));
164}
165
Patrick Venture597ebd62020-08-11 08:48:19 -0700166void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800167{
168 _thermals.push_back(std::move(pid));
169}
170
Patrick Venture597ebd62020-08-11 08:48:19 -0700171double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800172{
Josh Lehanb3005752022-02-22 20:48:07 -0800173 return _cachedValuesByName.at(name).scaled;
174}
175
176ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
177{
Patrick Ventured8012182018-03-08 08:21:38 -0800178 return _cachedValuesByName.at(name);
179}
180
Josh Lehanb3005752022-02-22 20:48:07 -0800181void DbusPidZone::setOutputCache(std::string_view name,
182 const ValueCacheEntry& values)
183{
184 _cachedFanOutputs[std::string{name}] = values;
185}
186
Patrick Venture597ebd62020-08-11 08:48:19 -0700187void DbusPidZone::addFanInput(const std::string& fan)
Patrick Ventured8012182018-03-08 08:21:38 -0800188{
189 _fanInputs.push_back(fan);
190}
191
Patrick Venture597ebd62020-08-11 08:48:19 -0700192void DbusPidZone::addThermalInput(const std::string& therm)
Patrick Ventured8012182018-03-08 08:21:38 -0800193{
194 _thermalInputs.push_back(therm);
195}
196
Josh Lehan55ccad62020-09-20 23:57:49 -0700197// Updates desired RPM setpoint from optional text file
198// Returns true if rpmValue updated, false if left unchanged
199static bool fileParseRpm(const std::string& fileName, double& rpmValue)
200{
201 static constexpr std::chrono::seconds throttlePace{3};
202
203 std::string errText;
204
205 try
206 {
207 std::ifstream ifs;
208 ifs.open(fileName);
209 if (ifs)
210 {
211 int value;
212 ifs >> value;
213
214 if (value <= 0)
215 {
216 errText = "File content could not be parsed to a number";
217 }
218 else if (value <= 100)
219 {
220 errText = "File must contain RPM value, not PWM value";
221 }
222 else
223 {
224 rpmValue = static_cast<double>(value);
225 return true;
226 }
227 }
228 }
229 catch (const std::exception& e)
230 {
231 errText = "Exception: ";
232 errText += e.what();
233 }
234
235 // The file is optional, intentionally not an error if file not found
236 if (!(errText.empty()))
237 {
238 tstamp now = std::chrono::high_resolution_clock::now();
239 if (allowThrottle(now, throttlePace))
240 {
241 std::cerr << "Unable to read from '" << fileName << "': " << errText
242 << "\n";
243 }
244 }
245
246 return false;
247}
248
Patrick Venture597ebd62020-08-11 08:48:19 -0700249void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800250{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800251 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800252 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800253
James Feist608304d2019-02-25 10:01:42 -0800254 if (_RPMCeilings.size() > 0)
255 {
256 result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800257 // if Max set point is larger than the lowest ceiling, reset to lowest
258 // ceiling.
259 if (*result < _maximumSetPoint)
260 {
261 _maximumSetPoint = *result;
262 // When using lowest ceiling, controller name is ceiling.
263 _maximumSetPointName = "Ceiling";
264 }
James Feist608304d2019-02-25 10:01:42 -0800265 }
266
Patrick Ventured8012182018-03-08 08:21:38 -0800267 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800268 * If the maximum RPM setpoint output is below the minimum RPM
269 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800270 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800271 if (minThermalThreshold >= _maximumSetPoint)
272 {
273 _maximumSetPoint = minThermalThreshold;
ykchiu7c6d35d2023-05-10 17:01:46 +0800274 _maximumSetPointName = "Minimum";
Nirav Shahccc8bb62022-02-17 21:06:51 -0800275 }
276 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
277 {
278 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
279 << _maximumSetPoint << " requested by "
280 << _maximumSetPointName;
281 for (const auto& sensor : _failSafeSensors)
282 {
283 if (sensor.find("Fan") == std::string::npos)
284 {
285 std::cerr << " " << sensor;
286 }
287 }
288 std::cerr << "\n";
289 _maximumSetPointNamePrev.assign(_maximumSetPointName);
290 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700291 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800292 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800293 /*
294 * We received no setpoints from thermal sensors.
295 * This is a case experienced during tuning where they only specify
296 * fan sensors and one large fan PID for all the fans.
297 */
298 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700299
Nirav Shahccc8bb62022-02-17 21:06:51 -0800300 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700301
302 // Allow per-zone setpoint files to override overall setpoint file
303 std::ostringstream zoneSuffix;
304 zoneSuffix << ".zone" << _zoneId;
305 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
306
Nirav Shahccc8bb62022-02-17 21:06:51 -0800307 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800308 }
Patrick Ventured8012182018-03-08 08:21:38 -0800309 return;
310}
311
Patrick Venture597ebd62020-08-11 08:48:19 -0700312void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800313{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700314 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800315 * 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 -0700316 */
Patrick Ventured8012182018-03-08 08:21:38 -0800317
Nirav Shahccc8bb62022-02-17 21:06:51 -0800318 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800319
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700320 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800321 {
Josh Lehanb3005752022-02-22 20:48:07 -0800322 _log << "," << f << "," << f << "_raw";
323 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800324 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700325 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700326 {
Josh Lehanb3005752022-02-22 20:48:07 -0800327 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700328 }
Josh Lehanb3005752022-02-22 20:48:07 -0800329
Patrick Venture5f02ad22018-04-24 10:18:40 -0700330 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800331 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800332}
333
Patrick Venture7a98c192020-08-12 08:35:16 -0700334void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800335{
Patrick Venture7a98c192020-08-12 08:35:16 -0700336 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800337}
Patrick Ventured8012182018-03-08 08:21:38 -0800338
339/*
340 * TODO(venture) This is effectively updating the cache and should check if the
341 * values they're using to update it are new or old, or whatnot. For instance,
342 * if we haven't heard from the host in X time we need to detect this failure.
343 *
344 * I haven't decided if the Sensor should have a lastUpdated method or whether
345 * that should be for the ReadInterface or etc...
346 */
347
348/**
349 * We want the PID loop to run with values cached, so this will get all the
350 * fan tachs for the loop.
351 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700352void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800353{
354 /* TODO(venture): Should I just make _log point to /dev/null when logging
355 * is disabled? I think it's a waste to try and log things even if the
356 * data is just being dropped though.
357 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800358 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700359 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800360 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800361 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
362 now.time_since_epoch())
363 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700364 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800365 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800366 }
Patrick Ventured8012182018-03-08 08:21:38 -0800367
Tom Tungdf1f1832022-11-14 19:26:52 +0800368 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800369
Patrick Venturede79ee02019-05-08 14:50:00 -0700370 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700371 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800372 for (const auto& t : _thermalInputs)
373 {
Josh Lehanb3005752022-02-22 20:48:07 -0800374 const auto& v = _cachedValuesByName[t];
375 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800376 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700377 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700378
Patrick Ventured8012182018-03-08 08:21:38 -0800379 return;
380}
381
Patrick Venture597ebd62020-08-11 08:48:19 -0700382void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800383{
Tom Tungdf1f1832022-11-14 19:26:52 +0800384 processSensorInputs</* fanSensorLogging */ false>(
385 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800386
387 return;
388}
389
Patrick Venture597ebd62020-08-11 08:48:19 -0700390void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800391{
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700392 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800393 {
Josh Lehand38ae272020-11-13 02:59:30 -0800394 _cachedValuesByName[f] = {0, 0};
395 _cachedFanOutputs[f] = {0, 0};
Will Liangded0ab52019-05-15 17:10:06 +0800396
397 // Start all fans in fail-safe mode.
398 _failSafeSensors.insert(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800399 }
400
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700401 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800402 {
Josh Lehand38ae272020-11-13 02:59:30 -0800403 _cachedValuesByName[t] = {0, 0};
Patrick Ventured8012182018-03-08 08:21:38 -0800404
405 // Start all sensors in fail-safe mode.
406 _failSafeSensors.insert(t);
407 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800408 // Initialize Pid FailSafePercent
409 initPidFailSafePercent();
Patrick Ventured8012182018-03-08 08:21:38 -0800410}
411
Patrick Venture597ebd62020-08-11 08:48:19 -0700412void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800413{
414 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700415 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800416 {
Josh Lehanb3005752022-02-22 20:48:07 -0800417 std::cerr << name << ": " << value.scaled << " " << value.unscaled
418 << "\n";
419 }
420
421 std::cerr << "Fan outputs now: \n";
422 for (const auto& [name, value] : _cachedFanOutputs)
423 {
424 std::cerr << name << ": " << value.scaled << " " << value.unscaled
425 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800426 }
427}
428
Patrick Venture597ebd62020-08-11 08:48:19 -0700429void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800430{
431 for (auto& p : _fans)
432 {
James Feist22c257a2018-08-31 14:07:12 -0700433 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800434 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700435
436 if (_redundantWrite)
437 {
438 // This is only needed once
439 _redundantWrite = false;
440 }
Patrick Ventured8012182018-03-08 08:21:38 -0800441}
442
Patrick Venture597ebd62020-08-11 08:48:19 -0700443void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800444{
445 for (auto& p : _thermals)
446 {
James Feist22c257a2018-08-31 14:07:12 -0700447 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800448 }
449}
450
Patrick Venture597ebd62020-08-11 08:48:19 -0700451Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800452{
Patrick Venturefe75b192018-06-08 11:19:43 -0700453 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800454}
455
Josh Lehana4146eb2020-10-01 11:49:09 -0700456bool DbusPidZone::getRedundantWrite(void) const
457{
458 return _redundantWrite;
459}
460
Patrick Venture597ebd62020-08-11 08:48:19 -0700461bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800462{
463 std::cerr << "manual: " << value << std::endl;
464 setManualMode(value);
465 return ModeObject::manual(value);
466}
467
Patrick Venture597ebd62020-08-11 08:48:19 -0700468bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800469{
470 return getFailSafeMode();
471}
Patrick Venturea0764872020-08-08 07:48:43 -0700472
Harvey Wu37180062023-10-02 09:42:50 +0800473void DbusPidZone::addPidControlProcess(std::string name, std::string type,
474 double setpoint, sdbusplus::bus_t& bus,
ykchiu7c6d35d2023-05-10 17:01:46 +0800475 std::string objPath, bool defer)
476{
477 _pidsControlProcess[name] = std::make_unique<ProcessObject>(
478 bus, objPath.c_str(),
479 defer ? ProcessObject::action::defer_emit
480 : ProcessObject::action::emit_object_added);
481 // Default enable setting = true
482 _pidsControlProcess[name]->enabled(true);
Harvey Wu37180062023-10-02 09:42:50 +0800483 _pidsControlProcess[name]->setpoint(setpoint);
484
485 if (type == "temp")
486 {
487 _pidsControlProcess[name]->classType("Temperature");
488 }
489 else if (type == "margin")
490 {
491 _pidsControlProcess[name]->classType("Margin");
492 }
493 else if (type == "power")
494 {
495 _pidsControlProcess[name]->classType("Power");
496 }
497 else if (type == "powersum")
498 {
499 _pidsControlProcess[name]->classType("PowerSum");
500 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800501}
502
503bool DbusPidZone::isPidProcessEnabled(std::string name)
504{
505 return _pidsControlProcess[name]->enabled();
506}
507
ykchiu9fe3a3c2023-05-11 13:43:54 +0800508void DbusPidZone::initPidFailSafePercent(void)
509{
510 // Currently, find the max failsafe percent pwm settings from zone and
511 // controller, and assign it to zone failsafe percent.
512
513 _failSafePercent = _zoneFailSafePercent;
514 std::cerr << "zone: Zone" << _zoneId
515 << " zoneFailSafePercent: " << _zoneFailSafePercent << "\n";
516
517 for (const auto& [name, value] : _pidsFailSafePercent)
518 {
519 _failSafePercent = std::max(_failSafePercent, value);
520 std::cerr << "pid: " << name << " failSafePercent: " << value << "\n";
521 }
522
523 // when the final failsafe percent is zero , it indicate no failsafe
524 // percent is configured  , set it to 100% as the default setting.
525 if (_failSafePercent == 0)
526 {
527 _failSafePercent = 100;
528 }
529 std::cerr << "Final zone" << _zoneId
530 << " failSafePercent: " << _failSafePercent << "\n";
531}
532
533void DbusPidZone::addPidFailSafePercent(std::string name, double percent)
534{
535 _pidsFailSafePercent[name] = percent;
536}
537
Harvey Wucc0232a2023-02-09 14:58:55 +0800538std::string DbusPidZone::leader() const
539{
540 return _maximumSetPointName;
541}
542
Harvey Wu37180062023-10-02 09:42:50 +0800543void DbusPidZone::updateThermalPowerDebugInterface(std::string pidName,
544 std::string leader,
545 double input, double output)
546{
547 if (leader.empty())
548 {
549 _pidsControlProcess[pidName]->output(output);
550 }
551 else
552 {
553 _pidsControlProcess[pidName]->leader(leader);
554 _pidsControlProcess[pidName]->input(input);
555 }
556}
557
Patrick Venturea0764872020-08-08 07:48:43 -0700558} // namespace pid_control