blob: e5eddca5e8a45384b1ece62bc0cebf9f33356a4d [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
Nirav Shahccc8bb62022-02-17 21:06:51 -0800123 _SetPoints.push_back(setPoint);
124 /*
125 * if there are multiple thermal controllers with the same
126 * value, pick the first one in the iterator
127 */
128 if (_maximumSetPoint < setPoint)
129 {
130 _maximumSetPoint = setPoint;
131 _maximumSetPointName = name;
132 }
Patrick Ventured8012182018-03-08 08:21:38 -0800133}
134
Patrick Venture597ebd62020-08-11 08:48:19 -0700135void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800136{
137 _RPMCeilings.push_back(ceiling);
138}
139
Patrick Venture597ebd62020-08-11 08:48:19 -0700140void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800141{
142 _RPMCeilings.clear();
143}
144
Patrick Venture597ebd62020-08-11 08:48:19 -0700145void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800146{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700147 _SetPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800148 _maximumSetPoint = 0;
ykchiu7c6d35d2023-05-10 17:01:46 +0800149 _maximumSetPointName.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800150}
151
Patrick Venture597ebd62020-08-11 08:48:19 -0700152double DbusPidZone::getFailSafePercent(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800153{
154 return _failSafePercent;
155}
156
Nirav Shahccc8bb62022-02-17 21:06:51 -0800157double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800158{
James Feist3484bed2019-02-25 13:28:18 -0800159 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800160}
161
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800162uint64_t DbusPidZone::getCycleIntervalTime(void) const
163{
164 return _cycleTime.cycleIntervalTimeMS;
165}
166
167uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
168{
169 return _cycleTime.updateThermalsTimeMS;
170}
171
Patrick Venture597ebd62020-08-11 08:48:19 -0700172void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800173{
174 _fans.push_back(std::move(pid));
175}
176
Patrick Venture597ebd62020-08-11 08:48:19 -0700177void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800178{
179 _thermals.push_back(std::move(pid));
180}
181
Patrick Venture597ebd62020-08-11 08:48:19 -0700182double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800183{
Josh Lehanb3005752022-02-22 20:48:07 -0800184 return _cachedValuesByName.at(name).scaled;
185}
186
187ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
188{
Patrick Ventured8012182018-03-08 08:21:38 -0800189 return _cachedValuesByName.at(name);
190}
191
Josh Lehanb3005752022-02-22 20:48:07 -0800192void DbusPidZone::setOutputCache(std::string_view name,
193 const ValueCacheEntry& values)
194{
195 _cachedFanOutputs[std::string{name}] = values;
196}
197
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800198void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800199{
200 _fanInputs.push_back(fan);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800201
202 if (missingAcceptable)
203 {
204 _missingAcceptable.emplace(fan);
205 }
Patrick Ventured8012182018-03-08 08:21:38 -0800206}
207
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800208void DbusPidZone::addThermalInput(const std::string& therm,
209 bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800210{
211 _thermalInputs.push_back(therm);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800212
213 if (missingAcceptable)
214 {
215 _missingAcceptable.emplace(therm);
216 }
Patrick Ventured8012182018-03-08 08:21:38 -0800217}
218
Josh Lehan55ccad62020-09-20 23:57:49 -0700219// Updates desired RPM setpoint from optional text file
220// Returns true if rpmValue updated, false if left unchanged
221static bool fileParseRpm(const std::string& fileName, double& rpmValue)
222{
223 static constexpr std::chrono::seconds throttlePace{3};
224
225 std::string errText;
226
227 try
228 {
229 std::ifstream ifs;
230 ifs.open(fileName);
231 if (ifs)
232 {
233 int value;
234 ifs >> value;
235
236 if (value <= 0)
237 {
238 errText = "File content could not be parsed to a number";
239 }
240 else if (value <= 100)
241 {
242 errText = "File must contain RPM value, not PWM value";
243 }
244 else
245 {
246 rpmValue = static_cast<double>(value);
247 return true;
248 }
249 }
250 }
251 catch (const std::exception& e)
252 {
253 errText = "Exception: ";
254 errText += e.what();
255 }
256
257 // The file is optional, intentionally not an error if file not found
258 if (!(errText.empty()))
259 {
260 tstamp now = std::chrono::high_resolution_clock::now();
261 if (allowThrottle(now, throttlePace))
262 {
263 std::cerr << "Unable to read from '" << fileName << "': " << errText
264 << "\n";
265 }
266 }
267
268 return false;
269}
270
Patrick Venture597ebd62020-08-11 08:48:19 -0700271void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800272{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800273 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800274 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800275
James Feist608304d2019-02-25 10:01:42 -0800276 if (_RPMCeilings.size() > 0)
277 {
278 result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800279 // if Max set point is larger than the lowest ceiling, reset to lowest
280 // ceiling.
281 if (*result < _maximumSetPoint)
282 {
283 _maximumSetPoint = *result;
284 // When using lowest ceiling, controller name is ceiling.
285 _maximumSetPointName = "Ceiling";
286 }
James Feist608304d2019-02-25 10:01:42 -0800287 }
288
Patrick Ventured8012182018-03-08 08:21:38 -0800289 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800290 * If the maximum RPM setpoint output is below the minimum RPM
291 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800292 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800293 if (minThermalThreshold >= _maximumSetPoint)
294 {
295 _maximumSetPoint = minThermalThreshold;
ykchiu7c6d35d2023-05-10 17:01:46 +0800296 _maximumSetPointName = "Minimum";
Nirav Shahccc8bb62022-02-17 21:06:51 -0800297 }
298 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
299 {
300 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
301 << _maximumSetPoint << " requested by "
302 << _maximumSetPointName;
303 for (const auto& sensor : _failSafeSensors)
304 {
305 if (sensor.find("Fan") == std::string::npos)
306 {
307 std::cerr << " " << sensor;
308 }
309 }
310 std::cerr << "\n";
311 _maximumSetPointNamePrev.assign(_maximumSetPointName);
312 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700313 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800314 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800315 /*
316 * We received no setpoints from thermal sensors.
317 * This is a case experienced during tuning where they only specify
318 * fan sensors and one large fan PID for all the fans.
319 */
320 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700321
Nirav Shahccc8bb62022-02-17 21:06:51 -0800322 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700323
324 // Allow per-zone setpoint files to override overall setpoint file
325 std::ostringstream zoneSuffix;
326 zoneSuffix << ".zone" << _zoneId;
327 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
328
Nirav Shahccc8bb62022-02-17 21:06:51 -0800329 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800330 }
Patrick Ventured8012182018-03-08 08:21:38 -0800331 return;
332}
333
Patrick Venture597ebd62020-08-11 08:48:19 -0700334void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800335{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700336 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800337 * 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 -0700338 */
Patrick Ventured8012182018-03-08 08:21:38 -0800339
Nirav Shahccc8bb62022-02-17 21:06:51 -0800340 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800341
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700342 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800343 {
Josh Lehanb3005752022-02-22 20:48:07 -0800344 _log << "," << f << "," << f << "_raw";
345 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800346 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700347 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700348 {
Josh Lehanb3005752022-02-22 20:48:07 -0800349 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700350 }
Josh Lehanb3005752022-02-22 20:48:07 -0800351
Patrick Venture5f02ad22018-04-24 10:18:40 -0700352 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800353 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800354}
355
Patrick Venture7a98c192020-08-12 08:35:16 -0700356void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800357{
Patrick Venture7a98c192020-08-12 08:35:16 -0700358 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800359}
Patrick Ventured8012182018-03-08 08:21:38 -0800360
361/*
362 * TODO(venture) This is effectively updating the cache and should check if the
363 * values they're using to update it are new or old, or whatnot. For instance,
364 * if we haven't heard from the host in X time we need to detect this failure.
365 *
366 * I haven't decided if the Sensor should have a lastUpdated method or whether
367 * that should be for the ReadInterface or etc...
368 */
369
370/**
371 * We want the PID loop to run with values cached, so this will get all the
372 * fan tachs for the loop.
373 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700374void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800375{
376 /* TODO(venture): Should I just make _log point to /dev/null when logging
377 * is disabled? I think it's a waste to try and log things even if the
378 * data is just being dropped though.
379 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800380 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700381 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800382 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800383 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
384 now.time_since_epoch())
385 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700386 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800387 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800388 }
Patrick Ventured8012182018-03-08 08:21:38 -0800389
Tom Tungdf1f1832022-11-14 19:26:52 +0800390 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800391
Patrick Venturede79ee02019-05-08 14:50:00 -0700392 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700393 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800394 for (const auto& t : _thermalInputs)
395 {
Josh Lehanb3005752022-02-22 20:48:07 -0800396 const auto& v = _cachedValuesByName[t];
397 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800398 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700399 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700400
Patrick Ventured8012182018-03-08 08:21:38 -0800401 return;
402}
403
Patrick Venture597ebd62020-08-11 08:48:19 -0700404void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800405{
Tom Tungdf1f1832022-11-14 19:26:52 +0800406 processSensorInputs</* fanSensorLogging */ false>(
407 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800408
409 return;
410}
411
Patrick Venture597ebd62020-08-11 08:48:19 -0700412void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800413{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800414 auto nan = std::numeric_limits<double>::quiet_NaN();
415
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700416 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800417 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800418 _cachedValuesByName[f] = {nan, nan};
419 _cachedFanOutputs[f] = {nan, nan};
Will Liangded0ab52019-05-15 17:10:06 +0800420
421 // Start all fans in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800422 markSensorMissing(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800423 }
424
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700425 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800426 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800427 _cachedValuesByName[t] = {nan, nan};
Patrick Ventured8012182018-03-08 08:21:38 -0800428
429 // Start all sensors in fail-safe mode.
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800430 markSensorMissing(t);
Patrick Ventured8012182018-03-08 08:21:38 -0800431 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800432 // Initialize Pid FailSafePercent
433 initPidFailSafePercent();
Patrick Ventured8012182018-03-08 08:21:38 -0800434}
435
Patrick Venture597ebd62020-08-11 08:48:19 -0700436void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800437{
438 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700439 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800440 {
Josh Lehanb3005752022-02-22 20:48:07 -0800441 std::cerr << name << ": " << value.scaled << " " << value.unscaled
442 << "\n";
443 }
444
445 std::cerr << "Fan outputs now: \n";
446 for (const auto& [name, value] : _cachedFanOutputs)
447 {
448 std::cerr << name << ": " << value.scaled << " " << value.unscaled
449 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800450 }
451}
452
Patrick Venture597ebd62020-08-11 08:48:19 -0700453void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800454{
455 for (auto& p : _fans)
456 {
James Feist22c257a2018-08-31 14:07:12 -0700457 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800458 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700459
460 if (_redundantWrite)
461 {
462 // This is only needed once
463 _redundantWrite = false;
464 }
Patrick Ventured8012182018-03-08 08:21:38 -0800465}
466
Patrick Venture597ebd62020-08-11 08:48:19 -0700467void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800468{
469 for (auto& p : _thermals)
470 {
James Feist22c257a2018-08-31 14:07:12 -0700471 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800472 }
473}
474
Patrick Venture597ebd62020-08-11 08:48:19 -0700475Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800476{
Patrick Venturefe75b192018-06-08 11:19:43 -0700477 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800478}
479
Josh Lehana4146eb2020-10-01 11:49:09 -0700480bool DbusPidZone::getRedundantWrite(void) const
481{
482 return _redundantWrite;
483}
484
Patrick Venture597ebd62020-08-11 08:48:19 -0700485bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800486{
487 std::cerr << "manual: " << value << std::endl;
488 setManualMode(value);
489 return ModeObject::manual(value);
490}
491
Patrick Venture597ebd62020-08-11 08:48:19 -0700492bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800493{
494 return getFailSafeMode();
495}
Patrick Venturea0764872020-08-08 07:48:43 -0700496
Harvey Wu37180062023-10-02 09:42:50 +0800497void DbusPidZone::addPidControlProcess(std::string name, std::string type,
498 double setpoint, sdbusplus::bus_t& bus,
ykchiu7c6d35d2023-05-10 17:01:46 +0800499 std::string objPath, bool defer)
500{
501 _pidsControlProcess[name] = std::make_unique<ProcessObject>(
502 bus, objPath.c_str(),
503 defer ? ProcessObject::action::defer_emit
504 : ProcessObject::action::emit_object_added);
505 // Default enable setting = true
506 _pidsControlProcess[name]->enabled(true);
Harvey Wu37180062023-10-02 09:42:50 +0800507 _pidsControlProcess[name]->setpoint(setpoint);
508
509 if (type == "temp")
510 {
511 _pidsControlProcess[name]->classType("Temperature");
512 }
513 else if (type == "margin")
514 {
515 _pidsControlProcess[name]->classType("Margin");
516 }
517 else if (type == "power")
518 {
519 _pidsControlProcess[name]->classType("Power");
520 }
521 else if (type == "powersum")
522 {
523 _pidsControlProcess[name]->classType("PowerSum");
524 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800525}
526
527bool DbusPidZone::isPidProcessEnabled(std::string name)
528{
529 return _pidsControlProcess[name]->enabled();
530}
531
ykchiu9fe3a3c2023-05-11 13:43:54 +0800532void DbusPidZone::initPidFailSafePercent(void)
533{
534 // Currently, find the max failsafe percent pwm settings from zone and
535 // controller, and assign it to zone failsafe percent.
536
537 _failSafePercent = _zoneFailSafePercent;
538 std::cerr << "zone: Zone" << _zoneId
539 << " zoneFailSafePercent: " << _zoneFailSafePercent << "\n";
540
541 for (const auto& [name, value] : _pidsFailSafePercent)
542 {
543 _failSafePercent = std::max(_failSafePercent, value);
544 std::cerr << "pid: " << name << " failSafePercent: " << value << "\n";
545 }
546
547 // when the final failsafe percent is zero , it indicate no failsafe
548 // percent is configured  , set it to 100% as the default setting.
549 if (_failSafePercent == 0)
550 {
551 _failSafePercent = 100;
552 }
553 std::cerr << "Final zone" << _zoneId
554 << " failSafePercent: " << _failSafePercent << "\n";
555}
556
557void DbusPidZone::addPidFailSafePercent(std::string name, double percent)
558{
559 _pidsFailSafePercent[name] = percent;
560}
561
Harvey Wucc0232a2023-02-09 14:58:55 +0800562std::string DbusPidZone::leader() const
563{
564 return _maximumSetPointName;
565}
566
Harvey Wu37180062023-10-02 09:42:50 +0800567void DbusPidZone::updateThermalPowerDebugInterface(std::string pidName,
568 std::string leader,
569 double input, double output)
570{
571 if (leader.empty())
572 {
573 _pidsControlProcess[pidName]->output(output);
574 }
575 else
576 {
577 _pidsControlProcess[pidName]->leader(leader);
578 _pidsControlProcess[pidName]->input(input);
579 }
580}
581
Patrick Venturea0764872020-08-08 07:48:43 -0700582} // namespace pid_control