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