blob: 0ccf35c17e03a30c7837cec2b7fba57aa9eadb60 [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 <thread>
31#include <vector>
32
Patrick Venturea0764872020-08-08 07:48:43 -070033namespace pid_control
34{
35
Johnathan Mantey5301aae2020-09-28 11:06:58 -070036static void processThermals(std::shared_ptr<ZoneInterface> zone)
James Feistce6a3f32019-03-12 11:20:16 -070037{
38 // Get the latest margins.
39 zone->updateSensors();
Patrick Venture9bbf3332019-07-16 10:50:37 -070040 // Zero out the set point goals.
41 zone->clearSetPoints();
James Feistce6a3f32019-03-12 11:20:16 -070042 zone->clearRPMCeilings();
43 // Run the margin PIDs.
44 zone->processThermals();
45 // Get the maximum RPM setpoint.
Patrick Venturef7a2dd52019-07-16 14:31:13 -070046 zone->determineMaxSetPointRequest();
James Feistce6a3f32019-03-12 11:20:16 -070047}
48
Johnathan Mantey5301aae2020-09-28 11:06:58 -070049void pidControlLoop(std::shared_ptr<ZoneInterface> zone,
50 std::shared_ptr<boost::asio::steady_timer> timer,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080051 const bool* isCanceling, bool first, uint64_t cycleCnt)
James Feistce6a3f32019-03-12 11:20:16 -070052{
Hao Jiangb6a0b892021-02-21 18:00:45 -080053 if (*isCanceling)
54 return;
55
Josh Lehan9f9a06a2022-12-14 10:39:45 -080056 std::chrono::steady_clock::time_point nextTime;
57
James Feistce6a3f32019-03-12 11:20:16 -070058 if (first)
59 {
Patrick Venturede79ee02019-05-08 14:50:00 -070060 if (loggingEnabled)
James Feistce6a3f32019-03-12 11:20:16 -070061 {
62 zone->initializeLog();
63 }
64
65 zone->initializeCache();
66 processThermals(zone);
Josh Lehan9f9a06a2022-12-14 10:39:45 -080067
68 nextTime = std::chrono::steady_clock::now();
69 }
70 else
71 {
72 nextTime = timer->expiry();
James Feistce6a3f32019-03-12 11:20:16 -070073 }
74
Josh Lehan9f9a06a2022-12-14 10:39:45 -080075 uint64_t msPerFanCycle = zone->getCycleIntervalTime();
76
77 // Push forward the original expiration time of timer, instead of just
78 // resetting it with expires_after() from now, to make sure the interval
79 // is of the expected duration, and not stretched out by CPU time taken.
80 nextTime += std::chrono::milliseconds(msPerFanCycle);
81 timer->expires_at(nextTime);
82 timer->async_wait([zone, timer, cycleCnt, isCanceling, msPerFanCycle](
Hao Jiangb6a0b892021-02-21 18:00:45 -080083 const boost::system::error_code& ec) mutable {
84 if (ec == boost::asio::error::operation_aborted)
85 {
86 return; // timer being canceled, stop loop
87 }
James Feist1fe08952019-05-07 09:17:16 -070088
Hao Jiangb6a0b892021-02-21 18:00:45 -080089 /*
90 * This should sleep on the conditional wait for the listen thread
91 * to tell us it's in sync. But then we also need a timeout option
92 * in case phosphor-hwmon is down, we can go into some weird failure
93 * more.
94 *
95 * Another approach would be to start all sensors in worst-case
96 * values, and fail-safe mode and then clear out of fail-safe mode
97 * once we start getting values. Which I think it is a solid
98 * approach.
99 *
100 * For now this runs before it necessarily has any sensor values.
101 * For the host sensors they start out in fail-safe mode. For the
102 * fans, they start out as 0 as input and then are adjusted once
103 * they have values.
104 *
105 * If a fan has failed, it's value will be whatever we're told or
106 * however we retrieve it. This program disregards fan values of 0,
107 * so any code providing a fan speed can set to 0 on failure and
108 * that fan value will be effectively ignored. The PID algorithm
109 * will be unhappy but nothing bad will happen.
110 *
111 * TODO(venture): If the fan value is 0 should that loop just be
112 * skipped? Right now, a 0 value is ignored in
113 * FanController::inputProc()
114 */
James Feistce6a3f32019-03-12 11:20:16 -0700115
Hao Jiangb6a0b892021-02-21 18:00:45 -0800116 // Check if we should just go back to sleep.
117 if (zone->getManualMode())
118 {
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800119 pidControlLoop(zone, timer, isCanceling, false, cycleCnt);
Hao Jiangb6a0b892021-02-21 18:00:45 -0800120 return;
121 }
James Feistce6a3f32019-03-12 11:20:16 -0700122
Hao Jiangb6a0b892021-02-21 18:00:45 -0800123 // Get the latest fan speeds.
124 zone->updateFanTelemetry();
James Feistce6a3f32019-03-12 11:20:16 -0700125
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800126 uint64_t msPerThermalCycle = zone->getUpdateThermalsCycle();
127
128 // Process thermal cycles at a rate that is less often than fan
129 // cycles. If thermal time is not an exact multiple of fan time,
130 // there will be some remainder left over, to keep the timing
131 // correct, as the intervals are staggered into one another.
132 if (cycleCnt >= msPerThermalCycle)
Hao Jiangb6a0b892021-02-21 18:00:45 -0800133 {
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800134 cycleCnt -= msPerThermalCycle;
James Feistce6a3f32019-03-12 11:20:16 -0700135
Hao Jiangb6a0b892021-02-21 18:00:45 -0800136 processThermals(zone);
137 }
James Feistce6a3f32019-03-12 11:20:16 -0700138
Hao Jiangb6a0b892021-02-21 18:00:45 -0800139 // Run the fan PIDs every iteration.
140 zone->processFans();
James Feistce6a3f32019-03-12 11:20:16 -0700141
Hao Jiangb6a0b892021-02-21 18:00:45 -0800142 if (loggingEnabled)
143 {
144 std::ostringstream out;
145 out << "," << zone->getFailSafeMode() << std::endl;
146 zone->writeLog(out.str());
147 }
James Feistce6a3f32019-03-12 11:20:16 -0700148
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800149 // Count how many milliseconds have elapsed, so we can know when
150 // to perform thermal cycles, in proper ratio with fan cycles.
151 cycleCnt += msPerFanCycle;
James Feistce6a3f32019-03-12 11:20:16 -0700152
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800153 pidControlLoop(zone, timer, isCanceling, false, cycleCnt);
Hao Jiangb6a0b892021-02-21 18:00:45 -0800154 });
James Feistce6a3f32019-03-12 11:20:16 -0700155}
Patrick Venturea0764872020-08-08 07:48:43 -0700156
157} // namespace pid_control