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" |
James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame^] | 21 | #include "failsafeloggers/failsafe_logger_utility.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | #include "pid/controller.hpp" |
| 23 | #include "pid/ec/pid.hpp" |
| 24 | #include "pid/fancontroller.hpp" |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 25 | #include "pid/stepwisecontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 26 | #include "pid/thermalcontroller.hpp" |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 27 | #include "pid/tuning.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 28 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <chrono> |
| 31 | #include <cstring> |
| 32 | #include <fstream> |
| 33 | #include <iostream> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 34 | #include <memory> |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 35 | #include <sstream> |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 36 | #include <string> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 37 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 38 | using tstamp = std::chrono::high_resolution_clock::time_point; |
| 39 | using namespace std::literals::chrono_literals; |
| 40 | |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 41 | // Enforces minimum duration between events |
| 42 | // Rreturns true if event should be allowed, false if disallowed |
| 43 | bool allowThrottle(const tstamp& now, const std::chrono::seconds& pace) |
| 44 | { |
| 45 | static tstamp then; |
| 46 | static bool first = true; |
| 47 | |
| 48 | if (first) |
| 49 | { |
| 50 | // Special case initialization |
| 51 | then = now; |
| 52 | first = false; |
| 53 | |
| 54 | // Initialization, always allow |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | auto elapsed = now - then; |
| 59 | if (elapsed < pace) |
| 60 | { |
| 61 | // Too soon since last time, disallow |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // It has been long enough, allow |
| 66 | then = now; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | namespace pid_control |
| 71 | { |
| 72 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 73 | double DbusPidZone::getMaxSetPointRequest(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 74 | { |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 75 | return _maximumSetPoint; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 78 | bool DbusPidZone::getManualMode(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 79 | { |
| 80 | return _manualMode; |
| 81 | } |
| 82 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 83 | void DbusPidZone::setManualMode(bool mode) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 84 | { |
| 85 | _manualMode = mode; |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 86 | |
| 87 | // If returning to automatic mode, need to restore PWM from PID loop |
| 88 | if (!mode) |
| 89 | { |
| 90 | _redundantWrite = true; |
| 91 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 94 | bool DbusPidZone::getFailSafeMode(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 95 | { |
| 96 | // If any keys are present at least one sensor is in fail safe mode. |
| 97 | return !_failSafeSensors.empty(); |
| 98 | } |
| 99 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 100 | void DbusPidZone::markSensorMissing(const std::string& name) |
| 101 | { |
| 102 | if (_missingAcceptable.find(name) != _missingAcceptable.end()) |
| 103 | { |
| 104 | // Disallow sensors in MissingIsAcceptable list from causing failsafe |
James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame^] | 105 | outputFailsafeLogWithZone(_zoneId, this->getFailSafeMode(), name, |
| 106 | "The sensor is missing but is acceptable."); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 110 | if (_sensorFailSafePercent[name] == 0) |
| 111 | { |
| 112 | _failSafeSensors[name] = _zoneFailSafePercent; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | _failSafeSensors[name] = _sensorFailSafePercent[name]; |
| 117 | } |
| 118 | |
| 119 | if (debugEnabled) |
| 120 | { |
| 121 | std::cerr << "Sensor " << name << " marked missing\n"; |
| 122 | } |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 125 | int64_t DbusPidZone::getZoneID(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 126 | { |
| 127 | return _zoneId; |
| 128 | } |
| 129 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 130 | void DbusPidZone::addSetPoint(double setPoint, const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 131 | { |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 132 | /* exclude disabled pidloop from _maximumSetPoint calculation*/ |
| 133 | if (!isPidProcessEnabled(name)) |
| 134 | { |
| 135 | return; |
| 136 | } |
| 137 | |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 138 | auto profileName = name; |
| 139 | if (getAccSetPoint()) |
| 140 | { |
| 141 | /* |
| 142 | * If the name of controller is Linear_Temp_CPU0. |
| 143 | * The profile name will be Temp_CPU0. |
| 144 | */ |
| 145 | profileName = name.substr(name.find("_") + 1); |
| 146 | _SetPoints[profileName] += setPoint; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | if (_SetPoints[profileName] < setPoint) |
| 151 | { |
| 152 | _SetPoints[profileName] = setPoint; |
| 153 | } |
| 154 | } |
| 155 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 156 | /* |
| 157 | * if there are multiple thermal controllers with the same |
| 158 | * value, pick the first one in the iterator |
| 159 | */ |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 160 | if (_maximumSetPoint < _SetPoints[profileName]) |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 161 | { |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 162 | _maximumSetPoint = _SetPoints[profileName]; |
| 163 | _maximumSetPointName = profileName; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 164 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 167 | void DbusPidZone::addRPMCeiling(double ceiling) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 168 | { |
| 169 | _RPMCeilings.push_back(ceiling); |
| 170 | } |
| 171 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 172 | void DbusPidZone::clearRPMCeilings(void) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 173 | { |
| 174 | _RPMCeilings.clear(); |
| 175 | } |
| 176 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 177 | void DbusPidZone::clearSetPoints(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 178 | { |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 179 | _SetPoints.clear(); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 180 | _maximumSetPoint = 0; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 181 | _maximumSetPointName.clear(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 184 | double DbusPidZone::getFailSafePercent(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 185 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 186 | std::map<std::string, double>::iterator maxData = std::max_element( |
| 187 | _failSafeSensors.begin(), _failSafeSensors.end(), |
| 188 | [](const std::pair<std::string, double> firstData, |
| 189 | const std::pair<std::string, double> secondData) { |
| 190 | return firstData.second < secondData.second; |
| 191 | }); |
| 192 | |
| 193 | // In dbus/dbusconfiguration.cpp, the default sensor failsafepercent is 0 if |
| 194 | // there is no setting in json. |
| 195 | // Therfore, if the max failsafe duty in _failSafeSensors is 0, set final |
| 196 | // failsafe duty to _zoneFailSafePercent. |
| 197 | if ((*maxData).second == 0) |
| 198 | { |
| 199 | return _zoneFailSafePercent; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | return (*maxData).second; |
| 204 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 207 | double DbusPidZone::getMinThermalSetPoint(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 208 | { |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 209 | return _minThermalOutputSetPt; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 212 | uint64_t DbusPidZone::getCycleIntervalTime(void) const |
| 213 | { |
| 214 | return _cycleTime.cycleIntervalTimeMS; |
| 215 | } |
| 216 | |
| 217 | uint64_t DbusPidZone::getUpdateThermalsCycle(void) const |
| 218 | { |
| 219 | return _cycleTime.updateThermalsTimeMS; |
| 220 | } |
| 221 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 222 | void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 223 | { |
| 224 | _fans.push_back(std::move(pid)); |
| 225 | } |
| 226 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 227 | void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 228 | { |
| 229 | _thermals.push_back(std::move(pid)); |
| 230 | } |
| 231 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 232 | double DbusPidZone::getCachedValue(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 233 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 234 | return _cachedValuesByName.at(name).scaled; |
| 235 | } |
| 236 | |
| 237 | ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name) |
| 238 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 239 | return _cachedValuesByName.at(name); |
| 240 | } |
| 241 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 242 | void DbusPidZone::setOutputCache(std::string_view name, |
| 243 | const ValueCacheEntry& values) |
| 244 | { |
| 245 | _cachedFanOutputs[std::string{name}] = values; |
| 246 | } |
| 247 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 248 | void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 249 | { |
| 250 | _fanInputs.push_back(fan); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 251 | |
| 252 | if (missingAcceptable) |
| 253 | { |
| 254 | _missingAcceptable.emplace(fan); |
| 255 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 258 | void DbusPidZone::addThermalInput(const std::string& therm, |
| 259 | bool missingAcceptable) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 260 | { |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 261 | /* |
| 262 | * One sensor may have stepwise and PID at the same time. |
| 263 | * Searching the sensor name before inserting it to avoid duplicated sensor |
| 264 | * names. |
| 265 | */ |
| 266 | if (std::find(_thermalInputs.begin(), _thermalInputs.end(), therm) == |
| 267 | _thermalInputs.end()) |
| 268 | { |
| 269 | _thermalInputs.push_back(therm); |
| 270 | } |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 271 | |
| 272 | if (missingAcceptable) |
| 273 | { |
| 274 | _missingAcceptable.emplace(therm); |
| 275 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 278 | // Updates desired RPM setpoint from optional text file |
| 279 | // Returns true if rpmValue updated, false if left unchanged |
| 280 | static bool fileParseRpm(const std::string& fileName, double& rpmValue) |
| 281 | { |
| 282 | static constexpr std::chrono::seconds throttlePace{3}; |
| 283 | |
| 284 | std::string errText; |
| 285 | |
| 286 | try |
| 287 | { |
| 288 | std::ifstream ifs; |
| 289 | ifs.open(fileName); |
| 290 | if (ifs) |
| 291 | { |
| 292 | int value; |
| 293 | ifs >> value; |
| 294 | |
| 295 | if (value <= 0) |
| 296 | { |
| 297 | errText = "File content could not be parsed to a number"; |
| 298 | } |
| 299 | else if (value <= 100) |
| 300 | { |
| 301 | errText = "File must contain RPM value, not PWM value"; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | rpmValue = static_cast<double>(value); |
| 306 | return true; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | catch (const std::exception& e) |
| 311 | { |
| 312 | errText = "Exception: "; |
| 313 | errText += e.what(); |
| 314 | } |
| 315 | |
| 316 | // The file is optional, intentionally not an error if file not found |
| 317 | if (!(errText.empty())) |
| 318 | { |
| 319 | tstamp now = std::chrono::high_resolution_clock::now(); |
| 320 | if (allowThrottle(now, throttlePace)) |
| 321 | { |
| 322 | std::cerr << "Unable to read from '" << fileName << "': " << errText |
| 323 | << "\n"; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return false; |
| 328 | } |
| 329 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 330 | void DbusPidZone::determineMaxSetPointRequest(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 331 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 332 | std::vector<double>::iterator result; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 333 | double minThermalThreshold = getMinThermalSetPoint(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 334 | |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 335 | if (_RPMCeilings.size() > 0) |
| 336 | { |
| 337 | result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end()); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 338 | // if Max set point is larger than the lowest ceiling, reset to lowest |
| 339 | // ceiling. |
| 340 | if (*result < _maximumSetPoint) |
| 341 | { |
| 342 | _maximumSetPoint = *result; |
| 343 | // When using lowest ceiling, controller name is ceiling. |
| 344 | _maximumSetPointName = "Ceiling"; |
| 345 | } |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 348 | /* |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 349 | * Combine the maximum SetPoint Name if the controllers have same profile |
| 350 | * name. e.g., PID_BB_INLET_TEMP_C + Stepwise_BB_INLET_TEMP_C. |
| 351 | */ |
| 352 | if (getAccSetPoint()) |
| 353 | { |
| 354 | auto profileName = _maximumSetPointName; |
| 355 | _maximumSetPointName = ""; |
| 356 | |
| 357 | for (auto& p : _thermals) |
| 358 | { |
| 359 | auto controllerID = p->getID(); |
| 360 | auto found = controllerID.find(profileName); |
| 361 | if (found != std::string::npos) |
| 362 | { |
| 363 | if (_maximumSetPointName.empty()) |
| 364 | { |
| 365 | _maximumSetPointName = controllerID; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | _maximumSetPointName += " + " + controllerID; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 376 | * If the maximum RPM setpoint output is below the minimum RPM |
| 377 | * setpoint, set it to the minimum. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 378 | */ |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 379 | if (minThermalThreshold >= _maximumSetPoint) |
| 380 | { |
| 381 | _maximumSetPoint = minThermalThreshold; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 382 | _maximumSetPointName = "Minimum"; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 383 | } |
| 384 | else if (_maximumSetPointName.compare(_maximumSetPointNamePrev)) |
| 385 | { |
| 386 | std::cerr << "PID Zone " << _zoneId << " max SetPoint " |
| 387 | << _maximumSetPoint << " requested by " |
| 388 | << _maximumSetPointName; |
| 389 | for (const auto& sensor : _failSafeSensors) |
| 390 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 391 | if (sensor.first.find("Fan") == std::string::npos) |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 392 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 393 | std::cerr << " " << sensor.first; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | std::cerr << "\n"; |
| 397 | _maximumSetPointNamePrev.assign(_maximumSetPointName); |
| 398 | } |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 399 | if (tuningEnabled) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 400 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 401 | /* |
| 402 | * We received no setpoints from thermal sensors. |
| 403 | * This is a case experienced during tuning where they only specify |
| 404 | * fan sensors and one large fan PID for all the fans. |
| 405 | */ |
| 406 | static constexpr auto setpointpath = "/etc/thermal.d/setpoint"; |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 407 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 408 | fileParseRpm(setpointpath, _maximumSetPoint); |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 409 | |
| 410 | // Allow per-zone setpoint files to override overall setpoint file |
| 411 | std::ostringstream zoneSuffix; |
| 412 | zoneSuffix << ".zone" << _zoneId; |
| 413 | std::string zoneSetpointPath = setpointpath + zoneSuffix.str(); |
| 414 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 415 | fileParseRpm(zoneSetpointPath, _maximumSetPoint); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 416 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 420 | void DbusPidZone::initializeLog(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 421 | { |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 422 | /* Print header for log file: |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 423 | * 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] | 424 | */ |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 425 | |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 426 | _log << "epoch_ms,setpt,requester"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 427 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 428 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 429 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 430 | _log << "," << f << "," << f << "_raw"; |
| 431 | _log << "," << f << "_pwm," << f << "_pwm_raw"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 432 | } |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 433 | for (const auto& t : _thermalInputs) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 434 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 435 | _log << "," << t << "," << t << "_raw"; |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 436 | } |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 437 | |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 438 | _log << ",failsafe"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 439 | _log << std::endl; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 442 | void DbusPidZone::writeLog(const std::string& value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 443 | { |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 444 | _log << value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 445 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * TODO(venture) This is effectively updating the cache and should check if the |
| 449 | * values they're using to update it are new or old, or whatnot. For instance, |
| 450 | * if we haven't heard from the host in X time we need to detect this failure. |
| 451 | * |
| 452 | * I haven't decided if the Sensor should have a lastUpdated method or whether |
| 453 | * that should be for the ReadInterface or etc... |
| 454 | */ |
| 455 | |
| 456 | /** |
| 457 | * We want the PID loop to run with values cached, so this will get all the |
| 458 | * fan tachs for the loop. |
| 459 | */ |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 460 | void DbusPidZone::updateFanTelemetry(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 461 | { |
| 462 | /* TODO(venture): Should I just make _log point to /dev/null when logging |
| 463 | * is disabled? I think it's a waste to try and log things even if the |
| 464 | * data is just being dropped though. |
| 465 | */ |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 466 | const auto now = std::chrono::high_resolution_clock::now(); |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 467 | if (loggingEnabled) |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 468 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 469 | _log << std::chrono::duration_cast<std::chrono::milliseconds>( |
| 470 | now.time_since_epoch()) |
| 471 | .count(); |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 472 | _log << "," << _maximumSetPoint; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 473 | _log << "," << _maximumSetPointName; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 474 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 475 | |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 476 | processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 477 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 478 | if (loggingEnabled) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 479 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 480 | for (const auto& t : _thermalInputs) |
| 481 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 482 | const auto& v = _cachedValuesByName[t]; |
| 483 | _log << "," << v.scaled << "," << v.unscaled; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 484 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 485 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 486 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 487 | return; |
| 488 | } |
| 489 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 490 | void DbusPidZone::updateSensors(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 491 | { |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 492 | processSensorInputs</* fanSensorLogging */ false>( |
| 493 | _thermalInputs, std::chrono::high_resolution_clock::now()); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 494 | |
| 495 | return; |
| 496 | } |
| 497 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 498 | void DbusPidZone::initializeCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 499 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 500 | auto nan = std::numeric_limits<double>::quiet_NaN(); |
| 501 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 502 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 503 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 504 | _cachedValuesByName[f] = {nan, nan}; |
| 505 | _cachedFanOutputs[f] = {nan, nan}; |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 506 | |
| 507 | // Start all fans in fail-safe mode. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 508 | markSensorMissing(f); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 511 | for (const auto& t : _thermalInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 512 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 513 | _cachedValuesByName[t] = {nan, nan}; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 514 | |
| 515 | // Start all sensors in fail-safe mode. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 516 | markSensorMissing(t); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 520 | void DbusPidZone::dumpCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 521 | { |
| 522 | std::cerr << "Cache values now: \n"; |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 523 | for (const auto& [name, value] : _cachedValuesByName) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 524 | { |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 525 | std::cerr << name << ": " << value.scaled << " " << value.unscaled |
| 526 | << "\n"; |
| 527 | } |
| 528 | |
| 529 | std::cerr << "Fan outputs now: \n"; |
| 530 | for (const auto& [name, value] : _cachedFanOutputs) |
| 531 | { |
| 532 | std::cerr << name << ": " << value.scaled << " " << value.unscaled |
| 533 | << "\n"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 537 | void DbusPidZone::processFans(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 538 | { |
| 539 | for (auto& p : _fans) |
| 540 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 541 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 542 | } |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 543 | |
| 544 | if (_redundantWrite) |
| 545 | { |
| 546 | // This is only needed once |
| 547 | _redundantWrite = false; |
| 548 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 551 | void DbusPidZone::processThermals(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 552 | { |
| 553 | for (auto& p : _thermals) |
| 554 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 555 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 559 | Sensor* DbusPidZone::getSensor(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 560 | { |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 561 | return _mgr.getSensor(name); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 562 | } |
| 563 | |
James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame^] | 564 | std::vector<std::string> DbusPidZone::getSensorNames(void) |
| 565 | { |
| 566 | return _thermalInputs; |
| 567 | } |
| 568 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 569 | bool DbusPidZone::getRedundantWrite(void) const |
| 570 | { |
| 571 | return _redundantWrite; |
| 572 | } |
| 573 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 574 | bool DbusPidZone::manual(bool value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 575 | { |
| 576 | std::cerr << "manual: " << value << std::endl; |
| 577 | setManualMode(value); |
| 578 | return ModeObject::manual(value); |
| 579 | } |
| 580 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 581 | bool DbusPidZone::failSafe() const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 582 | { |
| 583 | return getFailSafeMode(); |
| 584 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 585 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 586 | void DbusPidZone::addPidControlProcess(std::string name, std::string type, |
| 587 | double setpoint, sdbusplus::bus_t& bus, |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 588 | std::string objPath, bool defer) |
| 589 | { |
| 590 | _pidsControlProcess[name] = std::make_unique<ProcessObject>( |
| 591 | bus, objPath.c_str(), |
| 592 | defer ? ProcessObject::action::defer_emit |
| 593 | : ProcessObject::action::emit_object_added); |
| 594 | // Default enable setting = true |
| 595 | _pidsControlProcess[name]->enabled(true); |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 596 | _pidsControlProcess[name]->setpoint(setpoint); |
| 597 | |
| 598 | if (type == "temp") |
| 599 | { |
| 600 | _pidsControlProcess[name]->classType("Temperature"); |
| 601 | } |
| 602 | else if (type == "margin") |
| 603 | { |
| 604 | _pidsControlProcess[name]->classType("Margin"); |
| 605 | } |
| 606 | else if (type == "power") |
| 607 | { |
| 608 | _pidsControlProcess[name]->classType("Power"); |
| 609 | } |
| 610 | else if (type == "powersum") |
| 611 | { |
| 612 | _pidsControlProcess[name]->classType("PowerSum"); |
| 613 | } |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | bool DbusPidZone::isPidProcessEnabled(std::string name) |
| 617 | { |
| 618 | return _pidsControlProcess[name]->enabled(); |
| 619 | } |
| 620 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 621 | void DbusPidZone::addPidFailSafePercent(std::vector<std::string> inputs, |
| 622 | double percent) |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 623 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 624 | for (const auto& sensorName : inputs) |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 625 | { |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 626 | if (_sensorFailSafePercent.find(sensorName) != |
| 627 | _sensorFailSafePercent.end()) |
| 628 | { |
| 629 | _sensorFailSafePercent[sensorName] = |
| 630 | std::max(_sensorFailSafePercent[sensorName], percent); |
| 631 | if (debugEnabled) |
| 632 | { |
| 633 | std::cerr << "Sensor " << sensorName |
| 634 | << " failsafe percent updated to " |
| 635 | << _sensorFailSafePercent[sensorName] << "\n"; |
| 636 | } |
| 637 | } |
| 638 | else |
| 639 | { |
| 640 | _sensorFailSafePercent[sensorName] = percent; |
| 641 | if (debugEnabled) |
| 642 | { |
| 643 | std::cerr << "Sensor " << sensorName |
| 644 | << " failsafe percent set to " << percent << "\n"; |
| 645 | } |
| 646 | } |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 647 | } |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 648 | } |
| 649 | |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 650 | std::string DbusPidZone::leader() const |
| 651 | { |
| 652 | return _maximumSetPointName; |
| 653 | } |
| 654 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 655 | void DbusPidZone::updateThermalPowerDebugInterface( |
| 656 | std::string pidName, std::string leader, double input, double output) |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 657 | { |
| 658 | if (leader.empty()) |
| 659 | { |
| 660 | _pidsControlProcess[pidName]->output(output); |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | _pidsControlProcess[pidName]->leader(leader); |
| 665 | _pidsControlProcess[pidName]->input(input); |
| 666 | } |
| 667 | } |
| 668 | |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 669 | bool DbusPidZone::getAccSetPoint(void) const |
| 670 | { |
| 671 | return _accumulateSetPoint; |
| 672 | } |
| 673 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 674 | } // namespace pid_control |