blob: f8d7fd93112eb0df04a7d8c9152e00f3715a26bb [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 Venturec32e3fc2019-02-28 10:01:11 -080020#include "pid/tuning.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080021#include "sensors/sensor.hpp"
22
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include <chrono>
24#include <map>
25#include <memory>
26#include <thread>
27#include <vector>
Patrick Ventured8012182018-03-08 08:21:38 -080028
Patrick Venture7af157b2018-10-30 11:24:40 -070029static void processThermals(PIDZone* zone)
Patrick Ventured8012182018-03-08 08:21:38 -080030{
31 // Get the latest margins.
32 zone->updateSensors();
33 // Zero out the RPM set point goals.
34 zone->clearRPMSetPoints();
James Feist608304d2019-02-25 10:01:42 -080035 zone->clearRPMCeilings();
Patrick Ventured8012182018-03-08 08:21:38 -080036 // Run the margin PIDs.
Patrick Venture563a3562018-10-30 09:31:26 -070037 zone->processThermals();
Patrick Venture7280e272019-02-11 10:45:32 -080038 // Get the maximum RPM setpoint.
Patrick Ventured8012182018-03-08 08:21:38 -080039 zone->determineMaxRPMRequest();
40}
41
Patrick Venture7af157b2018-10-30 11:24:40 -070042void pidControlThread(PIDZone* zone)
Patrick Ventured8012182018-03-08 08:21:38 -080043{
44 int ms100cnt = 0;
45 /*
46 * This should sleep on the conditional wait for the listen thread to tell
47 * us it's in sync. But then we also need a timeout option in case
48 * phosphor-hwmon is down, we can go into some weird failure more.
49 *
50 * Another approach would be to start all sensors in worst-case values,
51 * and fail-safe mode and then clear out of fail-safe mode once we start
52 * getting values. Which I think it is a solid approach.
53 *
54 * For now this runs before it necessarily has any sensor values. For the
55 * host sensors they start out in fail-safe mode. For the fans, they start
56 * out as 0 as input and then are adjusted once they have values.
57 *
58 * If a fan has failed, it's value will be whatever we're told or however
59 * we retrieve it. This program disregards fan values of 0, so any code
60 * providing a fan speed can set to 0 on failure and that fan value will be
61 * effectively ignored. The PID algorithm will be unhappy but nothing bad
62 * will happen.
63 *
64 * TODO(venture): If the fan value is 0 should that loop just be skipped?
Patrick Venture563a3562018-10-30 09:31:26 -070065 * Right now, a 0 value is ignored in FanController::inputProc()
Patrick Ventured8012182018-03-08 08:21:38 -080066 */
Patrick Venturec32e3fc2019-02-28 10:01:11 -080067 if (tuningLoggingEnabled)
68 {
69 zone->initializeLog();
70 }
71
Patrick Ventured8012182018-03-08 08:21:38 -080072 zone->initializeCache();
Patrick Venture7af157b2018-10-30 11:24:40 -070073 processThermals(zone);
Patrick Ventured8012182018-03-08 08:21:38 -080074
75 while (true)
76 {
77 using namespace std::literals::chrono_literals;
78 std::this_thread::sleep_for(0.1s);
79
80 // Check if we should just go back to sleep.
81 if (zone->getManualMode())
82 {
83 continue;
84 }
85
86 // Get the latest fan speeds.
87 zone->updateFanTelemetry();
88
89 if (10 <= ms100cnt)
90 {
91 ms100cnt = 0;
92
Patrick Venture7af157b2018-10-30 11:24:40 -070093 processThermals(zone);
Patrick Ventured8012182018-03-08 08:21:38 -080094 }
95
96 // Run the fan PIDs every iteration.
Patrick Venture563a3562018-10-30 09:31:26 -070097 zone->processFans();
Patrick Ventured8012182018-03-08 08:21:38 -080098
Patrick Venturec32e3fc2019-02-28 10:01:11 -080099 if (tuningLoggingEnabled)
100 {
101 zone->getLogHandle() << "," << zone->getFailSafeMode();
102 zone->getLogHandle() << std::endl;
103 }
Patrick Ventured8012182018-03-08 08:21:38 -0800104
105 ms100cnt += 1;
106 }
107
108 return;
109}