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