blob: 08fb513d5b3e363b983fe9fb62d4a01c118f8194 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001/**
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
Patrick Ventured8012182018-03-08 08:21:38 -080017#include "pidthread.hpp"
18
James Feist22c257a2018-08-31 14:07:12 -070019#include "pid/pidcontroller.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080020#include "sensors/sensor.hpp"
21
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070022#include <chrono>
23#include <map>
24#include <memory>
25#include <thread>
26#include <vector>
Patrick Ventured8012182018-03-08 08:21:38 -080027
Patrick Venture7af157b2018-10-30 11:24:40 -070028static void processThermals(PIDZone* zone)
Patrick Ventured8012182018-03-08 08:21:38 -080029{
30 // Get the latest margins.
31 zone->updateSensors();
32 // Zero out the RPM set point goals.
33 zone->clearRPMSetPoints();
James Feist608304d2019-02-25 10:01:42 -080034 zone->clearRPMCeilings();
Patrick Ventured8012182018-03-08 08:21:38 -080035 // Run the margin PIDs.
Patrick Venture563a3562018-10-30 09:31:26 -070036 zone->processThermals();
Patrick Venture7280e272019-02-11 10:45:32 -080037 // Get the maximum RPM setpoint.
Patrick Ventured8012182018-03-08 08:21:38 -080038 zone->determineMaxRPMRequest();
39}
40
Patrick Venture7af157b2018-10-30 11:24:40 -070041void pidControlThread(PIDZone* zone)
Patrick Ventured8012182018-03-08 08:21:38 -080042{
43 int ms100cnt = 0;
44 /*
45 * This should sleep on the conditional wait for the listen thread to tell
46 * us it's in sync. But then we also need a timeout option in case
47 * phosphor-hwmon is down, we can go into some weird failure more.
48 *
49 * Another approach would be to start all sensors in worst-case values,
50 * and fail-safe mode and then clear out of fail-safe mode once we start
51 * getting values. Which I think it is a solid approach.
52 *
53 * For now this runs before it necessarily has any sensor values. For the
54 * host sensors they start out in fail-safe mode. For the fans, they start
55 * out as 0 as input and then are adjusted once they have values.
56 *
57 * If a fan has failed, it's value will be whatever we're told or however
58 * we retrieve it. This program disregards fan values of 0, so any code
59 * providing a fan speed can set to 0 on failure and that fan value will be
60 * effectively ignored. The PID algorithm will be unhappy but nothing bad
61 * will happen.
62 *
63 * TODO(venture): If the fan value is 0 should that loop just be skipped?
Patrick Venture563a3562018-10-30 09:31:26 -070064 * Right now, a 0 value is ignored in FanController::inputProc()
Patrick Ventured8012182018-03-08 08:21:38 -080065 */
66#ifdef __TUNING_LOGGING__
67 zone->initializeLog();
68#endif
69 zone->initializeCache();
Patrick Venture7af157b2018-10-30 11:24:40 -070070 processThermals(zone);
Patrick Ventured8012182018-03-08 08:21:38 -080071
72 while (true)
73 {
74 using namespace std::literals::chrono_literals;
75 std::this_thread::sleep_for(0.1s);
76
77 // Check if we should just go back to sleep.
78 if (zone->getManualMode())
79 {
80 continue;
81 }
82
83 // Get the latest fan speeds.
84 zone->updateFanTelemetry();
85
86 if (10 <= ms100cnt)
87 {
88 ms100cnt = 0;
89
Patrick Venture7af157b2018-10-30 11:24:40 -070090 processThermals(zone);
Patrick Ventured8012182018-03-08 08:21:38 -080091 }
92
93 // Run the fan PIDs every iteration.
Patrick Venture563a3562018-10-30 09:31:26 -070094 zone->processFans();
Patrick Ventured8012182018-03-08 08:21:38 -080095
96#ifdef __TUNING_LOGGING__
Patrick Venture5f02ad22018-04-24 10:18:40 -070097 zone->getLogHandle() << "," << zone->getFailSafeMode();
Patrick Ventured8012182018-03-08 08:21:38 -080098 zone->getLogHandle() << std::endl;
99#endif
100
101 ms100cnt += 1;
102 }
103
104 return;
105}