James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [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 | #include "pidloop.hpp" |
| 18 | |
| 19 | #include "pid/pidcontroller.hpp" |
| 20 | #include "pid/tuning.hpp" |
| 21 | #include "sensors/sensor.hpp" |
| 22 | |
| 23 | #include <boost/asio/steady_timer.hpp> |
| 24 | #include <chrono> |
| 25 | #include <map> |
| 26 | #include <memory> |
| 27 | #include <thread> |
| 28 | #include <vector> |
| 29 | |
| 30 | static void processThermals(PIDZone* zone) |
| 31 | { |
| 32 | // Get the latest margins. |
| 33 | zone->updateSensors(); |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 34 | // Zero out the set point goals. |
| 35 | zone->clearSetPoints(); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 36 | zone->clearRPMCeilings(); |
| 37 | // Run the margin PIDs. |
| 38 | zone->processThermals(); |
| 39 | // Get the maximum RPM setpoint. |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 40 | zone->determineMaxSetPointRequest(); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void pidControlLoop(PIDZone* zone, boost::asio::steady_timer& timer, bool first, |
| 44 | int ms100cnt) |
| 45 | { |
| 46 | if (first) |
| 47 | { |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 48 | if (loggingEnabled) |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 49 | { |
| 50 | zone->initializeLog(); |
| 51 | } |
| 52 | |
| 53 | zone->initializeCache(); |
| 54 | processThermals(zone); |
| 55 | } |
| 56 | |
| 57 | timer.expires_after(std::chrono::milliseconds(100)); |
| 58 | timer.async_wait( |
| 59 | [zone, &timer, ms100cnt](const boost::system::error_code& ec) mutable { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 60 | if (ec == boost::asio::error::operation_aborted) |
| 61 | { |
| 62 | return; // timer being canceled, stop loop |
| 63 | } |
| 64 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 65 | /* |
| 66 | * This should sleep on the conditional wait for the listen thread |
| 67 | * to tell us it's in sync. But then we also need a timeout option |
| 68 | * in case phosphor-hwmon is down, we can go into some weird failure |
| 69 | * more. |
| 70 | * |
| 71 | * Another approach would be to start all sensors in worst-case |
| 72 | * values, and fail-safe mode and then clear out of fail-safe mode |
| 73 | * once we start getting values. Which I think it is a solid |
| 74 | * approach. |
| 75 | * |
| 76 | * For now this runs before it necessarily has any sensor values. |
| 77 | * For the host sensors they start out in fail-safe mode. For the |
| 78 | * fans, they start out as 0 as input and then are adjusted once |
| 79 | * they have values. |
| 80 | * |
| 81 | * If a fan has failed, it's value will be whatever we're told or |
| 82 | * however we retrieve it. This program disregards fan values of 0, |
| 83 | * so any code providing a fan speed can set to 0 on failure and |
| 84 | * that fan value will be effectively ignored. The PID algorithm |
| 85 | * will be unhappy but nothing bad will happen. |
| 86 | * |
| 87 | * TODO(venture): If the fan value is 0 should that loop just be |
| 88 | * skipped? Right now, a 0 value is ignored in |
| 89 | * FanController::inputProc() |
| 90 | */ |
| 91 | |
| 92 | // Check if we should just go back to sleep. |
| 93 | if (zone->getManualMode()) |
| 94 | { |
| 95 | pidControlLoop(zone, timer, false, ms100cnt); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Get the latest fan speeds. |
| 100 | zone->updateFanTelemetry(); |
| 101 | |
| 102 | if (10 <= ms100cnt) |
| 103 | { |
| 104 | ms100cnt = 0; |
| 105 | |
| 106 | processThermals(zone); |
| 107 | } |
| 108 | |
| 109 | // Run the fan PIDs every iteration. |
| 110 | zone->processFans(); |
| 111 | |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 112 | if (loggingEnabled) |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 113 | { |
| 114 | zone->getLogHandle() << "," << zone->getFailSafeMode(); |
| 115 | zone->getLogHandle() << std::endl; |
| 116 | } |
| 117 | |
| 118 | ms100cnt += 1; |
| 119 | |
| 120 | pidControlLoop(zone, timer, false, ms100cnt); |
| 121 | }); |
| 122 | } |