blob: f046c9db0dfb6015899e7369b02a31bac7b9aa25 [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"
21#include "sensors/sensor.hpp"
22
23#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070024
James Feistce6a3f32019-03-12 11:20:16 -070025#include <chrono>
26#include <map>
27#include <memory>
28#include <thread>
29#include <vector>
30
Patrick Venturea0764872020-08-08 07:48:43 -070031namespace pid_control
32{
33
James Feistce6a3f32019-03-12 11:20:16 -070034static void processThermals(PIDZone* zone)
35{
36 // Get the latest margins.
37 zone->updateSensors();
Patrick Venture9bbf3332019-07-16 10:50:37 -070038 // Zero out the set point goals.
39 zone->clearSetPoints();
James Feistce6a3f32019-03-12 11:20:16 -070040 zone->clearRPMCeilings();
41 // Run the margin PIDs.
42 zone->processThermals();
43 // Get the maximum RPM setpoint.
Patrick Venturef7a2dd52019-07-16 14:31:13 -070044 zone->determineMaxSetPointRequest();
James Feistce6a3f32019-03-12 11:20:16 -070045}
46
47void pidControlLoop(PIDZone* zone, boost::asio::steady_timer& timer, bool first,
48 int ms100cnt)
49{
50 if (first)
51 {
Patrick Venturede79ee02019-05-08 14:50:00 -070052 if (loggingEnabled)
James Feistce6a3f32019-03-12 11:20:16 -070053 {
54 zone->initializeLog();
55 }
56
57 zone->initializeCache();
58 processThermals(zone);
59 }
60
61 timer.expires_after(std::chrono::milliseconds(100));
62 timer.async_wait(
63 [zone, &timer, ms100cnt](const boost::system::error_code& ec) mutable {
James Feist1fe08952019-05-07 09:17:16 -070064 if (ec == boost::asio::error::operation_aborted)
65 {
66 return; // timer being canceled, stop loop
67 }
68
James Feistce6a3f32019-03-12 11:20:16 -070069 /*
70 * This should sleep on the conditional wait for the listen thread
71 * to tell us it's in sync. But then we also need a timeout option
72 * in case phosphor-hwmon is down, we can go into some weird failure
73 * more.
74 *
75 * Another approach would be to start all sensors in worst-case
76 * values, and fail-safe mode and then clear out of fail-safe mode
77 * once we start getting values. Which I think it is a solid
78 * approach.
79 *
80 * For now this runs before it necessarily has any sensor values.
81 * For the host sensors they start out in fail-safe mode. For the
82 * fans, they start out as 0 as input and then are adjusted once
83 * they have values.
84 *
85 * If a fan has failed, it's value will be whatever we're told or
86 * however we retrieve it. This program disregards fan values of 0,
87 * so any code providing a fan speed can set to 0 on failure and
88 * that fan value will be effectively ignored. The PID algorithm
89 * will be unhappy but nothing bad will happen.
90 *
91 * TODO(venture): If the fan value is 0 should that loop just be
92 * skipped? Right now, a 0 value is ignored in
93 * FanController::inputProc()
94 */
95
96 // Check if we should just go back to sleep.
97 if (zone->getManualMode())
98 {
99 pidControlLoop(zone, timer, false, ms100cnt);
100 return;
101 }
102
103 // Get the latest fan speeds.
104 zone->updateFanTelemetry();
105
106 if (10 <= ms100cnt)
107 {
108 ms100cnt = 0;
109
110 processThermals(zone);
111 }
112
113 // Run the fan PIDs every iteration.
114 zone->processFans();
115
Patrick Venturede79ee02019-05-08 14:50:00 -0700116 if (loggingEnabled)
James Feistce6a3f32019-03-12 11:20:16 -0700117 {
118 zone->getLogHandle() << "," << zone->getFailSafeMode();
119 zone->getLogHandle() << std::endl;
120 }
121
122 ms100cnt += 1;
123
124 pidControlLoop(zone, timer, false, ms100cnt);
125 });
126}
Patrick Venturea0764872020-08-08 07:48:43 -0700127
128} // namespace pid_control