Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | /** |
| 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 Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 18 | #include "zone.hpp" |
| 19 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 20 | #include "conf.hpp" |
| 21 | #include "pid/controller.hpp" |
| 22 | #include "pid/ec/pid.hpp" |
| 23 | #include "pid/fancontroller.hpp" |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 24 | #include "pid/stepwisecontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 25 | #include "pid/thermalcontroller.hpp" |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 26 | #include "pid/tuning.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 27 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <chrono> |
| 30 | #include <cstring> |
| 31 | #include <fstream> |
| 32 | #include <iostream> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 33 | #include <memory> |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 34 | #include <sstream> |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 35 | #include <string> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 36 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 37 | using tstamp = std::chrono::high_resolution_clock::time_point; |
| 38 | using namespace std::literals::chrono_literals; |
| 39 | |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 40 | // Enforces minimum duration between events |
| 41 | // Rreturns true if event should be allowed, false if disallowed |
| 42 | bool 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 | |
| 69 | namespace pid_control |
| 70 | { |
| 71 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 72 | double DbusPidZone::getMaxSetPointRequest(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 73 | { |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 74 | return _maximumSetPoint; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 77 | bool DbusPidZone::getManualMode(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 78 | { |
| 79 | return _manualMode; |
| 80 | } |
| 81 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 82 | void DbusPidZone::setManualMode(bool mode) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 83 | { |
| 84 | _manualMode = mode; |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 85 | |
| 86 | // If returning to automatic mode, need to restore PWM from PID loop |
| 87 | if (!mode) |
| 88 | { |
| 89 | _redundantWrite = true; |
| 90 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 93 | bool DbusPidZone::getFailSafeMode(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 94 | { |
| 95 | // If any keys are present at least one sensor is in fail safe mode. |
| 96 | return !_failSafeSensors.empty(); |
| 97 | } |
| 98 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 99 | void 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 Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 110 | int64_t DbusPidZone::getZoneID(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 111 | { |
| 112 | return _zoneId; |
| 113 | } |
| 114 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 115 | void DbusPidZone::addSetPoint(double setPoint, const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 116 | { |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 117 | /* exclude disabled pidloop from _maximumSetPoint calculation*/ |
| 118 | if (!isPidProcessEnabled(name)) |
| 119 | { |
| 120 | return; |
| 121 | } |
| 122 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 123 | _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 Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 135 | void DbusPidZone::addRPMCeiling(double ceiling) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 136 | { |
| 137 | _RPMCeilings.push_back(ceiling); |
| 138 | } |
| 139 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 140 | void DbusPidZone::clearRPMCeilings(void) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 141 | { |
| 142 | _RPMCeilings.clear(); |
| 143 | } |
| 144 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 145 | void DbusPidZone::clearSetPoints(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 146 | { |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 147 | _SetPoints.clear(); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 148 | _maximumSetPoint = 0; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 149 | _maximumSetPointName.clear(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 152 | double DbusPidZone::getFailSafePercent(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 153 | { |
| 154 | return _failSafePercent; |
| 155 | } |
| 156 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 157 | double DbusPidZone::getMinThermalSetPoint(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 158 | { |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 159 | return _minThermalOutputSetPt; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 162 | uint64_t DbusPidZone::getCycleIntervalTime(void) const |
| 163 | { |
| 164 | return _cycleTime.cycleIntervalTimeMS; |
| 165 | } |
| 166 | |
| 167 | uint64_t DbusPidZone::getUpdateThermalsCycle(void) const |
| 168 | { |
| 169 | return _cycleTime.updateThermalsTimeMS; |
| 170 | } |
| 171 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 172 | void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 173 | { |
| 174 | _fans.push_back(std::move(pid)); |
| 175 | } |
| 176 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 177 | void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 178 | { |
| 179 | _thermals.push_back(std::move(pid)); |
| 180 | } |
| 181 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 182 | double DbusPidZone::getCachedValue(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 183 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 184 | return _cachedValuesByName.at(name).scaled; |
| 185 | } |
| 186 | |
| 187 | ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name) |
| 188 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 189 | return _cachedValuesByName.at(name); |
| 190 | } |
| 191 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 192 | void DbusPidZone::setOutputCache(std::string_view name, |
| 193 | const ValueCacheEntry& values) |
| 194 | { |
| 195 | _cachedFanOutputs[std::string{name}] = values; |
| 196 | } |
| 197 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 198 | void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 199 | { |
| 200 | _fanInputs.push_back(fan); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 201 | |
| 202 | if (missingAcceptable) |
| 203 | { |
| 204 | _missingAcceptable.emplace(fan); |
| 205 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 208 | void DbusPidZone::addThermalInput(const std::string& therm, |
| 209 | bool missingAcceptable) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 210 | { |
| 211 | _thermalInputs.push_back(therm); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 212 | |
| 213 | if (missingAcceptable) |
| 214 | { |
| 215 | _missingAcceptable.emplace(therm); |
| 216 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 219 | // Updates desired RPM setpoint from optional text file |
| 220 | // Returns true if rpmValue updated, false if left unchanged |
| 221 | static 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 Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 271 | void DbusPidZone::determineMaxSetPointRequest(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 272 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 273 | std::vector<double>::iterator result; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 274 | double minThermalThreshold = getMinThermalSetPoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 275 | |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 276 | if (_RPMCeilings.size() > 0) |
| 277 | { |
| 278 | result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end()); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 279 | // 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 Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 289 | /* |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 290 | * If the maximum RPM setpoint output is below the minimum RPM |
| 291 | * setpoint, set it to the minimum. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 292 | */ |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 293 | if (minThermalThreshold >= _maximumSetPoint) |
| 294 | { |
| 295 | _maximumSetPoint = minThermalThreshold; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 296 | _maximumSetPointName = "Minimum"; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 297 | } |
| 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 Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 313 | if (tuningEnabled) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 314 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 315 | /* |
| 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 Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 321 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 322 | fileParseRpm(setpointpath, _maximumSetPoint); |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 323 | |
| 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 Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 329 | fileParseRpm(zoneSetpointPath, _maximumSetPoint); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 330 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 334 | void DbusPidZone::initializeLog(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 335 | { |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 336 | /* Print header for log file: |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 337 | * 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 Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 338 | */ |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 339 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 340 | _log << "epoch_ms,setpt,requester"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 341 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 342 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 343 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 344 | _log << "," << f << "," << f << "_raw"; |
| 345 | _log << "," << f << "_pwm," << f << "_pwm_raw"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 346 | } |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 347 | for (const auto& t : _thermalInputs) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 348 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 349 | _log << "," << t << "," << t << "_raw"; |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 350 | } |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 351 | |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 352 | _log << ",failsafe"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 353 | _log << std::endl; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 356 | void DbusPidZone::writeLog(const std::string& value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 357 | { |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 358 | _log << value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 359 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 360 | |
| 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 Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 374 | void DbusPidZone::updateFanTelemetry(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 375 | { |
| 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 Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 380 | const auto now = std::chrono::high_resolution_clock::now(); |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 381 | if (loggingEnabled) |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 382 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 383 | _log << std::chrono::duration_cast<std::chrono::milliseconds>( |
| 384 | now.time_since_epoch()) |
| 385 | .count(); |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 386 | _log << "," << _maximumSetPoint; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 387 | _log << "," << _maximumSetPointName; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 388 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 389 | |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 390 | processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 391 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 392 | if (loggingEnabled) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 393 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 394 | for (const auto& t : _thermalInputs) |
| 395 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 396 | const auto& v = _cachedValuesByName[t]; |
| 397 | _log << "," << v.scaled << "," << v.unscaled; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 398 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 399 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 400 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 401 | return; |
| 402 | } |
| 403 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 404 | void DbusPidZone::updateSensors(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 405 | { |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 406 | processSensorInputs</* fanSensorLogging */ false>( |
| 407 | _thermalInputs, std::chrono::high_resolution_clock::now()); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 408 | |
| 409 | return; |
| 410 | } |
| 411 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 412 | void DbusPidZone::initializeCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 413 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 414 | auto nan = std::numeric_limits<double>::quiet_NaN(); |
| 415 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 416 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 417 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 418 | _cachedValuesByName[f] = {nan, nan}; |
| 419 | _cachedFanOutputs[f] = {nan, nan}; |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 420 | |
| 421 | // Start all fans in fail-safe mode. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 422 | markSensorMissing(f); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 425 | for (const auto& t : _thermalInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 426 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 427 | _cachedValuesByName[t] = {nan, nan}; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 428 | |
| 429 | // Start all sensors in fail-safe mode. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame^] | 430 | markSensorMissing(t); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 431 | } |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 432 | // Initialize Pid FailSafePercent |
| 433 | initPidFailSafePercent(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 436 | void DbusPidZone::dumpCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 437 | { |
| 438 | std::cerr << "Cache values now: \n"; |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 439 | for (const auto& [name, value] : _cachedValuesByName) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 440 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 441 | 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 Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 453 | void DbusPidZone::processFans(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 454 | { |
| 455 | for (auto& p : _fans) |
| 456 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 457 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 458 | } |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 459 | |
| 460 | if (_redundantWrite) |
| 461 | { |
| 462 | // This is only needed once |
| 463 | _redundantWrite = false; |
| 464 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 467 | void DbusPidZone::processThermals(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 468 | { |
| 469 | for (auto& p : _thermals) |
| 470 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 471 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 475 | Sensor* DbusPidZone::getSensor(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 476 | { |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 477 | return _mgr.getSensor(name); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 480 | bool DbusPidZone::getRedundantWrite(void) const |
| 481 | { |
| 482 | return _redundantWrite; |
| 483 | } |
| 484 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 485 | bool DbusPidZone::manual(bool value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 486 | { |
| 487 | std::cerr << "manual: " << value << std::endl; |
| 488 | setManualMode(value); |
| 489 | return ModeObject::manual(value); |
| 490 | } |
| 491 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 492 | bool DbusPidZone::failSafe() const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 493 | { |
| 494 | return getFailSafeMode(); |
| 495 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 496 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 497 | void DbusPidZone::addPidControlProcess(std::string name, std::string type, |
| 498 | double setpoint, sdbusplus::bus_t& bus, |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 499 | 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 Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 507 | _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 | } |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | bool DbusPidZone::isPidProcessEnabled(std::string name) |
| 528 | { |
| 529 | return _pidsControlProcess[name]->enabled(); |
| 530 | } |
| 531 | |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 532 | void 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 | |
| 557 | void DbusPidZone::addPidFailSafePercent(std::string name, double percent) |
| 558 | { |
| 559 | _pidsFailSafePercent[name] = percent; |
| 560 | } |
| 561 | |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 562 | std::string DbusPidZone::leader() const |
| 563 | { |
| 564 | return _maximumSetPointName; |
| 565 | } |
| 566 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 567 | void 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 Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 582 | } // namespace pid_control |