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