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 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 107 | 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 Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 122 | int64_t DbusPidZone::getZoneID(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 123 | { |
| 124 | return _zoneId; |
| 125 | } |
| 126 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 127 | void DbusPidZone::addSetPoint(double setPoint, const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 128 | { |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 129 | /* exclude disabled pidloop from _maximumSetPoint calculation*/ |
| 130 | if (!isPidProcessEnabled(name)) |
| 131 | { |
| 132 | return; |
| 133 | } |
| 134 | |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 135 | 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 Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 153 | /* |
| 154 | * if there are multiple thermal controllers with the same |
| 155 | * value, pick the first one in the iterator |
| 156 | */ |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 157 | if (_maximumSetPoint < _SetPoints[profileName]) |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 158 | { |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 159 | _maximumSetPoint = _SetPoints[profileName]; |
| 160 | _maximumSetPointName = profileName; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 161 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 164 | void DbusPidZone::addRPMCeiling(double ceiling) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 165 | { |
| 166 | _RPMCeilings.push_back(ceiling); |
| 167 | } |
| 168 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 169 | void DbusPidZone::clearRPMCeilings(void) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 170 | { |
| 171 | _RPMCeilings.clear(); |
| 172 | } |
| 173 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 174 | void DbusPidZone::clearSetPoints(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 175 | { |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 176 | _SetPoints.clear(); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 177 | _maximumSetPoint = 0; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 178 | _maximumSetPointName.clear(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 181 | double DbusPidZone::getFailSafePercent(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 182 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 183 | 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 Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 204 | double DbusPidZone::getMinThermalSetPoint(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 205 | { |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 206 | return _minThermalOutputSetPt; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 209 | uint64_t DbusPidZone::getCycleIntervalTime(void) const |
| 210 | { |
| 211 | return _cycleTime.cycleIntervalTimeMS; |
| 212 | } |
| 213 | |
| 214 | uint64_t DbusPidZone::getUpdateThermalsCycle(void) const |
| 215 | { |
| 216 | return _cycleTime.updateThermalsTimeMS; |
| 217 | } |
| 218 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 219 | void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 220 | { |
| 221 | _fans.push_back(std::move(pid)); |
| 222 | } |
| 223 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 224 | void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 225 | { |
| 226 | _thermals.push_back(std::move(pid)); |
| 227 | } |
| 228 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 229 | double DbusPidZone::getCachedValue(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 230 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 231 | return _cachedValuesByName.at(name).scaled; |
| 232 | } |
| 233 | |
| 234 | ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name) |
| 235 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 236 | return _cachedValuesByName.at(name); |
| 237 | } |
| 238 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 239 | void DbusPidZone::setOutputCache(std::string_view name, |
| 240 | const ValueCacheEntry& values) |
| 241 | { |
| 242 | _cachedFanOutputs[std::string{name}] = values; |
| 243 | } |
| 244 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 245 | void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 246 | { |
| 247 | _fanInputs.push_back(fan); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 248 | |
| 249 | if (missingAcceptable) |
| 250 | { |
| 251 | _missingAcceptable.emplace(fan); |
| 252 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 255 | void DbusPidZone::addThermalInput(const std::string& therm, |
| 256 | bool missingAcceptable) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 257 | { |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 258 | /* |
| 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 Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 268 | |
| 269 | if (missingAcceptable) |
| 270 | { |
| 271 | _missingAcceptable.emplace(therm); |
| 272 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 275 | // Updates desired RPM setpoint from optional text file |
| 276 | // Returns true if rpmValue updated, false if left unchanged |
| 277 | static 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 Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 327 | void DbusPidZone::determineMaxSetPointRequest(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 328 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 329 | std::vector<double>::iterator result; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 330 | double minThermalThreshold = getMinThermalSetPoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 331 | |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 332 | if (_RPMCeilings.size() > 0) |
| 333 | { |
| 334 | result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end()); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 335 | // 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 Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 345 | /* |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 346 | * 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 Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 373 | * If the maximum RPM setpoint output is below the minimum RPM |
| 374 | * setpoint, set it to the minimum. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 375 | */ |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 376 | if (minThermalThreshold >= _maximumSetPoint) |
| 377 | { |
| 378 | _maximumSetPoint = minThermalThreshold; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 379 | _maximumSetPointName = "Minimum"; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 380 | } |
| 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 Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 388 | if (sensor.first.find("Fan") == std::string::npos) |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 389 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 390 | std::cerr << " " << sensor.first; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | std::cerr << "\n"; |
| 394 | _maximumSetPointNamePrev.assign(_maximumSetPointName); |
| 395 | } |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 396 | if (tuningEnabled) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 397 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 398 | /* |
| 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 Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 404 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 405 | fileParseRpm(setpointpath, _maximumSetPoint); |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 406 | |
| 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 Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 412 | fileParseRpm(zoneSetpointPath, _maximumSetPoint); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 413 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 417 | void DbusPidZone::initializeLog(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 418 | { |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 419 | /* Print header for log file: |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 420 | * 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] | 421 | */ |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 422 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 423 | _log << "epoch_ms,setpt,requester"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 424 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 425 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 426 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 427 | _log << "," << f << "," << f << "_raw"; |
| 428 | _log << "," << f << "_pwm," << f << "_pwm_raw"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 429 | } |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 430 | for (const auto& t : _thermalInputs) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 431 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 432 | _log << "," << t << "," << t << "_raw"; |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 433 | } |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 434 | |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 435 | _log << ",failsafe"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 436 | _log << std::endl; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 439 | void DbusPidZone::writeLog(const std::string& value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 440 | { |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 441 | _log << value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 442 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 443 | |
| 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 Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 457 | void DbusPidZone::updateFanTelemetry(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 458 | { |
| 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 Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 463 | const auto now = std::chrono::high_resolution_clock::now(); |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 464 | if (loggingEnabled) |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 465 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 466 | _log << std::chrono::duration_cast<std::chrono::milliseconds>( |
| 467 | now.time_since_epoch()) |
| 468 | .count(); |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 469 | _log << "," << _maximumSetPoint; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 470 | _log << "," << _maximumSetPointName; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 471 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 472 | |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 473 | processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 474 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 475 | if (loggingEnabled) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 476 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 477 | for (const auto& t : _thermalInputs) |
| 478 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 479 | const auto& v = _cachedValuesByName[t]; |
| 480 | _log << "," << v.scaled << "," << v.unscaled; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 481 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 482 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 483 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 484 | return; |
| 485 | } |
| 486 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 487 | void DbusPidZone::updateSensors(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 488 | { |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 489 | processSensorInputs</* fanSensorLogging */ false>( |
| 490 | _thermalInputs, std::chrono::high_resolution_clock::now()); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 491 | |
| 492 | return; |
| 493 | } |
| 494 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 495 | void DbusPidZone::initializeCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 496 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 497 | auto nan = std::numeric_limits<double>::quiet_NaN(); |
| 498 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 499 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 500 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 501 | _cachedValuesByName[f] = {nan, nan}; |
| 502 | _cachedFanOutputs[f] = {nan, nan}; |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 503 | |
| 504 | // Start all fans in fail-safe mode. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 505 | markSensorMissing(f); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 508 | for (const auto& t : _thermalInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 509 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 510 | _cachedValuesByName[t] = {nan, nan}; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 511 | |
| 512 | // Start all sensors in fail-safe mode. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 513 | markSensorMissing(t); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 517 | void DbusPidZone::dumpCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 518 | { |
| 519 | std::cerr << "Cache values now: \n"; |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 520 | for (const auto& [name, value] : _cachedValuesByName) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 521 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 522 | 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 Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 534 | void DbusPidZone::processFans(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 535 | { |
| 536 | for (auto& p : _fans) |
| 537 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 538 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 539 | } |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 540 | |
| 541 | if (_redundantWrite) |
| 542 | { |
| 543 | // This is only needed once |
| 544 | _redundantWrite = false; |
| 545 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 548 | void DbusPidZone::processThermals(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 549 | { |
| 550 | for (auto& p : _thermals) |
| 551 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 552 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 556 | Sensor* DbusPidZone::getSensor(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 557 | { |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 558 | return _mgr.getSensor(name); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 561 | bool DbusPidZone::getRedundantWrite(void) const |
| 562 | { |
| 563 | return _redundantWrite; |
| 564 | } |
| 565 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 566 | bool DbusPidZone::manual(bool value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 567 | { |
| 568 | std::cerr << "manual: " << value << std::endl; |
| 569 | setManualMode(value); |
| 570 | return ModeObject::manual(value); |
| 571 | } |
| 572 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 573 | bool DbusPidZone::failSafe() const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 574 | { |
| 575 | return getFailSafeMode(); |
| 576 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 577 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 578 | void DbusPidZone::addPidControlProcess(std::string name, std::string type, |
| 579 | double setpoint, sdbusplus::bus_t& bus, |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 580 | 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 Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 588 | _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 | } |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | bool DbusPidZone::isPidProcessEnabled(std::string name) |
| 609 | { |
| 610 | return _pidsControlProcess[name]->enabled(); |
| 611 | } |
| 612 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 613 | void DbusPidZone::addPidFailSafePercent(std::vector<std::string> inputs, |
| 614 | double percent) |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 615 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 616 | for (const auto& sensorName : inputs) |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 617 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 618 | 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 | } |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 639 | } |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 640 | } |
| 641 | |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 642 | std::string DbusPidZone::leader() const |
| 643 | { |
| 644 | return _maximumSetPointName; |
| 645 | } |
| 646 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 647 | void DbusPidZone::updateThermalPowerDebugInterface( |
| 648 | std::string pidName, std::string leader, double input, double output) |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 649 | { |
| 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 Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 661 | bool DbusPidZone::getAccSetPoint(void) const |
| 662 | { |
| 663 | return _accumulateSetPoint; |
| 664 | } |
| 665 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 666 | } // namespace pid_control |