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