blob: 1cdf019e70cd05f2bb76029df534f38473653bfb [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
Patrick Venture7a98c192020-08-12 08:35:16 -070036static void processThermals(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
Patrick Venture7a98c192020-08-12 08:35:16 -070049void pidControlLoop(ZoneInterface* zone, boost::asio::steady_timer& timer,
Patrick Venture597ebd62020-08-11 08:48:19 -070050 bool first, int ms100cnt)
James Feistce6a3f32019-03-12 11:20:16 -070051{
52 if (first)
53 {
Patrick Venturede79ee02019-05-08 14:50:00 -070054 if (loggingEnabled)
James Feistce6a3f32019-03-12 11:20:16 -070055 {
56 zone->initializeLog();
57 }
58
59 zone->initializeCache();
60 processThermals(zone);
61 }
62
63 timer.expires_after(std::chrono::milliseconds(100));
64 timer.async_wait(
65 [zone, &timer, ms100cnt](const boost::system::error_code& ec) mutable {
James Feist1fe08952019-05-07 09:17:16 -070066 if (ec == boost::asio::error::operation_aborted)
67 {
68 return; // timer being canceled, stop loop
69 }
70
James Feistce6a3f32019-03-12 11:20:16 -070071 /*
72 * This should sleep on the conditional wait for the listen thread
73 * to tell us it's in sync. But then we also need a timeout option
74 * in case phosphor-hwmon is down, we can go into some weird failure
75 * more.
76 *
77 * Another approach would be to start all sensors in worst-case
78 * values, and fail-safe mode and then clear out of fail-safe mode
79 * once we start getting values. Which I think it is a solid
80 * approach.
81 *
82 * For now this runs before it necessarily has any sensor values.
83 * For the host sensors they start out in fail-safe mode. For the
84 * fans, they start out as 0 as input and then are adjusted once
85 * they have values.
86 *
87 * If a fan has failed, it's value will be whatever we're told or
88 * however we retrieve it. This program disregards fan values of 0,
89 * so any code providing a fan speed can set to 0 on failure and
90 * that fan value will be effectively ignored. The PID algorithm
91 * will be unhappy but nothing bad will happen.
92 *
93 * TODO(venture): If the fan value is 0 should that loop just be
94 * skipped? Right now, a 0 value is ignored in
95 * FanController::inputProc()
96 */
97
98 // Check if we should just go back to sleep.
99 if (zone->getManualMode())
100 {
101 pidControlLoop(zone, timer, false, ms100cnt);
102 return;
103 }
104
105 // Get the latest fan speeds.
106 zone->updateFanTelemetry();
107
108 if (10 <= ms100cnt)
109 {
110 ms100cnt = 0;
111
112 processThermals(zone);
113 }
114
115 // Run the fan PIDs every iteration.
116 zone->processFans();
117
Patrick Venturede79ee02019-05-08 14:50:00 -0700118 if (loggingEnabled)
James Feistce6a3f32019-03-12 11:20:16 -0700119 {
Patrick Venture7a98c192020-08-12 08:35:16 -0700120 std::ostringstream out;
121 out << "," << zone->getFailSafeMode() << std::endl;
122 zone->writeLog(out.str());
James Feistce6a3f32019-03-12 11:20:16 -0700123 }
124
125 ms100cnt += 1;
126
127 pidControlLoop(zone, timer, false, ms100cnt);
128 });
129}
Patrick Venturea0764872020-08-08 07:48:43 -0700130
131} // namespace pid_control