blob: 9d30798c7040a0c38f77b40ac7091e07303c9c90 [file] [log] [blame]
James Feistce6a3f32019-03-12 11:20:16 -07001/**
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 Venture7a98c192020-08-12 08:35:16 -070021#include "pid/zone_interface.hpp"
James Feistce6a3f32019-03-12 11:20:16 -070022#include "sensors/sensor.hpp"
23
24#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025
James Feistce6a3f32019-03-12 11:20:16 -070026#include <chrono>
27#include <map>
28#include <memory>
Patrick Venture7a98c192020-08-12 08:35:16 -070029#include <sstream>
James Feistce6a3f32019-03-12 11:20:16 -070030#include <vector>
31
Patrick Venturea0764872020-08-08 07:48:43 -070032namespace pid_control
33{
34
Ed Tanousd2768c52025-06-26 11:42:57 -070035static void processThermals(const std::shared_ptr<ZoneInterface>& zone)
James Feistce6a3f32019-03-12 11:20:16 -070036{
37 // Get the latest margins.
38 zone->updateSensors();
Patrick Venture9bbf3332019-07-16 10:50:37 -070039 // Zero out the set point goals.
40 zone->clearSetPoints();
James Feistce6a3f32019-03-12 11:20:16 -070041 zone->clearRPMCeilings();
42 // Run the margin PIDs.
43 zone->processThermals();
44 // Get the maximum RPM setpoint.
Patrick Venturef7a2dd52019-07-16 14:31:13 -070045 zone->determineMaxSetPointRequest();
James Feistce6a3f32019-03-12 11:20:16 -070046}
47
Ed Tanousd2768c52025-06-26 11:42:57 -070048void pidControlLoop(const std::shared_ptr<ZoneInterface>& zone,
49 const std::shared_ptr<boost::asio::steady_timer>& timer,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080050 const bool* isCanceling, bool first, uint64_t cycleCnt)
James Feistce6a3f32019-03-12 11:20:16 -070051{
Hao Jiangb6a0b892021-02-21 18:00:45 -080052 if (*isCanceling)
Ed Tanousd2768c52025-06-26 11:42:57 -070053 {
Hao Jiangb6a0b892021-02-21 18:00:45 -080054 return;
Ed Tanousd2768c52025-06-26 11:42:57 -070055 }
Hao Jiangb6a0b892021-02-21 18:00:45 -080056
Josh Lehan9f9a06a2022-12-14 10:39:45 -080057 std::chrono::steady_clock::time_point nextTime;
58
James Feistce6a3f32019-03-12 11:20:16 -070059 if (first)
60 {
Patrick Venturede79ee02019-05-08 14:50:00 -070061 if (loggingEnabled)
James Feistce6a3f32019-03-12 11:20:16 -070062 {
63 zone->initializeLog();
64 }
65
66 zone->initializeCache();
67 processThermals(zone);
Josh Lehan9f9a06a2022-12-14 10:39:45 -080068
69 nextTime = std::chrono::steady_clock::now();
70 }
71 else
72 {
73 nextTime = timer->expiry();
James Feistce6a3f32019-03-12 11:20:16 -070074 }
75
Josh Lehan9f9a06a2022-12-14 10:39:45 -080076 uint64_t msPerFanCycle = zone->getCycleIntervalTime();
77
78 // Push forward the original expiration time of timer, instead of just
79 // resetting it with expires_after() from now, to make sure the interval
80 // is of the expected duration, and not stretched out by CPU time taken.
81 nextTime += std::chrono::milliseconds(msPerFanCycle);
82 timer->expires_at(nextTime);
83 timer->async_wait([zone, timer, cycleCnt, isCanceling, msPerFanCycle](
Hao Jiangb6a0b892021-02-21 18:00:45 -080084 const boost::system::error_code& ec) mutable {
85 if (ec == boost::asio::error::operation_aborted)
86 {
87 return; // timer being canceled, stop loop
88 }
James Feist1fe08952019-05-07 09:17:16 -070089
Hao Jiangb6a0b892021-02-21 18:00:45 -080090 /*
91 * This should sleep on the conditional wait for the listen thread
92 * to tell us it's in sync. But then we also need a timeout option
93 * in case phosphor-hwmon is down, we can go into some weird failure
94 * more.
95 *
96 * Another approach would be to start all sensors in worst-case
97 * values, and fail-safe mode and then clear out of fail-safe mode
98 * once we start getting values. Which I think it is a solid
99 * approach.
100 *
101 * For now this runs before it necessarily has any sensor values.
102 * For the host sensors they start out in fail-safe mode. For the
103 * fans, they start out as 0 as input and then are adjusted once
104 * they have values.
105 *
106 * If a fan has failed, it's value will be whatever we're told or
107 * however we retrieve it. This program disregards fan values of 0,
108 * so any code providing a fan speed can set to 0 on failure and
109 * that fan value will be effectively ignored. The PID algorithm
110 * will be unhappy but nothing bad will happen.
111 *
112 * TODO(venture): If the fan value is 0 should that loop just be
113 * skipped? Right now, a 0 value is ignored in
114 * FanController::inputProc()
115 */
James Feistce6a3f32019-03-12 11:20:16 -0700116
Hao Jiangb6a0b892021-02-21 18:00:45 -0800117 // Check if we should just go back to sleep.
118 if (zone->getManualMode())
119 {
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800120 pidControlLoop(zone, timer, isCanceling, false, cycleCnt);
Hao Jiangb6a0b892021-02-21 18:00:45 -0800121 return;
122 }
James Feistce6a3f32019-03-12 11:20:16 -0700123
Hao Jiangb6a0b892021-02-21 18:00:45 -0800124 // Get the latest fan speeds.
125 zone->updateFanTelemetry();
James Feistce6a3f32019-03-12 11:20:16 -0700126
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800127 uint64_t msPerThermalCycle = zone->getUpdateThermalsCycle();
128
129 // Process thermal cycles at a rate that is less often than fan
130 // cycles. If thermal time is not an exact multiple of fan time,
131 // there will be some remainder left over, to keep the timing
132 // correct, as the intervals are staggered into one another.
133 if (cycleCnt >= msPerThermalCycle)
Hao Jiangb6a0b892021-02-21 18:00:45 -0800134 {
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800135 cycleCnt -= msPerThermalCycle;
James Feistce6a3f32019-03-12 11:20:16 -0700136
Hao Jiangb6a0b892021-02-21 18:00:45 -0800137 processThermals(zone);
138 }
James Feistce6a3f32019-03-12 11:20:16 -0700139
Hao Jiangb6a0b892021-02-21 18:00:45 -0800140 // Run the fan PIDs every iteration.
141 zone->processFans();
James Feistce6a3f32019-03-12 11:20:16 -0700142
Hao Jiangb6a0b892021-02-21 18:00:45 -0800143 if (loggingEnabled)
144 {
145 std::ostringstream out;
146 out << "," << zone->getFailSafeMode() << std::endl;
147 zone->writeLog(out.str());
148 }
James Feistce6a3f32019-03-12 11:20:16 -0700149
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800150 // Count how many milliseconds have elapsed, so we can know when
151 // to perform thermal cycles, in proper ratio with fan cycles.
152 cycleCnt += msPerFanCycle;
James Feistce6a3f32019-03-12 11:20:16 -0700153
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800154 pidControlLoop(zone, timer, isCanceling, false, cycleCnt);
Hao Jiangb6a0b892021-02-21 18:00:45 -0800155 });
James Feistce6a3f32019-03-12 11:20:16 -0700156}
Patrick Venturea0764872020-08-08 07:48:43 -0700157
158} // namespace pid_control