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