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" |
| 26 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <chrono> |
| 29 | #include <cstring> |
| 30 | #include <fstream> |
| 31 | #include <iostream> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 32 | #include <memory> |
| 33 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 34 | using tstamp = std::chrono::high_resolution_clock::time_point; |
| 35 | using namespace std::literals::chrono_literals; |
| 36 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 37 | double PIDZone::getMaxRPMRequest(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 38 | { |
| 39 | return _maximumRPMSetPt; |
| 40 | } |
| 41 | |
| 42 | bool PIDZone::getManualMode(void) const |
| 43 | { |
| 44 | return _manualMode; |
| 45 | } |
| 46 | |
| 47 | void PIDZone::setManualMode(bool mode) |
| 48 | { |
| 49 | _manualMode = mode; |
| 50 | } |
| 51 | |
| 52 | bool PIDZone::getFailSafeMode(void) const |
| 53 | { |
| 54 | // If any keys are present at least one sensor is in fail safe mode. |
| 55 | return !_failSafeSensors.empty(); |
| 56 | } |
| 57 | |
Patrick Venture | 0bbeaf8 | 2018-10-30 18:50:31 -0700 | [diff] [blame] | 58 | int64_t PIDZone::getZoneID(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 59 | { |
| 60 | return _zoneId; |
| 61 | } |
| 62 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 63 | void PIDZone::addRPMSetPoint(double setpoint) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 64 | { |
| 65 | _RPMSetPoints.push_back(setpoint); |
| 66 | } |
| 67 | |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 68 | void PIDZone::addRPMCeiling(double ceiling) |
| 69 | { |
| 70 | _RPMCeilings.push_back(ceiling); |
| 71 | } |
| 72 | |
| 73 | void PIDZone::clearRPMCeilings(void) |
| 74 | { |
| 75 | _RPMCeilings.clear(); |
| 76 | } |
| 77 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 78 | void PIDZone::clearRPMSetPoints(void) |
| 79 | { |
| 80 | _RPMSetPoints.clear(); |
| 81 | } |
| 82 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 83 | double PIDZone::getFailSafePercent(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 84 | { |
| 85 | return _failSafePercent; |
| 86 | } |
| 87 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 88 | double PIDZone::getMinThermalRPMSetpoint(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 89 | { |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 90 | return _minThermalOutputSetPt; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 91 | } |
| 92 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 93 | void PIDZone::addFanPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 94 | { |
| 95 | _fans.push_back(std::move(pid)); |
| 96 | } |
| 97 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 98 | void PIDZone::addThermalPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 99 | { |
| 100 | _thermals.push_back(std::move(pid)); |
| 101 | } |
| 102 | |
| 103 | double PIDZone::getCachedValue(const std::string& name) |
| 104 | { |
| 105 | return _cachedValuesByName.at(name); |
| 106 | } |
| 107 | |
Patrick Venture | c399f6f | 2018-10-30 19:24:47 -0700 | [diff] [blame] | 108 | void PIDZone::addFanInput(const std::string& fan) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 109 | { |
| 110 | _fanInputs.push_back(fan); |
| 111 | } |
| 112 | |
Patrick Venture | c399f6f | 2018-10-30 19:24:47 -0700 | [diff] [blame] | 113 | void PIDZone::addThermalInput(const std::string& therm) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 114 | { |
| 115 | _thermalInputs.push_back(therm); |
| 116 | } |
| 117 | |
| 118 | void PIDZone::determineMaxRPMRequest(void) |
| 119 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 120 | double max = 0; |
| 121 | std::vector<double>::iterator result; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 122 | |
| 123 | if (_RPMSetPoints.size() > 0) |
| 124 | { |
| 125 | result = std::max_element(_RPMSetPoints.begin(), _RPMSetPoints.end()); |
| 126 | max = *result; |
| 127 | } |
| 128 | |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 129 | if (_RPMCeilings.size() > 0) |
| 130 | { |
| 131 | result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end()); |
| 132 | max = std::min(max, *result); |
| 133 | } |
| 134 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 135 | /* |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 136 | * If the maximum RPM setpoint output is below the minimum RPM |
| 137 | * setpoint, set it to the minimum. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 138 | */ |
Patrick Venture | e6a7a2e | 2018-10-30 19:30:02 -0700 | [diff] [blame] | 139 | max = std::max(getMinThermalRPMSetpoint(), max); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 140 | |
| 141 | #ifdef __TUNING_LOGGING__ |
| 142 | /* |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 143 | * We received no setpoints from thermal sensors. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 144 | * This is a case experienced during tuning where they only specify |
| 145 | * fan sensors and one large fan PID for all the fans. |
| 146 | */ |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 147 | static constexpr auto setpointpath = "/etc/thermal.d/setpoint"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 148 | try |
| 149 | { |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 150 | std::ifstream ifs; |
| 151 | ifs.open(setpointpath); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 152 | if (ifs.good()) |
| 153 | { |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 154 | int value; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 155 | ifs >> value; |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 156 | |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 157 | /* expecting RPM setpoint, not pwm% */ |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 158 | max = static_cast<double>(value); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | catch (const std::exception& e) |
| 162 | { |
| 163 | /* This exception is uninteresting. */ |
| 164 | std::cerr << "Unable to read from '" << setpointpath << "'\n"; |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | _maximumRPMSetPt = max; |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | #ifdef __TUNING_LOGGING__ |
| 173 | void PIDZone::initializeLog(void) |
| 174 | { |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 175 | /* Print header for log file: |
| 176 | * epoch_ms,setpt,fan1,fan2,fanN,sensor1,sensor2,sensorN,failsafe |
| 177 | */ |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 178 | |
| 179 | _log << "epoch_ms,setpt"; |
| 180 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 181 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 182 | { |
| 183 | _log << "," << f; |
| 184 | } |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 185 | for (const auto& t : _thermalInputs) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 186 | { |
| 187 | _log << "," << t; |
| 188 | } |
| 189 | _log << ",failsafe"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 190 | _log << std::endl; |
| 191 | |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | std::ofstream& PIDZone::getLogHandle(void) |
| 196 | { |
| 197 | return _log; |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | /* |
| 202 | * TODO(venture) This is effectively updating the cache and should check if the |
| 203 | * values they're using to update it are new or old, or whatnot. For instance, |
| 204 | * if we haven't heard from the host in X time we need to detect this failure. |
| 205 | * |
| 206 | * I haven't decided if the Sensor should have a lastUpdated method or whether |
| 207 | * that should be for the ReadInterface or etc... |
| 208 | */ |
| 209 | |
| 210 | /** |
| 211 | * We want the PID loop to run with values cached, so this will get all the |
| 212 | * fan tachs for the loop. |
| 213 | */ |
| 214 | void PIDZone::updateFanTelemetry(void) |
| 215 | { |
| 216 | /* TODO(venture): Should I just make _log point to /dev/null when logging |
| 217 | * is disabled? I think it's a waste to try and log things even if the |
| 218 | * data is just being dropped though. |
| 219 | */ |
| 220 | #ifdef __TUNING_LOGGING__ |
| 221 | tstamp now = std::chrono::high_resolution_clock::now(); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 222 | _log << std::chrono::duration_cast<std::chrono::milliseconds>( |
| 223 | now.time_since_epoch()) |
| 224 | .count(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 225 | _log << "," << _maximumRPMSetPt; |
| 226 | #endif |
| 227 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 228 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 229 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 230 | auto sensor = _mgr.getSensor(f); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 231 | ReadReturn r = sensor->read(); |
| 232 | _cachedValuesByName[f] = r.value; |
| 233 | |
| 234 | /* |
| 235 | * TODO(venture): We should check when these were last read. |
| 236 | * However, these are the fans, so if I'm not getting updated values |
| 237 | * for them... what should I do? |
| 238 | */ |
| 239 | #ifdef __TUNING_LOGGING__ |
| 240 | _log << "," << r.value; |
| 241 | #endif |
| 242 | } |
| 243 | |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 244 | #ifdef __TUNING_LOGGING__ |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 245 | for (const auto& t : _thermalInputs) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 246 | { |
| 247 | _log << "," << _cachedValuesByName[t]; |
| 248 | } |
| 249 | #endif |
| 250 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | |
| 254 | void PIDZone::updateSensors(void) |
| 255 | { |
| 256 | using namespace std::chrono; |
| 257 | /* margin and temp are stored as temp */ |
| 258 | tstamp now = high_resolution_clock::now(); |
| 259 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 260 | for (const auto& t : _thermalInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 261 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 262 | auto sensor = _mgr.getSensor(t); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 263 | ReadReturn r = sensor->read(); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 264 | int64_t timeout = sensor->getTimeout(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 265 | |
| 266 | _cachedValuesByName[t] = r.value; |
| 267 | tstamp then = r.updated; |
| 268 | |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 269 | auto duration = duration_cast<std::chrono::seconds>(now - then).count(); |
| 270 | auto period = std::chrono::seconds(timeout).count(); |
| 271 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 272 | if (sensor->getFailed()) |
| 273 | { |
| 274 | _failSafeSensors.insert(t); |
| 275 | } |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 276 | else if (timeout != 0 && duration >= period) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 277 | { |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 278 | // std::cerr << "Entering fail safe mode.\n"; |
| 279 | _failSafeSensors.insert(t); |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | // Check if it's in there: remove it. |
| 284 | auto kt = _failSafeSensors.find(t); |
| 285 | if (kt != _failSafeSensors.end()) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 286 | { |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 287 | _failSafeSensors.erase(kt); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | void PIDZone::initializeCache(void) |
| 296 | { |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 297 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 298 | { |
| 299 | _cachedValuesByName[f] = 0; |
| 300 | } |
| 301 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 302 | for (const auto& t : _thermalInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 303 | { |
| 304 | _cachedValuesByName[t] = 0; |
| 305 | |
| 306 | // Start all sensors in fail-safe mode. |
| 307 | _failSafeSensors.insert(t); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void PIDZone::dumpCache(void) |
| 312 | { |
| 313 | std::cerr << "Cache values now: \n"; |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 314 | for (const auto& k : _cachedValuesByName) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 315 | { |
| 316 | std::cerr << k.first << ": " << k.second << "\n"; |
| 317 | } |
| 318 | } |
| 319 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 320 | void PIDZone::processFans(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 321 | { |
| 322 | for (auto& p : _fans) |
| 323 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 324 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 328 | void PIDZone::processThermals(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 329 | { |
| 330 | for (auto& p : _thermals) |
| 331 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 332 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Patrick Venture | 2d8e785 | 2018-10-30 19:14:07 -0700 | [diff] [blame] | 336 | Sensor* PIDZone::getSensor(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 337 | { |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 338 | return _mgr.getSensor(name); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | bool PIDZone::manual(bool value) |
| 342 | { |
| 343 | std::cerr << "manual: " << value << std::endl; |
| 344 | setManualMode(value); |
| 345 | return ModeObject::manual(value); |
| 346 | } |
| 347 | |
| 348 | bool PIDZone::failSafe() const |
| 349 | { |
| 350 | return getFailSafeMode(); |
| 351 | } |