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