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