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 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 99 | int64_t DbusPidZone::getZoneID(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 100 | { |
| 101 | return _zoneId; |
| 102 | } |
| 103 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 104 | void DbusPidZone::addSetPoint(double setpoint) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 105 | { |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 106 | _SetPoints.push_back(setpoint); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 109 | void DbusPidZone::addRPMCeiling(double ceiling) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 110 | { |
| 111 | _RPMCeilings.push_back(ceiling); |
| 112 | } |
| 113 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 114 | void DbusPidZone::clearRPMCeilings(void) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 115 | { |
| 116 | _RPMCeilings.clear(); |
| 117 | } |
| 118 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 119 | void DbusPidZone::clearSetPoints(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 120 | { |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 121 | _SetPoints.clear(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 124 | double DbusPidZone::getFailSafePercent(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 125 | { |
| 126 | return _failSafePercent; |
| 127 | } |
| 128 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 129 | double DbusPidZone::getMinThermalSetpoint(void) const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 130 | { |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 131 | return _minThermalOutputSetPt; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 134 | void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 135 | { |
| 136 | _fans.push_back(std::move(pid)); |
| 137 | } |
| 138 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 139 | void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 140 | { |
| 141 | _thermals.push_back(std::move(pid)); |
| 142 | } |
| 143 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 144 | double DbusPidZone::getCachedValue(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 145 | { |
| 146 | return _cachedValuesByName.at(name); |
| 147 | } |
| 148 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 149 | void DbusPidZone::addFanInput(const std::string& fan) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 150 | { |
| 151 | _fanInputs.push_back(fan); |
| 152 | } |
| 153 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 154 | void DbusPidZone::addThermalInput(const std::string& therm) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 155 | { |
| 156 | _thermalInputs.push_back(therm); |
| 157 | } |
| 158 | |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 159 | // Updates desired RPM setpoint from optional text file |
| 160 | // Returns true if rpmValue updated, false if left unchanged |
| 161 | static bool fileParseRpm(const std::string& fileName, double& rpmValue) |
| 162 | { |
| 163 | static constexpr std::chrono::seconds throttlePace{3}; |
| 164 | |
| 165 | std::string errText; |
| 166 | |
| 167 | try |
| 168 | { |
| 169 | std::ifstream ifs; |
| 170 | ifs.open(fileName); |
| 171 | if (ifs) |
| 172 | { |
| 173 | int value; |
| 174 | ifs >> value; |
| 175 | |
| 176 | if (value <= 0) |
| 177 | { |
| 178 | errText = "File content could not be parsed to a number"; |
| 179 | } |
| 180 | else if (value <= 100) |
| 181 | { |
| 182 | errText = "File must contain RPM value, not PWM value"; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | rpmValue = static_cast<double>(value); |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | catch (const std::exception& e) |
| 192 | { |
| 193 | errText = "Exception: "; |
| 194 | errText += e.what(); |
| 195 | } |
| 196 | |
| 197 | // The file is optional, intentionally not an error if file not found |
| 198 | if (!(errText.empty())) |
| 199 | { |
| 200 | tstamp now = std::chrono::high_resolution_clock::now(); |
| 201 | if (allowThrottle(now, throttlePace)) |
| 202 | { |
| 203 | std::cerr << "Unable to read from '" << fileName << "': " << errText |
| 204 | << "\n"; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 211 | void DbusPidZone::determineMaxSetPointRequest(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 212 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 213 | double max = 0; |
| 214 | std::vector<double>::iterator result; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 215 | |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 216 | if (_SetPoints.size() > 0) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 217 | { |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 218 | result = std::max_element(_SetPoints.begin(), _SetPoints.end()); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 219 | max = *result; |
| 220 | } |
| 221 | |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 222 | if (_RPMCeilings.size() > 0) |
| 223 | { |
| 224 | result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end()); |
| 225 | max = std::min(max, *result); |
| 226 | } |
| 227 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 228 | /* |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 229 | * If the maximum RPM setpoint output is below the minimum RPM |
| 230 | * setpoint, set it to the minimum. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 231 | */ |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 232 | max = std::max(getMinThermalSetpoint(), max); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 233 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 234 | if (tuningEnabled) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 235 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 236 | /* |
| 237 | * We received no setpoints from thermal sensors. |
| 238 | * This is a case experienced during tuning where they only specify |
| 239 | * fan sensors and one large fan PID for all the fans. |
| 240 | */ |
| 241 | static constexpr auto setpointpath = "/etc/thermal.d/setpoint"; |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 242 | |
Josh Lehan | 55ccad6 | 2020-09-20 23:57:49 -0700 | [diff] [blame] | 243 | fileParseRpm(setpointpath, max); |
| 244 | |
| 245 | // Allow per-zone setpoint files to override overall setpoint file |
| 246 | std::ostringstream zoneSuffix; |
| 247 | zoneSuffix << ".zone" << _zoneId; |
| 248 | std::string zoneSetpointPath = setpointpath + zoneSuffix.str(); |
| 249 | |
| 250 | fileParseRpm(zoneSetpointPath, max); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 251 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 252 | |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 253 | _maximumSetPoint = max; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 257 | void DbusPidZone::initializeLog(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 258 | { |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 259 | /* Print header for log file: |
| 260 | * epoch_ms,setpt,fan1,fan2,fanN,sensor1,sensor2,sensorN,failsafe |
| 261 | */ |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 262 | |
| 263 | _log << "epoch_ms,setpt"; |
| 264 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 265 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 266 | { |
| 267 | _log << "," << f; |
| 268 | } |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 269 | for (const auto& t : _thermalInputs) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 270 | { |
| 271 | _log << "," << t; |
| 272 | } |
| 273 | _log << ",failsafe"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 274 | _log << std::endl; |
| 275 | |
| 276 | return; |
| 277 | } |
| 278 | |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 279 | void DbusPidZone::writeLog(const std::string& value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 280 | { |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 281 | _log << value; |
| 282 | return; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 283 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * TODO(venture) This is effectively updating the cache and should check if the |
| 287 | * values they're using to update it are new or old, or whatnot. For instance, |
| 288 | * if we haven't heard from the host in X time we need to detect this failure. |
| 289 | * |
| 290 | * I haven't decided if the Sensor should have a lastUpdated method or whether |
| 291 | * that should be for the ReadInterface or etc... |
| 292 | */ |
| 293 | |
| 294 | /** |
| 295 | * We want the PID loop to run with values cached, so this will get all the |
| 296 | * fan tachs for the loop. |
| 297 | */ |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 298 | void DbusPidZone::updateFanTelemetry(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 299 | { |
| 300 | /* TODO(venture): Should I just make _log point to /dev/null when logging |
| 301 | * is disabled? I think it's a waste to try and log things even if the |
| 302 | * data is just being dropped though. |
| 303 | */ |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 304 | tstamp now = std::chrono::high_resolution_clock::now(); |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 305 | if (loggingEnabled) |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 306 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 307 | _log << std::chrono::duration_cast<std::chrono::milliseconds>( |
| 308 | now.time_since_epoch()) |
| 309 | .count(); |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 310 | _log << "," << _maximumSetPoint; |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 311 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 312 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 313 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 314 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 315 | auto sensor = _mgr.getSensor(f); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 316 | ReadReturn r = sensor->read(); |
| 317 | _cachedValuesByName[f] = r.value; |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 318 | int64_t timeout = sensor->getTimeout(); |
| 319 | tstamp then = r.updated; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 320 | |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 321 | auto duration = |
| 322 | std::chrono::duration_cast<std::chrono::seconds>(now - then) |
| 323 | .count(); |
| 324 | auto period = std::chrono::seconds(timeout).count(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 325 | /* |
| 326 | * TODO(venture): We should check when these were last read. |
| 327 | * However, these are the fans, so if I'm not getting updated values |
| 328 | * for them... what should I do? |
| 329 | */ |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 330 | if (loggingEnabled) |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 331 | { |
| 332 | _log << "," << r.value; |
| 333 | } |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 334 | |
| 335 | // check if fan fail. |
| 336 | if (sensor->getFailed()) |
| 337 | { |
| 338 | _failSafeSensors.insert(f); |
| 339 | } |
| 340 | else if (timeout != 0 && duration >= period) |
| 341 | { |
| 342 | _failSafeSensors.insert(f); |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | // Check if it's in there: remove it. |
| 347 | auto kt = _failSafeSensors.find(f); |
| 348 | if (kt != _failSafeSensors.end()) |
| 349 | { |
| 350 | _failSafeSensors.erase(kt); |
| 351 | } |
| 352 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 355 | if (loggingEnabled) |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 356 | { |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 357 | for (const auto& t : _thermalInputs) |
| 358 | { |
| 359 | _log << "," << _cachedValuesByName[t]; |
| 360 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 361 | } |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 362 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 366 | void DbusPidZone::updateSensors(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 367 | { |
| 368 | using namespace std::chrono; |
| 369 | /* margin and temp are stored as temp */ |
| 370 | tstamp now = high_resolution_clock::now(); |
| 371 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 372 | for (const auto& t : _thermalInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 373 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 374 | auto sensor = _mgr.getSensor(t); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 375 | ReadReturn r = sensor->read(); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 376 | int64_t timeout = sensor->getTimeout(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 377 | |
| 378 | _cachedValuesByName[t] = r.value; |
| 379 | tstamp then = r.updated; |
| 380 | |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 381 | auto duration = duration_cast<std::chrono::seconds>(now - then).count(); |
| 382 | auto period = std::chrono::seconds(timeout).count(); |
| 383 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 384 | if (sensor->getFailed()) |
| 385 | { |
| 386 | _failSafeSensors.insert(t); |
| 387 | } |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 388 | else if (timeout != 0 && duration >= period) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 389 | { |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 390 | // std::cerr << "Entering fail safe mode.\n"; |
| 391 | _failSafeSensors.insert(t); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | // Check if it's in there: remove it. |
| 396 | auto kt = _failSafeSensors.find(t); |
| 397 | if (kt != _failSafeSensors.end()) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 398 | { |
James Feist | 473d68d | 2019-02-25 09:19:13 -0800 | [diff] [blame] | 399 | _failSafeSensors.erase(kt); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return; |
| 405 | } |
| 406 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 407 | void DbusPidZone::initializeCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 408 | { |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 409 | for (const auto& f : _fanInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 410 | { |
| 411 | _cachedValuesByName[f] = 0; |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 412 | |
| 413 | // Start all fans in fail-safe mode. |
| 414 | _failSafeSensors.insert(f); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 417 | for (const auto& t : _thermalInputs) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 418 | { |
| 419 | _cachedValuesByName[t] = 0; |
| 420 | |
| 421 | // Start all sensors in fail-safe mode. |
| 422 | _failSafeSensors.insert(t); |
| 423 | } |
| 424 | } |
| 425 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 426 | void DbusPidZone::dumpCache(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 427 | { |
| 428 | std::cerr << "Cache values now: \n"; |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 429 | for (const auto& [name, value] : _cachedValuesByName) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 430 | { |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 431 | std::cerr << name << ": " << value << "\n"; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 435 | void DbusPidZone::processFans(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 436 | { |
| 437 | for (auto& p : _fans) |
| 438 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 439 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 440 | } |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 441 | |
| 442 | if (_redundantWrite) |
| 443 | { |
| 444 | // This is only needed once |
| 445 | _redundantWrite = false; |
| 446 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 449 | void DbusPidZone::processThermals(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 450 | { |
| 451 | for (auto& p : _thermals) |
| 452 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 453 | p->process(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 457 | Sensor* DbusPidZone::getSensor(const std::string& name) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 458 | { |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 459 | return _mgr.getSensor(name); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 462 | bool DbusPidZone::getRedundantWrite(void) const |
| 463 | { |
| 464 | return _redundantWrite; |
| 465 | } |
| 466 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 467 | bool DbusPidZone::manual(bool value) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 468 | { |
| 469 | std::cerr << "manual: " << value << std::endl; |
| 470 | setManualMode(value); |
| 471 | return ModeObject::manual(value); |
| 472 | } |
| 473 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 474 | bool DbusPidZone::failSafe() const |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 475 | { |
| 476 | return getFailSafeMode(); |
| 477 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 478 | |
| 479 | } // namespace pid_control |