Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | /** |
| 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 Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 17 | #include "pidthread.hpp" |
| 18 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 19 | #include "pid/pidcontroller.hpp" |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 20 | #include "sensors/sensor.hpp" |
| 21 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | #include <chrono> |
| 23 | #include <map> |
| 24 | #include <memory> |
| 25 | #include <thread> |
| 26 | #include <vector> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 27 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 28 | static void processThermals(PIDZone* zone) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 29 | { |
| 30 | // Get the latest margins. |
| 31 | zone->updateSensors(); |
| 32 | // Zero out the RPM set point goals. |
| 33 | zone->clearRPMSetPoints(); |
| 34 | // Run the margin PIDs. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 35 | zone->processThermals(); |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 36 | // Get the maximum RPM setpoint. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 37 | zone->determineMaxRPMRequest(); |
| 38 | } |
| 39 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 40 | void pidControlThread(PIDZone* zone) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 41 | { |
| 42 | int ms100cnt = 0; |
| 43 | /* |
| 44 | * This should sleep on the conditional wait for the listen thread to tell |
| 45 | * us it's in sync. But then we also need a timeout option in case |
| 46 | * phosphor-hwmon is down, we can go into some weird failure more. |
| 47 | * |
| 48 | * Another approach would be to start all sensors in worst-case values, |
| 49 | * and fail-safe mode and then clear out of fail-safe mode once we start |
| 50 | * getting values. Which I think it is a solid approach. |
| 51 | * |
| 52 | * For now this runs before it necessarily has any sensor values. For the |
| 53 | * host sensors they start out in fail-safe mode. For the fans, they start |
| 54 | * out as 0 as input and then are adjusted once they have values. |
| 55 | * |
| 56 | * If a fan has failed, it's value will be whatever we're told or however |
| 57 | * we retrieve it. This program disregards fan values of 0, so any code |
| 58 | * providing a fan speed can set to 0 on failure and that fan value will be |
| 59 | * effectively ignored. The PID algorithm will be unhappy but nothing bad |
| 60 | * will happen. |
| 61 | * |
| 62 | * TODO(venture): If the fan value is 0 should that loop just be skipped? |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 63 | * Right now, a 0 value is ignored in FanController::inputProc() |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 64 | */ |
| 65 | #ifdef __TUNING_LOGGING__ |
| 66 | zone->initializeLog(); |
| 67 | #endif |
| 68 | zone->initializeCache(); |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 69 | processThermals(zone); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 70 | |
| 71 | while (true) |
| 72 | { |
| 73 | using namespace std::literals::chrono_literals; |
| 74 | std::this_thread::sleep_for(0.1s); |
| 75 | |
| 76 | // Check if we should just go back to sleep. |
| 77 | if (zone->getManualMode()) |
| 78 | { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | // Get the latest fan speeds. |
| 83 | zone->updateFanTelemetry(); |
| 84 | |
| 85 | if (10 <= ms100cnt) |
| 86 | { |
| 87 | ms100cnt = 0; |
| 88 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 89 | processThermals(zone); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Run the fan PIDs every iteration. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 93 | zone->processFans(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 94 | |
| 95 | #ifdef __TUNING_LOGGING__ |
Patrick Venture | 5f02ad2 | 2018-04-24 10:18:40 -0700 | [diff] [blame] | 96 | zone->getLogHandle() << "," << zone->getFailSafeMode(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 97 | zone->getLogHandle() << std::endl; |
| 98 | #endif |
| 99 | |
| 100 | ms100cnt += 1; |
| 101 | } |
| 102 | |
| 103 | return; |
| 104 | } |