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" |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 21 | #include "pid/zone_interface.hpp" |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 22 | #include "sensors/sensor.hpp" |
| 23 | |
| 24 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 25 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 26 | #include <chrono> |
| 27 | #include <map> |
| 28 | #include <memory> |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 29 | #include <sstream> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 32 | namespace pid_control |
| 33 | { |
| 34 | |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 35 | static void processThermals(std::shared_ptr<ZoneInterface> zone) |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 36 | { |
| 37 | // Get the latest margins. |
| 38 | zone->updateSensors(); |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 39 | // Zero out the set point goals. |
| 40 | zone->clearSetPoints(); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 41 | zone->clearRPMCeilings(); |
| 42 | // Run the margin PIDs. |
| 43 | zone->processThermals(); |
| 44 | // Get the maximum RPM setpoint. |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 45 | zone->determineMaxSetPointRequest(); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 48 | void pidControlLoop(std::shared_ptr<ZoneInterface> zone, |
| 49 | std::shared_ptr<boost::asio::steady_timer> timer, |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 50 | const bool* isCanceling, bool first, uint64_t cycleCnt) |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 51 | { |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 52 | if (*isCanceling) |
| 53 | return; |
| 54 | |
Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 55 | std::chrono::steady_clock::time_point nextTime; |
| 56 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 57 | if (first) |
| 58 | { |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 59 | if (loggingEnabled) |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 60 | { |
| 61 | zone->initializeLog(); |
| 62 | } |
| 63 | |
| 64 | zone->initializeCache(); |
| 65 | processThermals(zone); |
Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 66 | |
| 67 | nextTime = std::chrono::steady_clock::now(); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | nextTime = timer->expiry(); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 74 | uint64_t msPerFanCycle = zone->getCycleIntervalTime(); |
| 75 | |
| 76 | // Push forward the original expiration time of timer, instead of just |
| 77 | // resetting it with expires_after() from now, to make sure the interval |
| 78 | // is of the expected duration, and not stretched out by CPU time taken. |
| 79 | nextTime += std::chrono::milliseconds(msPerFanCycle); |
| 80 | timer->expires_at(nextTime); |
| 81 | timer->async_wait([zone, timer, cycleCnt, isCanceling, msPerFanCycle]( |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 82 | const boost::system::error_code& ec) mutable { |
| 83 | if (ec == boost::asio::error::operation_aborted) |
| 84 | { |
| 85 | return; // timer being canceled, stop loop |
| 86 | } |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 87 | |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 88 | /* |
| 89 | * This should sleep on the conditional wait for the listen thread |
| 90 | * to tell us it's in sync. But then we also need a timeout option |
| 91 | * in case phosphor-hwmon is down, we can go into some weird failure |
| 92 | * more. |
| 93 | * |
| 94 | * Another approach would be to start all sensors in worst-case |
| 95 | * values, and fail-safe mode and then clear out of fail-safe mode |
| 96 | * once we start getting values. Which I think it is a solid |
| 97 | * approach. |
| 98 | * |
| 99 | * For now this runs before it necessarily has any sensor values. |
| 100 | * For the host sensors they start out in fail-safe mode. For the |
| 101 | * fans, they start out as 0 as input and then are adjusted once |
| 102 | * they have values. |
| 103 | * |
| 104 | * If a fan has failed, it's value will be whatever we're told or |
| 105 | * however we retrieve it. This program disregards fan values of 0, |
| 106 | * so any code providing a fan speed can set to 0 on failure and |
| 107 | * that fan value will be effectively ignored. The PID algorithm |
| 108 | * will be unhappy but nothing bad will happen. |
| 109 | * |
| 110 | * TODO(venture): If the fan value is 0 should that loop just be |
| 111 | * skipped? Right now, a 0 value is ignored in |
| 112 | * FanController::inputProc() |
| 113 | */ |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 114 | |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 115 | // Check if we should just go back to sleep. |
| 116 | if (zone->getManualMode()) |
| 117 | { |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 118 | pidControlLoop(zone, timer, isCanceling, false, cycleCnt); |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 119 | return; |
| 120 | } |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 121 | |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 122 | // Get the latest fan speeds. |
| 123 | zone->updateFanTelemetry(); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 124 | |
Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 125 | uint64_t msPerThermalCycle = zone->getUpdateThermalsCycle(); |
| 126 | |
| 127 | // Process thermal cycles at a rate that is less often than fan |
| 128 | // cycles. If thermal time is not an exact multiple of fan time, |
| 129 | // there will be some remainder left over, to keep the timing |
| 130 | // correct, as the intervals are staggered into one another. |
| 131 | if (cycleCnt >= msPerThermalCycle) |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 132 | { |
Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 133 | cycleCnt -= msPerThermalCycle; |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 134 | |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 135 | processThermals(zone); |
| 136 | } |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 137 | |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 138 | // Run the fan PIDs every iteration. |
| 139 | zone->processFans(); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 140 | |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 141 | if (loggingEnabled) |
| 142 | { |
| 143 | std::ostringstream out; |
| 144 | out << "," << zone->getFailSafeMode() << std::endl; |
| 145 | zone->writeLog(out.str()); |
| 146 | } |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 147 | |
Josh Lehan | 9f9a06a | 2022-12-14 10:39:45 -0800 | [diff] [blame] | 148 | // Count how many milliseconds have elapsed, so we can know when |
| 149 | // to perform thermal cycles, in proper ratio with fan cycles. |
| 150 | cycleCnt += msPerFanCycle; |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 151 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 152 | pidControlLoop(zone, timer, isCanceling, false, cycleCnt); |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 153 | }); |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 154 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 155 | |
| 156 | } // namespace pid_control |